-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVela_Cede_2.js
312 lines (208 loc) · 8.12 KB
/
Vela_Cede_2.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
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
const request = require('request');
var fs = require('fs');
const moment = require('moment');
async function Bucle (resolution){
let array_acciones = ["AAPL","AMAT","AMD","C","CSCO","EBAY","GILD","INTC","KO","LVS","MET","MRK","MS","MU","ORCL","QCOM","SBUX","SNAP","TWTR","UAL","UBER","VZ","WFC","XOM"];
let operaciones_encontradas = [];
console.log("Comenzando a analizar los gráficos de "+resolution+" minutos");
let array_promises = [];
for(let accion of array_acciones){
let operacion = Peticion(accion,resolution)
array_promises.push(operacion);
}
let array_operaciones= [];
await Promise.all(array_promises).then(values =>{
for(let a of values){
if(a.jugadas[0]){
array_operaciones.push(a);
}
}
console.log("Revisión de los gráficos de "+resolution+" completada");
})
for(let operacion of array_operaciones){
for(let orden of operacion.jugadas){
await Crear_Json(orden);
}
}
return;
}
function Peticion(accion,resolution) {
let now= new Date();
let fecha= Math.round(now.getTime()/1000);
let inicio = fecha - 360000 * resolution;
let operacion={
divisas:accion,
tiempo:resolution,
jugadas:[]
};
return new Promise(resolve => {
request('https://finnhub.io/api/v1/stock/candle?symbol='+accion+'&resolution='+resolution+'&from='+inicio+'&to='+fecha+'&token=c2qih02ad3ickc1loc60', { json: true }, (err, res, body) => {
if (err) { return reject(err); }
let datos = Formatear(res.body);
let datos_procesados = Procesar(datos);
let elefante = VelaElefante(datos_procesados);
if(elefante) {
console.log("Vela elefante en "+accion);
elefante.simbolo = operacion.divisas;
elefante.fecha = new Date().toLocaleString();
operacion.jugadas.push(elefante);
};
let VC = VelaCola(datos_procesados);
if(VC) {
console.log("Vela de cola en "+ accion)
VC.simbolo = operacion.divisas;
VC.fecha = new Date().toLocaleString();
operacion.jugadas.push(VC);
};
resolve(operacion);
});
});
};
async function Inicio(){
let minutos_5= Bucle(1);
}
function VelaElefante (array_datos){
if(!array_datos.h){
console.log(array_datos);
}
let operacion={
ru : 50
};
let high = array_datos.h[array_datos.h.length-1];
let low = array_datos.l[array_datos.l.length-1];
let close = array_datos.c[array_datos.c.length-1];
let open = array_datos.o[array_datos.o.length-1];
let rango = Math.abs(high - low);
let cuerpo = Math.abs(open - close);
let MM20_10 = Media20(array_datos.c,array_datos.c.length-9);
let MM200 = Media200(array_datos.c,array_datos.c.length)
if(close > open && cuerpo > (0.7 * rango) && close > MM200 && MM200 > MM20_10){
operacion.jugada = "Vela Elefante";
operacion.direccion = "largo";
operacion.entrada = high;
operacion.stop = low;
operacion.riesgo = Math.round((high - low) * 100000) / 100000 ;
operacion.lotes = Math.round((operacion.ru /operacion.riesgo) * 100000) / 100000;
return operacion;
}
if(close < open && cuerpo > (0.7 * rango) && close < MM200 && MM200 < MM20_10){
operacion.jugada = "Vela Elefante";
operacion.direccion = "Corto";
operacion.entrada = low;
operacion.stop = high;
operacion.riesgo = Math.round((high - low) * 100000) / 100000 ;
operacion.lotes = Math.round((operacion.ru /operacion.riesgo) * 100000) / 100000;
return operacion;
}
return false;
}
function VelaCola (array_datos){
if(!array_datos.h){
console.log(array_datos);
}
let operacion={
ru : 50
};
let high = array_datos.h[array_datos.h.length-1];
let low = array_datos.l[array_datos.l.length-1];
let close = array_datos.c[array_datos.c.length-1];
let open = array_datos.o[array_datos.o.length-1];
let rango = Math.abs(high - low);
let cuerpo = Math.abs(open - close);
let MM20_10 = Media20(array_datos.c,array_datos.c.length-9);
let MM200 = Media200(array_datos.c,array_datos.c.length)
if(cuerpo < (0.3 * rango) && close > MM200 && MM200 > MM20_10 && open >= low + (0.6*rango) && close >= low + (0.6*rango)){
operacion.jugada = "Vela Elefante";
operacion.direccion = "largo";
operacion.entrada = high;
operacion.stop = low;
operacion.riesgo = Math.round((high - low) * 100000) / 100000 ;
operacion.lotes = Math.round((operacion.ru /operacion.riesgo) * 100000) / 100000;
return operacion;
}
if(cuerpo < (0.3 * rango) && close < MM200 && MM200 < MM20_10 && open <= low + (0.4*rango) && close <= low + (0.4*rango)){
operacion.jugada = "Vela Elefante";
operacion.direccion = "Corto";
operacion.entrada = low;
operacion.stop = high;
operacion.riesgo = Math.round((high - low) * 100000) / 100000 ;
operacion.lotes = Math.round((operacion.ru /operacion.riesgo) * 100000) / 100000;
return operacion;
}
return false;
}
function Formatear (array_datos){
//console.log(array_datos);
let datos_procesados = {
c:[],
h:[],
l:[],
o:[],
t:[]
}
for(i=0;i<array_datos.t.length; i++){
let date = new Date(array_datos.t[i] * 1000).toISOString();
let date2 = date.split("T")[0] + "T20:00:00.000Z";
let date3 = date.split("T")[0] + "T13:29:00.000Z"
if(moment(date).isBefore(date2) && moment(date).isAfter(date3)){
datos_procesados.c.push(array_datos.c[i]);
datos_procesados.h.push(array_datos.h[i]);
datos_procesados.l.push(array_datos.l[i]);
datos_procesados.o.push(array_datos.o[i]);
datos_procesados.t.push(array_datos.t[i]);
}
}
return datos_procesados;
}
function Procesar (array_datos){
let datos_procesados = {
c:[],
h:[],
l:[],
o:[],
t:[]
}
for(i=array_datos.c.length-1;i>0; i-=2){
datos_procesados.c.unshift(array_datos.c[i]);
datos_procesados.o.unshift(array_datos.o[i-1]);
datos_procesados.h.unshift((array_datos.h[i]>array_datos.h[i-1])? array_datos.h[i]:array_datos.h[i-1]);
datos_procesados.l.unshift((array_datos.l[i]<array_datos.l[i-1])? array_datos.l[i]:array_datos.l[i-1]);
}
return datos_procesados;
}
function Media20 (data,position){
let cierres = data.slice(position-20, position);
return CalcularMedia(cierres);
}
function Media8 (data,position){
let cierres = data.slice(position-8, position);
return CalcularMedia(cierres);
}
function Media200 (data,position){
let cierres = data.slice(position-200, position);
return CalcularMedia(cierres);
}
function CalcularMedia (datos){
let media = 0;
for(let a of datos){
media = media + a;
}
return media/datos.length;
}
function Crear_Json(jugada){
return new Promise(resolve => {
fs.readFile('./Estadisticas/Json/Estadisticas_VC2min.json', 'utf8', function readFileCallback(err, data){
if (err){
console.log(err);
} else {
obj = JSON.parse(data); //now it an object
obj.table.push(jugada); //add some data
json = JSON.stringify(obj); //convert it back to json
fs.writeFile('./Estadisticas/Json/Estadisticas_VC2min.json', json, 'utf8', ()=>{ resolve(console.log("hecho"))}); // write it back
}});
});
}
// MM20(res.body.c,res.body.c.length-1) Media movil vela anterior a la en desarrollo pero es la que voy a usar para ver el patrón
// MM20(res.body.c,res.body.c.length-10) Media movil de -11 contando la que está en desarrollo
// MM20(res.body.c,res.body.c.length-20)
Inicio();