[BOUNTY COMPLETD] fix DEMA example strategy
#3
No, but you can have my basic TEMA-strategy (tulip) that would hardly be impossible to convert to using DEMA instead:

js
Code:
/*
    TEMA
    Triple EMA strategy with 'safety net'
    ---
    Uses two TEMA's to go long/short and a long SMA
    in order to completely stay out of market if the
    long trend is really terrible.
    ---
    The general idea is to lower the risk.
*/

// req's
var _ = require ('lodash');
var log = require ('../core/log.js');

// conf
var config = require ('../core/util.js').getConfig();
var async = require ('async');

// create method
var method = {};


// init
method.init = function()
{

   this.name = 'Triple TEMA';

   // state, current trend
   this.trend = {
       direction: 'none',
       duration: 0,
       persisted: false,
       adviced: false
   };
    
    this.useSafety = false;

   // yes
   this.requiredHistory = config.tradingAdvisor.historySize;
    
    // add params    
    this.addTulipIndicator('maSlow', 'tema', { optInTimePeriod: this.settings.TEMA_long });
    this.addTulipIndicator('maFast', 'tema', { optInTimePeriod: this.settings.TEMA_short });
    
    if( this.settings.SMA_long > 0 )
    {
        this.useSafety = true;
        this.addTulipIndicator('maSlowest', 'sma', { optInTimePeriod: this.settings.SMA_long });
    }
 
}

// what happens on every new candle?
method.update = function(candle) {} // nothing
method.log = function() {} // nothing

method.check = function (candle)
{
   if (candle.close.length < this.requiredHistory) { return; } // still needed?
    
    // fetch indicators
    let ti = this.tulipIndicators;
    let maFast = ti.maFast.result.result,
        maSlow = ti.maSlow.result.result,
        maSlowest = false,
        useSafety = this.useSafety;
        
    if( useSafety ) maSlowest = ti.maSlowest.result.result;
        
    
    // rules
    let goLong, goShort;
    
    goLong = maFast > maSlow ? true : false;
    goShort = maFast < maSlow ? true : false;
    
    // safety
    if( useSafety ) {
        goLong = maFast > maSlow && maSlow > maSlowest; // slow must be over the really slow trend
    }
    
    
    // LONG
    if( goLong )
    {
        // new trend? (only act on new trends)
        if (this.trend.direction !== 'up')
        {
            log.debug ('In positive trend since', this.trend.duration, 'candle (s)');
            
            // reset the state for the new trend
            this.trend = {
                duration: 0,
                persisted: false,
                direction: 'up',
                adviced: false
            };

            this.trend.duration ++;
            
            if( !this.trend.adviced )
            {
                this.trend.adviced = true;
                this.advice('long');
            }
            else
            {
                this.advice();
            }
        } // !== 'up'    
    }
    
    // SHORT
    else if( goShort )
    {
        // new trend?
        if( this.trend.direction !== 'down' ){
            
            log.debug ('In downtrend since', this.trend.duration, 'candle (s)');
            
            // reset state
            this.trend = {
                duration: 0,
                persisted: false,
                direction: 'down',
                adviced: false
            };

            this.trend.duration ++;

            if( !this.trend.adviced )
            {
                this.trend.adviced = true;
                this.advice ('short');
            }
            else
            {
                this.advice();
            }
        
        }
    }
    else
    {
        log.debug('In no trend');
        this.advice();
    }
   
} // method.check()

module.exports = method;

toml
Code:
# Triple EMA's
TEMA_short = 30
TEMA_long = 80

# Long SMA (safety net)
SMA_long = 200

# Set SMA_long to 0 (zero) to disable it

Btw -- any reason why you wouldn't just use a library for the TA's? It seems quite unessecary to reinvent the wheel?
  Reply


Messages In This Thread
RE: [BOUNTY] fix DEMA example strategy - by tommiehansen - 01-31-2018, 03:21 PM

Forum Jump:


Users browsing this thread: