Gekko Forum
Proper stop losses coming soon - Printable Version

+- Gekko Forum (https://forum.gekko.wizb.it)
+-- Forum: Gekko (https://forum.gekko.wizb.it/forum-13.html)
+--- Forum: General Discussion (https://forum.gekko.wizb.it/forum-14.html)
+--- Thread: Proper stop losses coming soon (/thread-57314.html)

Pages: 1 2 3 4 5 6


RE: Proper stop losses coming soon - askmike - 08-27-2018

Quick summary:

- Native trailing stops have landed in Gekko 0.6.6! Note that they are still considered experimental.
- You can implement them in your strategy: when advicing a BUY, pass an object indicating that you want a trailingStop. And Gekko will make sure the trailing stop gets created. When asking for a trailingStop you need to pass either a fixed value (trailValue or a trailPercentage). For an example, see here: https://gekko.wizb.it/docs/strategies/creating_a_strategy.html#check-function
- in simulation Gekko will use OHLC data, but when running a live trader Gekko will actually watch the orderbook more closely, and fire events as the ticker (best bid) moves up or down.
- If your strategy fires a SELL advice before the trigger fires, Gekko will delete the trigger.
- Once the trailing stop triggers because the price moves down, Gekko will automatically sell (right now using a sticky order).
- Your strategy can use the (experimental) onTrade handle to be notified of all executed trades (including those fired from trailing stops).


RE: Proper stop losses coming soon - mark.sch - 09-03-2018

Did pretty much trailing stop loss coding/testing last days. Just adding it into any existing strategy will optimize a couple of percent, at least what I have tested. So I was thinking of a strategy to take real advantage of this feature. @askmike, what are your use cases for this feature?

I started looking to catch strong price drops to take advantage of bounce backs by buying with a trailing stop loss. The closest thing I found so far for this was the PPO indicator, to get the crossing lines of two EMAs together with a price power info, e.g. buy on macd when ppo is < -1.5 with the goal to catch a percent on the re bounce. Often this lead to roundtrips of -0.8% or -1.1%. The buy advice was with SL 2%, so it turned out that the price made +1.2% and +0.9% but the 2% trailing dropped it into the negative direction. Therefor I implemented a trailing stop loss with target class. So you specify your target gain with the trigger, e.g. a target of +1% and the trailing will narrow when the target is reached. So it starts with a trailing of 2% for example and narrows the trailing to 0.2% when the price reaches 1.2% gain. So the price is still able to climb and trails but on a further small price drop it will sell and save the target gain.

This turned the strategy into positive values, but there is still optimization need for the buy strategy since gains are to small. BTW: It was also a good testing for the 1 minute candles passed into the strategy. The above approach used 4 different candle sizes batched from the 1 min events. Any brainstorming/ideas for getting good buy signals for buying with trailing stop losses will be great...


RE: Proper stop losses coming soon - askmike - 09-03-2018

Quote:So you specify your target gain with the trigger, e.g. a target of +1% and the trailing will narrow when the target is reached. So it starts with a trailing of 2% for example and narrows the trailing to 0.2% when the price reaches 1.2% gain. So the price is still able to climb and trails but on a further small price drop it will sell and save the target gain.

Very cool!

Quote:Did pretty much trailing stop loss coding/testing last days. Just adding it into any existing strategy will optimize a couple of percent, at least what I have tested. So I was thinking of a strategy to take real advantage of this feature. @askmike, what are your use cases for this feature?

The biggest one being risk management: Gekko strategies should be trying to ride trends upwards, however after going "all in" riding the trend strategies need some ways to dynamically control risk. People have been asking for stop losses for years, and people have been implementing them inside their strategies up until now. Note that stoplosses aren't new or anything, I just noticed that a lot of people want them so I added them. Having them native and not in a strategy opens a lot of doors, such as using native stop losses on exchanges that support them (currently not done yet, but planned in the future).

That said I am trying to not take part in the debate on what indicators / strategies are good and what is bad. A stop loss is a tool that traders have been using for decades. As such I want people who use Gekko to have the ability to use them. I think that they make sense in a lot of scenarios, but you can always get stopped by a flash crash before the price continues to go up. There are definitely scenarios where they cause more harm than good.


RE: Proper stop losses coming soon - mark.sch - 09-04-2018

(09-03-2018, 04:58 AM)askmike Wrote: The biggest one being risk management: Gekko strategies should be trying to ride trends upwards, however after going "all in" riding the trend strategies need some ways to dynamically control risk. People have been asking for stop losses for years, and people have been implementing them inside their strategies up until now. Note that stoplosses aren't new or anything, I just noticed that a lot of people want them so I added them. Having them native and not in a strategy opens a lot of doors, such as using native stop losses on exchanges that support them (currently not done yet, but planned in the future).

That said I am trying to not take part in the debate on what indicators / strategies are good and what is bad. A stop loss is a tool that traders have been using for decades. As such I want people who use Gekko to have the ability to use them. I think that they make sense in a lot of scenarios, but you can always get stopped by a flash crash before the price continues to go up. There are definitely scenarios where they cause more harm than good.

Thanks for your comprehensive feedback. I like the new core implementation of stop losses since it steps out from the one minute candle limit and monitor SL in near real time. Good to know being one of the fastest sellers during a flash crash ;-)

For potential strong strategy gain improvements it has to be used with caution, it stops your losses on the one hand but also your gains if SL range is small. I can imagine it is worth to test the new SL with bollinger bands - instead of selling when hitting the upper bollinger band, a stop loss can try to catch further gains when the chart climbs its way upwards the band (BB often sells too early). I wonder, there is a PR for a BB indicator implementation but no complete/advanced BB strategy out there. Do you know projects using BB with Gekko and do you plan to integrate the BB PR?

Since SL was a long awaited feature from the community, I often read questions about multi time frames and multi markets. With the workaround of manual candle batching, the multi time frames work fine already. Like many others I can imagine that candles from a second market offer new potentials for trading strategies. I consider to achieve this with one of the following four ways:

- little helper function to read a (foreign market) candle from db, running from inside the strategy (quick and dirty solution)
- cloning the market functionality inside core
- use of remote candles. Connecting to a socket server to request a certain candle. Similar to the leecher mode, but not using the db but a socket connection, e.g. jabber/xmpp.
- using redis inside the strategy or on a lower layer to communicate between two local running gekkos

What do you think?


RE: Proper stop losses coming soon - travislwilson - 09-05-2018

Thank you for adding a native stop-loss, I've been using a helper file to add a stop-loss before this and it worked but this is much cleaner and with this I can access bid/ask data Smile Now that it is trailing profit I'm trying to get it to trail down aka if the EMA cross's over or RSI signals a buy start trailing the bid price down by a percent once the bid price starts moving up go long and set a stop-loss on profit. So far I've copied and renamed trailingStop.js to trailingStopDown.js and reversed the logic on line 27 and 33. But i'm having a hard time getting it to trigger without a long advice signal. If the logic of what I'm trying to do makes sense is it possible to trail the price down on a indicator signal then buy when the bid price goes back up?


RE: Proper stop losses coming soon - askmike - 09-06-2018

Quote:if the EMA cross's over or RSI signals a buy start trailing the bid price down by a percent once the bid price starts moving up go long and set a stop-loss on profit.

So once you detect your initial buy signal you want to to have some form of a trailing mechanism that confirms the signal? Most example strategies in Gekko come with a "persistence" system, that checks if the buy signal persists for X candles. Note that this is all implemented directly inside the strategy.


Quote:So far I've copied and renamed trailingStop.js to trailingStopDown.js and reversed the logic on line 27 and 33. But i'm having a hard time getting it to trigger without a long advice signal. If the logic of what I'm trying to do makes sense is it possible to trail the price down on a indicator signal then buy when the bid price goes back up?

I would advice against this: in order to have trailing stoplosses I had to implement them in a lot of places in the gekko codebase:

- I had to create the base class.
- I had to wrap that in the paper trader (based on 1min candles)
- I had to wrap that in the real trader (based on ticker polls)
- I had to introduce new gekko events
- I had to handle all the logic around: if a strat signalled a buy with a stoploss, set it up and listen to it triger.
- if before it triggered the strategy signaled a sell clean it up.
- make sure to always update local state after it triggers so gekko is aware that the exposure reversed without a trade signal (but a trailing stop trigger).

It took me a while and there were a ton of bugs, some I was able to shield with unit tests but I still get bug reports here and there.

As though why the trailing stops only go one way now: Gekko is build on some core assumptions, one being that signaling a LONG is risky (since you are about to buy into exposure) but signalling a SHORT is not (since you are selling out of exposure).


-----


In your case I would stick all the required logic inside your strategy and only signal LONG when you are sure (with your own trailing logic). You can definitely hack the trailingStop class I created, but I would import it into your strategy and don't monkey patch it into gekko!


RE: Proper stop losses coming soon - wewantmoore - 09-06-2018

(08-27-2018, 12:27 PM)askmike Wrote: Quick summary:
- Your strategy can use the (experimental) onTrade handle to be notified of all executed trades (including those fired from trailing stops).

Mike,

Could you explain how to use the onTrade handle to be notified of a stoploss sell?


RE: Proper stop losses coming soon - mark.sch - 09-06-2018

Quote:Could you explain how to use the onTrade handle to be notified of a stoploss sell?


You need only to add a couple of lines into your strategy to get notified when the gekko event system is firing a trade completed event. It is the same way like you add the init or check function to your strategy:

strat.onTrade = function (trade) {


    //catch all trades.  Whether from manual buy/sell (telegram bot), a stoploss trigger or from a strategy advice
  
    if (trade.action == 'sell') {
        // do something here
    }

}


From console.log(trade) output you see all properties the trade object is containing:

{ id: 'trade-7',
  adviceId: 'advice-4',
  action: 'buy',
  cost: 1.4524059748649476,
  amount: 0.07355882,
  price: 13143.47,
  portfolio: { asset: 0.07355882, currency: 0 },
  balance: 966.8181439053999,
  date: moment("2018-01-16T01:45:00.000"),
  effectivePrice: 13123.754795,
  feePercent: 0.15,
  status: 'tradeCompleted',
  trigger: { origin: 'trailingStop', trailPercentage: 2 } }


RE: Proper stop losses coming soon - BotsEdge - 09-09-2018

(09-03-2018, 04:58 AM)askmike Wrote:
Quote:So you specify your target gain with the trigger, e.g. a target of +1% and the trailing will narrow when the target is reached. So it starts with a trailing of 2% for example and narrows the trailing to 0.2% when the price reaches 1.2% gain. So the price is still able to climb and trails but on a further small price drop it will sell and save the target gain.

Very cool!

Quote:Did pretty much trailing stop loss coding/testing last days. Just adding it into any existing strategy will optimize a couple of percent, at least what I have tested. So I was thinking of a strategy to take real advantage of this feature. @askmike, what are your use cases for this feature?

The biggest one being risk management: Gekko strategies should be trying to ride trends upwards, however after going "all in" riding the trend strategies need some ways to dynamically control risk. People have been asking for stop losses for years, and people have been implementing them inside their strategies up until now. Note that stoplosses aren't new or anything, I just noticed that a lot of people want them so I added them. Having them native and not in a strategy opens a lot of doors, such as using native stop losses on exchanges that support them (currently not done yet, but planned in the future).

That said I am trying to not take part in the debate on what indicators / strategies are good and what is bad. A stop loss is a tool that traders have been using for decades. As such I want people who use Gekko to have the ability to use them. I think that they make sense in a lot of scenarios, but you can always get stopped by a flash crash before the price continues to go up. There are definitely scenarios where they cause more harm than good.

Following the whole thread i´d like to contribute.


First of all if you ask me as a seasoned trader i would like to see the following order types in Gekko:
1. Limit order
2. Stoploss order ( a Stop Loss Limit order is not needed since most traders will not use the stop limit window)
3. Profit Stop order (Trailing stop already integrated)(again a Profit Stop Limit order is not needed for most traders)
4. Limit Maker order

Then for the first quote above. The system of trailing down the profit factor can be done by using an existing TA Indicator called Parabolic SAR. It is included with the Tulip Indicator library that we all installed when installing Gekko. The Parabolic SAR was invented by Welles J. Wilder and is widely used in the trading world. It basically is a reversal system with an accelerator factor that closes in on the price while in a constant trend. When the accelerator function finally hits the price it reverses. 
The problem with this as a profit factor feature is, that it can only work if we have any means of defining the profit itself.
Sure we can say let´s define a 1% gain target but 1% might not be enough or it might be too much.


In order for this to be a valid feature that is helpful it needs to be coordinated with a portfoliomanagement system.

For the stoploss orders i agree. Any trading bot that wants to ride any kind of trend needs a stoploss order system as a native feature. 
Statistically markets are in trend situations for about 20% of the time and in sideway mode for about 80% of the time, so in order to "work" a market with any kind of automated system the bot needs a working stoploss system that is connected to a moneymanagement system so it can actually find out whether it wants to trade with the kind of market volatility that it sees.

Looking through a lot of strategies for the Gekko bot i have to say that most of the stuff is not new but general technical analysis knowledge and i very much doubt that any one is profitable for a long period of time. Reason being is not that they are programmed the wrong way or that they don´t work in general on crypto, the reason is that none of these are connected to moneymanagement.
In the tradingworld apart from cryptocurrencies TA is used a lot but it is never used alone without any riskmanagement or moneymanagement. Plus the more complex a TA Indicator gets the more prone to failure it is. Most of the time complexity only means "overfiltering" the strategy for a given time period (backtesting).

So what Gekko needs is kind of an advanced tab where a trader can put in his money-, risk-, portfoliomanagement parameters.


RE: Proper stop losses coming soon - wewantmoore - 09-10-2018

(09-06-2018, 11:13 PM)mark.sch Wrote:
Quote:Could you explain how to use the onTrade handle to be notified of a stoploss sell?


You need only to add a couple of lines into your strategy to get notified when the gekko event system is firing a trade completed event. It is the same way like you add the init or check function to your strategy:

strat.onTrade = function (trade) {


    //catch all trades.  Whether from manual buy/sell (telegram bot), a stoploss trigger or from a strategy advice
  
    if (trade.action == 'sell') {
        // do something here
    }

}


From console.log(trade) output you see all properties the trade object is containing:

{ id: 'trade-7',
  adviceId: 'advice-4',
  action: 'buy',
  cost: 1.4524059748649476,
  amount: 0.07355882,
  price: 13143.47,
  portfolio: { asset: 0.07355882, currency: 0 },
  balance: 966.8181439053999,
  date: moment("2018-01-16T01:45:00.000"),
  effectivePrice: 13123.754795,
  feePercent: 0.15,
  status: 'tradeCompleted',
  trigger: { origin: 'trailingStop', trailPercentage: 2 } }

Thanks a lot!