Questions about EMA
-
Hello everyone, i am new to BT, and trying to use EMA for backtesting,
i am going to use EMA(period=60)(green line in the plot).My question :
I want to start the backtest at 2012-01-1 (The starting point of green line is dated on 2012-01-01,), however, i have setup some calculations about the year high and year low in next(self), but it seems that the calculations is started from the beginning from the data that i input(2011-10-01).Is there anyway i can do so that the calculations on next will be starting from 2012-01-01 ? i have tried to change the data input starting from 2012-01-01, however its not enough preload data for ema(60) to generate.
-
@Anthony-Chow said in Questions about EMA:
Is there anyway i can do so that the calculations on next will be starting from 2012-01-01 ?
Use datetimefrom datetime import date
Place a conditional statement at the beginning of next.
def next(self): if self.datas[0].datetime.date() < date(2012, 1, 1): return
-
@run-out Thanks for the reply, i tried and there are still some problems that,
print(self.data.num2date(), Dayhigh, Daylow)
by printing this, it show the following, it seems that the calculations(Dayhigh) still focusing on the data before date(2012, 1, 1) and what i want is the Green One on the below drawing which is calculated starting from (2012, 1, 1) and the (Dayhigh)value should be 195.
Really thanks for the help!!! -
Your dayhigh and daylow code is still grabing the max for len(self) with is the whole length of the data. You need to capture the length of self once the date condition is met. Add in a new variable in the if statment that will capture the len when the date condition is met, then subtract this from lenght of self.
I would have made this for you but you are using code in images which prevents from copying and pasting. See instructions at the top of the page regarding how to add code to your quetsions. Thanks
-
@run-out Thank you sir, really appreciated
def __init__(self): self.dataclose = self.datas[0].close self.order = None self.buyprice = None self.buycomm = None self.datahigh = self.datas[0].high self.datalow = self.datas[0].low self.datatime = self.datas[0].datetime
def next(self): if self.datas[0].datetime.date() < date(2012, 1, 4): return #Find Range Dayhigh = max(self.datahigh.get(size=(len(self)),ago=0)) Daylow = min(self.datalow.get(size=(len(self)),ago=0)) Range = (Dayhigh - Daylow) print(self.data.num2date(), Dayhigh, Daylow)
-
I've made a few changes to your code. Thanks for posting like that. In my code below, startdate would more likely be an input parameter but I left it like this for the illustration. You may wish to add an exception if startlength is None after the start date, just in case.
from datetime import date def __init__(self): self.start_length = None # Your other stuff here. def next(self): startdate = date(2012, 1, 4) # This would probably actually be an input parameter. date = self.datas[0].datetime.date() if date < startdate: return elif date == startdate: self.start_length = len(self) else: if self.start_length is None: raise ValueError("Error: start_length not properly set.") # Find Range num_bars = len(self) - start_length Dayhigh = max(self.datahigh.get(size=num_bars, ago=0)) Daylow = min(self.datalow.get(size=num_bars, ago=0)) Range = (Dayhigh - Daylow) print(date, Dayhigh, Daylow, Range)
-
@run-out Thank you SIR, Its work=]
-
@run-out Sorry to keep bothering you, i am really a newbie.
There is another problem for the first Bar(num_bars = 0), max cannot be found
so i tried to fix it like this, but i failed, the first bar (high and low cannot be added to the Dayhigh and Daylow.
num_bars = len(self) - self.start_length if num_bars == 0 : Dayhigh = self.datahigh[0] Daylow = self.datalow[0] else: Dayhigh = max(self.datahigh.get(size=num_bars, ago=0)) Daylow = min(self.datalow.get(size=num_bars, ago=0)) Range = (Dayhigh - Daylow) print(date, Dayhigh, Daylow, Range)
It should be 192.77
-
@Anthony-Chow said in Questions about EMA:
Dayhigh = max(self.datahigh.get(size=num_bars, ago=0))
Try using
bt.Max()
as discussed here. This will evaluate the max at each bar in the line object.