askmike / gekko

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

A sample working strategy #1312

Closed roneetshaw closed 6 years ago

roneetshaw commented 6 years ago

I am trying to write a custom strategy on Gekko but not able to follow the documentation very well. Hence, I need some support. I'm not asking for anyone's best working strategy but I need some help to create one.

` var strat = {}   
strat.init = function() {

this.buyPrice = 0.000081;

// setting sell price
this.sellPrice = 0.00009;

} // What happens on every new candle? strat.update = function(candle) {

}

// For debugging purposes. strat.log = function() {

}

// Based on the newly calculated // information, check if we should // update or not. strat.check = function() {

 // buy when it hits buy price
if(candle.close <= this.buyPrice) {
    this.advice("long");
    // do some output
    console.log("buying BTC @", candle.close);
    return;
}

// sell when it hits sell price
if(candle.close >= this.sellPrice) {
    this.advice("short");
    // do some output
    console.log("selling BTC @", candle.close);
    console.log("Profit:", (candle.close-this.buyPrice));
    return;
}

} module.exports = strat;`

This is my sample code where I am just trying to buy BTC @0.000081 and sell @0.00009. However when I save it in custom.js and load it on the UI. It gives some result like +10%, -40%, 3%, etc different result at different time ran with same parameter and for same date range. I am not sure even my strategy is executing or not. Any help will be appreciated.

shameed123 commented 6 years ago

I don't know if it is just a copy/paste issue or I see strat.init declared twice. You can add console.log in your init to see if your strategy is running at all. I have also noticed in gekko that sometimes it would run on a smaller time interval than it is asked for. But usually it happens only the first or second time around. After that it consistently runs on the interval provided. There is probably some thread that catches up with the backtesting and it takes a little bit time. One more thing I would suggest that you should save the candle.close price as your buy price and use it to calculate the profit instead of your buy threshold.

roneetshaw commented 6 years ago

Thanks for your reply. I checked the logs it is not logging anything that means it's not executing. Do I have to enable any debug in any config file to make it write logs?

eashton1 commented 6 years ago

