windowshopr
Member
I'm trying to calculate the SMA and EMA using a variable length, which you cannot do without getting the "CL constant value" error of course.
What I want to do is calculate the SMA and EMA for the premarket session of each day, so from time 00:00 to 09:30, which can be dynamic each day. After 09:30, the value can just be premarketSMA[1] for the rest of the day.
The code I have so far looks like this:
As you see, I want the "length" value of the moving averages to be "premarketCloseBarNumber - premarketOpenBarNumber", once trading has officially started for the day, basically simulating the average and exponential close prices for the pre-market sessions. Possible?
What I want to do is calculate the SMA and EMA for the premarket session of each day, so from time 00:00 to 09:30, which can be dynamic each day. After 09:30, the value can just be premarketSMA[1] for the rest of the day.
The code I have so far looks like this:
Code:
# Check if we are at midnight/new trading day, pre-market starts
def newDayTrigger = GetDay() <> GetDay()[1];
# Store the midnight/new day's bar number
def premarketOpenBarNumber = if newDayTrigger then BarNumber() else premarketOpenBarNumber[1];
# Wait for 0930
def isRegularTradingSession = SecondsFromTime(0930) >= 0 and SecondsTillTime(1600) > 0;
# Get the premarket close's bar number at 0930
def premarketCloseBarNumber = if isRegularTradingSession and isRegularTradingSession[1]==no then BarNumber() else premarketCloseBarNumber[1];
def premarketTotalBars = premarketCloseBarNumber - premarketOpenBarNumber;
def premarketSMA = if isRegularTradingSession and isRegularTradingSession[1]==no then SimpleMovingAvg(close, premarketTotalBars) else premarketSMA[1]; # Error
def premarketEMA = if isRegularTradingSession and isRegularTradingSession[1]==no then MovAvgExponential(close, premarketTotalBars) else premarketEMA[1]; # Error
As you see, I want the "length" value of the moving averages to be "premarketCloseBarNumber - premarketOpenBarNumber", once trading has officially started for the day, basically simulating the average and exponential close prices for the pre-market sessions. Possible?