Mark Xue Market Model

Mark Xue Market Model preview image

1 collaborator

Default-person Mark Xue (Author)

Tags

(This model has yet to be categorized with any tags)
Model group MAM-2013 | Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.4 • Viewed 777 times • Downloaded 96 times • Run 0 times
Download the 'Mark Xue Market Model' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Info tab cannot be displayed because of an encoding error

Comments and Questions

run time error

(pertains to version 7 with description "changed graphs" 1. First run-time error: Can't take logarithm of -0.026. error while observer running LN called by procedure SET-VAR called by procedure GO called by Button 'go' This line is highlighted let j (ln (n / m)) ;;calculate price changes between ticks 2. in a different run, got a divide by zero run-time error on that same line

Posted about 11 years ago

Click to Run Model

turtles-own
[
  confidence          ;;confidence level of agent
]

globals
[
  index-price         ;;index price in the market
  previous-price      ;;price of index in the previous period
  all-time-high       ;;all time high price of index in market
  all-time-low        ;;all time low price of index in market
  moving-average      ;;50 tick moving average
  price-list          ;;list of index prices in the previous 50 ticks
  volatility          ;;annualized volatility using past 50 tick price data
]

;;setup

to setup
  clear-all
  setup-turtles
  setup-patches
  setup-constants
  set-turtle-color
  check-bounds
  move-turtle
  reset-ticks
end 


;;run 

to go
  set-memory
  set-market-price
  change-turtle-confidence
  set-turtle-color
  check-bounds
  move-turtle
  set-price-list
  if moving-average? [
  calculate-moving-average
  ]
  set-var
  tick
end 


;;setup turtles

to setup-turtles
  set-default-shape turtles "person"
  crt 100
    [ 
      set size 3  ;; easier to see
    ]
end 


;;setup patches

to setup-patches
  ask patches
  [
    set pcolor black
  ]
end 


;;setup constants

to setup-constants
  ask turtles
  [
    ;;set each turtle's initial confidence to normal distribution
    let temp random-normal initial-confidence 10  
    set confidence temp
    if temp < -100 [
      set confidence -100
    ]
    if confidence > 100 [
      set confidence 100
    ]
  ]
  set index-price 1000
  set all-time-high 1000
  set all-time-low 1000
  set price-list []
  set volatility 0
end 


;;set the colors of the turtles

to set-turtle-color
  ask turtles [
    ;;agent is bearish, so color set to red
    if confidence < 0 [set color red]
    ;;agent is bullish, so color is set to green
    if confidence > 0 [set color green]
    ;;agent is neutral, so color is white
    if confidence = 0 [set color white]
  ]
end 


;;sets the global variables

to set-market-price
  let confidence-sum 0
  ask turtles [
    set confidence-sum (confidence-sum + confidence)
  ]
  ifelse index-price < 0
  [set index-price 0]           ;; 0 lower bound
  [set index-price (index-price + (confidence-sum / 10))]
  
  
  ;;set all-time-high level
  if index-price > all-time-high [set all-time-high index-price]
  ;;set all-time-low level  
  if index-price < all-time-low [set all-time-low index-price]
end 


;;how the turtle confidence changes based on what happened in the previous tick

to change-turtle-confidence
  ask turtles [
    if previous-price > index-price
    [
      ;; market trending up, maybe bull run, so buy?
      set confidence (confidence + random 10)
    ]
    
    if previous-price < index-price
    [
      ;; market sliding down, maybe crash coming, so sell?
      set confidence (confidence - random 10)
    ]
    
    ;;effect of alltime highs/lows
    if ticks > 100 [    ;;need enough "historical" data
      if index-price = all-time-high
      [ ;;all time highs reached, sell off to realize profits
        if 3 < (random 10)
        [set confidence (confidence - 10)]
      ]
      
      if index-price = all-time-low
      [ ;;all time lows reached, good price to buy in
        if 3 < (random 10)
        [set confidence (confidence + 10)]
      ]   
    ]
    
    if moving-average? [
      ;;effect of 50-tick moving average, mean reversion strategy
      if ticks > 50 [
        if (moving-average > index-price) [    ;;moving average is higher than index price, prompt agents to sell off to converage to mean
          if 1 < (random 10)
          [set confidence (confidence - 0.5)]
        ]
        
        if (moving-average < index-price) [    ;;moving averge is lower than index price, prompt agents to buy to converage to mean
          if 1 < (random 10)
          [set confidence (confidence + 0.5)]
        ]   
      ]
    ]   
    
  ]
end 


;;sets confidence bounds

to check-bounds
  ask turtles [
    ;; confidence bounds
    if confidence > 100 
    [
      set confidence 100
    ]
    
    if confidence < -100
    [
      set confidence -100
    ]   
  ]
end 

;;sets the memory of the turtles, tell them what happened in the previous turn

to set-memory
  set previous-price index-price
end 


;;introduces a confidence shock in the market, e.g. a big economic news that came out

to add-shock
  ask turtles [
    set confidence (((shock / 100) *  (abs confidence)) + confidence)  
  ]
end 


;;moves the turtle according to it's confidence

to move-turtle
  ask turtles [
    setxy ((who - 50) + (who * 4)) (confidence /  2)
  ]  
end 

;;calculates the 50-tick moving average

to calculate-moving-average
  if (ticks > 50) [
    let price-sum (sum price-list)
    set moving-average (price-sum / 50)
  ]
end 

;;updates the price-list to the 50 previous prices

to set-price-list
  ifelse (ticks < 50) [
    set price-list lput index-price price-list   ;;adds in the newest price at the end
  ]
  [
    set price-list but-first price-list          ;;pops out the oldest price
    set price-list lput index-price price-list   ;;adds in new price at the end
  ]
end 

;;calculates the variance

to set-var
  let temp-var 0                            ;;stores price variance for calculations
  if (ticks > 50) [
      let i 49                              ;;iterates through price-list to calcuate the variance of price changes
      while [i > 0] [
        let m (item (49 - i) price-list)    ;;last price
        let n (item (50 - i) price-list)    ;;second-to-last price
        let j (ln (n / m))                  ;;calculate price changes between ticks
        set temp-var (temp-var + (j * j))   ;;sums running total of price changes
        set i (i - 1)                       ;;decrement  
      ]
      set temp-var (sqrt (temp-var / 49))   ;;standard deviation of price changes during 50 tick period
      set temp-var (temp-var * (sqrt 252))  ;;annualize the volatility, assuming 252 trading ticks/days in a year
      set volatility (temp-var * 100)       ;;set global variable
  ]
end 

There are 7 versions of this model.

Uploaded by When Description Download
Mark Xue over 11 years ago changed graphs Download this version
Mark Xue over 11 years ago Added volatility calculations and chart Download this version
Mark Xue about 12 years ago mean reversion effect added Download this version
Mark Xue about 12 years ago modified market pricing and updated visualization Download this version
Mark Xue about 12 years ago added shock function and profit-taking logic Download this version
Mark Xue about 12 years ago market moves, but one-sided Download this version
Mark Xue about 12 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Mark Xue Market Model.png preview Preview for 'Mark Xue Market Model' about 12 years ago, by Mark Xue Download
Xue_Mark_FinalPaper_v1.pdf pdf Final Project Paper v1 over 11 years ago, by Mark Xue Download
Xue_Mark_FinalPaper_v2.pdf pdf Final Project Paper v2 over 11 years ago, by Mark Xue Download
Xue_Mark_FinalPaper_v3.pdf pdf Final Project Paper v3 over 11 years ago, by Mark Xue Download
Xue_Mark_Poster.pdf pdf Final Project Poster Slides about 12 years ago, by Mark Xue Download
XueMark_June3.pdf pdf progress report about 12 years ago, by Mark Xue Download
XueMark_May13.pdf pdf XueMark_May13 about 12 years ago, by Mark Xue Download
XueMark_May20.pdf pdf progress report about 12 years ago, by Mark Xue Download
XueMark_May27.pdf pdf progress report about 12 years ago, by Mark Xue Download

This model does not have any ancestors.

This model does not have any descendants.