4
4
package com .autoStock .backtest ;
5
5
6
6
import java .util .ArrayList ;
7
+ import java .util .Arrays ;
7
8
import java .util .List ;
8
9
9
10
import org .encog .engine .network .activation .ActivationFunction ;
34
35
import com .autoStock .database .DatabaseQuery ;
35
36
import com .autoStock .finance .SecurityTypeHelper .SecurityType ;
36
37
import com .autoStock .generated .basicDefinitions .TableDefinitions .DbStockHistoricalPrice ;
38
+ import com .autoStock .internal .ApplicationStates ;
37
39
import com .autoStock .tools .ArrayTools ;
38
40
import com .autoStock .tools .DateTools ;
39
41
import com .autoStock .tools .ListTools ;
50
52
*
51
53
*/
52
54
public class BacktestPredictFuture {
53
- private static final int POINT_SIZE = 4 ;
55
+ private static final int INPUT_POINT_SIZE = 4 ;
54
56
private static final int INPUT_POINTS = 10 ;
57
+ private static final int OUTPUT_POINT_SIZE = 4 ;
55
58
private static final int OUTPUT_POINTS = 1 ;
56
- private static final int IDEAL_OFFSET = 0 ;
57
- private static final int INPUT_NEURONS = INPUT_POINTS * POINT_SIZE ;
58
- private static final int OUTPUT_NEURONS = OUTPUT_POINTS * POINT_SIZE ;
59
+ private static final int IDEAL_OFFSET = 8 ;
60
+ private static final int INPUT_NEURONS = INPUT_POINTS * INPUT_POINT_SIZE ;
61
+ private static final int OUTPUT_NEURONS = OUTPUT_POINTS * OUTPUT_POINT_SIZE ;
59
62
private ActivationFunction activationFunction = new ActivationTANH ();
60
63
private ArrayList <PredictionResult > listOfPredictionResult = new ArrayList <PredictionResult >();
61
64
@@ -70,61 +73,51 @@ public PredictionResult(int countCorrect, int countIncorrect) {
70
73
}
71
74
72
75
public void run (){
73
- HistoricalData historicalDataIS = new HistoricalData (new Exchange ("NYSE" ), new Symbol ("MS" , SecurityType .type_stock ), DateTools .getDateFromString ("09/08/2014" ), DateTools .getDateFromString ("09/11/2014" ), Resolution .min );
76
+ // Load the data
77
+ HistoricalData historicalDataIS = new HistoricalData (new Exchange ("NYSE" ), new Symbol ("MS" , SecurityType .type_stock ), DateTools .getDateFromString ("09/08/2014" ), DateTools .getDateFromString ("09/08/2014" ), Resolution .min );
74
78
historicalDataIS .setStartAndEndDatesToExchange ();
75
79
ArrayList <DbStockHistoricalPrice > listOfResultsIS = (ArrayList <DbStockHistoricalPrice >) new DatabaseQuery ().getQueryResults (BasicQueries .basic_historical_price_range , new QueryArg (QueryArgs .symbol , historicalDataIS .symbol .symbolName ), new QueryArg (QueryArgs .exchange , new Exchange ("NYSE" ).exchangeName ), new QueryArg (QueryArgs .resolution , Resolution .min .asMinutes ()), new QueryArg (QueryArgs .startDate , DateTools .getSqlDate (historicalDataIS .startDate )), new QueryArg (QueryArgs .endDate , DateTools .getSqlDate (historicalDataIS .endDate )));
76
80
77
- HistoricalData historicalDataOS = new HistoricalData (new Exchange ("NYSE" ), new Symbol ("MS" , SecurityType .type_stock ), DateTools .getDateFromString ("09/12 /2014" ), DateTools .getDateFromString ("09/12 /2014" ), Resolution .min );
81
+ HistoricalData historicalDataOS = new HistoricalData (new Exchange ("NYSE" ), new Symbol ("MS" , SecurityType .type_stock ), DateTools .getDateFromString ("09/08 /2014" ), DateTools .getDateFromString ("09/08 /2014" ), Resolution .min );
78
82
historicalDataOS .setStartAndEndDatesToExchange ();
79
83
ArrayList <DbStockHistoricalPrice > listOfResultsOS = (ArrayList <DbStockHistoricalPrice >) new DatabaseQuery ().getQueryResults (BasicQueries .basic_historical_price_range , new QueryArg (QueryArgs .symbol , historicalDataOS .symbol .symbolName ), new QueryArg (QueryArgs .exchange , new Exchange ("NYSE" ).exchangeName ), new QueryArg (QueryArgs .resolution , Resolution .min .asMinutes ()), new QueryArg (QueryArgs .startDate , DateTools .getSqlDate (historicalDataOS .startDate )), new QueryArg (QueryArgs .endDate , DateTools .getSqlDate (historicalDataOS .endDate )));
80
84
81
85
Co .println ("--> Size: " + listOfResultsIS .size () + ", " + listOfResultsOS .size ());
82
86
83
87
BasicMLDataSet dataSet = new BasicMLDataSet ();
84
88
85
- int sequence = 0 ;
86
-
89
+ // Build the input & ideal lists as % deltas
87
90
ArrayList <Double > percentChangeWindow = new ArrayList <Double >();
88
91
ArrayList <Pair <DbStockHistoricalPrice , MLData >> priceIdealList = new ArrayList <>();
89
92
90
93
for (DbStockHistoricalPrice slice : listOfResultsIS ){
91
- Co .println ("--> Slice: (" + sequence + ")" + slice .dateTime + ", " + slice .priceClose );
92
- MathTools .addPercentChangeList (percentChangeWindow , slice .priceOpen , slice .priceHigh , slice .priceLow , slice .priceClose );
93
-
94
- if (sequence != 0 && sequence % INPUT_POINTS == 0 ){
95
- Co .println ("--> Would add slice at: " + sequence );
96
- priceIdealList .add (new Pair <DbStockHistoricalPrice , MLData >(slice , null ));
97
- }
98
-
99
- Co .println ("" );
100
- sequence ++;
94
+ // Co.println("--> Slice: (" + sequence + ")" + slice.dateTime + ", " + slice.priceClose);
95
+ MathTools .genPercentChangeList (percentChangeWindow , IDEAL_OFFSET , slice .priceOpen , slice .priceHigh , slice .priceLow , slice .priceClose );
96
+ priceIdealList .add (new Pair <DbStockHistoricalPrice , MLData >(slice , new BasicMLData (ArrayTools .getDoubleArray (ListTools .getLast (percentChangeWindow , 4 )))));
101
97
}
102
98
103
- Co .println ("--> A: " + percentChangeWindow .size ());
104
- percentChangeWindow = ListTools .subList (percentChangeWindow , POINT_SIZE , -1 );
105
- Co .println ("--> A: " + percentChangeWindow .size () + ", " + priceIdealList .size ());
106
-
107
- int sequenceAlign = 0 ;
108
-
109
- for (int i =0 ; i <percentChangeWindow .size () - INPUT_NEURONS ; i += INPUT_NEURONS ){
99
+ //Remove the fist nW as it will be 0
100
+ Co .println ("--> A1: " + percentChangeWindow .size ());
101
+ percentChangeWindow = ListTools .subList (percentChangeWindow , INPUT_NEURONS + ((IDEAL_OFFSET -1 ) * INPUT_POINTS ), -1 );
102
+ priceIdealList = ListTools .subList (priceIdealList , INPUT_POINTS , -1 );
103
+ Co .println ("--> A2: " + percentChangeWindow .size ());
104
+ Co .println ("--> A3 ideal: " + priceIdealList .size ());
105
+
106
+ // Setup MLData accordingly
107
+ for (int i =0 ; i <percentChangeWindow .size () - INPUT_NEURONS - OUTPUT_NEURONS ; i ++){
110
108
MLData input = new BasicMLData (ArrayTools .getDoubleArray (percentChangeWindow .subList (i , i + INPUT_NEURONS )));
111
109
MLData ideal = new BasicMLData (ArrayTools .getDoubleArray (percentChangeWindow .subList (i + INPUT_NEURONS , i + INPUT_NEURONS + OUTPUT_NEURONS )));
112
110
113
111
dataSet .add (input , ideal );
114
-
115
- priceIdealList .get (sequenceAlign ).second = ideal ;
116
-
117
- sequenceAlign ++;
118
-
119
- Co .println ("--> B " + sequenceAlign );
120
112
}
121
113
122
- for (Pair <DbStockHistoricalPrice , MLData > item : priceIdealList ){
123
- if (item .second != null ){
124
- Co .println ("--> Price close, predication: " + item .first .dateTime + ", " + item .first .priceClose + ", " + item .second .getData (3 ) + " = " + ((item .first .priceClose * (item .second .getData (3 )/100 )) + item .first .priceClose ));
125
- }
126
- }
114
+ // for (Pair<DbStockHistoricalPrice, MLData> item : priceIdealList){
115
+ // if (item.second != null){
116
+ // Co.println("--> Price close, predication: " + item.first.dateTime + ", " + item.first.priceClose + ", " + item.second.getData(3) + " = " + ((item.first.priceClose * (item.second.getData(3)/100)) + item.first.priceClose));
117
+ // }
118
+ // }
127
119
120
+ // Train the network
128
121
BasicNetwork network = getMLNetwork (INPUT_NEURONS , OUTPUT_NEURONS );
129
122
MLTrain train = new ResilientPropagation (network , dataSet );
130
123
@@ -133,27 +126,31 @@ public void run(){
133
126
System .out .println ("" + train .getError () * 1000 );
134
127
}
135
128
136
- for (int i =0 ; i <percentChangeWindow .size () - INPUT_NEURONS ; i += INPUT_NEURONS ){
129
+ // Test the network
130
+ for (int i =0 ; i <percentChangeWindow .size () - INPUT_NEURONS - OUTPUT_NEURONS ; i ++){
137
131
MLData input = new BasicMLData (ArrayTools .getDoubleArray (percentChangeWindow .subList (i , i + INPUT_NEURONS )));
138
- MLData ideal = new BasicMLData (ArrayTools .getDoubleArray (percentChangeWindow .subList (i + INPUT_NEURONS + IDEAL_OFFSET , i + INPUT_NEURONS + OUTPUT_NEURONS + IDEAL_OFFSET )));
132
+ MLData ideal = new BasicMLData (ArrayTools .getDoubleArray (percentChangeWindow .subList (i + INPUT_NEURONS , i + INPUT_NEURONS + OUTPUT_NEURONS )));
139
133
MLData computed = network .compute (input );
140
134
141
135
for (int c =0 ; c <OUTPUT_POINTS ; c ++){
142
136
Co .println ("--> c: " + c );
143
- Co .println ("--> Computed, ideal: " + computed .getData (c +0 ) + " -> " + ideal .getData (c +0 ));
144
- Co .println ("--> Computed, ideal: " + computed .getData (c +1 ) + " -> " + ideal .getData (c +1 ));
145
- Co .println ("--> Computed, ideal: " + computed .getData (c +2 ) + " -> " + ideal .getData (c +2 ));
146
- Co .println ("--> Computed, ideal: " + computed .getData (c +3 ) + " -> " + ideal .getData (c +3 ));
137
+ for (int d =0 ; d <OUTPUT_POINT_SIZE ; d ++){
138
+ Co .println ("--> Computed, ideal: " + computed .getData (c +d ) + " -> " + ideal .getData (c +d ));
139
+ }
147
140
}
148
141
}
149
142
}
150
143
144
+ public void getPercentChange (ArrayList <Double > percentChangeWindow , int toIndex ){
145
+
146
+ }
147
+
151
148
public BasicNetwork getMLNetwork (int inputSize , int outputSize ){
152
149
FeedForwardPattern pattern = new FeedForwardPattern ();
153
150
pattern .setInputNeurons (inputSize );
154
151
pattern .addHiddenLayer (inputSize /2 );
155
152
pattern .addHiddenLayer (inputSize /3 );
156
- pattern .addHiddenLayer (inputSize / 4 );
153
+ pattern .addHiddenLayer (outputSize * 2 );
157
154
pattern .setOutputNeurons (outputSize );
158
155
pattern .setActivationFunction (activationFunction );
159
156
return (BasicNetwork ) pattern .generate ();
0 commit comments