forked from xFFFFF/Gekko-Strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn8.js
258 lines (191 loc) · 6.61 KB
/
n8.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
256
257
258
// Downloaded from: https://github.com/xFFFFF/Gekko-Strategies
// Source: https://justhodl.blogspot.com/
var convnetjs = require('convnetjs')
var z = require('zero-fill')
var stats = require('stats-lite')
var n = require('numbro')
var math = require('mathjs')
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
var _ = require('lodash');
var gauss = require('gauss');
const deepqlearn = require('convnetjs/build/deepqlearn');
var log = require('../core/log.js');
// the below line starts you at 0 threads
global.forks = 0
// the below line is for calculating the last mean vs the now mean.
var oldmean = 0
getOption = function () {
}
// let's create our own method
var method = {};
options = "Options Set";
options.period = "1m";
options.period_length = "1m";
options.activation_1_type = "regression";
options.neurons_1 = 25;
options.depth = 1;
options.selector = "Gdax.BTC-USD";
options.min_periods = 1500;
options.min_predict = 1;
options.momentum =0.2;
options.decay = 0.1;
options.threads = 4;
options.learns = 2;
/*
[myStoch]
highThreshold = 80
lowThreshold = 20
optInFastKPeriod = 14
optInSlowKPeriod = 5
optInSlowDPeriod = 5
[myLongEma]
optInTimePeriod = 100
[myShortEma]
optInTimePeriod = 50
[stopLoss]
percent = 0.9*/
var hasbought = false;
var stochParams = {
optInFastKPeriod: 8,
optInSlowKPeriod: 3,
optInSlowDPeriod: 3
};
var VarList = new gauss.Collection();
neural = undefined;
// prepare everything our method needs
method.init = function() {
this.requiredHistory = this.tradingAdvisor.historySize;
if (neural === undefined) {
// Create the net the first time it is needed and NOT on every run
neural = {
net : new convnetjs.Net(),
layer_defs : [
{type:'input', out_sx:4, out_sy:4, out_depth:options.depth},
{type:'fc', num_neurons:options.neurons_1, activation:options.activation_1_type},
{type:'regression', num_neurons:5}
],
neuralDepth: options.depth
}
neural.net.makeLayers(neural.layer_defs);
neural.trainer = new convnetjs.SGDTrainer(neural.net, {learning_rate:0.05, momentum:options.momentum, batch_size:10, l2_decay:options.decay});
}
}
var haspredicted = false;
var predictioncount = 0;
var maxaccuracy = 0;
var lowaccuracy = 0;
var highpeak =6;
var lowpeak =-100;
// what happens on every new candle?
method.update = function(candle) {
this.HCL = (this.candle.high + this.candle.close + this.candle.open) /3;
Price.push(candle.close);
if(Price.length > 2)
{
var tlp = []
var tll = []
var my_data = Price;
var learn = function () {
for (var i = 0; i < Price.length - 1; i++) {
var data = my_data.slice(i, i + 1);
var real_value = [my_data[i + 1]];
var x = new convnetjs.Vol(data);
neural.trainer.train(x, real_value);
var predicted_values =neural.net.forward(x);
var accuracy = predicted_values.w[0] -real_value
var accuracymatch = predicted_values.w[0] == real_value;
var rewardtheybitches = neural.net.backward(accuracymatch);
if(accuracy > 0)
{
if(accuracy > maxaccuracy) {maxaccuracy = accuracy}
}
if(accuracy <0)
{
if(accuracy < lowaccuracy) {lowaccuracy = accuracy}
}
predictioncount++;
haspredicted = true;
}
}
learn();
// var json = neural.net.toJSON();
// // the entire object is now simply string. You can save this somewhere
// var str = JSON.stringify(json);
// log.debug(str);
}
}
method.log = function() {
}
method.handleposition = function(){
}
var Price = [];
function per(num, amount){
return num*amount/100;
}
ManageSize = function(){
var calculatedpercent = per(Price.length,5);
Price.splice(0,calculatedpercent);
}
method.check = function() {
//Learn
var predict = function(data) {
var x = new convnetjs.Vol(data);
var predicted_value = neural.net.forward(x);
return predicted_value.w[0];
}
this.HCL = (this.candle.high + this.candle.close + this.candle.open) /3;
if(haspredicted & predictioncount > 1000)
{
var item = Price;
prediction = predict(item)
mean = Price[Price.length -1];
oldmean = prediction
meanp = math.mean(prediction, mean)
global.meanp = meanp
global.mean = mean
var percentvar = (meanp-mean)/mean * 100;
VarList.push(percentvar);
if(percentvar < 0) {
prediction += lowaccuracy;
percentvar += lowaccuracy;
if(lowpeak > percentvar) { lowpeak = percentvar;}
}
if(percentvar > 0) {
prediction -= maxaccuracy;
percentvar -= maxaccuracy;
if(highpeak < percentvar) { highpeak = percentvar;}
}
var VectorVar = VarList.toVector();
var Variance =( VectorVar.variance());
var Density30 = VectorVar.density(30);
var top = per(Density30[Density30.length -1],-30);
var low = per(Density30[0],10);
global.sig0 = global.meanp < global.mean && meanp != 0
if (global.sig0 === false && percentvar > low)
{
log.debug("IA - Buy - Predicted variation: ",percentvar);
log.debug("Variance :",Variance);
log.debug("Density 30: low ",low);
hasbought = true;
meanp = 0;
mean = 0;
haspredicted = false;
ManageSize();
return this.advice('long');
}
else if
(global.sig0 === true && percentvar < top)
{
// log.debug("IA - Sell - Predicted variation: ",percentvar);
// log.debug("Density 30 high : ",top);
// log.debug("Variance :",Variance);
meanp = 0;
mean = 0;
hasbought = false;
haspredicted = false;
return this.advice('short');
}
}
}
module.exports = method;