it seems dead loop if just simple cross strategy
-
In my market, it can send short order not just close the long order. So I make a simple strategy when KDJ's K cross D then place long order, and close short-order at same time(if it has), if K lower then D, then place short order and close long order at same time.
if self.position.size == 0: if self.k[0] > self.d[0] : self.order = self.buy() else: self.order = self.sell() elif self.position.size < 0: if self.k[0] > self.d[0] : self.order = self.close() elif self.position.size > 0: if self.k[0] < self.d[0] : self.order = self.close()
It seems mess . It place orders ramdom, seems the previous order not finished then place new order, or Closing order not finish then come up next order...not sure.. just like dead loop till funds goes down to reject new orders.
Any suggestion for this?
-
@goldcar said in it seems dead loop if just simple cross strategy:
if self.position.size == 0: if self.k[0] > self.d[0] : self.order = self.buy() else: self.order = self.sell() elif self.position.size < 0: if self.k[0] > self.d[0] : self.order = self.close() elif self.position.size > 0: if self.k[0] < self.d[0] : self.order = self.close()
You will only execute one of these conditionals per loop. This means that you cannot close and go short for example, in the same loop, using this code. You should also note that the buys and sells will not happen instantly. So to correct you may wish to try the following:
if self.k[0] > self.d[0]: if self.position: self.close() self.order = self.buy() elif self.k[0] < self.d[0]: if self.position: self.close() self.order = self.sell()
-
@run-out said in it seems dead loop if just simple cross strategy:
@goldcar said in it seems dead loop if just simple cross strategy:
if self.position.size == 0: if self.k[0] > self.d[0] : self.order = self.buy() else: self.order = self.sell() elif self.position.size < 0: if self.k[0] > self.d[0] : self.order = self.close() elif self.position.size > 0: if self.k[0] < self.d[0] : self.order = self.close()
You will only execute one of these conditionals per loop. This means that you cannot close and go short for example, in the same loop, using this code. You should also note that the buys and sells will not happen instantly. So to correct you may wish to try the following:
if self.k[0] > self.d[0]: if self.position: self.close() self.order = self.buy() elif self.k[0] < self.d[0]: if self.position: self.close() self.order = self.sell()
oh...I see. thanks a lot