TrailingStopLoss and TakeProfit - cyberjunky/3commas-cyber-bots GitHub Wiki

DCA Trailing stoploss, safety order and profit bot helper named trailingstoploss_tp.py

Type = stop loss

What does it do?

It will change the trailing stoploss (and optionally the profit %) of a DCA deal when the profit % is above the activation-percentage setting. Also, it provides the option to apply trailing to the buy moment of Safety Orders. This works for both long and short deals.

How does it work?

The bot does run on two intervals;

  • a check interval to check the active deals
  • a monitor interval for monitoring deals when a stoploss is active or the buy moment of a Safety Order is near.

The shorter interval is required, in order to keep the deal up to date and don't miss profit changes to interact on.

Both intervals perform the same steps. First the bots are read, their active deals are checked for profit %. Based on the profit, it's determined if the profit or safety part needs to be evaluated.

While processing the deals, the script will keep track of:

  • The number of deals with SL activated, or for which the buy of a Safety Order is near, which is required to determine which time interval (check or monitor) to use.
  • The active deals. Deals which where monitored before and are not active anymore (closed) are removed from the database in order to prevent an every growing database.
  • Resetting trailing and TP values. For instance, a deal which was in profit and the TP was increased for, will be reset to the default TP if the profit falls below the first configured target in the profit-config.

Then the bot helper will sleep for the set interval time, after which it will repeat these steps.

This script can be used for multiple bots with different TSL, SO and TP settings by creating multiple tsl_tp_ sections in the configuration file. Each section is processed as described above. Make sure each section starts with tsl_tp_ between the square brackets, what follows does not matter and can be used to give a descriptive name for yourself.

Profit

This script will take over the trailing take profit from 3C, so make sure TTP is disabled for your bot(s) and deal(s). Advantage is the limit order is already in the orderbook, so on a sudden pump the deal will close as with the 3C TTP functionality the market order could be left unfilled.

If the profit is above or equal to activation-percentage, the initial SL is calculated, like so:

new_stoploss = initial-stoploss + (actual_profit_percentage - activation_percentage)

This script also supports a configurable timeout for the stoploss, which will be activated (and updated) when the stoploss is activated. For example, with a timeout of 60 seconds, 3Commas will sell the deal (market order) when after 60 seconds the current price is still below the set stoploss price.

The take profit can also be increased using the tp-increment-factor and the calculation is like this:

new_takeprofit = takeprofit + ((actual_percentage - activation_percentage) * tp-increment-factor)

Configuring the tp-increment-factor to 0.0 will disable the increment and leave the TP untouched to what is configured in the bot.

Do note that extra profit is directly included if the increment-factor is greater than 0.0! So, for example, when the activation-percentage is set to 3.0% and the actual profit is 3.2%, this 0.2% is immediately added to the initial-stoploss.

The last profit percentage of the deal is stored to be used for next iterations, so the bot only evaluates deals for which the % profit has increased to avoid unnecessary processing. In the calcutions shown above the current and last profit percentage will then be used.

Safety

Before explaining the calculation part, it's good to understand 3C doesn't offer trailing Safety Orders. The bot configuration contains only fixed percentages and for this script to work the Max active safety orders count needs to be set to 0! Also do only enable the script for new deals as it will NOT take already filled Safety Orders into account. This all will result in a bot (and therefor deal) having a number of Safety Orders, which are not activily managed by 3C. That's were this script takes over.

If the profit is below the configured activation-percentage, the Add Funds percentage is calculated based on the initial-buy-percentage and buy-increment-factor. For example; SO is on -1.0%, current profit is -1.5%, initial-buy-percentage is 0.1% and buy-increment-factor is 0.4 will result in an Add Funds percentage of -1.26%.

Calculation explained:

  • Difference is 0.5 (1.5 - 1.0).
  • Initial buy is 0.1, meaning the Add Funds will be placed at least on -1.1%.
  • Remaining is 0.4% (the difference of 0.5 minus the initial buy) on which the increment factor of 0.4 is applied, resulting in 0.16%. This is added to the previous -1.1% resulting in the -1.26%.

This means that the Safety Order is executed when the profit reaches, or passes, the -1.26%. If the profit drops further, this percentage is increased and thus resulting in the trailing behaviour.

When the profit drops further, it could be it passed also the next configured Safety Order. When the price start increasing again after passing more than one Safety Order levels, these Safety Orders are combined and bought in one order. This is what the safety-mode set to merge means.

A disadvantage to preset Safety Orders would be the limit vs market order. The only way trailing can be used is by using market orders. Therefor this script also monitors the profit after placing a manual Add Funds order; when the profit rises too much (exceeding the percentage the Safety Order must be on) the order is cancelled and trailing reset. When the profit drops again, the whole trailing as explained above will start again.

Advanced configuration

This script supports some advanced configuration which should be understood! The basic purpose is to provide a trailing stoploss and optionally increasing take profit in the profit-config. Provide the safety configuration in the safety-config. Leaving one of these empty means it won't be applied and 3C will manage it according to the bot settings.

When a deal starts to make profit the price will go up and down and therefor some space should be available to do so. So, for example, at 2% the SL can be set around 0.5% so the 1.5% can be used to go up and down. When the profit increases, for example to 4%, you may want to set the SL to 3% to avoid missing some profit (otherwise the SL would still be around 2%, depending on the sl-increment-factor). And sometimes, you just want to prevent a deal going down and rather have a fixed SL at a certain percentage.

