askmike / gekko

A bitcoin trading bot written in node - https://gekko.wizb.it/
MIT License
10.07k stars 3.95k forks source link

[GDAX] zero value returned using onTrade(trade) handle #1852

Closed ihdk closed 6 years ago

ihdk commented 6 years ago

Hello, I am not sure if it's kind of bug or I am doing something wrong, but I am trying to correct prices using onTrade handle to real sell or buy values.

I hope the attached screenshot describe problem. Price value which is returned by candle.close in time when Gekko runs .check method may not be the real price with which is filled order on GDAX - real price may be higher or lower. That's fine, as there is onTrade handle using which I can get the real price of last trade. This is what I am doing in onTrade handle:

method.onTrade = function(trade) { this.trade = trade; console.log("order completed, can continue..."); console.log("Correct calculations based on real trade:"); //correct calculations according to real trade price if(this.lastAction == "buy"){ console.log("real BUY price was @", this.trade.price); this.buyPrice = this.trade.price; console.log("will try to sell at more than ", this.buyPrice this.increase); }else if(this.lastAction == "sell"){ console.log("**real SELL price was @", this.trade.price); this.sellPrice = this.trade.price; console.log("will try to buy at less than ", this.sellPrice * this.decrease); } this.waitingInOrder = false; }

So after each trade is corrected price returned by candle.close to real price of trade. All of that working wery well, problem comes when order is not filled at once, but is divided automatically to smaller parts on GDAX - in this case, trade.price return just zero - can be seen in logs on attached screenshot.

Like I said, I am not sure if it's some bug. Really appreciate any comment or advice on that. If there is a way to force order to be not splitted but fill at once, that would be great:)

Thanks for any comment.

2018-02-03_2315

ihdk commented 6 years ago

here is another one screenshot which may describe situation... I am not sure now if it's caused by splitted order to smaller parts, or if it's because of cancelled orders or any other reason:/

2018-02-03_2349

ihdk commented 6 years ago

tradebot error log: 2018-02-03 23:38:13 (INFO): Setting up Gekko in realtime mode 2018-02-03 23:38:13 (INFO): 2018-02-03 23:38:13 (INFO): Setting up: 2018-02-03 23:38:13 (INFO): Candle writer 2018-02-03 23:38:13 (INFO): Store candles in a database 2018-02-03 23:38:13 (INFO):

2018-02-03 23:38:13 (INFO): Setting up: 2018-02-03 23:38:13 (INFO): Trading Advisor 2018-02-03 23:38:13 (INFO): Calculate trading advice 2018-02-03 23:38:13 (INFO): Using the strategy: ivanSimpleBuySellDEMA 2018-02-03 23:38:13 (WARN): TALIB indicators could not be loaded, they will be unavailable. 2018-02-03 23:38:13 (WARN): TULIP indicators could not be loaded, they will be unavailable. 2018-02-03 23:38:13 (INFO):

2018-02-03 23:38:13 (INFO): Setting up: 2018-02-03 23:38:13 (INFO): Trader 2018-02-03 23:38:13 (INFO): Follows the advice and create real orders. 2018-02-03 23:38:13 (DEBUG): getting ticker, balance & fee from GDAX 2018-02-03 23:38:13 (INFO):

2018-02-03 23:38:14 (INFO): trading at GDAX ACTIVE 2018-02-03 23:38:14 (INFO): GDAX trading fee will be: 0% 2018-02-03 23:38:14 (INFO): GDAX portfolio: 2018-02-03 23:38:14 (INFO): EUR: 0.014934936600 2018-02-03 23:38:14 (INFO): BTC: 0.041094700000 2018-02-03 23:38:14 (INFO): Setting up: 2018-02-03 23:38:14 (INFO): Performance Analyzer 2018-02-03 23:38:14 (INFO): Analyzes performances of trades 2018-02-03 23:38:14 (INFO):

2018-02-03 23:39:14 (INFO): Trader Received advice to go short. Selling BTC 2018-02-03 23:39:15 (INFO): Attempting to SELL 0.0410947 BTC at GDAX price: 7395.95 2018-02-03 23:40:16 (INFO): SELL was successfull 2018-02-03 23:41:14 (INFO): Trader Received advice to go long. Buying BTC 2018-02-03 23:41:15 (INFO): Attempting to BUY 0.041169194534094185 BTC at GDAX price: 7382.21 2018-02-03 23:42:17 (INFO): BUY order was not (fully) filled, cancelling and creating new order 2018-02-03 23:42:18 (INFO): Attempting to BUY 0.041171369393406605 BTC at GDAX price: 7382.53 2018-02-03 23:43:19 (INFO): BUY order was not (fully) filled, cancelling and creating new order 2018-02-03 23:43:20 (INFO): Attempting to BUY 0.041106501374944385 BTC at GDAX price: 7392.57 2018-02-03 23:44:22 (INFO): BUY order was not (fully) filled, cancelling and creating new order 2018-02-03 23:44:23 (INFO): Attempting to BUY 0.04109455235989692 BTC at GDAX price: 7395.64 2018-02-03 23:45:24 (INFO): BUY order was not (fully) filled, cancelling and creating new order 2018-02-03 23:45:25 (INFO): Attempting to BUY 0.04109816412012943 BTC at GDAX price: 7395.68 2018-02-03 23:46:26 (INFO): BUY was successfull 2018-02-03 23:46:26 (ERROR): [gdax.js] (getOrder) returned an irrecoverable error: NotFound

ihdk commented 6 years ago

seems like the same error: 2018-02-04 00:12:22 (ERROR): [gdax.js] (getOrder) returned an irrecoverable error: NotFound I am experiencing always when the order is more times cancelled. On the following screenshot is logged also content of "trade" which is returned by handle onTrade(trade) mentioned in previous message...

Thank your for any hints on that, because of this issue I am unable to left running gekko more than hour...:)

