Since you are trying to come up with some tricks, than you don't use intraday data, am I right?
If this is a case, than check out the data filters, in particular Docs - Daysplitter_Close.
Lets figure out what do you want first. Do you want to execute stop (1) at stop price as soon as price hit stop or (2) at the open of the bar next to the bar where price hit stop?
(1) is already introduced in the
bt with the regular stop orders. Notification about the stop order execution comes at the open of the bar next to the bar where price hit stop. This way I would call
(2) is easy to incorporate: just check price vs stop relation in the
next() and if price crosses/touches stop price, than issue simple market order to close position. It will be executed at the open of the bar next to the bar where price hit stop.
Seems you didn't set time frame and compression for baseline
data. If I remember correctly
bts default time frame is
Daily. So you are re-sampling from days to intraday now.
@juliev it seems you have some issues with the data feeds. First I've ran you script on the daily bars (I have no intraday data) and it worked well with one exception: for order notification you have the following cases only:
order.isbuy() and self.position.size > 0for
order.issell() and self.position.size > 0for
order.isbuy() and self.position.size == 0for
CLOSING SHORT POSITION
order.issell() and self.position.size == 0for
CLOSING LONG POSITION
Second, your order issue statements (BUY CREATE, SELL CREATE) should be printed with te current bar timestamp, but order notification statements sould be printed with the timestamp of the next bar. It seems to me that somehow
next() is called twice in your case. Add the following logging line:
def next(self): # Check if an order is pending ... if yes, we cannot send a 2nd one if self.orderid: return self.log('%s %0.2f - %s %0.2f: pos %d' % (self.data0._name, self.dataclose1, self.data1._name, self.dataclose2, self.position.size)) ...
If it appears only once per timestamp in the output than it is good.
You can create the line with only
self.data.low elements only like this:
self.lows = self.data.low
If you have certain conditions and only part of the new line elements should be equal to
self.data.low elements, then you need to define a placeholder for missing elements.
Also if you want to pass variables between class methods, then these variables should be defined as
self.variable, but not
variable as in the script above.
And other question on this:
if only_low[-1]>only_low[-2]: self.buy() # enter long
Say element [-2] has no
low value (condition didn't meet) and element[-1] has
low value. What do you expect from the script?
@YELNAr you use two different scripts in this post. One of them uses
dataclose[-2] and one don't. Would you be so kind and post the script which causes the problem. But removed from that script all un-necessary parts.
Which means that the problem can be in the configuration of the environment on your laptop.
self.dataX.get(size=self.p.periodXX) results and check what do you pass to
pearsonr function. Maybe it is not what it takes.