Slicing fails upon inputting signal generation logic
-
Good Day,
I have the following code for my strategy:
class Strat(bt.Strategy): params = (("WindowSize", 5), ("Dummy1", 1), ("Dummy2", 2)) def __init__(self): self.DataFeed1 = self.datas[0] self.DataFeed2 = self.datas[1] def log(self, Input): print("Date: {0}, Text: {1}".format(self.DataFeed1.datetime.date(0), Input)) def next(self): #Method that is called on every bar Slice1 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize)) Slice2 = np.array(self.DataFeed2.close.get(ago = 0, size = self.params.WindowSize)) self.log(Slice1) self.log(Slice2)
The code above is able to successfully slice the current value and the previous 4 values resulting in a numpy array of size 5:
Date: 2020-11-23, Text: [0.73049 0.73051 0.72977 0.72978 0.72948] Date: 2020-11-23, Text: [0.69375 0.69385 0.69314 0.69327 0.69295]
However when I change the code to this:
class Strat(bt.Strategy): params = (("WindowSize", 5), ("Dummy1", 1), ("Dummy2", 2)) def __init__(self): self.DataFeed1 = self.datas[0] self.DataFeed2 = self.datas[1] def log(self, Input): print("Date: {0}, Text: {1}".format(self.DataFeed1.datetime.date(0), Input)) def next(self): #Method that is called on every bar Slice1 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize)) Slice2 = np.array(self.DataFeed2.close.get(ago = 0, size = self.params.WindowSize)) self.log(Slice1) self.log(Slice2) Logic(Slice1, Slice2)
The slicing fails and I get this:
Date: 2013-11-25, Text: [] Date: 2013-11-25, Text: []
Is this behaviour intended and is anyone else having the same issue?
-
Could you clarify what you are talking about?
-
The difference between the first strategy class and the second strategy class is the presence of the
Logic(Slice1, Slice2)
function.The
Logic(Slice1, Slice2)
function contains the signal generation logic for my strategy and it takes inSlice1
andSlice2
as arguments.Each of the slices contains close prices in the following format
[t-5, t-4, t-3, t-2, t-1, t]
wheret
indicates the current timestamp.Without the
Logic(Slice1, Slice2)
function in thenext()
method of the first strategy class, bothSlice1
andSlice2
are able to be converted into numpy arrays with the previously mentioned format on every call of thenext()
method. However, including theLogic(Slice1, Slice2)
in thenext()
method of the second strategy class magically results in bothSlice1
andSlice2
becoming empty numpy arrays on every call of thenext()
method. -
@Ron-Aw said in Slicing fails upon inputting signal generation logic:
However, including the Logic(Slice1, Slice2) in the next() method of the second strategy class magically results in both Slice1 and Slice2 becoming empty numpy arrays on every call of the next() method.
I think the 'magical' problem is contained in, or caused by,
Logic(Slikce1, Slice2)
, which like all good magic tricks, can't be solved when you cannot see what's going on in the trick. OK, I'm just kidding.You need to debug your code and we can't help you with this since a custom code is causing your problem and we don't know what your custom code is doing.
Try creating more log outputs or debug your code in your ide.
-
I don't think that it's
Logic(Slice1, Slice2)
causing the issue because the error I get when running the second strategy class isTypeError: expected non-empty vector for x
. It appears that the two lines:Slice1 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize)) Slice2 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize))
are returning empty numpy arrays whenever
Logic(Slice1, Slice2)
is included in thenext()
method. Hence, theTypeError
thatLogic(Slice1, Slice2)
is raising is correct becauseLogic(Slice1, Slice2)
has no values to work with. The print output below also confirms this.Date: 2013-11-25, Text: [] Date: 2013-11-25, Text: []
In the
next()
method of the second strategy class, I've tried replacingLogic(Slice1, Slice2)
withCalculation = np.polyfit(Slice1, Slice2, 1)[0]
and again, I getTypeError: expected non-empty vector for x
. This means that the previously mentioned 2 lines are magically producing empty numpy arrays whenever something else is included in thenext()
method. -
@Ron-Aw said in Slicing fails upon inputting signal generation logic:
alculation = np.polyfit(Slice1, Slice2, 1)[0]
When you first run the program,
@Ron-Aw said in Slicing fails upon inputting signal generation logic:
Slice1 = np.array(self.DataFeed1.close.get(ago = 0, size = self.params.WindowSize))
Slice2 = np.array(self.DataFeed2.close.get(ago = 0, size = self.params.WindowSize))next() needs to run
("WindowSize", 5),
bars before you get any values. YourLogic
is trying to make calculations before the 5 bars are loaded, so you are making calculations on empty arrays.
Simply check the length of the Slice1 and/or Slice2 before calling your Logic function.if len(Slice1) > 0 and len(Slice2) > 0: calculation = np.polyfit(Slice1, Slice2, 1)[0] self.log(calculation)
Results in output:
Date: 2019-11-04: Slice1: [] Date: 2019-11-04: Slice2: [] Date: 2019-11-05: Slice1: [] Date: 2019-11-05: Slice2: [] Date: 2019-11-06: Slice1: [] Date: 2019-11-06: Slice2: [] Date: 2019-11-07: Slice1: [] Date: 2019-11-07: Slice2: [] Date: 2019-11-08: Slice1: [63.12 63.03 63.05 63.78 63.95] Date: 2019-11-08: Slice2: [194.72 194.32 191.55 190.42 190.84] Date: 2019-11-08: Calculation: -3.41 Date: 2019-11-11: Slice1: [63.03 63.05 63.78 63.95 64.46] Date: 2019-11-11: Slice2: [194.32 191.55 190.42 190.84 189.61] Date: 2019-11-11: Calculation: -2.42 Date: 2019-11-12: Slice1: [63.05 63.78 63.95 64.46 64.4 ] Date: 2019-11-12: Slice2: [191.55 190.42 190.84 189.61 194.47] Date: 2019-11-12: Calculation: 0.38
-
Great, this makes so much sense now! Thank you so much for your help!