-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Close portion of trade and leave remainder to run #992
CadeHalcyon
started this conversation in
General
-
Hi there, I have a simple problem, which is driving me crazy. I've simplified a scenario below... I want to:
- close 50% of long trade when price rises 5% above the trade entry price
- then move the stop loss to the entry price
- leave the rest of the trade to run until it hits the tp target of +10% set via
self.buy(tp=price*1.10....)
The problem:
The trade will continue to reduce by 50% again and again after price moves 5% above entry price. Trade size will go from 1000 to 500 to 250 to 125 etc...
Question:
How do I only close out a portion of the trade once, please? In my mind, I want to somehow reference the size of the trade when it was first opened to create a condition for closing 50%, but trade.size
in self.trades
and position.size
in self.position
both get overwritten each time a portion of the trade is closed.
Grateful for any help! Simplified code below:
price = self.data.Close[-1]
if not self.position:
self.buy(sl=price*0.98, size=1000, tp=price*1.10)
if self.position:
for trade in self.trades:
if trade.is_long:
if price > (trade.entry_price * 1.05): #want to close out half of position when first TP of +5% is hit
self.position.close(portion=0.5) #how to make this only happen once for each trade?
trade.sl = trade.entry_price #bring stop loss to entry and let remaining position ride until either SL or 10%TP hit
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment