Never Closes Position
-
Hello,
I have a problem with the order execution of my algorithm. Even though the conditions are met it only executes the order once at the beginning of the time period and then it just holds the position forever.def next(self): self.status = "neutral" if not self.position: if self.beta.spread[0] >= self.beta.spread_2sd[0]: self.log(f'BUY CREATE {1} ETH, @ {self.datas[0].close[0]} ') # Keep track of the created order to avoid a 2nd order self.order = self.buy(data=self.datas[0], size=1) self.log(f'SHORT CREATE {self.beta.beta[0]:.3f} BTC, @ {self.datas[1].close[0]}') self.order = self.sell(data=self.datas[1], size=self.beta.beta[0]) self.status = "long spread" elif self.beta.spread[0] <= self.beta.spread_m2sd[0]: self.log(f'BUY CREATE {self.beta.beta[0]:.3f} BTC, @ {self.datas[1].close[0]}') # Keep track of the created order to avoid a 2nd order self.order = self.buy(data=self.datas[1], size=self.beta.beta[0]) self.log(f'SHORT CREATE {1} ETH, @ {self.datas[0].close[0]}') self.order = self.sell(data=self.datas[0], size=1) self.status = "short spread" elif self.position.size !=0: if self.status == "long spread" and self.beta.spread[0] == self.beta.spread_sma15[0]: self.log(f'CLOSE LONG {1} ETH, @ {self.datas[0].close[0]}') self.order = self.sell(data=self.datas[0], size=1) # Keep track of the created order to avoid a 2nd order self.log(f'COVER SHORT {self.beta.beta[0]:.3f} BTC, @ {self.data[1].close[0]}') self.order = self.buy(data=self.data1, size=self.beta.beta[0]) self.status = "neutral" elif self.status == "short spread" and self.beta.spread[0] == self.beta.spread_sma15[0]: self.log(f'COVER SHORT {1} ETH, @ {self.datas[0].close[0]}') # Keep track of the created order to avoid a 2nd order self.order = self.buy(data=self.datas[0].close, size=1) self.log(f'COVER LONG {self.beta.beta[0]:.3f} BTC, @ {self.datas[1].close[0]}') self.order = self.sell(data=self.datas[1].close, size=self.beta.beta[0]) self.status = "neutral"
-
@ssbilis typically orders are not executed if there is not enough cash on the account. i would recommend you to add more logging in your script to see what is actually going on.
-
@ab_trader
Cash isnt the problem. If I remove the
if not self.position
it makes a bunch of trades, but I dont want it to do that.
I dont want it to re-enter if I already have an open position. Also, I dont understand why it never closes the position utilizing the conditions that are after theelse:
What logging do you recommend?
Currently, I havedef log(self, txt, dt=None): dt = dt or self.data.datetime[0] if isinstance(dt, float): dt = bt.num2date(dt) print("%s, %s" % (dt.date(), txt))
Which ends up looking like this
Starting Portfolio Value: 1000000.00 2020-11-09, BUY CREATE 0.533 BTC, @ 15332.32 2020-11-09, SHORT CREATE 1 ETH, @ 444.16 ================================================== Starting Value - 1000000.00 Ending Value - 1016074.43 ================================================== Final Portfolio Value: 1016074.43
-
@ssbilis I'm not sure your conditions are being met on the second half of the transaction. There is a log statement at the beginning of each and you have no log outputs. The problem is likely in here although I have no way of knowing:
self.beta.spread[0] == self.beta.spread_sma15[0]
-
@run-out I don't get it. This should work. You can even see on the graph that the condition is clearly met.
Would it help if I provided more of my code? -
@ssbilis said in Never Closes Position:
What logging do you recommend?
Usually I recommend to print all data related to the issue in the script. In your case everything related to position closing algorithm which apparently doesn't work. So the following maybe printed (based on your script):
self.status
,self.beta.spread[0]
,self.beta.spread_sma15[0]
,self.beta.beta[0]
as well as prices which are used to execute orders. Cash as well.Than looking thru this data you can see if your conditions met or not. Until that time I would agree with @run-out.
-
@ssbilis In this line:
@run-out said in Never Closes Position:
self.beta.spread[0] == self.beta.spread_sma15[0]
These two values must be exactly equal for this to be true. Is that the case?
-
@run-out I dont know.
But I changed the conditions to
self.beta.spread[0] <= self.beta.spread_sma15[0]
and
self.beta.spread[0] >= self.beta.spread_sma15[0]
and I get the same error -
@run-out Well I fixed it.
It was a stupid mistake.
I am initializing self.status outside of the if statement and it is becoming ='neutral' after each trade, which is why then the closing executions never meet part of the condition