;; STARTUP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; when started, setup to startup setup end ;; setup -- for the first time and when parameters are changed to setup clear-all set-default-shape turtles "person" create-all-buyers create-all-sellers set trading-day-number 0 set transaction-number 0 set current-deal-price -1 reset ; view-patches end ;; GLOBAL VARIABLES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; globals [ ;; demand and supply before any trading takes place total-inverse-supply total-inverse-demand ;; demand and supply at the moment current-inverse-supply current-inverse-demand ;; theoretic equilibrium quantity before any trading takes place total-equilibrium-quantity ;; theoretic equilibrium prices -""- total-equilibrium-supply-price total-equilibrium-demand-price ;; theoretic equilibrium quantity with the remaining S&D current-equilibrium-quantity ;; theoretic equilibrium price -""- current-equilibrium-supply-price current-equilibrium-demand-price ;; theoretic sum of buyers' and sellers' surplus before any trading takes place total-potential-surplus ;; quantity really traded up to the point -- needn't to be calculated -- you can ask traders current-quantity-traded ;; counter of trading days and transactions trading-day-number transaction-number ;; variable containing the last deal price current-deal-price ] ;; SELLERS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; breed [ sellers seller ] sellers-own [ ;; reservation price of all units the trader has to trade -- the same every trading day reservation-price ;; quantity-to-trade = the same every trading day; now is 1 but may be different later quantity-to-trade ;; remaing units in this trading day; on the daybreak it's reset to quantity-to-trade non-satisfied-quantity ;; gains-from-trade within one trading day gains-from-trade ;; for ZI traders: ;; price actually quoted (bid/ask) haggling-price ;; for ZIP traders margin beta gamma last-change ] ;; create and initialize sellers to create-all-sellers ;; create and position sellers create-sellers number-of-sellers [ setxy random-pxcor random-pycor set heading random 360 ] ;; set reservation prices ifelse ( supply-and-demand-generation-type = "equidistant") [ let step ( ( max-sell-price - min-sell-price) / ( number-of-sellers - 1 ) ) let tmp min-sell-price ask sellers [ set reservation-price tmp set tmp tmp + step ] ] [ ask sellers [ set reservation-price random ( max-sell-price - min-sell-price + 1) + min-sell-price ] ] ;; set ZIP trader's properties ask sellers [ initialize-zip-trader ] ;; set quantity to be traded and set intraday variables ask sellers [ set quantity-to-trade 1 reset-seller ] end ;; reset seller' intraday variables to reset-seller set non-satisfied-quantity quantity-to-trade set gains-from-trade 0 visualize-seller end ;; set seller's color according to his state to visualize-seller ifelse ( non-satisfied-quantity > 0 ) [ set color yellow + 1 ] [ set color yellow - 2 ] end ;; BUYERS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; breed [ buyers buyer ] buyers-own [ reservation-price quantity-to-trade non-satisfied-quantity gains-from-trade haggling-price margin beta gamma last-change ] ;; create and initialize buyers to create-all-buyers ;; create and position buyers create-buyers number-of-buyers [ setxy random-pxcor random-pycor set heading random 360 ] ;; set reservation prices ifelse ( supply-and-demand-generation-type = "equidistant") [ let step ( ( max-buy-price - min-buy-price ) / ( number-of-buyers - 1 ) ) let tmp min-buy-price ask buyers [ set reservation-price tmp set tmp tmp + step ] ] [ ask buyers [ set reservation-price random ( max-buy-price - min-buy-price + 1) + min-buy-price ] ] ;; set ZIP trader's properties ask buyers [ initialize-zip-trader ] ;; set quantity to be traded and set intraday variables ask buyers [ set quantity-to-trade 1 reset-buyer ] end ;; reset buyer's intraday variables to reset-buyer set non-satisfied-quantity quantity-to-trade set gains-from-trade 0 visualize-buyer end ;; set buyers's color according to his state to visualize-buyer ifelse ( non-satisfied-quantity > 0 ) [ set color red + 1 ] [ set color red - 2 ] end to visualize-trader ifelse ( breed = sellers ) [ visualize-seller ] [ visualize-buyer ] end ;; CARRYING THE MARKET OUT ( GO + GO-ONCE ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; one step in one trading day (should be run by a forever button or go procedure) to go-once ;; if there is no opportunity to trade, stop if ( equilibrium-quantity (inverse-supply) (inverse-demand) = 0 ) [ stop ] ;; move move ;; do trading trade ;; update traders' look ask turtles [ visualize-trader ] ;; tick and do plotting if ( where-to-tick? = "one quote" ) [ tick ] set current-inverse-demand inverse-demand set current-inverse-supply inverse-supply set current-equilibrium-quantity equilibrium-quantity current-inverse-supply current-inverse-demand set current-equilibrium-supply-price equilibrium-supply-price current-equilibrium-quantity current-inverse-supply set current-equilibrium-demand-price equilibrium-demand-price current-equilibrium-quantity current-inverse-demand do-plots ;view-patches end ;; carry out number-of-trading-days ;to go ; set trading-day-number 0 ; let go-counter 0 ; let last-q 0 ; repeat number-of-trading-days ; [ set go-counter 0 ; set last-q current-equilibrium-quantity ; set trading-day-number trading-day-number + 1 ; ;; reset intraday variables ; ask buyers [ reset-buyer ] ; ask sellers [ reset-seller ] ; reset ; ;; one trading day ; while [ ( equilibrium-quantity (inverse-supply) (inverse-demand) > 0 ) and ( go-counter < max-idle-ticks-per-trading-day ) ] ; [ set go-counter go-counter + 1 ; go-once ; if ( last-q > current-equilibrium-quantity ) ; [ ;show go-counter ; set go-counter 0 ; set last-q current-equilibrium-quantity ] ; ] ; ;; end-of-the-day plotting ; tick ; plot-day-marker-to-prices ; plot-alpha ; plot-gained-surplus ; plot-inefficient-traders ; plot-rho ; ] ;end ;; experimental version of go to go if ( trading-day-number >= number-of-trading-days ) [ stop ] set trading-day-number ( trading-day-number + 1 ) let go-counter 0 let last-q 0 set go-counter 0 set last-q current-equilibrium-quantity ;; reset intraday variables ask buyers [ reset-buyer ] ask sellers [ reset-seller ] reset ;; one trading day while [ ( equilibrium-quantity (inverse-supply) (inverse-demand) > 0 ) and ( go-counter < max-idle-ticks-per-trading-day ) ] [ set go-counter go-counter + 1 go-once if ( last-q > current-equilibrium-quantity ) [ ;show go-counter set go-counter 0 set last-q current-equilibrium-quantity ] ] ;; end-of-the-day plotting if ( where-to-tick? = "trading day" ) [ tick ] plot-day-marker-to-prices plot-alpha plot-gained-surplus plot-inefficient-traders plot-rho end ;; select those who can trade and ask them to trade to trade ifelse who-offers? = "buyers" [ ask one-of buyers with [ non-satisfied-quantity > 0 ] [ do-trade ] ] [ ifelse who-offers? = "sellers" [ ask one-of sellers with [ non-satisfied-quantity > 0] [ do-trade ] ] [ ask one-of turtles with [ non-satisfied-quantity > 0] [ do-trade ] ] ] end ;; select the right trading algorithm to do-trade run word traders-type "-trade" end ;; MOVING ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; there can be many moving algorithms; this procedure selects among them to move if ( moving-type = "wiggle" ) [ ask turtles [ wiggle-move ] ] end ;; wiggle-moving to wiggle-move rt random 30 lt random 30 fd 1 end ;; TRADING ALGORITHMS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; FAIR TRADING ;; fair-trading -- if a trade is possible, a pair always trade for the price ;; being an average between the seller's and the buyer's reservation price to fair-trade set current-deal-price -1 ifelse breed = sellers [ if non-satisfied-quantity = 0 [ stop ] if any? buyers in-radius vision-radius with [ non-satisfied-quantity > 0 ] [ let partner one-of buyers in-radius vision-radius with [ non-satisfied-quantity > 0 ] if [ reservation-price ] of partner >= reservation-price [ let price ( [ reservation-price ] of partner + reservation-price ) / 2 let gain price - reservation-price set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + gain set current-quantity-traded current-quantity-traded + 1 ask partner [ set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + gain ] plot-prices price set current-deal-price price ] ] ] [ if non-satisfied-quantity = 0 [ stop ] if any? sellers in-radius vision-radius with [ non-satisfied-quantity > 0 ] [ let partner one-of sellers in-radius vision-radius with [ non-satisfied-quantity > 0 ] if [ reservation-price ] of partner <= reservation-price [ let price ( [ reservation-price ] of partner + reservation-price ) / 2 let gain reservation-price - price set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + gain set current-quantity-traded current-quantity-traded + 1 ask partner [ set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + gain ] plot-prices price set current-deal-price price ] ] ] end ;; ZI-TRADING ;; Gode-Sunder's algorithm to zi-trade set current-deal-price -1 if non-satisfied-quantity = 0 [ stop ] adjust-zi-haggling-price let price haggling-price let partner nobody ifelse breed = sellers [ ask buyers in-radius vision-radius with [ non-satisfied-quantity > 0 ] [ adjust-zi-haggling-price ] ifelse public-offers? [ set partner one-of buyers in-radius vision-radius with [ ( non-satisfied-quantity > 0 ) and ( haggling-price >= price ) ] ] [ set partner one-of buyers in-radius vision-radius with [ non-satisfied-quantity > 0 ] ] if ( ( partner != nobody ) and ( [haggling-price ] of partner >= haggling-price ) ) [ set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + price - reservation-price set current-quantity-traded current-quantity-traded + 1 ask partner [ set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + reservation-price - price ] plot-prices price set current-deal-price price ] ; if partner != nobody [ show ( list [ who ] of partner price [ haggling-price ] of partner ) ] ] [ ask sellers in-radius vision-radius with [ non-satisfied-quantity > 0 ] [ adjust-zi-haggling-price ] ifelse public-offers? [ set partner one-of sellers in-radius vision-radius with [ ( non-satisfied-quantity > 0 ) and ( haggling-price <= price ) ] ] [ set partner one-of sellers in-radius vision-radius with [ non-satisfied-quantity > 0 ] ] if ( ( partner != nobody ) and ( [haggling-price ] of partner <= haggling-price ) ) [ set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + reservation-price - price set current-quantity-traded current-quantity-traded + 1 ask partner [ set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + price - reservation-price ] plot-prices price set current-deal-price price ] ] end to adjust-zi-haggling-price ifelse breed = buyers [ set haggling-price random ( reservation-price + 1 ) ] [ set haggling-price reservation-price + random ( max list max-buy-price max-sell-price - reservation-price + 1 ) ] end ;; ZIP-TRADING (Cliff's algorithm, Cliff, 1997) ;; to zip-trade set current-deal-price -1 if non-satisfied-quantity = 0 [ stop ] let price haggling-price let partner nobody let dtype "OFFER" let daccepted false ifelse breed = sellers [ ifelse public-offers? [ set partner one-of buyers in-radius vision-radius with [ ( non-satisfied-quantity > 0 ) and ( haggling-price >= price ) ] ] [ set partner one-of buyers in-radius vision-radius with [ non-satisfied-quantity > 0 ] ] if ( ( partner != nobody ) and ( [haggling-price ] of partner >= haggling-price ) ) [ set daccepted true set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + price - reservation-price set current-quantity-traded current-quantity-traded + 1 ask partner [ set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + reservation-price - price ] plot-prices price set current-deal-price price ] ] [ set dtype "BID" ifelse public-offers? [ set partner one-of sellers in-radius vision-radius with [ ( non-satisfied-quantity > 0 ) and ( haggling-price <= price ) ] ] [ set partner one-of sellers in-radius vision-radius with [ non-satisfied-quantity > 0 ] ] if ( ( partner != nobody ) and ( [haggling-price ] of partner <= haggling-price ) ) [ set daccepted true set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + reservation-price - price set current-quantity-traded current-quantity-traded + 1 ask partner [ set non-satisfied-quantity non-satisfied-quantity - 1 set gains-from-trade gains-from-trade + price - reservation-price ] plot-prices price set current-deal-price price ] ] ;; adjust margins of all traders that can hear the outcome of this attempt to trade ifelse public-hearing? [ ask turtles in-radius vision-radius [ adjust-zip-haggling-price dtype daccepted price ] ] [ adjust-zip-haggling-price dtype daccepted price if ( partner != nobody ) [ ask partner [ adjust-zip-haggling-price dtype daccepted price ] ] ] end ;; initialize ZIP trader's properties to initialize-zip-trader ifelse ( breed = sellers ) [ set margin ( min-initial-margin / 100 ) + random-float ( max-initial-margin - min-initial-margin ) / 100 ] [ set margin ( - ( ( min-initial-margin / 100 ) + random-float ( max-initial-margin - min-initial-margin ) / 100 ) ) ] set beta ( ( min-beta / 100 ) + random-float ( ( max-beta - min-beta ) / 100 ) ) set gamma ( ( min-gamma / 100 ) + random-float ( ( max-gamma - min-gamma ) / 100 ) ) set haggling-price round-price ( reservation-price * ( 1 + margin ) ) set last-change 0 end ;; adjust a zip trader's margin and haggling-price to adjust-zip-haggling-price [ deal-type deal-accepted? deal-price ] ; show word "Pred " haggling-price ifelse ( breed = sellers ) ;; for sellers [ ifelse deal-accepted? ;; for accepted deals [ ifelse ( haggling-price <= deal-price ) ;; if his haggling-price is less than or equal to the deal price, raise the margin [ do-adjust-zip-haggling-price zip-higher-price deal-price ] ;; wouldn't have got this deal, so decrease the margin [ if ( ( deal-type = "BID" ) and ( not willing-to-trade? deal-price ) and ( non-satisfied-quantity > 0 ) ) [ do-adjust-zip-haggling-price zip-lower-price deal-price ] ] ] ;; no deal [ if ( ( deal-type = "OFFER" ) and ( haggling-price >= deal-price ) and (non-satisfied-quantity > 0 ) ) ;; would have asked for more and lost the deal, so reduce margin [ do-adjust-zip-haggling-price zip-lower-price deal-price ] ] ] ;; ;; for buyers [ ifelse deal-accepted? ;; for accepted deals [ ifelse ( haggling-price >= deal-price ) ;;could get lower price -- trhy rasing margin [ do-adjust-zip-haggling-price zip-lower-price deal-price ] ;; wouldn't have got this deal, so reduce the profit [ if ( ( deal-type = "OFFER" ) and ( not willing-to-trade? deal-price ) and ( non-satisfied-quantity > 0 ) ) [ do-adjust-zip-haggling-price zip-higher-price deal-price ] ] ] ;; no-deal [ if ( (deal-type = "BID" ) and ( haggling-price <= deal-price ) and (non-satisfied-quantity > 0 ) ) ;; would have bid less and also lost the deal, so reduce profit [ do-adjust-zip-haggling-price zip-higher-price deal-price ] ] ] ; show word "Po " haggling-price end to-report zip-lower-price [ p ] report ( p * ( 1 - ( random-float delta-p / 100 ) ) - ( random-float delta-a / 100 ) ) end to-report zip-higher-price [ p ] report ( p * ( 1 + ( random-float delta-p / 100 ) ) + ( random-float delta-a / 100 ) ) end to-report willing-to-trade? [ p ] ifelse ( breed = buyers ) [ if ( ( non-satisfied-quantity > 0 ) and ( haggling-price >= p ) ) [ report true ] ] [ if ( ( non-satisfied-quantity > 0 ) and ( haggling-price <= p ) ) [ report true ] ] report false end to do-adjust-zip-haggling-price [ target-price ] ; show (sentence reservation-price ", " target-price ", " haggling-price) let diff target-price - haggling-price let change ( 1 - gamma ) * beta * diff + gamma * last-change set last-change change let newmargin ( haggling-price + change ) / reservation-price - 1 ifelse ( breed = sellers ) [ if ( newmargin > 0 ) [ set margin newmargin ] ] [ if ( newmargin < 0 ) [ set margin newmargin ] ] set haggling-price round-price ( ( 1 + margin ) * reservation-price ) ; show (sentence newmargin ", " margin ", " haggling-price) end ;; EFFICIENTY CALCULATIONS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; SMITH (JPE, 1962) ALPHA ;; this version works only if the total quantity traded by a single trader is one ;; otherwise the list of prices had to be recorded directly to-report calculate-alpha let n count sellers with [ non-satisfied-quantity = 0 ] let p ( total-equilibrium-supply-price + total-equilibrium-demand-price ) / 2 let price-list [ ( reservation-price + gains-from-trade ) ] of sellers with [ non-satisfied-quantity = 0 ] let sigma 0 foreach price-list [ set sigma sigma + ( ? - p ) ^ 2 ] ifelse ( n >= 2 ) [ set sigma sqrt ( sigma / ( n - 1 ) ) ] [ set sigma 10 ^ 100 ] report ( 100 * sigma / p ) end ;; total surplus gained by all traders in percents of the the theoretic one to-report calculate-gained-surplus report ( sum [ gains-from-trade ] of turtles / total-potential-surplus * 100) end ;; number of intramarginal traders that failed to trade ;; temporaty code that works only if there are not two traders with the same reservation price to-report calculate-intramarginal-nontraded let p ( total-equilibrium-supply-price + total-equilibrium-demand-price ) / 2 report ( count sellers with [ ( reservation-price <= p ) and ( non-satisfied-quantity > 0 ) ] + count buyers with [ ( reservation-price >= p ) and ( non-satisfied-quantity > 0 ) ] ) end ;; number of extramarginal traders that managed to trade ;; temporaty code that works only if there are not two traders with the same reservation price to-report calculate-extramarginal-traded let p ( total-equilibrium-supply-price + total-equilibrium-demand-price ) / 2 report ( count sellers with [ ( reservation-price > p ) and ( non-satisfied-quantity = 0 ) ] + count buyers with [ ( reservation-price < p ) and ( non-satisfied-quantity = 0 ) ] ) end ;; a complex measure of market inefficiency; get zero for the perfect market ;; rho is sum of absolute values of deviation between theoretic and actual gains from trade ;; of all traders divided by the total theoretic gain from trade ;; temporaty code that works only if there are not two traders with the same reservation price to-report calculate-rho let p ( total-equilibrium-supply-price + total-equilibrium-demand-price ) / 2 let summa 0 ;; add gains of extramarginal traders set summa summa + sum [ gains-from-trade ] of sellers with [ reservation-price > p ] set summa summa + sum [ gains-from-trade ] of buyers with [ reservation-price < p ] ;; add absolute values of difference between theoretic and real gains of intramarginal traders set summa summa + sum [ abs ( ( p - reservation-price ) - gains-from-trade ) ] of sellers with [ reservation-price <= p ] set summa summa + sum [ abs ( ( reservation-price - p ) - gains-from-trade ) ] of buyers with [ reservation-price >= p ] report ( 100 * summa / total-potential-surplus ) end ;; PLOTTING ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; draw supply and demand -- at every tick to do-plots set-current-plot "Supply and Demand" clear-plot set-current-plot-pen "total demand" draw-inverse-demand total-inverse-demand set-current-plot-pen "total supply" draw-inverse-supply total-inverse-supply set-current-plot-pen "current supply" draw-inverse-supply current-inverse-supply set-current-plot-pen "current demand" draw-inverse-demand current-inverse-demand set-current-plot-pen "default" draw-total-equilibrium end ;; draw evolution of prices -- at every tick to plot-prices [ price ] set transaction-number transaction-number + 1 set-current-plot "Prices" set-current-plot-pen "theoretic low" plot total-equilibrium-supply-price set-current-plot-pen "theoretic high" ; plot total-equilibrium-demand-price plotxy transaction-number total-equilibrium-demand-price ; set-current-plot-pen "current low" ; plot current-equilibrium-supply-price ; set-current-plot-pen "current high" ; plot current-equilibrium-demand-price set-current-plot-pen "current real" plot price end ;; draw a line delimiting trading days in the price chart -- at every trading day to plot-day-marker-to-prices set-current-plot "Prices" set-current-plot-pen "date-range" plot-pen-up ; plotxy plot-x-max 0 plotxy transaction-number min (list min-sell-price min-buy-price ) plot-pen-down ; plotxy plot-x-max plot-y-max plotxy transaction-number max (list max-sell-price max-buy-price ) plot-pen-up end ;; plot evolution of Smith's alpha -- at every trading day to plot-alpha set-current-plot "Alpha" set-current-plot-pen "default" plot calculate-alpha end ;; plot evolution of total gained surplus in per cents -- at every trading day to plot-gained-surplus set-current-plot "Gained Surplus" set-current-plot-pen "default" plot calculate-gained-surplus end ;; plot number of inefficient trades and traders to plot-inefficient-traders set-current-plot "Inefficient Traders" set-current-plot-pen "nontraded" plot current-equilibrium-quantity set-current-plot-pen "intramarg. nontraded" plot calculate-intramarginal-nontraded set-current-plot-pen "extramarg. traded" plot calculate-extramarginal-traded set-current-plot-pen "sum of inefficient" plot ( calculate-intramarginal-nontraded + calculate-extramarginal-traded ) end ;; plot rho to plot-rho set-current-plot "Rho" set-current-plot-pen "default" plot calculate-rho end ;; AUXILIARY CODE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; VISION RADIUS ;; returns the radius that allows a trader to see vision% % of the world to-report vision-radius ;;report vision% * sqrt ( ( world-width / 2 ) ^ 2 + ( world-height / 2 ) ^ 2 ) / 100 report world-fraction-to-radius-function ( vision% / 100 ) end ;; returns the radius that allows a trader to see world-frac % of the world to-report world-fraction-to-radius-function [ world-frac ] if (world-frac < .79) [ report sqrt ( world-frac * world-width * world-height / pi) ] ; table of numerical approximations. ; starts at .79, goes up by .01: let approx-list [ 0.501568 ; .79 0.505272 0.50927 0.513527 0.518035 0.522796 ; .80 to .84 0.527818 0.533114 0.538703 0.54461 0.550864 ; .85 to .89 0.557507 0.564589 0.572176 0.580358 0.589258 ; .90 to .94 0.599054 0.610022 0.622622 0.637758 0.657758 ; .95 to .99 0.707107 ; 1.00 this last one equals 1 / sqrt(2.0) 0 ; dummy value, never really used, but prevents ;an array index out of bounds problem when world-frac = 1 ] let index world-frac * 100 - 79 let index-low floor index report world-width * ((item index-low approx-list) * (1 - (index - index-low)) + (item (index-low + 1) approx-list) * (index - index-low)) end ;; rounding to-report round-price [ p ] if ( price-rounding-to = "none" ) [ report p ] if ( price-rounding-to = "pennies" ) [ report ( round ( p * 100 ) / 100 ) ] if ( price-rounding-to = "dimes" ) [ report ( round ( p * 10 ) / 10 ) ] report ( round p ) end ;;;;;;;;;;;;;;;;;;;;;;;;;;; NEW CODE BOREDER ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; show / hide patches ;to view-patches ; ifelse patches-visible? ; [ ; ask patches with [(pxcor + pycor) mod 2 = 0] [ set pcolor green - 3 ] ; ask patches with [(pxcor + pycor) mod 2 = 1] [ set pcolor black ] ; ] ; [ ask patches [ set pcolor black ] ] ;end ;; SETUP & GO ;; reset every market day ;; ATTENTION: traders must be reset here as well!!! to reset set total-inverse-demand inverse-demand set total-inverse-supply inverse-supply set current-inverse-demand total-inverse-demand set current-inverse-supply total-inverse-supply set total-equilibrium-quantity equilibrium-quantity total-inverse-supply total-inverse-demand set total-equilibrium-supply-price equilibrium-supply-price total-equilibrium-quantity total-inverse-supply set total-equilibrium-demand-price equilibrium-demand-price total-equilibrium-quantity total-inverse-demand set current-equilibrium-quantity total-equilibrium-quantity set current-equilibrium-supply-price total-equilibrium-supply-price set current-equilibrium-demand-price total-equilibrium-demand-price set total-potential-surplus potential-surplus total-inverse-supply total-inverse-demand set current-quantity-traded 0 do-plots end ;; DIAGNOSTICS ;; inverse demand and supply (i.e. reservation prices for each subsequent unit) ;; each trader may have as many units to trade as he wishes ;; but he must have the same reservation-price for them all to-report inverse-supply ; stara funkcni verze: ; report sort [reservation-price ] of sellers with [ non-satisfied-quantity > 0 ] let supply [] ask sellers [ set supply sentence reservation-prices-of-quantities-to-sold supply ] report sort supply end to-report inverse-demand ; stara funkcni verze: ; report sort-by [?1 > ?2] [reservation-price ] of buyers with [ non-satisfied-quantity > 0 ] let demand [] ask buyers [ set demand sentence reservation-prices-of-quantities-to-sold demand ] report sort-by [?1 > ?2] demand end to-report reservation-prices-of-quantities-to-sold report n-values non-satisfied-quantity [ reservation-price ] end ;; potential gains from trade (sellers' and buyers' total surplus) calculated from inverse demand and supply to-report potential-surplus [ supply demand ] let surplus 0 while [ (not empty? demand) and (not empty? supply) and (( first demand ) > ( first supply )) ] [ set surplus surplus + first demand - first supply set demand but-first demand set supply but-first supply ] report surplus end ;; equilibrium quantity calculated from inverse demand and supply ;; (the last pair with zero surplus is included if exists) to-report equilibrium-quantity [ supply demand ] let quantity 0 while [ (not empty? demand) and (not empty? supply) and (( first demand ) >= ( first supply )) ] [ set demand but-first demand set supply but-first supply set quantity quantity + 1 ] report quantity end ;; equilibrium price calculated from inverse supply (the lower limit) to-report equilibrium-supply-price [ eq-quantity supply ] ifelse eq-quantity > 0 [ report item ( eq-quantity - 1) supply ] [ report "None" ] end ;; equilibrium price calculated from inverse demand (the upper limit) to-report equilibrium-demand-price [ eq-quantity demand ] ifelse eq-quantity > 0 [ report item ( eq-quantity - 1) demand ] [ report "None" ] end ;; drawing inverse supply to draw-inverse-supply [ supply ] let price 0 let quantity 0 plot-pen-up plotxy quantity price plot-pen-down foreach supply [ set price ? plotxy quantity price set quantity quantity + 1 plotxy quantity price ] plotxy quantity ( ( max list max-sell-price max-buy-price ) + 5 ) plot-pen-up end ;; drawing inverse demand to draw-inverse-demand [ demand ] let price ( max list max-sell-price max-buy-price ) + 5 let quantity 0 plot-pen-up plotxy quantity price plot-pen-down foreach demand [ set price ? plotxy quantity price set quantity quantity + 1 plotxy quantity price ] plotxy quantity 0 plot-pen-up end to draw-total-equilibrium ; plot-pen-down ; plotxy equilibrium-quantity inverse-supply inverse-demand 0 ; plotxy equilibrium-quantity inverse-supply inverse-demand plot-y-max ; plot-pen-up end @#$#@#$#@ GRAPHICS-WINDOW 490 10 814 355 16 16 9.52 1 10 1 1 1 0 1 1 1 -16 16 -16 16 0 0 1 ticks CC-WINDOW 5 869 823 964 Command Center 0 BUTTON 5 10 65 43 NIL setup NIL 1 T OBSERVER NIL NIL NIL NIL SLIDER 5 455 185 488 number-of-sellers number-of-sellers 1 100 19 1 1 NIL HORIZONTAL SLIDER 5 495 187 528 number-of-buyers number-of-buyers 1 100 19 1 1 NIL HORIZONTAL SLIDER 5 535 177 568 min-sell-price min-sell-price 1 100 10 1 1 NIL HORIZONTAL SLIDER 5 575 177 608 max-sell-price max-sell-price min-sell-price 100 100 1 1 NIL HORIZONTAL SLIDER 5 615 177 648 min-buy-price min-buy-price 1 100 10 1 1 NIL HORIZONTAL SLIDER 5 655 177 688 max-buy-price max-buy-price min-buy-price 100 100 1 1 NIL HORIZONTAL PLOT 80 10 280 160 Supply and Demand Q P 0.0 1.0 0.0 1.0 true false PENS "default" 1.0 0 -16777216 true "total supply" 1.0 0 -10899396 true "total demand" 1.0 0 -13345367 true "current supply" 1.0 0 -1184463 true "current demand" 3.0 0 -2674135 true MONITOR 90 220 166 269 curr. P*s current-equilibrium-supply-price 17 1 12 MONITOR 90 165 163 214 curr. P*d current-equilibrium-demand-price 17 1 12 MONITOR 170 165 244 214 theor. Q* total-equilibrium-quantity 17 1 12 MONITOR 5 220 84 269 theor. P*s total-equilibrium-supply-price 17 1 12 MONITOR 5 165 86 214 theor. P*d total-equilibrium-demand-price 17 1 12 CHOOSER 335 490 473 535 traders-type traders-type "fair" "zi" "zip" 2 SLIDER 335 540 507 573 vision% vision% 0 100 100 1 1 NIL HORIZONTAL MONITOR 335 290 435 339 gained surp. % precision calculate-gained-surplus 1 17 1 12 SWITCH 335 680 484 713 public-offers? public-offers? 0 1 -1000 CHOOSER 335 580 460 625 who-offers? who-offers? "sellers" "buyers" "buyers & sellers" 2 PLOT 285 10 485 160 Prices transaction # P 0.0 10.0 0.0 100.0 true false PENS "default" 1.0 0 -16777216 true "theoretic low" 1.0 0 -16777216 true "current real" 1.0 0 -2674135 true "theoretic high" 1.0 0 -16777216 true "current low" 1.0 0 -5825686 true "current high" 1.0 0 -5825686 true "date-range" 1.0 0 -13345367 true MONITOR 250 220 309 269 curr. Q current-quantity-traded 17 1 12 CHOOSER 5 695 285 740 supply-and-demand-generation-type supply-and-demand-generation-type "equidistant" "random" 0 CHOOSER 335 630 473 675 moving-type moving-type "still" "wiggle" 1 SLIDER 5 415 229 448 number-of-trading-days number-of-trading-days 1 50 5 1 1 NIL HORIZONTAL SWITCH 335 720 496 753 public-hearing? public-hearing? 0 1 -1000 SLIDER 5 745 289 778 max-idle-ticks-per-trading-day max-idle-ticks-per-trading-day 500 2500 1000 1 1 NIL HORIZONTAL PLOT 325 165 485 285 Alpha trading day alpha 0.0 10.0 0.0 10.0 true false PENS "default" 1.0 1 -16777216 true PLOT 5 290 165 410 Gained Surplus trading day surp. % 0.0 10.0 0.0 10.0 true false PENS "default" 1.0 1 -16777216 true PLOT 335 360 649 480 Inefficient Traders trading day numbers 0.0 10.0 0.0 10.0 true true PENS "sum of inefficient" 1.0 0 -16777216 true "nontraded" 1.0 0 -2674135 true "intramarg. nontraded" 1.0 0 -13345367 true "extramarg. traded" 1.0 0 -10899396 true PLOT 170 290 330 410 Rho trading day rho 0.0 10.0 0.0 10.0 true false PENS "default" 1.0 1 -16777216 true BUTTON 5 50 65 83 NIL go T 1 T OBSERVER NIL NIL NIL NIL MONITOR 250 165 312 214 curr. Q* current-equilibrium-quantity 17 1 12 SLIDER 530 570 702 603 min-beta min-beta 0 100 10 1 1 NIL HORIZONTAL SLIDER 530 610 702 643 max-beta max-beta min-beta 100 50 1 1 NIL HORIZONTAL SLIDER 530 650 702 683 min-gamma min-gamma 0 100 20 1 1 NIL HORIZONTAL SLIDER 530 690 702 723 max-gamma max-gamma min-gamma 100 80 1 1 NIL HORIZONTAL SLIDER 530 730 702 763 delta-p delta-p 0 100 5 1 1 NIL HORIZONTAL SLIDER 530 770 702 803 delta-a delta-a 0 100 5 1 1 NIL HORIZONTAL CHOOSER 530 810 672 855 price-rounding-to price-rounding-to "none" "pennies" "dimes" "bucks" 1 CHOOSER 653 434 791 479 where-to-tick? where-to-tick? "trading day" "one quote" 0 SLIDER 530 490 708 523 min-initial-margin min-initial-margin 0 100 5 1 1 NIL HORIZONTAL SLIDER 530 530 712 563 max-initial-margin max-initial-margin min-initial-margin 100 35 1 1 NIL HORIZONTAL MONITOR 5 90 65 139 day # trading-day-number 17 1 12 @#$#@#$#@ WHAT IS IT? ----------- This model explores a simple market with buyers and sellers. Each of them has its own reservation price, and its goal quantity to trade. His goal is to maximize its surplus (i.e. its part of the gains of the trade). HOW IT WORKS ------------ This section could explain what rules the agents use to create the overall behavior of the model. HOW TO USE IT ------------- This section could explain how to use the model, including a description of each of the items in the interface tab. THINGS TO NOTICE ---------------- This section could give some ideas of things for the user to notice while running the model. THINGS TO TRY ------------- This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model. EXTENDING THE MODEL ------------------- This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc. NETLOGO FEATURES ---------------- This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features. RELATED MODELS -------------- This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest. CREDITS AND REFERENCES ---------------------- This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references. @#$#@#$#@ default true 0 Polygon -7500403 true true 150 5 40 250 150 205 260 250 airplane true 0 Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15 arrow true 0 Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 box false 0 Polygon -7500403 true true 150 285 285 225 285 75 150 135 Polygon -7500403 true true 150 135 15 75 150 15 285 75 Polygon -7500403 true true 15 75 15 225 150 285 150 135 Line -16777216 false 150 285 150 135 Line -16777216 false 150 135 15 75 Line -16777216 false 150 135 285 75 bug true 0 Circle -7500403 true true 96 182 108 Circle -7500403 true true 110 127 80 Circle -7500403 true true 110 75 80 Line -7500403 true 150 100 80 30 Line -7500403 true 150 100 220 30 butterfly true 0 Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240 Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240 Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163 Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165 Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225 Circle -16777216 true false 135 90 30 Line -16777216 false 150 105 195 60 Line -16777216 false 150 105 105 60 car false 0 Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180 Circle -16777216 true false 180 180 90 Circle -16777216 true false 30 180 90 Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89 Circle -7500403 true true 47 195 58 Circle -7500403 true true 195 195 58 circle false 0 Circle -7500403 true true 0 0 300 circle 2 false 0 Circle -7500403 true true 0 0 300 Circle -16777216 true false 30 30 240 cow false 0 Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167 Polygon -7500403 true true 73 210 86 251 62 249 48 208 Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123 cylinder false 0 Circle -7500403 true true 0 0 300 dot false 0 Circle -7500403 true true 90 90 120 face happy false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240 face neutral false 0 Circle -7500403 true true 8 7 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Rectangle -16777216 true false 60 195 240 225 face sad false 0 Circle -7500403 true true 8 8 285 Circle -16777216 true false 60 75 60 Circle -16777216 true false 180 75 60 Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183 fish false 0 Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166 Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165 Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60 Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166 Circle -16777216 true false 215 106 30 flag false 0 Rectangle -7500403 true true 60 15 75 300 Polygon -7500403 true true 90 150 270 90 90 30 Line -7500403 true 75 135 90 135 Line -7500403 true 75 45 90 45 flower false 0 Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135 Circle -7500403 true true 85 132 38 Circle -7500403 true true 130 147 38 Circle -7500403 true true 192 85 38 Circle -7500403 true true 85 40 38 Circle -7500403 true true 177 40 38 Circle -7500403 true true 177 132 38 Circle -7500403 true true 70 85 38 Circle -7500403 true true 130 25 38 Circle -7500403 true true 96 51 108 Circle -16777216 true false 113 68 74 Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218 Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240 house false 0 Rectangle -7500403 true true 45 120 255 285 Rectangle -16777216 true false 120 210 180 285 Polygon -7500403 true true 15 120 150 15 285 120 Line -16777216 false 30 120 270 120 leaf false 0 Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195 Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195 line true 0 Line -7500403 true 150 0 150 300 line half true 0 Line -7500403 true 150 0 150 150 pentagon false 0 Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120 person false 0 Circle -7500403 true true 110 5 80 Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90 Rectangle -7500403 true true 127 79 172 94 Polygon -7500403 true true 195 90 240 150 225 180 165 105 Polygon -7500403 true true 105 90 60 150 75 180 135 105 plant false 0 Rectangle -7500403 true true 135 90 165 300 Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285 Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285 Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210 Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135 Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135 Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60 Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90 square false 0 Rectangle -7500403 true true 30 30 270 270 square 2 false 0 Rectangle -7500403 true true 30 30 270 270 Rectangle -16777216 true false 60 60 240 240 star false 0 Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108 target false 0 Circle -7500403 true true 0 0 300 Circle -16777216 true false 30 30 240 Circle -7500403 true true 60 60 180 Circle -16777216 true false 90 90 120 Circle -7500403 true true 120 120 60 tree false 0 Circle -7500403 true true 118 3 94 Rectangle -6459832 true false 120 195 180 300 Circle -7500403 true true 65 21 108 Circle -7500403 true true 116 41 127 Circle -7500403 true true 45 90 120 Circle -7500403 true true 104 74 152 triangle false 0 Polygon -7500403 true true 150 30 15 255 285 255 triangle 2 false 0 Polygon -7500403 true true 150 30 15 255 285 255 Polygon -16777216 true false 151 99 225 223 75 224 truck false 0 Rectangle -7500403 true true 4 45 195 187 Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194 Rectangle -1 true false 195 60 195 105 Polygon -16777216 true false 238 112 252 141 219 141 218 112 Circle -16777216 true false 234 174 42 Rectangle -7500403 true true 181 185 214 194 Circle -16777216 true false 144 174 42 Circle -16777216 true false 24 174 42 Circle -7500403 false true 24 174 42 Circle -7500403 false true 144 174 42 Circle -7500403 false true 234 174 42 turtle true 0 Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210 Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105 Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105 Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87 Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210 Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99 wheel false 0 Circle -7500403 true true 3 3 294 Circle -16777216 true false 30 30 240 Line -7500403 true 150 285 150 15 Line -7500403 true 15 150 285 150 Circle -7500403 true true 120 120 60 Line -7500403 true 216 40 79 269 Line -7500403 true 40 84 269 221 Line -7500403 true 40 216 269 79 Line -7500403 true 84 40 221 269 x false 0 Polygon -7500403 true true 270 75 225 30 30 225 75 270 Polygon -7500403 true true 30 75 75 30 270 225 225 270 @#$#@#$#@ NetLogo 4.0.2 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@ setup go calculate-alpha calculate-rho calculate-gained-surplus current-equilibrium-quantity calculate-intramarginal-nontraded calculate-extramarginal-traded setup go current-deal-price calculate-alpha @#$#@#$#@ @#$#@#$#@ default 0.0 -0.2 0 0.0 1.0 0.0 1 1.0 0.0 0.2 0 0.0 1.0 link direction true 0 Line -7500403 true 150 150 90 180 Line -7500403 true 150 150 210 180 @#$#@#$#@