forked from jjuribe/Indicators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadCSV.cs
382 lines (325 loc) · 12.2 KB
/
LoadCSV.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
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
using System.IO;
using System.Windows.Forms;
//using System.Xml.Linq;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class LoadCSV : Indicator
{
private string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
private string[] fileNames;
private struct TradeResults
{
/// Trade Count, Date In, Date Out, Ticker, profit, cumProfit,
public int tradeNum { get; set; }
public string dateEntry { get; set; }
public string dateExit { get; set; }
public string ticker { get; set; }
public double profit { get; set; }
public double cumProfit { get; set; }
/// exit name, profit factor, winPct, Consecutive losers,
public string exitName { get; set; }
public double profitFactor { get; set; }
public double winPct { get; set; }
public int consecutiveLosers { get; set; }
/// largest loser, largest winner, profit per month
public double largestLoser { get; set; }
public double largestWinner { get; set; }
public double profitPerMonth { get; set; }
};
private List<TradeResults> allTradeResults = new List<TradeResults>();
private int fileCount;
private int filesParsedCount;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Indicator here.";
Name = "Load CSV";
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;
AddPlot(Brushes.DodgerBlue, "Profit");
AddPlot(new Stroke(Brushes.LightSkyBlue, 2), PlotStyle.Bar, "Cost");
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
ClearOutputWindow();
fileNames = getFileNames();
readAllofThe(files: fileNames, debug: false);
}
}
protected override void OnBarUpdate()
{
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Filter Struct
///
/// ////////////////////////////////////////////////////////////////////////////////////////////////
private void ListFiltering(List<TradeResults> allStructs)
{
Print("List Filtering");
/// get a list of tickers in struct array
List<string> tickers = getPTickersFrom(allStructs: allStructs, debug: true);
/// get a list of all profit fact0rs
// List<double> allPF = new List<double>();
// foreach ( var symbol in tickers ) {
// allPF.Add(getProfitFactor(allStructs: allStructs, ticker: symbol, debug: true));
// }
/// private List<TradeResults> allTradeResults = new List<TradeResults>();
// /// get an average of the pf averages
// List<double> avgPF = new List<double>();
// foreach ( var list in allPF ) {
// /// show PF
// Print("Profit factor array (sum, avg, min, max) \t " + list);
// avgPF.Add(list.Item2);
// /// show profit
// }
// var avg = allPF.Average();
// Print("the avg profit factor is " + avg);
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////
/// Get Tickers fromStruct Array
/// ////////////////////////////////////////////////////////////////////////////////////////////////
private List<string> getPTickersFrom(List<TradeResults> allStructs, bool debug) {
Print("\nGet Ticker Names" );
List<string> tickerItems = new List<string>();
foreach ( var thing in allStructs ) {
tickerItems.Add(thing.ticker);
}
List<string> noDupestickerItems = tickerItems.Distinct().ToList();
if ( debug ) {
foreach ( var ticker in noDupestickerItems ) {
Print(ticker);
}
}
return noDupestickerItems;
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////
/// Get Profit Factor
/// ////////////////////////////////////////////////////////////////////////////////////////////////
private double getProfitFactor(List<TradeResults> allStructs, string ticker , bool debug) {
var tickerFilter = allStructs.Where(o => o.ticker.Contains(ticker));
List<double> itemsPF = new List<double>();
Print("\nArray of PF for " + ticker );
foreach ( var thing in tickerFilter ) {
itemsPF.Add(thing.profitFactor);
Print(thing.profitFactor);
}
Print("");
Print("last profit factor " + itemsPF.Last() );
return itemsPF.Last();
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////
/// Get Min max avg
/// ////////////////////////////////////////////////////////////////////////////////////////////////
private Tuple<double, double, double, double> getAvgMinMax(List<double> items, string ticker , bool debug) {
// This will give you a double[3] array with the items of the list.
double[] itemsArray = items.ToArray();
var sum = itemsArray.Sum();
var avg = itemsArray.Average();
var min = itemsArray.Min();
var max = itemsArray.Max();
if ( debug ) {
Print(ticker + "\tSum " + sum.ToString("0.0") + "\tAvg " + avg.ToString("0.0") + "\tMin " + min.ToString("0.0") + "\t Max " + max.ToString("0.0"));
}
return new Tuple<double, double, double, double>(sum, avg, min, max);
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////
/// Create Struct from line
/// ////////////////////////////////////////////////////////////////////////////////////////////////
private void createStructFrom(string[] oneFile, bool debug) {
foreach (String rows in oneFile) {
TradeResults tradeResults = new TradeResults{};
//Print(rows);
/// Trade Count, Date In, Date Out, Ticker, lastProfitCurrency, cumProfit, exit name, profit factor, winPct Consecutive losers, largest loser, largest winner, profit per month
var columns = rows.Split(',');
if ( debug ) { Print(
columns[0] +"\t"+ // count
columns[1] +"\t"+ // Date In
columns[2] +"\t" + // Date out
columns[3] +"\t"+ // Ticker
columns[4] +"\t"+ // profit
columns[5] +"\t" + // cumulative
columns[6] +"\t" + // exit name
columns[7] +"\t"+ // pf
columns[8] +"\t"+ // winPct
columns[9] +"\t" + // consec loser
columns[10] +"\t"+ // LL
columns[11] +"\t"+ // LW
columns[12] +"\t"); } // monthlyProfit
tradeResults.tradeNum = Convert.ToInt16(columns[0]);
tradeResults.dateEntry = columns[1];
tradeResults.dateExit = columns[2];
tradeResults.ticker = columns[3];
tradeResults.profit = Convert.ToDouble( columns[4]);
tradeResults.cumProfit = Convert.ToDouble( columns[5]);
tradeResults.exitName = columns[6];
tradeResults.profitFactor = Convert.ToDouble( columns[7]);
tradeResults.winPct = Convert.ToDouble( columns[8]);
tradeResults.consecutiveLosers = Convert.ToInt16(columns[9]);
tradeResults.largestLoser = Convert.ToDouble( columns[10]);
tradeResults.largestWinner = Convert.ToDouble( columns[11]);
tradeResults.profitPerMonth = Convert.ToDouble( columns[12]);
allTradeResults.Add(tradeResults);
//arr.Append(tradeResults);
}
filesParsedCount += 1;
///show struct array when filished
if ( filesParsedCount == fileCount ) {
Print("\n"+ filesParsedCount + " of " + fileCount + " ticker backtest files parsed\n");
Print("\nFinished with creating Structs...");
foreach (TradeResults thing in allTradeResults) {
if ( debug ) { Print(thing.tradeNum +" \t\t\t"+ thing.dateEntry +"\t\t\t"+ thing.dateExit +" \t\t\t"+ thing.ticker +"\t\t\t"+ thing.profit + "\t\t\t"+ thing.cumProfit +
" \t\t\t"+ thing.exitName +"\t\t\t"+ thing.profitFactor +" \t\t\t"+ thing.winPct +"\t\t\t"+ thing.consecutiveLosers +
"\t\t\t"+ thing.largestLoser +" \t\t\t"+ thing.largestWinner +"\t\t\t"+ thing.profitPerMonth
); }
}
ListFiltering(allStructs: allTradeResults);
}
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Get files in folder
///
/// ////////////////////////////////////////////////////////////////////////////////////////////////
private void readAllofThe(string[] files, bool debug) {
Print("--- Got " + files.Length.ToString() + " CSV Files: ---");
foreach (string name in fileNames)
{
if ( debug ) { Print(name); }
readAllLinesFrom(filePath: name, debug: debug);
}
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Get file names
///
/// ////////////////////////////////////////////////////////////////////////////////////////////////
private string[] getFileNames() {
var dateTime = DateTime.Today.ToString("MM_dd_yyyy") ;
var filePath = systemPath+ @"\Channel"+"_"+ dateTime;
// Put all file names in root directory into array.
string[] array1 = Directory.GetFiles(filePath);
fileCount = array1.Length;
return array1;
}
/// ////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Read entire CSV
///
/// ////////////////////////////////////////////////////////////////////////////////////////////////
private void readAllLinesFrom(string filePath, bool debug) {
try
{
string[] oneFile = System.IO.File.ReadAllLines(filePath);
createStructFrom(oneFile: oneFile, debug: false);
}
catch(Exception e)
{
Print("\nALERT !!! readAllLinesFrom() Exception: " + e.Message);
}
finally
{
if ( debug ) { Print("Executing finally block."); }
}
}
#region Properties
[Browsable(false)]
[XmlIgnore]
public Series<double> Profit
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> Cost
{
get { return Values[1]; }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private LoadCSV[] cacheLoadCSV;
public LoadCSV LoadCSV()
{
return LoadCSV(Input);
}
public LoadCSV LoadCSV(ISeries<double> input)
{
if (cacheLoadCSV != null)
for (int idx = 0; idx < cacheLoadCSV.Length; idx++)
if (cacheLoadCSV[idx] != null && cacheLoadCSV[idx].EqualsInput(input))
return cacheLoadCSV[idx];
return CacheIndicator<LoadCSV>(new LoadCSV(), input, ref cacheLoadCSV);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.LoadCSV LoadCSV()
{
return indicator.LoadCSV(Input);
}
public Indicators.LoadCSV LoadCSV(ISeries<double> input )
{
return indicator.LoadCSV(input);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.LoadCSV LoadCSV()
{
return indicator.LoadCSV(Input);
}
public Indicators.LoadCSV LoadCSV(ISeries<double> input )
{
return indicator.LoadCSV(input);
}
}
}
#endregion