-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockGenerator.java
More file actions
153 lines (126 loc) · 3.9 KB
/
StockGenerator.java
File metadata and controls
153 lines (126 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
*
* Author : ACIMS(Arizona Centre for Integrative Modeling & Simulation)
* Version : DEVSJAVA 2.7
* Date : 08-15-02
*/
package StockSim;
import java.awt.Color;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import GenCol.entity;
import model.modeling.content;
import model.modeling.message;
import view.modeling.ViewableAtomic;
public class StockGenerator extends ViewableAtomic{
DecimalFormat df = new DecimalFormat("#.00");
int day;
private Random randomGenerator;
ArrayList<String> stockSymbols;
Map<String, Map> stocksMap;
int totalDaysForSimulation = 0;
double openMarketHours = 6.5;
double currentMarketTime = 0.0;
String stockPrices = "";
String randomStockSymbol = "";
public StockGenerator(String name, ArrayList<String> stockSymbols, Map<String, Map> stocksMap){
super(name);
this.stockSymbols = stockSymbols;
this.stocksMap = stocksMap;
addOutport("OutPrices");
addOutport("OutSymbol");
addOutport("OutState");
setBackgroundColor(Color.GREEN);
}
public void initialize(){
holdIn("active", 0);
day = 1;
totalDaysForSimulation = stocksMap.size() + 1;
super.initialize();
}
public void deltext(double e, message x){}
public void deltint( ){
randomGenerator = new Random();
randomStockSymbol = (String)stockSymbols.get(randomGenerator.nextInt(stockSymbols.size()));
stockPrices = "";
if(day == totalDaysForSimulation + 1)
sigma = INFINITY;
else if(day == totalDaysForSimulation){
phase = "end";
sigma = 1;
day++;
}else if(currentMarketTime == 0.0){
phase = "open";
getOpenOrClosePrice(phase);
sigma = .5;
currentMarketTime = 0.5;
}else if(currentMarketTime == 6.0){
phase = "close";
getOpenOrClosePrice(phase);
sigma = 0.5;
currentMarketTime = 0.0;
day++;
}else{
phase = "mid";
populateRandomHourlyValues();
sigma = 0.5;
currentMarketTime = currentMarketTime + 0.5;
}
}
public message out( )
{
message m = new message();
content con1 = makeContent("OutPrices", new entity(stockPrices));
m.add(con1);
content con2 = makeContent("OutSymbol", new entity(randomStockSymbol));
m.add(con2);
content con3 = makeContent("OutState", new entity(phase));
m.add(con3);
return m;
}
private void getOpenOrClosePrice(String phase){
for (int i = 0; i < stockSymbols.size(); i++) {
StockInfo stockData = (StockInfo) stocksMap.get(Integer.toString(day)).get(stockSymbols.get(i));
if(i != 0){
stockPrices = stockPrices + ":";
}
if(phase.equalsIgnoreCase("open")){
stockPrices = stockPrices + stockSymbols.get(i) + ":" + stockData.getOpenValue();
stockData.setPreviousValue(stockData.getOpenValue());
}else{
stockPrices = stockPrices + stockSymbols.get(i) + ":" + stockData.getCloseValue();
}
}
}
private double standardRandomNormalVariable(){
Random rand = new Random();
double VARIANCE = 3.0;
return VARIANCE * rand.nextGaussian();
}
private double logReturn(double drift, double volatility){
return drift + volatility * standardRandomNormalVariable();
}
private double generatedPriceValue(double initialValue, double drift, double volatility){
return initialValue * Math.exp(logReturn(drift, volatility));
}
private void populateRandomHourlyValues(){
for (int i = 0; i < stockSymbols.size(); i++) {
StockInfo stockData = (StockInfo) stocksMap.get(Integer.toString(day)).get(stockSymbols.get(i));
double previousPrice = stockData.getPreviousValue();
double generatedPrice = generatedPriceValue(previousPrice, stockData.getDrift(), stockData.getVolatility());
stockData.setPreviousValue(generatedPrice);
if(i != 0){
stockPrices = stockPrices + ":";
}
stockPrices = stockPrices + stockSymbols.get(i) + ":" + df.format(generatedPrice);
}
}
public String getTooltipText(){
return
super.getTooltipText()
+"\n"+" Day: " + day
+"\n"+" Market Time: " + currentMarketTime;
}
}