-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_dataset.js
More file actions
165 lines (140 loc) · 6.33 KB
/
create_dataset.js
File metadata and controls
165 lines (140 loc) · 6.33 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
153
154
155
156
157
158
159
160
161
162
163
164
165
const DataSource = require('./datasource.js').DataSource;
const DataSourceTimeframe = require('./datasource.js').DataSourceTimeframe;
const DEBUG = require('./debug.js');
const Utils = require('./utils.js');
const {BacktesterDataType, BacktesterTradeDirection, TechnicalIndicator} = require('./enums.js');
const ChartSegment = require('./chart_segment.js');
const fs = require('fs');
// DEBUG.showWarnings = false;
let options = {
//Relative path to the data file
dataFile: "./data/forex/gbp_usd_2019_ohlc.csv",
//The type of the data either OHLC, or Bid/Ask (BA)
dataType: BacktesterDataType.OHLC,
// The delimeter the data will be split on line by line
// (e.g. given an example line of data: 20190101 171100,1.272270,1.272270,1.272270,1.272270,0)
// we split on the comma
delimeter: ",",
//The expected number of columns in a data line (used to ignore empty lines)
expectedLineLength: 8,
//The index in the split data line of each part of data
indexes: {
open: 1,
high: 2,
low: 3,
close: 4
},
//Ignores the first line of the data file, often because that line is used for headers for columns and not data
ignoreFirstLine: true,
//The resolution of the inputted data file as a DataSourceTimeframe object (1 minute, 5 minute, 10 minute etc...)
resolution: DataSourceTimeframe.M_1,
//Optional - a lower resolution that the incoming data will be downsampled to (e.g. from M_1 to M_5 will DS from 1m bars to 5m bars)
// downsampleResolution: DataSourceTimeframe.M_5,
//Optionally request for some technical indicators to be calculated
indicators: [
TechnicalIndicator.EMA_12,
TechnicalIndicator.EMA_30
]
}
//My variables
let pFast, pSlow, pCross, pUid;
let bufLength = 26;
let buf = [];
let values = {};
let results = [];
const MarketDirection = {
Uptrend: 1,
Ranging: 0,
Downtrend: -1
}
const minimumMovement = 0.15;
//Create data source pointing to passed in file
const source = new DataSource(options.dataFile);
//Start streaming the data from the file
source.start(function(data) {
//Get current indicator values
let fast = data.indicators[options.indicators[0].name];
let slow = data.indicators[options.indicators[1].name];
//Setup buffer
buf.push(data);
if (buf.length >= bufLength) {
buf = buf.slice(buf.length-bufLength, buf.length)
}
//Check if the previous values are present
if (pFast != null && pSlow != null && buf.length == bufLength) {
//Crossing to uptrend
if (pSlow > pFast && slow < fast) {
if (pCross != null) {
let uid = Utils.guid();
let segment = new ChartSegment(buf, options.indicators[0].name, options.indicators[1].name);
let shapeHash = segment.calculateShapeHash(3);
let percentChange = Utils.percentageChange(data.close, pCross);
DEBUG.warning(`Ending a downtrend, pCross was ${pCross}, price is now ${data.close} pd of ${percentChange}`);
values[uid] = {
shapeHash: shapeHash,
crossDirection: MarketDirection.Uptrend
}
if (pUid != null) {
if (pUid in values) {
let marketDirection;
if (percentChange >= minimumMovement) {
marketDirection = MarketDirection.Uptrend;
} else if (percentChange < minimumMovement && percentChange > (-1 * minimumMovement)) {
marketDirection = MarketDirection.Ranging;
} else if (percentChange <= (-1 * minimumMovement)) {
marketDirection = MarketDirection.Downtrend;
}
values[pUid]["change"] = percentChange;
values[pUid]["direction"] = marketDirection
results.push(JSON.parse(JSON.stringify(values[pUid])));
delete values[pUid];
DEBUG.warning("Finished downtrend");
}
}
pUid = uid;
}
DEBUG.warning(`Setting pCross to ${data.close}, starting an uptrend`);
pCross = data.close;
}
//Crossing to downtrend
if (pSlow < pFast && slow > fast) {
if (pCross != null) {
let uid = Utils.guid();
let segment = new ChartSegment(buf, options.indicators[0].name, options.indicators[1].name);
let shapeHash = segment.calculateShapeHash(3);
let percentChange = Utils.percentageChange(data.close, pCross);
DEBUG.warning(`Ending an uptrend, pCross was ${pCross}, price is now ${data.close} pd of ${percentChange}`);
values[uid] = {
shapeHash: shapeHash,
crossDirection: MarketDirection.Downtrend
}
if (pUid != null) {
if (pUid in values) {
let marketDirection;
if (percentChange >= minimumMovement) {
marketDirection = MarketDirection.Uptrend;
} else if (percentChange < minimumMovement && percentChange > (-1 * minimumMovement)) {
marketDirection = MarketDirection.Ranging;
} else if (percentChange <= (-1 * minimumMovement)) {
marketDirection = MarketDirection.Downtrend;
}
values[pUid]["change"] = percentChange;
values[pUid]["direction"] = marketDirection
results.push(JSON.parse(JSON.stringify(values[pUid])));
DEBUG.warning("Finished uptrend");
delete values[pUid];
}
}
pUid = uid;
}
DEBUG.warning(`Setting pCross to ${data.close}, starting a downtrend`);
pCross = data.close;
}
}
pFast = fast;
pSlow = slow;
}, function() {
//Write results to a file as JSON
fs.writeFileSync("./results.json", JSON.stringify(results, null, 2), 'utf8');
DEBUG.log(`Written results to file`);
}, options)