askmike / gekko

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

Basic Strategy Example crashes ReferenceError: candle is not defined #2202

Closed Tribic closed 6 years ago

Tribic commented 6 years ago

Hi, my problem was already discussed, solved and closed here #1312 , but i do not understand the solution :( in short: what exactly do i need to replace with "this.candle"? Thanks in advance :)

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 ;)

askmike commented 6 years ago

It's complaining about this line:

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

}

Since candle is not defined in that scope. For example you can remove that line.

You've posted this in the issue tracker for the project were we discuss bugs and talk about Gekko's code, you'll get more help building strategies on the forum or on discord, see this link: https://gekko.wizb.it/docs/introduction/getting_help.html

Tribic commented 6 years ago

Great - everything´s working like expected! Thank you!