2018-02-04_0012

askmike commented 6 years ago

Will continue to work on this, this is new func.

@werkkrew does this work for you? If so, what exchange did you test this on.

@ihdk This (unfinished) PR will have more stable event support: #1850.

ihdk commented 6 years ago

Thank you, another related issue may be mentioned in my last comment here: #1848 Its related to checkOrder order function, instead of getOrder in this discussion.

werkkrew commented 6 years ago

I tested it on Binance, I did see an occasional duplicate trade get emitted to the strategy with a price of 0 and I never discovered why. It didn't happen often but basically a valid trade would get emitted and then shortly afterwards I'd see another trade event with incomplete data in it.

Generally speaking though, if the trade data is complete upstream, the changes I made to the strategy would see it.

Looking at the screenshot you posted above, it probably has something to do with how Gekko is handling multiple trades making up one larger order (partial fills).

ihdk commented 6 years ago

Thanks for response, just did a few more tests on GDAX... Interesting may be that gekko returns zero for price in onTrade method, even the order was really not filled on GDAX and still waiting in order - attached screenshot below. 2018-02-04_1730_001

In time I am writing this message, buy order still wating in order and the code is processed further, even it should not be processed until order isn't filled. In onTrade handle I have defined variable which allow or do not allow but/sell actions inside .check method while there is still something on trade...

Using the paper trader, there was not really problem for over 200 candles in minute size. So I run it live and this problem appeared after few minutes, looks like something related to live trade and how gekko getting or checking information from excange....

werkkrew commented 6 years ago

I'll do some more testing later tonight or tomorrow, going to watch the game.

The implementation relies simply on the emitted trades from Gekko so its quite possible that there is another issue elsewhere, or it may be specific to GDAX, I am not sure. I can test on GDAX so when I get some time I will try to figure it out.

Thanks for the comprehensive post with the screenshots and all.

ihdk commented 6 years ago

Thank you, it may be really caused by GDAX, as I experienced problem only on live trade, when order wait in queue...using paper trade no way to experience that as there is no need to wait and order is "filled" immediately... Maybe when I'd know exactly where the trade or onTrade handle is processed, I could look at it deeper and process more tests why there a zero returned...

ihdk commented 6 years ago

I am sorry for so frequent messaging here, just would like to confirm that to reproduce problem it's necessary to have orders cancelled and filled again...just then trade object has zero price. I have strategy as simple as possible to just sell asset and buy back in a bit lower price, just for testing purposes - attached below.

Using this simple strategy I was able to reproduce problem with zero price in trade object. Thanks again for your effort.

`// helpers var _ = require('lodash'); var log = require('../core/log.js'); // let's create our own method var method = {};

// prepare everything our method needs method.init = function() { this.name = 'simpleDowntradeStrategy'; this.trade; this.lastAction = "buy"; //force sell on start this.waitingInOrder = false; }

// what happens on every new candle? method.update = function(candle) { // nothing! } method.onTrade = function(trade) { this.trade = trade; console.log("order completed, can continue..."); console.log(""); console.log("trade object:"); console.log(trade); console.log(""); console.log(""); console.log("Correct calculations based on real trade:"); this.lastPrice = this.trade.price; console.log("real last price price was @", this.lastPrice);
this.waitingInOrder = false; }

method.check = function(candle) { this.currentPrice = candle.close; if(this.waitingInOrder){
console.log(""); console.log("\tdo nothing, still waiting in order..."); return; }

console.log("current price: ", this.currentPrice);

if(this.lastAction == "buy"){

      this.advice("short");
      this.waitingInOrder = true;
      this.lastPrice = this.currentPrice;
      this.lastAction = "sell";          
      console.log("\t\t===== SELL @", this.lastPrice);
      console.log("\t\twill try to buy at less than ", this.lastPrice);         

}else if(this.lastAction == "sell" && ( this.currentPrice < this.lastPrice ) ){
      //buy at lower price         
      this.advice("long");
      this.waitingInOrder = true;
      this.lastAction = "buy";
      console.log("\t\t===== BUY @", this.currentPrice);
}else{
    //do nothing
    if(this.lastAction == "sell") {
      console.log("\t\tcannot buy");
      console.log("\t\texpected price to buy is lower than: ", this.lastPrice );
    }
}

}

module.exports = method;`

askmike commented 6 years ago

@ihdk i'm pretty sure I've found the problem, see here: https://github.com/askmike/gekko/pull/1834#issuecomment-362970448

Since this is new and experimental features, I'd like to fix this all in one go. Stay tuned!

Closing this for now, so we can keep all dicussion regarding better execution logic (including fixing this bug) in #1834.

ihdk commented 6 years ago

That's perfect, thank you Mike.

Ivan.