Stop Loss using Timing and Current Profit
How would one implement the following stop loss:
IF Length of Trade >= 20 minutes AND Current Profit on Trade < 0.1%: self.close()
mics last edited by
I've previously used this approach for counting bars from when a condition or order has been placed. There is probably a more elegant solution but this approach has worked for me.
It needs to be noted that the 'enter_price' is only the price that triggers the placement of the order, it is not the executed price.
Perhaps someone else can provide some more insight on how to improve this by calling the executed price. This would improve the accuracy, particularly if you are using bracket orders.
def __init__(self): self.len_bar = 20 self.set_bar = 0 self.enter_price = 0 def next(self): if not self.position: if self.data.close > [YOUR LONG CONDITION IS MET]: self.buy() self.set_bar = len(self.data0) #Bar when order is placed self.enter_price = self.data.close #Price that order is palced on if self.position.size > 0 if (len(self.data0) - self.freezebar) >= self.len_bar: #Greater than 20mins from buy order if (self.data.close - self.enter_price) < (0.001 * self.enter_price): self.sell() self.set_bar = 0 #RESET elif (self.data.close - self.enter_price) >= (0.001 * self.enter_price): [DO SOMETHING ELSE]