Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options/Ideas for fixed TP/SL (take profit/stoploss) ratio at trade-entry #7414

Closed
valentijnk007 opened this issue Sep 14, 2022 · 12 comments
Closed
Labels
Question Questions - will be closed after some period of inactivity. Strategy assistance Help with a strategy

Comments

@valentijnk007
Copy link

Hi All,

First I want to thank all the contributors, this is a great trading bot supporting a lot of functionality!!! Below my situation and my research that I have done on the freqtrade website (places links to info that I already found), there is a lot of content the website also many thanks to all those contributors!!!

Looking forward to anyone's respons, maybe we can open an discussion how we could handle these kind of trades.

The TP and SL style that I use

I have done some manual trading and I try to convert my trading rules to a strategie in freqtrade. One of the things that I do manualy after entering is the following:

  1. Calculate stoploss price, based on ATR multiplier / SAR / last pivot low + ATR
  2. Place a sell/stopmarket order on my stoploss price
  3. Calculate difference between entry price and my stoploss price
  4. Multiply this difference by 2 and use this value to calculate the take profit target
  5. Place a sell/stopmarket order on my takeprofit price (because it need a stopprice to be triggerd it will stay until that price is triggers, see attachment)
  6. Result fixed TP/SL ration of 2:1

result see picture (i didn't the calculation for take profit target on this one)
afbeelding

My environment

  • Operating system: ubuntu server 22.04 (docker)
  • Python Version: Python 3.10.6 (in docker-container)
  • CCXT version: ccxt==1.92.20 (I use kucoin as an exchange)
  • Freqtrade Version: freqtrade docker-16af10a5

My question

Has anyone experience with the way of using stoploss and take profit as described by above "TP and SL style"? Below are my suggestions I think I have a solution for the "fixed" stoploss, but for the "fixed" take profit I don't have complete idea. I also placed links to the freqtrade website with the information.

Possible solution fixed stoploss on start of trade (I think):
custom_stoploss: this will be triggered continuously so the ATR values also continuously changed. I think I can fix this by check the time in trade, if it is grater than 1m I won't adjust the price anymore (did not test this).

This would be a combination of time based trail stop and trail stop on SAR and example ATR from stoplos_from_absolut

My idea of realising fixed take profit:
Option 1a:
I think I need to use the custom exit signal. But the dataframe is continuously changing, so also the ATR/SAR values... so also my TP/SL ratio. Does anyone know if you can request the SL price and entry price in that function? If that is accessible in that function, I could do the same calculation as above and use helper stoploss_from_absolute to calculate the percentage ratio for generationg the exit signal.

Option 1b:
Use custom exit, but maybe we can save custom variables at the start of the trade and request them in the custom exit function?

Option 2:
Another solution would be to place a second sell/stopmarket order on the exchange at the start of the trade. Does any one know if this is possible? Or are the more supporters for this function? Maybe we could figure something out together.

@valentijnk007 valentijnk007 added the Question Questions - will be closed after some period of inactivity. label Sep 14, 2022
@xmatthias
Copy link
Member

for stoploss, using custom-stop is clearly the right (and only) approach.
you do get the trade object - so you can compare the open date with the candle date - and only act if they are close enough together.

take-profit you can do something similar using custom_exit - the open rate / values are available as part of the trade object (trade.open_rate, trade.stop_loss, ...).

You may want to look at the advanced strategy part - which should contain exactly what you need.

Now the take-profit order will not be on exchange - but freqtrade will take care of cancelling the stoploss and placing the take-profit order.

@xmatthias xmatthias added the Strategy assistance Help with a strategy label Sep 14, 2022
@valentijnk007
Copy link
Author

@xmatthias thanks for your fast response!!

nice to hear freqtrade can cancel the stoplos order, going to try that.

I am experimenting with the custom_exit, i have downloaded the example advanced template strategy
(freqtrade new-strategy xxxx --template advanced)

But I get an error on the trade: 'Trade', see below. Did I miss something?
afbeelding

@xmatthias
Copy link
Member

xmatthias commented Sep 14, 2022

you should be able to import Trade (and Order) from persistence

add from freqtrade.persistence import Trade, Order to the imports section.

There's however a difference between a pylance warning - and an actual error.
you'll notice that the strategy will run just fine.

@valentijnk007
Copy link
Author

@xmatthias thanks again for your fast response!!!! warnings are gone 👍

@valentijnk007
Copy link
Author

@xmatthias or someone else,

Is it possible to accesss the custom information (descibed here) in the custom_stoploss and custom_exit functions?

I want to use the custom info for the TP/SL targets and reqeust them in those functions.

to se the custom info i think you need metadata, se below example
afbeelding

But this is only accessible in below functions.
afbeelding

Is it possible to access the custom_info in other functions?

Thanks in advance!!

@xmatthias
Copy link
Member

access won't change based on the method - it's still a property of the instance (self.custom_whatever).
you don't need metadata though - you can just use "trade.pair" - or pair (depending on the callback.

You'll need to be aware however that this info is NOT persisted - so handle non-existence gracefully (as the docs also mention) - for example after you restart your bot - this data might be empty.

@valentijnk007
Copy link
Author

@xmatthias thank you very much. Sounds ovious hehe, sorry for the question.

I want to share my code, see below examples. At the confirm trade entry, I calculate the tp and sl for that pair. And in the custom stoploss and exit I request those values again.
afbeelding
afbeelding

@xmatthias
Copy link
Member

If you restart the bot with this code and an open trade, your stoploss / exit will crash with KeyError (although the bot will handle this - and simply act as if the methods were not implemented).

The documentation for this is quite clear - and requires checking if the content is actually there.

@valentijnk007
Copy link
Author

Thanks for the tip and will keep that in mind! Thanks again for your fast response!!!

I have enough info to figure something out! This was very helpfull!!

Enjoy your day!!

@Alirezaadham
Copy link

Thanks for the tip and will keep that in mind! Thanks again for your fast response!!!

I have enough info to figure something out! This was very helpfull!!

Enjoy your day!!

Hi, could you please share your code for the sl and tp part? My question is as same as yours but I couldn't figure out a way to set custom sl and tp based on ATR. I come from a poor background of coding and it's hard for me to work things out.

@asuiu

This comment was marked as off-topic.

@xmatthias
Copy link
Member

Creating a take oco order through ccxt directly is neither a problem, nor the problem stated here.

as this code is not compatible with freqtrade (if you place this in a strategy - freqtade will lose track of orders created directly).
Because of this, i'll be hiding this comment to avoid people fall into the trap of assuming it's actually working with freqtrade.

@freqtrade freqtrade locked as resolved and limited conversation to collaborators Feb 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question Questions - will be closed after some period of inactivity. Strategy assistance Help with a strategy
Projects
None yet
Development

No branches or pull requests

4 participants