Gekko Forum
Need help with trading bot functionality - Printable Version

+- Gekko Forum (https://forum.gekko.wizb.it)
+-- Forum: Gekko (https://forum.gekko.wizb.it/forum-13.html)
+--- Forum: Technical Support (https://forum.gekko.wizb.it/forum-19.html)
+--- Thread: Need help with trading bot functionality (/thread-1459.html)



Need help with trading bot functionality - BitPerson - 02-09-2018

Hey there!

I developed my working strategy and I like how it works good sometimes.

Here there are some problems with my usage of Gekko (trading bot with API from exchanges):

1) I save the last price I bought/sold like:


Code:
this.lastprice = candle.close;


This is a big problem when you buy/sell at a price xx% (where xx is big) higher/lower from the price that triggered the advice.

Example: 
Advice: buy @0.00015 -> lastprice = 0.00015 but then it buys @0.00019 -> sell @0.00017 -> I expect +0.00002 but I'm like -0.00002. 

Is there a solution? Is there the possibility to get the last price traded instead of the way I use?

2) I also would like to stop orders repetition (for example: if there's a long advice when price is 0.00015 and then price goes to 0.00030 while the order is not yet satisfied, I would like to cancel the order, not changing it to buy @0.00030). Is it possible?

3) Am I right to use advices (this.advice("long"); / this.advice("short")Wink even if I'm using trading bot and not paper trader? Or is it possible to use something more useful? For example:

3.a) Is it possible to suggest prices instead of using the best to buy/sell?

3.b) Is it possible to force some (or any, but I would prefer just some) order to "taker" instead of "maker" (market price usage) in Gdax?



Sorry for my bad english, hope everything is clear!   Tongue 
Sorry if there is an obvious solution but I'm not a developer  Sad 


Thank you for your software and your time, you already made a fantastic work!


RE: Need help with trading bot functionality - BitPerson - 02-12-2018

Hello, can someone help me please?  Big Grin


RE: Need help with trading bot functionality - askmike - 02-13-2018

I thought I replied to this, not sure where the reply went...

> Is there a solution? Is there the possibility to get the last price traded instead of the way I use?

This is in the works, if you use the develop branch you can use this: https://github.com/askmike/gekko/pull/1810

> 2) I also would like to stop orders repetition (for example: if there's a long advice when price is 0.00015 and then price goes to 0.00030 while the order is not yet satisfied, I would like to cancel the order, not changing it to buy @0.00030). Is it possible?
> 3.a) Is it possible to suggest prices instead of using the best to buy/sell?
> 3.b) Is it possible to force some (or any, but I would prefer just some) order to "taker" instead of "maker" (market price usage) in Gdax?

Not yet, but all these features (except for 3a) are being worked on right now.

> 3) Am I right to use advices (this.advice("long"); / this.advice("short")Wink even if I'm using trading bot and not paper trader? Or is it possible to use something more useful? For example:

Yes: the only thing your strategy can do is advice. Whether there will be a real trader (tradebot) or simulated trader (paper trader) depends on how you configured Gekko. Your strategy is completely unaware of this.


RE: Need help with trading bot functionality - tommiehansen - 02-13-2018

For a really simple stoploss you could do something like below:

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

// create method
var method = {
    
    
    /* INIT */
    init: function()
    {
        this.name = 'Double TEMA';
        
        // state, current trend
        this.resetTrend();

        // yes
        this.requiredHistory = config.tradingAdvisor.historySize;
        
        
        /* TEMP: add programmatically */
        this.settings.stoploss = -10; // %
        this.settings.TEMA_long = 200; // /4 = 4 hour candle
        this.settings.TEMA_short = 50;
        
        // add params    
        this.addTulipIndicator('maSlow', 'tema', { optInTimePeriod: this.settings.TEMA_long });
        this.addTulipIndicator('maFast', 'tema', { optInTimePeriod: this.settings.TEMA_short });
        
    }, // init()
    
    
    /* RESET TREND */
    resetTrend: function()
    {
        var trend = {
            duration: 0,
            direction: 'none',
            longPos: false,
        };
    
        this.trend = trend;
    },
    
    
    /* STOPLOSS */
    stoploss: function( maxDiff, cur, old )
    {
    
        let diff = ((cur/old)-1) * 100;
            diff = diff.toFixed(2),
            ret = false;
        
        if( maxDiff >= diff )
        {
            ret = true;
            let str = 'Stoploss hit! Stoploss @ ' + maxDiff + '% Current diff: ' + diff + '%';
            log.debug(str);
        }
        
        return ret;
    },
    
    
    /* CHECK */
    check: function( candle )
    {
        // fetch indicators
        let ti = this.tulipIndicators;
        let maFast = ti.maFast.result.result,
            maSlow = ti.maSlow.result.result;
        
        // base rules
        let goLong, goShort;
        
        goLong = maFast > maSlow ? true : false;
        goShort = maFast < maSlow ? true : false;
        
        // stoploss (if a long pos has been taken)
        if( this.trend.longPos )
        {
            // check
            let stop = this.stoploss( this.settings.stoploss, candle.close, this.trend.longPos );
            
            // if true act on it
            if( stop ){
                goLong = false;
                goShort = true;
                this.trend.direction = 'none';
            }
        }
        
    
        // LONG
        if( goLong )
        {
            // new trend? (only act on new trends)
            if (this.trend.direction !== 'up')
            {
                this.resetTrend(); // reset
                
                this.trend.direction = 'up';
                this.trend.longPos = candle.close; // set long position
                this.advice('long'); // go long        
                //log.debug('LONG');
                
            } // !== 'up'
            
            //this.trend.duration++;
            //log.debug ('Pos trend', this.trend.duration, 'candle(s)');
        }
        
        // SHORT
        else if( goShort )
        {
            // new trend?
            if( this.trend.direction !== 'down' )
            {    
                this.resetTrend(); // reset
                this.trend.direction = 'down';
                this.advice('short');
                //log.debug('SHORT');
                
            } // !== 'down'
            
            //this.trend.duration++;
            //log.debug ('Downtrend since ', this.trend.duration, 'candle(s)');
        }
        
        else
        {
            //log.debug('In no trend');
            this.advice();
        }
        
    } // check()
    
}; // method {}

module.exports = method;


Note that this is just an example. It could ofc be improved upon e.g. we could skip X amount of candles for the stoploss check etc or begin doing stuff such as having 5 min candles but 
doing long/short on 2x candles (which would equal 10 minutes) in order to have a stoploss that checks each 5 min candle and a strategy that works on 10 min candles.

Do note that this isn't a 'true' stoploss since candle resolution (time e.g. 10 minutes) will affect the stoploss since the strategy will not be aware of anything that happens in-between candles. In order for it to know such things you would need to use the minimum resolution available (which would be 1 minute in Gekko) and then use some other value for the strategy itself like i write about above. Actually that's pretty interesting so might write something that does it but does it inline (so it doesn't require 1 billion dependencies).

Your ability to write code is the limit.


RE: Need help with trading bot functionality - BitPerson - 02-13-2018

(02-13-2018, 06:04 AM)askmike Wrote: [...]

Hi @askmike, thank you for your reply.
I'm not a developer, so it's hard for me to understand everything from that thread. 
Can you help me just understanding how I can: 

- get the last price traded


Code:
this.lastprice = ???




- get the buy/sell information:


Code:
// If I bought = 1; if I sold = 2
//
if (???) {
this.buysell = 1;
}
else if (???) {
this.buysell = 2;
}


Not least, if I place a manual order on the exchange (so not made by Gekko), will this ontrade function get the information too?


Thank you!