BBands strategy idea
#2
Hey,


I tried something quick, and come with this code :

BBandCheck.js :
Code:
/*
    BBands crossing strat
    
    (CC-BY-SA 4.0) ManuManu for Gekko
    Code derived from RSI_BULL_BEAR by Tommie Hansen ( https://github.com/tommiehansen/gekko_tools/tree/master/strategies )
    https://creativecommons.org/licenses/by-sa/4.0/
*/

// req's
var log = require ('../core/log.js');
var config = require ('../core/util.js').getConfig();

// strategy
var strat = {
    
    /* INIT */
    init: function()
    {
        this.name = 'BBandCrossing';
        this.requiredHistory = config.tradingAdvisor.historySize;
        this.resetTrend();
        
        // debug set to false to disable all logging (improves performance)
        this.debug = true;
        
        // add indicators
        this.addTulipIndicator('BBands', 'bbands', { optInTimePeriod: this.settings.TimePeriod, optInNbStdDevs :this.settings.NbStdDevs });
        
        this.sellLimit = 9999999.0;
        
        this.startTime = new Date();
                
        
    }, // init()
    
    resetTrend: function()
    {
        var trend = {
            duration: 0,
            direction: 'none',
            longPos: false,
        };
    
        this.trend = trend;
    },
    /* CHECK */
    check: function()
    {
        if( this.candle.close.length < this.requiredHistory ) { return; } // check if candle length is correct
        
        // get all indicators
        let ind = this.tulipIndicators,
            BBlower = ind.BBands.result.bbandsLower,
            BBupper = ind.BBands.result.bbandsUpper;
            
//            log.debug('BBand : ' + Object.keys(ind.BBands.result) );
            //log.debug('BBAnd : ' + BBlower + " - " + BBupper);
            //log.debug('close : ' + this.candle.close);
            //log.debug('low : ' + this.candle.low);
            
            
        if ( this.candle.low < BBlower )
        {
            if ( this.long() )
            {
                this.sellLimit = BBupper;
                log.debug("sell Limit : " + this.sellLimit );
            }
        }
        
        if ( this.trend.direction =="up" && this.candle.close > this.sellLimit)
        {
            this.short();
        }
    }, // check()
    
    
    /* LONG */
    long: function()
    {
        if( this.trend.direction !== 'up' ) // new trend? (only act on new trends)
        {
            this.resetTrend();
            this.trend.direction = 'up';
            this.advice('long');
            log.debug('go long');
            return true;
        }
        return false;
    },
    
    
    /* SHORT */
    short: function()
    {
        if( this.trend.direction !== 'down' )
        {
            this.resetTrend();
            this.trend.direction = 'down';
            this.advice('short');
            log.debug('go short at ' + this.candle.close);
        }
    },
    
    
    /* END backtest */
    end: function(){
        
        let seconds = ((new Date()- this.startTime)/1000),
            minutes = seconds/60,
            str;
            
        minutes < 1 ? str = seconds + ' seconds' : str = minutes + ' minutes';
        
        log.debug('Finished in ' + str);
    }
    
};

module.exports = strat;

You also need a toml config file for settings :
( I tried the fisrt values google returned me, not sure at all what are the best values : )
BBandCheck.toml :
Code:
# BBand Params :
TimePeriod = 12
NbStdDevs = 2

I tried to backtest it, and it works some times, BUT :
* if you go long, and the asset is then going down, you can reach this situation where you will never going short again, and just loose.
* the other point is that I'm not sure how good Gekko is for 1 minute trading.
I'm not sure how it's working, and AskMike or anyone with more experience could you more, but I have the feeling that with 1 minute candle, your real buy price can be different from the price the strategy was thinking, and so be loosing from this.

Tell us what your experience is with this strategy !!

Any comment on the code also appreciated ( as it's kind of my first own strategy coding )

ManuManu
  Reply


Messages In This Thread
BBands strategy idea - by mhmdalsaqqal - 02-20-2018, 02:22 AM
RE: BBands strategy idea - by ManuManu - 02-20-2018, 04:29 PM
RE: BBands strategy idea - by ManuManu - 02-20-2018, 07:05 PM
RE: BBands strategy idea - by tommiehansen - 02-21-2018, 11:37 AM
RE: BBands strategy idea - by ManuManu - 02-21-2018, 01:13 PM

Forum Jump:


Users browsing this thread: