forked from kartikmadan11/MetaTraderForecast
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RNNProject_NT8_Indi.cs
537 lines (459 loc) · 16.2 KB
/
RNNProject_NT8_Indi.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Windows.Media;
using NinjaTrader.NinjaScript.DrawingTools;
using System.Net.Sockets;
using Newtonsoft.Json;
using System.Dynamic;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class RNNProject_NT8_Indi : Indicator
{
#region Enum Declaration
public enum Optimizer
{
RMSProp,
SGD,
Adam,
Adagrad
};
public enum Architecture
{
LSTM,
GRU,
BidirectionalLSTM,
BidirectionalGRU
};
public enum Loss
{
MSE,
R2
};
#endregion
#region Private Variables
private Architecture architecture = Architecture.LSTM; // RNN Architecture
private Optimizer optimizer = Optimizer.RMSProp; // Optimizer
private Loss loss = Loss.MSE; // Loss
private bool gpu = true; // Allow GPU Computations ?
private bool train = true; // Allow Train ?
//Train size must be greater than window_size = 60
private int trainingSize = 500 ; // Train Size
private int epochs = 10; // Epochs
private int scale = 100; // Scale
private string fileName = "model1"; // File Name to export model
private double momentum = 0.9; // Momentum (for SGD)
private double learningRate = 0.001; // Learning Rate
private double testingPart = 10; // Percentage of Train/Test Split
private double testingWeight = 50; // Percentage of Train/Test Score Weights
private int bars = 5; // Number of future bars to predict
private int retrainInterval = 10; // Interval (in bars) after which to automatically retrain
private int prevTrain = 0; // Index of bar on which model was previously trained
private bool isTrained = false; // Varibale to check if the model has been trained or not
private bool DEBUG = true;
private bool isReceived = false;
public TcpClient socket; // Creating client for connection via socket
public NetworkStream stream; // NetworkStream variable to read and write data
#endregion
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Indicator to Predict Time Series data using a model trained by RNN";
Name = "RNNProject_NT8_Indi";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
retrain = true;
}
else if (State == State.Configure)
{
}
}
protected void receivePred()
{
if (DEBUG) Print("Can Read : " + stream.CanRead.ToString());
if (DEBUG) Print("Is Data Available : " + stream.DataAvailable.ToString());
if(stream.DataAvailable)
{
byte[] data = new Byte[2*256];
string response = string.Empty;
Int32 bytes = stream.Read(data, 0, data.Length);
response = Encoding.UTF8.GetString(data,0,bytes);
if(response != string.Empty)
{
if (DEBUG) Print("Received : " + response);
Print("Data Received Successfully!");
Draw.TextFixed(this, "Chart Message", "Predictions Received!\nPlotting on Chart..." , TextPosition.TopRight);
dynamic jsonObject = new ExpandoObject();
jsonObject = JsonConvert.DeserializeObject(response);
// Plotting the predictions on the chart
for (int i=-1; i>=-1*bars; i--)
{
double ypred = double.Parse(jsonObject.Pred[(-1*i)-1].ToString());
Draw.Dot(this, "Prediction " + i.ToString(), true, i, ypred, Brushes.Aqua);
Draw.TextFixed(this, "Chart Message", "" , TextPosition.TopRight);
}
stream.Close();
socket.Close();
}
}
else
{
Print("Please Wait... Loading Predictions...");
Draw.TextFixed(this, "Chart Message", "Loading Predictions\nPlease Wait..." , TextPosition.TopRight);
}
}
protected override void OnBarUpdate()
{
// For Running on Real Time Data
if (State == State.Historical)
return;
if ( train == true)
{
// Collect Enough Data
if (CurrentBar < trainingSize)
{
Draw.TextFixed(this, "Chart Message", "Not Enough data for Training!" , TextPosition.TopRight);
return;
}
// Number of bars elapsed since previous Training
int interval = CurrentBar - prevTrain;
if (!isTrained || (retrain && interval == retrainInterval))
{
// Establishing connection
try
{
socket = new TcpClient();
socket.Connect("localhost", 9090); // Connecting to python server on localhost
stream = socket.GetStream(); // Creating stream to read and write data
}
catch (ArgumentNullException e)
{
Print(Time[0].ToString()+ " Exception Occured! The hostname parameter is null. "+ e.ToString());
}
catch (ArgumentOutOfRangeException e)
{
Print(Time[0].ToString()+ " Exception Occured! The port parameter is not between MinPort and MaxPort."+ e.ToString());
}
catch (SocketException e)
{
Print(Time[0].ToString()+ " Exception Occured! "+ e.ToString());
}
catch (ObjectDisposedException e)
{
Print(Time[0].ToString()+ " Exception Occured! TcpClient is closed. "+ e.ToString());
}
if (socket.Connected)
{
Print("connected to localhost : 9090");
Draw.TextFixed(this, "Chart Message", "Connected!" , TextPosition.TopRight);
// Collecting close Price and Dates data
List<string> closePrice = new List<string>();
List<string> time = new List<string>();
for (int index = 0; index < trainingSize; index++)
{
closePrice.Add(Close[index].ToString() );
time.Add(Time[index].ToString());
}
closePrice.Reverse();
time.Reverse();
// Creating dynamic object to store model parameters
dynamic jsonObject = new ExpandoObject();
jsonObject.Data = closePrice;
jsonObject.Time = time;
jsonObject.FileName = fileName;
jsonObject.Train = train;
jsonObject.GPU = gpu;
jsonObject.Architecture = (int)architecture;
jsonObject.Optimizer = (int)optimizer;
jsonObject.Loss = (int)loss;
jsonObject.LearningRate = learningRate;
jsonObject.Epochs = epochs;
jsonObject.Scale = scale;
jsonObject.Momentum = momentum;
jsonObject.TestingPart = testingPart;
jsonObject.TestingWeight = testingWeight;
jsonObject.Bars = bars;
string jsonString = JsonConvert.SerializeObject(jsonObject);
Byte[] data = Encoding.UTF8.GetBytes(jsonString);
if (stream.CanWrite)
{
stream.Write(data, 0, data.Length);
if (DEBUG)
Print("Sent : " + jsonString);
Print("Data Sent Successfully!");
Draw.TextFixed(this, "Chart Message", "Data Sent..." , TextPosition.TopRight);
isTrained = true;
prevTrain = CurrentBar;
}
else
{
Print("Data cannot be sent to the stream!");
stream.Close();
socket.Close();
return;
}
}
else
{
Print("Connection Failed!");
Draw.TextFixed(this, "Chart Message", "Connection Failed!" , TextPosition.TopRight);
}
}
// Receiving result from trained model
if(socket.Connected)
{
receivePred();
}
} // end of function implementing prediction with real time training
// When Train is False
else if(!isReceived)
{
// Establishing connection
try
{
socket = new TcpClient();
socket.Connect("localhost", 9090); // Connecting to python server on localhost
stream = socket.GetStream(); // Creating stream to read and write data
}
catch (ArgumentNullException e)
{
Print(Time[0].ToString()+ " Exception Occured! The hostname parameter is null. "+ e.ToString());
}
catch (ArgumentOutOfRangeException e)
{
Print(Time[0].ToString()+ " Exception Occured! The port parameter is not between MinPort and MaxPort."+ e.ToString());
}
catch (SocketException e)
{
Print(Time[0].ToString()+ " Exception Occured! "+ e.ToString());
}
catch (ObjectDisposedException e)
{
Print(Time[0].ToString()+ " Exception Occured! TcpClient is closed. "+ e.ToString());
}
if (socket.Connected)
{
Print("Connected to localhost : 9090");
Draw.TextFixed(this, "Chart Message", "Connected!" , TextPosition.TopRight);
// Creating dynamic object to store model parameters
dynamic jsonObject = new ExpandoObject();
jsonObject.FileName = fileName;
jsonObject.Train = train;
jsonObject.Bars = bars;
string jsonString = JsonConvert.SerializeObject(jsonObject);
Byte[] data = Encoding.UTF8.GetBytes(jsonString);
if (stream.CanWrite)
{
stream.Write(data, 0, data.Length);
Print("Data Sent Successfully!");
Draw.TextFixed(this, "Chart Message", "Data Sent..." , TextPosition.TopRight);
}
else
{
Print("Data cannot be sent to the stream!");
stream.Close();
socket.Close();
return;
}
if (DEBUG) Print("Can Read : " + stream.CanRead.ToString());
if (DEBUG) Print("Is Data Available : " + stream.DataAvailable.ToString());
if(stream.CanRead)
{
byte[] recData = new Byte[256];
string response = string.Empty;
Int32 bytes = stream.Read(recData, 0, recData.Length);
response = Encoding.UTF8.GetString(recData,0,bytes);
if(response != string.Empty)
{
if (DEBUG) Print("Received : " + response);
Print("Data Received Successfully!");
Draw.TextFixed(this, "Chart Message", "Predictions Received!\nPlotting on Chart..." , TextPosition.TopRight);
dynamic jsonObj = new ExpandoObject();
jsonObj = JsonConvert.DeserializeObject(response);
// Plotting the predictions on the chart
for (int i=-1; i>=-1*bars; i--)
{
double ypred = double.Parse(jsonObj.Pred[(-1*i)-1].ToString());
Draw.Dot(this, "Prediction " + i.ToString(), true, i, ypred, Brushes.Aqua);
Draw.TextFixed(this, "Chart Message", "" , TextPosition.TopRight);
}
stream.Close();
socket.Close();
}
}
else
{
Print("Prediction Data Not Available!\nPlease Train the Model...");
Draw.TextFixed(this, "Chart Message", "Predictions Not Available!\nPlease Train the Model..." , TextPosition.TopRight);
}
isReceived = true;
}
else
{
Print("Connection Failed!");
Draw.TextFixed(this, "Chart Message", "Connection Failed!" , TextPosition.TopRight);
}
} // end of function implementing prediction without training
} // end of OnBarUpdate()
#region Properties
[Display(Name = "Architecture", Order = 0, Description="Architecture of the Training Model")]
public Architecture m_architecture
{
get { return architecture; }
set { architecture = value; }
}
[Display(Name = "Optimizer", Order = 1, Description="Optimizer to be Used")]
public Optimizer m_optimizer
{
get { return optimizer; }
set { optimizer = value; }
}
[Display(Name = "Loss", Order = 2, Description="Loss Function")]
public Loss m_loss
{
get { return loss; }
set { loss = value; }
}
[Display(Name = "GPU", Order = 3,Description="If GPU is enabled")]
public bool m_gpu
{
get {return gpu;}
set{gpu = value;}
}
[Display(Name = "Train", Order = 4, Description="If training is enabled")]
public bool m_train
{
get {return train;}
set{train = value;}
}
[Display(Name = "Training Size", Order = 5, Description="Size of data to be sent for training")]
public int m_trainingSize
{
get {return trainingSize;}
set{ trainingSize = value;}
}
[Display(Name = "Epochs", Order = 6, Description="Epochs")]
public int m_epochs
{
get {return epochs;}
set{epochs = value;}
}
[Display(Name = "Scale", Order = 7, Description="Scaling Parameter")]
public int m_scale
{
get {return scale;}
set{scale = value;}
}
[Display(Name = "Bars to Predict", Order = 8, Description="Number of future bars to predict")]
public int m_bars
{
get {return bars;}
set{bars = value;}
}
[Display(Name = "FileName", Order = 9, Description="Name of file to store Model")]
public string m_fileName
{
get {return fileName;}
set {fileName = value;}
}
[Display(Name = "Momentum", Order = 10, Description="Momentum")]
public double m_momentum
{
get {return momentum;}
set{momentum = value;}
}
[Display(Name = "Learning Rate", Order = 11, Description="Learning Rate for the model")]
public double m_learningRate
{
get {return learningRate;}
set{learningRate = value;}
}
[Display(Name = "Testing Part", Order = 12, Description="Train/Test data split (in percentage)")]
public double m_testingPart
{
get {return testingPart ;}
set{ testingPart = value;}
}
[Display(Name = "Testing Weight", Order = 13,Description="Train/Test score(in percentage)")]
public double m_testingWeight
{
get {return testingWeight;}
set{testingWeight = value;}
}
[Display(Name = "Retrain", Order = 14, Description="Whether Retraining should be done or Not")]
public bool retrain
{
get; set;
}
[Display(Name = "Retrain Interval(in bars)", Order = 15, Description="Interval after which to automatically retrain")]
public int m_retrainInterval
{
get {return retrainInterval;}
set{retrainInterval = value;}
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private RNNProject_NT8_Indi[] cacheRNNProject_NT8_Indi;
public RNNProject_NT8_Indi RNNProject_NT8_Indi()
{
return RNNProject_NT8_Indi(Input);
}
public RNNProject_NT8_Indi RNNProject_NT8_Indi(ISeries<double> input)
{
if (cacheRNNProject_NT8_Indi != null)
for (int idx = 0; idx < cacheRNNProject_NT8_Indi.Length; idx++)
if (cacheRNNProject_NT8_Indi[idx] != null && cacheRNNProject_NT8_Indi[idx].EqualsInput(input))
return cacheRNNProject_NT8_Indi[idx];
return CacheIndicator<RNNProject_NT8_Indi>(new RNNProject_NT8_Indi(), input, ref cacheRNNProject_NT8_Indi);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.RNNProject_NT8_Indi RNNProject_NT8_Indi()
{
return indicator.RNNProject_NT8_Indi(Input);
}
public Indicators.RNNProject_NT8_Indi RNNProject_NT8_Indi(ISeries<double> input )
{
return indicator.RNNProject_NT8_Indi(input);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.RNNProject_NT8_Indi RNNProject_NT8_Indi()
{
return indicator.RNNProject_NT8_Indi(Input);
}
public Indicators.RNNProject_NT8_Indi RNNProject_NT8_Indi(ISeries<double> input )
{
return indicator.RNNProject_NT8_Indi(input);
}
}
}
#endregion