-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtests.js
121 lines (90 loc) · 3.79 KB
/
tests.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
// Importar os módulos
var config = require('./config.js');
var log = require('./core/log.js');
var profit = require('./core/profit.js');
var advice = require('./core/advice.js');
var exchange = require('./exchanges/' + config.watch.exchange + '.js');
var EMASMA = require('./indicators/emasma.js')
//Comandos de tela
process.stdout.write('\x1Bc'); //Limpar tela de console
process.title = '[POLONIEX BOT]'; //Adicionar titulo ao projeto
// Mandar mensagem indicando inicialização do BOT
log.info('start', "O BOT foi iniciado. Estou analisando mercado agora...");
function manipularOrdens() {
log.info('debug', "manipularOrdens >> started");
// Selecionar os close dos candlesticks
var candles = exchange.getCandles();
//Se EMASMA: true
var buyable = EMASMA.calculate(candles);
log.info('debug', "manipularOrdens >> buyable: "+buyable);
if ( advice.buyOrderId == 0 && advice.sellOrderId == 0) {
log.info('debug', "manipularOrdens >> Não existe nenhuma ordem aberta");
if (buyable == true) {
advice.opt('buy');
}
}
if( advice.buyOrderId != 0 && advice.sellOrderId == 0) {
log.info('debug', "manipularOrdens >> A ordem de compra está aberta e a ordem de venda fechada");
if ( !exchange.isOpen(advice.buyOrderId)) {
log.info('debug', "manipularOrdens >> A ordem de compra já foi finalizda");
log.info('debug', "manipularOrdens >> sellOrderId: " +advice.sellOrderId + " buyOrderId: "+ advice.buyOrderId);
log.info('debug', "manipularOrdens >> criar uma ordem de venda com saldo em btc");
advice.buyOrderId = 0;
advice.opt('sell');
}
}
if( advice.sellOrderId != 0 && advice.buyOrderId == 0) {
log.info('debug', "manipularOrdens >> Ambas ordens já foram abertas");
if ( !exchange.isOpen(sellOrderId)) {
log.info('debug', "manipularOrdens >> Orderm de venda fechada");
log.info('info', "manipularOrdens >> Trade efetuado com sucesso");
sellOrderId = 0;
buyOrderId = 0;
}
}
}
function checkAccount(){
log.info('debug', "checkAccount >> started");
var showProfitTime = 1800000; //30 min
var runStopLossTime = 10000; //10 seg
var runTraderTime = 15000; // 15 seg
advice.updateBalance();
log.info('info', "Saldo em " + config.watch.currency + ": " + advice.currency);
log.info('info', "Saldo em " + config.watch.asset + ": " + advice.asset);
profit.init(advice.currency, advice.asset);
//Imprime de 30 em 30 minutos o saldo
setInterval(function(){
log.info('info', "Saldo em " + config.watch.currency + ": " + advice.currency);
log.info('info', "Saldo em " + config.watch.asset + ": " + advice.asset);
profit.init(advice.currency, advice.asset);
},showProfitTime);
// caso ele tenha saldo começar a manipular as ordens
setInterval(function c(){ manipularOrdens(); return c; }() , runTraderTime);
//Rodar o stop loss para verificar ordens de compra em aberto
//Cancelar ordens fora de preço de mercado
setInterval(function(){
advice.stopLoss();
},runStopLossTime);
}
if (exchange.config.trader.enabled){
exchange.setCredential(config.trader.key, config.trader.secret);
exchange.setPair(config.watch.currency + '_' + config.watch.asset);
var check = setInterval(
function() {
if(!exchange.lastPrice() > 0) {
log.info('warn', "Recebendo informações de preço ...");
}
else if( !(exchange.getCandles().length > 0)) {
log.info('warn', "Recebendo informações de candlesticks ...");
}
else if(!(exchange.balanceVal[exchange.config.watch.currency] > 0)) {
log.info('warn', "Recebendo informações da sua conta ...");
}
else {
checkAccount()
clearInterval(check);
}
}, 10000)
} else {
log.info('error', "Trader inativo. Edite config.js para trader.enable: true");
}