;;;; England lost her Barings ;;; The collapse of the Baring Brothers establishment ;;; provides an abject Leeson for the banking community. ;;; -- GJS (define (make-market name initial-price) (let ((price initial-price) (price-serializer (make-serializer)) (pending-orders '()) (orders-serializer (make-serializer))) (define (the-market m) (cond ((eq? m 'new-price!) (price-serializer (lambda (update) (set! price (update price))))) ((eq? m 'get-price) price) ((eq? m 'new-order!) (orders-serializer (lambda (new-order) (set! pending-orders (append pending-orders (list new-order)))))) ((eq? m 'execute-an-order) (((orders-serializer (lambda () (if (not (null? pending-orders)) (let ((outstanding-order (car pending-orders))) (set! pending-orders (cdr pending-orders)) outstanding-order) (lambda () 'nothing-to-do))))))) ((eq? m 'get-name) name) (else (error "Wrong message" m)))) the-market)) ;;; Using this we make two markets, both starting at the same price: (define nikkei-fundamental 16680.0) (define tokyo (make-market "Tokyo:Nikkei-225" nikkei-fundamental)) (define singapore (make-market "Singapore:Nikkei-225" nikkei-fundamental)) ;;; Traders buy and sell contracts at a market using TRANSACT. ;;; The trader gives the market permission to subtract from the ;;; trader's monetary balance the cost of the contracts purchased ;;; and to add to the trader's stash the contracts he purchased. (define (buy ncontracts market permission) (transact ncontracts market permission)) (define (sell ncontracts market permission) (transact (- ncontracts) market permission)) (define (transact ncontracts market permission) ((market 'new-order!) (lambda () (permission (- (* ncontracts (market 'get-price))) ncontracts)))) (define transaction-cost 1.0) (define (make-arbitrager name balance contracts authorization) (let ((trader-serializer (make-serializer))) (define (change-assets delta-money delta-contracts) ((trader-serializer (lambda () (set! balance (+ balance delta-money)) (set! contracts (+ contracts delta-contracts)))))) (define (a (- high-price low-price) transaction-cost) (let ((amount-to-gamble (min authorization balance))) (let ((ncontracts ;round to nearest integer (round (/ amount-to-gamble (- high-price low-price))))) (buy ncontracts low-place change-assets) (sell ncontracts high-place change-assets))))) (define (consider-a-trade) (let ((nikkei-225-tokyo (tokyo 'get-price)) (nikkei-225-singapore (singapore 'get-price))) (if (< nikkei-225-tokyo nikkei-225-singapore) (a