This same mechanics is also something you want to have for determining the best buy moment of a Safety Order.

The great thing is; this is all possible with this script! The profit-config of each section can contain zero or more configurations which will be used. Make sure the configuration are in order, increasing in activation-percentage! As example:

  • The first configuration could have a lower activation-percentage of 2.0%, an initial-stoploss-percentage of 0.5% and the increment-factors are set to 0.0 (disabled).
  • The second configuration could have a activation-percentage of 3.0%, an initial-stoploss-percentage of 2.0% and the increment-factors are greater than 0.0 (enabled). This will result in a fixed SL of 0.5% when the profit reaches 2.0%. Even when the price or market dumps, you don't end up with a red bag because of this SL. Between 2.0% and the 3.0%, the SL remains untouched. As soon as the profit reaches the 3.0%, the second configuration will be used and the SL is directly set to 2.0%; and from there the trailing starts.

The same applies for the safety-config of each section which can also contain zero or more configurations to be used.

See the configuration below as an example on how to achieve this.

Note: the config is order sensitive! This means it must be sorted by having the lowest activation-percentage first and only increasing. When you want to apply different rules for the same activation-percentage based on the activation-so-count, then must sort it by increasing activation-so-count.

In depth

The percentages and how the stoploss works at 3C can be confusing. Please read the following document to understand this better: in-depth

Author of this script is amargedon.

Configuration

This is the layout of the config file used by the trailingstoploss_tp.py bot helper:

  • timezone - timezone. (default is 'Europe/Amsterdam')

  • check-interval - update interval in Seconds when no deals with SL are active. (default is 120)

  • monitorinterval - update interval in Seconds when there are deals with SL active. (default is 60)

  • debug - set to true to enable debug logging to file. (default is False)

  • logrotate - number of days to keep logs. (default = 7)

  • 3c-apikey - your 3Commas API key value.

  • 3c-apisecret - your 3Commas API key secret value.

  • 3c-apikey-path - path to your own generated RSA private key, or empty.

  • notifications - set to true to enable notifications. (default = False)

  • notify-urls - one or a list of apprise notify urls, each in " " seperated with commas. See Apprise website for more information.

  • notify-trailing-start - controls whether notifications about trailing start are send. (default = True)

  • notify-trailing-update - controls whether notifications about trailing update are send. (default = True)

  • notify-trailing-reset - sometimes the profit makes a hugh jump, resulting in incorrect trailing levels (buying a SO above the configured percentage, for example) which are prevented and the trailing will be reset. This option controls if there is notification send. (default = True)

  • [tsl_tp_]

  • botids - a list of bot id's to manage separated with commas.

  • profit-config - a list of objects with the settings for the profit configuration.

    • activation-percentage - from % of profit this object is valid for.
    • initial-stoploss-percentage - % of stoploss to set when activation-percentage is reached.
    • sl-timeout - stoploss timeout in seconds. (default = 0)
    • activation-so-count - don't activate before this number of safety order's are active for deal. (default = 0)
    • sl-increment-factor - % to increase the SL with, based on % profit after activation-percentage.
    • tp-increment-factor - % to increase the TP with, based on % profit after activation-percentage.
  • safety-config - a list of objects with the settings for the safety configuration.

    • activation-percentage - from % of profit this object is valid for.
    • activation-so-count - don't activate before this number of safety order's are active for deal. (default = 0)
    • initial-buy-percentage - % to add to the configured Safety Order percentage.
    • buy-increment-factor - % to increase the Add Funds percentage with, based on % profit after activation-percentage and keeping the initial-buy-percentage into account.
  • safety-mode - the safety mode to use. For now only merge is supported. (default = merge)

Example: (keys are bogus)

[settings]
timezone = Europe/Amsterdam
check-interval = 120
monitor-interval = 60
debug = False
logrotate = 7
3c-apikey = 4mzhnpio6la4h1158ylt2
3c-apisecret = 4mzhnpio6la4h1158ylt4mzhnpio6la4h1158ylt4mzhnpio6la4h1158ylt4mzhnpio6la4h1158ylt4mzhnpio6la4h1158ylt4mzhnpio6la4h1158ylt4mzhnpio6la4h1158ylt4mzhnpio6la4h1158ylt
3c-apikey-path = 
notifications = True
notify-urls = [ "tgram://9995888120:BoJPor6opeHyxx5VVZPX-BoJPor6opeHyxx5VVZPX/" ]
notify-trailing-start = True
notify-trailing-update = True
notify-trailing-reset = True

[tsl_tp_default]
botids = [ 123456 ]
profit-config = [{"activation-percentage": "2.0","initial-stoploss-percentage": "0.5","sl-timeout": "0","activation-so-count": "0","sl-increment-factor": "0.0","tp-increment-factor": "0.0"},{"activation-percentage": "3.0","initial-stoploss-percentage": "2.0","sl-timeout": "800","activation-so-count": "0","sl-increment-factor": "0.4","tp-increment-factor": "0.4"}]
safety-config = [{"activation-percentage": "0.25","activation-so-count": "0","initial-buy-percentage": "0.0","buy-increment-factor": "0.5"},{"activation-percentage": "0.75","activation-so-count": "0","initial-buy-percentage": "0.0","buy-increment-factor": "0.8"}]
safety-mode = merge

Example output for Profit

Trailingstoploss_tp

Example output for Safety

Trailingstoploss_tp