Understand long revers short logic
-
Hello,
I find this in a blog https://backtest-rookies.com/2017/08/22/backtrader-multiple-data-feeds-indicators/?unapproved=1976&moderation-hash=b8dac3607f1017db7f47ff98c5d042e6#comment-1976regardless the multiple data feed. I am not fully understand the logic of the next method :
pos = self.getposition(d).size if not pos: # no market / no orders if self.inds[d]['cross'][0] == 1: self.buy(data=d, size=1000) elif self.inds[d]['cross'][0] == -1: self.sell(data=d, size=1000) else: if self.inds[d]['cross'][0] == 1: self.close(data=d) self.buy(data=d, size=1000) elif self.inds[d]['cross'][0] == -1: self.close(data=d) self.sell(data=d, size=1000)
Suppose that we are not in position and first signal of the crossover is a long signal, ok we go long.
Than I don’ t understand this:
If the next bar is stil in crossover = 1 ( fast ma is above) what stops the self.close() command to close the the long submitted in the previous bar while open another non sense long in the same time/bar?
What am I missing here? :/thx, regards
-
@jas2210 I can't see the indicator but I imagine it's a
crossover
indicator, meaning it only equals one or minus one when the signal crosses over, not the whole time it is over. -
@run-out ahhh ok that's what I missed..
And what value does crosover indicatore take from the moment when cross in one direction to the moment of crossing in the opposite direction? 0 ? or None? -
-
@run-out right thanks, it seems unknown the value between
-
@jas2210
The value will be 0 when there is no cross.
From the docs:
"Formula:- diff = data - data1
- upcross = last_non_zero_diff < 0 and data0(0) > data1(0)
- downcross = last_non_zero_diff > 0 and data0(0) < data1(0)
- crossover = upcross - downcross"
So when upcross = False (0) and downcross = False (0), then crossover = 0 - 0.