I am attempting this exact same code (except I'm using USD for buy and sell) Following the basic strategy example at: https://gekko.wizb.it/docs/strategies/creating_a_strategy.html For me it is running my custom strategy, however I am getting a candle is not defined error:

2017-11-18 20:14:02 (INFO): Setting up Gekko in backtest mode 2017-11-18 20:14:02 (INFO): 2017-11-18 20:14:02 (INFO): Setting up: 2017-11-18 20:14:02 (INFO): Trading Advisor 2017-11-18 20:14:02 (INFO): Calculate trading advice 2017-11-18 20:14:02 (INFO): Using the strategy: custom 2017-11-18 20:14:02 (INFO):

2017-11-18 20:14:02 (INFO): Setting up: 2017-11-18 20:14:02 (INFO): Paper Trader 2017-11-18 20:14:02 (INFO): Paper trader that simulates fake trades. 2017-11-18 20:14:02 (INFO):

2017-11-18 20:14:02 (INFO): Setting up: 2017-11-18 20:14:02 (INFO): Performance Analyzer 2017-11-18 20:14:02 (INFO): Analyzes performances of trades 2017-11-18 20:14:02 (INFO):

2017-11-18 20:14:02 (INFO): WARNING: BACKTESTING FEATURE NEEDS PROPER TESTING 2017-11-18 20:14:02 (INFO): WARNING: ACT ON THESE NUMBERS AT YOUR OWN RISK! 2017-11-18 20:14:02 (WARN): Simulation based on incomplete market data (1 missing between 2017-11-17 23:12:00 and 2017-11-18 00:01:00). /home/Eric/gekko/node_modules/sqlite3/lib/trace.js:27 throw err; ^ ReferenceError: candle is not defined at Base.strat.check (/home/Eric/gekko/strategies/custom.js:37:8) at Base.bound (/home/Eric/gekko/node_modules/lodash/dist/lodash.js:729:21) at Base.propogateTick (/home/Eric/gekko/plugins/tradingAdvisor/baseTradingMethod.js:251:10) at Base.bound [as propogateTick] (/home/Eric/gekko/node_modules/lodash/dist/lodash.js:729:21) at Base.tick (/home/Eric/gekko/plugins/tradingAdvisor/baseTradingMethod.js:187:10) at Base.bound [as tick] (/home/Eric/gekko/node_modules/lodash/dist/lodash.js:729:21) at Actor.processCustomCandle (/home/Eric/gekko/plugins/tradingAdvisor/tradingAdvisor.js:79:15) at CandleBatcher.bound (/home/Eric/gekko/node_modules/lodash/dist/lodash.js:729:21) at emitOne (events.js:96:13) at CandleBatcher.emit (events.js:188:7) --> in Database#all('\n SELECT * from candles_USD_BTC\n WHERE start <= 1510966319 AND start >= 1510963320\n ORDER BY start ASC\n ', [Function]) at Reader.get (/home/Eric/gekko/plugins/sqlite/reader.js:98:11) at Reader.bound [as get] (/home/Eric/gekko/node_modules/lodash/dist/lodash.js:729:21) at Market.get (/home/Eric/gekko/core/markets/backtest.js:61:15) at Market.bound [as get] (/home/Eric/gekko/node_modules/lodash/dist/lodash.js:729:21) at Market.processCandles (/home/Eric/gekko/core/markets/backtest.js:105:10) at bound (/home/Eric/gekko/node_modules/lodash/dist/lodash.js:729:21) at Statement. (/home/Eric/gekko/plugins/sqlite/reader.js:108:5) xxx POST /api/backtest 500 459ms -

Error: non-error thrown: Child process has died. at Object.onerror (/home/Eric/gekko/node_modules/koa/lib/context.js:105:40) at process._tickCallback (internal/process/next_tick.js:109:7)

If I run the same in paper trader I will eventually throw the error as well. Don't mean to highjack thread, but if we are running the same strategy I imagine he will get the same error. Advice?

roneetshaw commented 6 years ago

I was actually using pm2 and that is why I was not getting the any error. However, when I ran using node gekko --ui I also got the same error which I solved by using this.candle. After then my strategy started working.

roneetshaw commented 6 years ago

Closing the issue.

GO1984 commented 6 years ago

Thank you for sharing your thougts and your solution. It helped me too.

Tribic commented 6 years ago

Hi, i`m pretty new to all of this plus having the same problem and not understanding the solution. Here´s my code (Should do nothing except going through the functions and print logs:

// Let's create our own buy and sell strategy 
var strat = {};

// Prepare everything our strat needs
strat.init = function() {
    console.log("Prepare");
  // your code!
}

// What happens on every new candle?
    strat.update = function(candle) {
    // setting buy price
    console.log("Update: Candle:", (candle.close));

    //*this.buyPrice = candle.close;

    // setting sell price
    //*this.sellPrice = candle.close*1.004;

}

// For debugging purposes.
    strat.log = function() {
    // your code!
    console.log("Log: Candle:", (candle.close));

}

// Based on the newly calculated
// information, check if we should
// update or not.
strat.check = function(candle) {
    // buy when it hits buy price
    console.log("Check: Candle:", (candle.close));
    this.buyPrice = candle.close
    if(candle.close <= this.buyPrice) {
        //*this.advice("long");
        // do some output
        console.log("buying BTC @", candle.close);
        return;
    }

    // sell when it hits sell price
    if(candle.close >= this.sellPrice) {
        //*this.advice("short");
        // do some output
        console.log("selling BTC @", candle.close);
        console.log("Profit:", (candle.close-this.buyPrice));
        return;
    }
}

module.exports = strat;

Result:

... <-- GET /api/configPart/paperTrader --> GET /api/configPart/paperTrader 200 1ms 132b <-- POST /api/startGekko Gekko 354377285452275 started --> POST /api/startGekko 200 10ms 147b <-- POST /api/startGekko Gekko 3358024190265545 started --> POST /api/startGekko 200 4ms 327b Prepare Update: Candle: 0.00004904 Update: Candle: 0.00004904 C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\node_modules\sqlite3\lib\trace.js:27 throw err; ^

ReferenceError: candle is not defined at Base.strat.log (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\strategies\Trib1.js:25:34) at Base.bound [as log] (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\node_modules\lodash\dist\lodash.js:729:21) at Base.propogateTick (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\plugins\tradingAdvisor\baseTradingMethod.js:242:10) at Base.bound [as propogateTick] (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\node_modules\lodash\dist\lodash.js:729:21) at Base.tick (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\plugins\tradingAdvisor\baseTradingMethod.js:153:10) at Base.bound [as tick] (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\node_modules\lodash\dist\lodash.js:729:21) at Actor.processCustomCandle (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\plugins\tradingAdvisor\tradingAdvisor.js:82:15) at CandleBatcher.bound (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\node_modules\lodash\dist\lodash.js:729:21) at emitOne (events.js:116:13) at CandleBatcher.emit (events.js:211:7) --> in Database#all('\n SELECT * from candles_BTC_LINK\n WHERE start <= 1527013831 AND start >= 1527013440\n ORDER BY start ASC\n ', [Function]) at Reader.get (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\plugins\sqlite\reader.js:98:11) at Reader.bound [as get] (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\node_modules\lodash\dist\lodash.js:729:21) at Market.get (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\core\markets\leech.js:63:15) at Timeout.bound [as _onTimeout] (C:\Users\Tribic\Downloads\gekko-stable\gekko-stable\node_modules\lodash\dist\lodash.js:729:21) at ontimeout (timers.js:482:11) at tryOnTimeout (timers.js:317:5) at Timer.listOnTimeout (timers.js:277:5) RECEIVED ERROR IN GEKKO 3358024190265545 Child process has died.

Where/What do i need to replace with

this.candle

?

Thanks in Advance from the new bot Noob ;)