Sorry. I missed this commit :)
But it works !
@backtrader back on this topic, are trades specific to one instrument, or shared ?
I use tradeid to differentiate the trades on each weekly product.
In this case, when data1 compensate data0:
here, on 08/08/2016, I would expect that the trade with id 1 is closed. But it's not.
Would this be possible ?
I use plotmaster option to plot the two streams on the same plot:
self.data1.plotinfo.plotmaster = self.data0
I expect the two lines (red and black) to share the same y axis. but I'm not sure it's the case here.
Any idea ?
Does one of the y axis concern the buy and sell tickers ?
I think that with your "compensate" method, I'm almost able to do my backtest.
One more thing:
when data0's position is 5 and I sell 5 on data1, , the position of data0 is set to 0 thanks to you're new method.
But the trade is not closed, right ?
One additional question given that
data0expires (the position disappears), but
data1is a spot price
- What happens with the
-5trade in the real world on
the -5 on data1 are sold on the da ahead spot exchange. It's sold to counterparts of the exchange who need to rebalance their short physical position.
With that table at hand your desired use case seems clearer.
But you need to specifically address both
data1. There is nothing which ties both data feeds together, i.e.: an action on one of them
sellhas no actual effect on the other.
The platform has no way to know that your wish is that by opening a short position on
data1at the end of Week_N which has the implication that at the start of the Week_N+1 positions on both
data1have to be closed.
Let's imagine that somehow a link can be established between
data1. Because you are using
-5it seems natural to assume that
5 + (-5) = 0. But questions on the implementation of the logic quickly arise
Are positions on
data1simply added? What happens if you buy
data1, should the system then end up with
You wish that on the next trading day, but ... Is it always the case? Does it have to be on week boundaries?
Not always week boundaries. In this case I consider weekly power futures, but I could also deal monthly of quarterly futures for example
The strange thing about the use case is (there are for sure good reasons for it)
Why opening a short position on
data1at the end of week with the expectation to have no positions open on the next trading day?
It seems easier to simply close the open position on
data0 = weekly power delivery future. When I buy 5 on data0 in W1 it means that, If I don't sell it, I will have a physical delivery of the power on W2.
This I don't want because I'm not an energy provider. I want to avoid the physical delivery.
If the 5 are not sold before the delivery week (W2), I will sell the energy on the Spot Day Ahead exchange every day.
data1 represent the average spot dahead price of the week W2.
This is why, in this case, I buy 5 on data0 and sell 5 on data1.
In the end, I really have a net position of 0 because I sold the energy I bought.
Ok. When I do this, here is what happens with the positions:
At the end of the 05/08/2016, the position in my example is +5 on data0 and -5 on data1.
What I want to do is net the position for the next trading day:
on 08/08/2016: Position 0 on data0 and 0 on data1
Can I do something like this?
Here is a sample where I tried to integrate all what we said before:
def islastdayofweek(self, data): return data.datetime.date(0).isocalendar() != data.datetime.date(1).isocalendar() def next(self): islastdayofweek = self.islastdayofweek(self.data0) p = self.getposition().size if (self.signal < 0.0): s = self.getsizing(isbuy=True) if (p + s <= self.params.limit): self.log('BUY CREATE , %.2f, %.2f MWh' % (self.data0.close, s)) self.buy(size=s) p = p + s else: self.log('BUY LIMIT EXCEEDED. ORDER CANCELLED.') elif (self.signal > 0.0): s = self.getsizing(isbuy=False) if (p - s >= 0): self.log('SELL CREATE , %.2f, %.2f MWh' % (self.data0.close, s)) self.sell(size=s) p = p - s if islastdayofweek and p > 0: self.sell(self.data1, size=p) self.log('SELL SETTLEMENT, %.2f, %.2f MWh' % (self.data1.close, p)) # Here, is my position equal to 0 after execution of the order? Or short on data1 and long on data0 ?
That's true. I need to do it on the last day of the week.
And the set_coc will help me for sure.
But what about selling on another instrument (data1) while i bought on data0 ?
Date Close Week id
01/08/2016 27.09 W1 <-- buy 1 @27.09
02/08/2016 27.46 W1
03/08/2016 27.5 W1
04/08/2016 27.04 W1
05/08/2016 26.94 W1 <-- at the end of the week, no oportunity identified by my indicator => so automaticly sell 1 @ data1.close (and not data0.close=26.94)