-
Notifications
You must be signed in to change notification settings - Fork 16
/
bruteforce.js
255 lines (202 loc) · 9.17 KB
/
bruteforce.js
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
const async = require('async');
const nodemailer = require('nodemailer');
const randomExt = require('random-ext');
const rp = require('request-promise');
const { some } = require('bluebird');
const fs = require('fs-extra');
const flat = require('flat');
const util = require('util');
const humanize = require('humanize');
var replaceall = require("replaceall");
//setup variables
var viableStrategies = [];
var stratKey = "";
var configs =[];
var count=0;
//configuration elements
//starting with paths and the all important gekko config
//where are your gekko strategies?
var strategiesFolder = '../gekko/strategies/';
//where is your config?
var configFile = '../gekko/config-backtester.js';
//where is your api server? (default is port 3000)
//to run the server type node gekko --ui in to the console
var apiUrl= "http://localhost:3000";
const config = require(configFile);
//then we setup the filewriter to store the backtests
//if you don't want to write the output to a file then set this to false, but then why the fuck else would you run this....derp
var writecsv = true;
//by default we throw the results into the folder and file you see below, the results will be appended...again....derp.
var resultCsv = __dirname+"/results/bruteforce.csv";
//then we load up the important shit!
//how many backtests do you want to run parralel, 1928374982734 I bet but unless you're armed with a serious bit of pro, multi cpu kit...how about you keep this lower than the number of cores you have for now?
var parallelqueries = 2;
//this is where it gets interesting right?
//RIGHT!!!!
//setup params for backtesting
//fuck json, this is pure arrays as god intended us pony coders to use
//throw in the candle sizes here
var candleSizes = [240,480,960];
//list different history sizes here
var historySizes = [10,20];
//ooo this looks fun - this is where you set up the trading pairs and back testing exchange data
//you can load up as many sets as you like
var tradingPairs = [["poloniex","btc","eth"],["binance","btc","omg"]];
//so this is the number of configs that will be generated with different strategy settings
//if you multiply this by the number of candle sizes and history sizes and trading pairs you'll get the total number of backtests this sucker will run
//Note: if you wanna test candle sizes, against the same config setup then just set this to 1. Cute right???
var numberofruns = 1000;
let dirCont = fs.readdirSync( strategiesFolder );
//oh wait, there's more....
//so there is another version of this script that will run every single strategy in your strategy file that has an entry in the config but while useful...it was a bit crap when it came to brute forcing shit. So now you have to enter in your strategy name.
//make sure the strategy has a config entry in the config below
let strategies = ["SchaffTrendCycle"];
for (var a = 0, len4 = tradingPairs.length; a < len4; a++) {
for (var j = 0, len1 = candleSizes.length; j < len1; j++) {
for (var k = 0, len2 = historySizes.length; k < len2; k++) {
//check which strategies have equivalent config entries for in the config
for (var i = 0, len = numberofruns; i < len; i++) {
stratKey = strategies[0];
config.tradingAdvisor.method = stratKey;
config.tradingAdvisor.candleSize = candleSizes[j];
config.tradingAdvisor.historySize = historySizes[k];
config.watch.exchange = tradingPairs[a][0];
config.watch.currency = tradingPairs[a][1];
config.watch.asset = tradingPairs[a][2];
this.baseConfig = {
gekkoConfig: {
debug: config.debug,
watch: {
exchange: config.watch.exchange,
currency: config.watch.currency,
asset: config.watch.asset,
},
SchaffTrendCycle: {
"stcLength": randomExt.integer(12,1),
"fastLength": randomExt.integer(20,1),
"slowLength": randomExt.integer(80,20),
"factor": 0.5,
"threshold_high": 80,
"threshold_low": 20,
"enable_stop_loss": true,
"stoploss_threshold": 5,
"adjust_false_signal": true,
"threshold_adjustment": 5
},
paperTrader: {
slippage: config.paperTrader.slippage,
feeTaker: config.paperTrader.feeTaker,
feeMaker: config.paperTrader.feeMaker,
feeUsing: config.paperTrader.feeUsing,
simulationBalance: config.paperTrader.simulationBalance,
reportRoundtrips: true,
enabled: true
},
tradingAdvisor: {
enabled: true,
method: config.tradingAdvisor.method,
candleSize: config.tradingAdvisor.candleSize ,
historySize: config.tradingAdvisor.historySize ,
},
trader: {
enabled: false,
},
backtest: {
daterange: config.backtest.daterange
},
performanceAnalyzer: {
'riskFreeReturn': 5,
'enabled': true
},
valid: true,
},
data: {
candleProps: ['close', 'start', 'open', 'high', 'volume','vwp'],
indicatorResults: false,
report: true,
roundtrips: false,
trades: false
}
};
configs.push(this.baseConfig);
}
}
}
}
//by this point you have an array of all the configs you're gonna run.
//run the backtests against all the stored configs.
hitApi(configs);
//this might look familiar...that's cos it's ripped from Gekkoga <3
async function hitApi(configs){
const results = await queue(configs, parallelqueries, async (data) => {
console.log("Running strategy - "+data.gekkoConfig.tradingAdvisor.method +" on "+data.gekkoConfig.tradingAdvisor.candleSize +" minute(s) candle size on "+ data.gekkoConfig.watch.exchange +" for "+ data.gekkoConfig.watch.currency + data.gekkoConfig.watch.asset);
const body = await rp.post({
url: `${apiUrl}/api/backtest`,
json: true,
body: data,
headers: { 'Content-Type': 'application/json' },
timeout: 1200000
});
// These properties will be outputted every epoch, remove property if not needed
const properties = ['balance', 'profit', 'sharpe', 'market', 'relativeProfit', 'yearlyProfit', 'relativeYearlyProfit', 'startPrice', 'endPrice', 'trades'];
const report = body.report;
let result = { profit: 0, metrics: false };
if (report) {
let picked = properties.reduce((o, k) => {
o[k] = report[k];
return o;
}, {});
result = { strat: data.gekkoConfig.tradingAdvisor.method, startdate: data.gekkoConfig.backtest.daterange.from, todate: data.gekkoConfig.backtest.daterange.to, profit: body.report.profit, sharpe: body.report.sharpe, metrics: picked };
}
//now we write the backtest results to file:
if(writecsv===true){
let runDate = humanize.date('d-m-Y');
let runTime = humanize.date('H:i:s');
var sharpe = 0;
if(report.sharpe){
sharpe = report.sharpe;
}
let currencyPair = report.currency+report.asset;
let configCsvTmp1 = JSON.stringify(data.gekkoConfig[data.gekkoConfig.tradingAdvisor.method]);
let configCsv = replaceall(",", "|", configCsvTmp1)
headertxt = "Strategy, Market performance(%),Strat performance (%),Profit,Run date, Run time, Start date, End date,Currency pair, Candle size, History size,Currency, Asset, Timespan,Yearly profit, Yearly profit (%), Start price, End price, Trades, Start balance, Sharpe, Alpha, Config\n";
outtxt = data.gekkoConfig.tradingAdvisor.method+","+ report.market+","+ report.relativeProfit+","+ report.profit+","+runDate+","+runTime+","+ data.gekkoConfig.backtest.daterange.from+","+ data.gekkoConfig.backtest.daterange.to+","+ currencyPair+","+ data.gekkoConfig.tradingAdvisor.candleSize+","+ data.gekkoConfig.tradingAdvisor.historySize+","+ report.currency+","+ report.asset+","+ report.timespan+","+ report.yearlyProfit+","+ report.relativeYearlyProfit+","+ report.startPrice+","+ report.endPrice+","+ report.trades+","+ report.startBalance+","+ sharpe+","+ report.alpha+","+ configCsv+"\n";
if (fs.existsSync(resultCsv)){
fs.appendFileSync(resultCsv, outtxt, encoding = 'utf8');
}else{
fs.appendFileSync(resultCsv, headertxt, encoding = 'utf8');
fs.appendFileSync(resultCsv, outtxt, encoding = 'utf8');
}
//to do
//write strategy file to a new file with a key
//ensure the config it appended to the strategy file
}
return result;
})
.catch((err)=>{
//console.log(err)
throw err
});
return results;
}
function queue(items, parallel, ftc) {
const queued = [];
return Promise.all(items.map((item) => {
const mustComplete = Math.max(0, queued.length - parallel + 1);
const exec = some(queued, mustComplete).then(() => ftc(item));
queued.push(exec);
return exec;
}))
.catch((err)=>{
console.log(err)
throw err
});
}
function getConfig(data, stratName) {
const conf = Object.assign({}, this.baseConfig);
conf.gekkoConfig[stratName] = Object.keys(data).reduce((acc, key) => {
acc[key] = data[key];
return acc;
}, {});
return conf;
}