Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Trailing Stop Loss on % #918

Unanswered
lmiller251 asked this question in Q&A
Feb 22, 2023 · 1 comments · 4 replies
Discussion options

Hey All,

I saw some posts from a few years ago that talked about adding a feature for calculating trailing stops based on %. I cant find much commentary on how to utilize this feature. Was it ever added and am I just overlooking prior discussions? Appreciate anyone pointing me in the right direction.

Thanks for creating and maintaining all of this.

You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

Hi,

The following applies a 25% trailing stop to all open trades. For every bar, it sets the stop as the maximum of the existing stop and 75% of the last close.

def next(self):
 price = self.data.Close[-1] 
 if self.position: 
 for trade in self.trades:
 trade.sl = max(trade.sl, price * .75) 

Is this what you mean?

You must be logged in to vote
4 replies
Comment options

Thanks for the response. So i coded this in with an inital sl = 2% (price*.98), and then I changed your example to a trailing 2% stop loss (trade.sl = max(trade.sl, price * .98) and that is exiting trades at a 2% loss, even if that price never hit. Have you ever had this error? Larger code below for reference.

start_price = self.data.Close[-1]
if self.sma < current_price * .99:
self.buy(size=.99, sl = start_price*.98)

price = self.data.Close[-1]
if self.position:
for trade in self.trades:
trade.sl = max(trade.sl, price * .98)

Comment options

I think self.sma should be a series, so you want to take a specific value, e.g. self.sma[-1] will take the current bar.

Is current_price defined elsewhere in your code or is it the same as price and start_price?

size = .99 would take 99% of your current (available or total?) equity. I would either set it to size=1 for one share (if you want multiple positions) or leave it blank to use all your equity (all-in).

Having the buy block outside of the if self.position block will mean that your code tries to place a buy order every bar that the sma is less than the (current price x 0.99). If you want it to only buy if there is no existing position, use this:

price = self.data.Close[-1]
sma = self.sma[-1]
if self.position:
 for trade in self.trades:
 trade.sl = max(trade.sl, price * .98)
elif sma < price * .99:
 self.buy(sl = price*.98)

The code above will enter a buy signal if there is no existing position and the (current price x 0.99) is above the SMA. Once a position exists, there will be a 2% trailing stop.

Not sure anything of what I have said will help with positions exiting at a price that has not been met. That shouldn't be possible.

Comment options

That helps clarify the trailing. I am writing this for a short strategy as well, can you advise what exactly the price *.98 in the trade.sl is doing? Would that not act the same as a take profit essentially? my understanding is that this would set the stop loss at 2% below the previous days closing price (ie. a take profit). The reason I ask is I inadvertently inserted that number and it is skewing my results (in a positive way) but I want to understand what it is doing. Appreciate the help.

price = self.data.Close[-1]
if self.position.is_short:
 for trade in self.trades:
 trade.sl = min(trade.entry_price * 1.02, price * .98)
 trade.tp = trade.entry_price * .98
Comment options

for a long trade, to set new values: sl < price < tp
for a short trade, sl > price > tp

In your code, you are updating the stop based on the minimum of the entry * 1.02 (this will not change) and the current price * 0.98. This doesn't make sense to me. Similarly, you are updating the take profit based on the entry, so this value will not change.

I think you want:

if self.position.is_short:
 for trade in self.trades:
 trade.sl = min(trade.sl, price * 1.02) #sets a 2% trailing stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /