How to discard the data of previous day and and start fresh from new day

hello, i want to calculate moving average on 5 minute bar but i do not want to use previous day data ,,i i want my moving average to start from new day

@abhirajdhamija I don't day trade so this is just a guess. An example of a 5 bar moving average that will start it's calculation every new day on the 5th bar of the day.

So no moving average is calculated until you have enough data to use 5 bars (which of course you could parameterize.

newDay = dn != Ref( dn, -1 ); // first bar of new day
TodayMA5 = IIf( BarsSince( newDay ) >= 4, MA( C, 5 ), null );
// remember 5th bar of day is 4 bars after the first bar of the day


Do you guys realize that starting MA calculation from new day and calculating MA entirely from first visible bar or from first bar of array do plot the same MA? The only difference is that MA calculated from new day starts n-bars later.

So It is quite pointless starting at new day. What for? What is the benefit?

You just add unnecessary calculations coming to same end result.

Red MA -> standard MA
Orange MA -> Calculated since new day

Result: same values, just one starting later.
What is the benefit?



did you visit "Resetting Indicators" link ?


Maybe something like this:

newDay = dn != Ref( dn, -1 ); // first bar of new day
TodayMA = MA( C, BarsSince( newDay ) + 1 );
1 Like

@bigalgator no your solution will keep increasing the "look back" period by one more bar, so instead of a 5 bar moving average it becomes a 6,7,8,9 etc bar moving average.

If using 5 minute bars, you end up with a much different moving average later in the day than possibly intended.

Here is an example Exploration to debug your code,

dn = Day();
newDay = dn != Ref( dn, -1 ); // first bar of new day
TodayMA5 = IIf( BarsSince( newDay ) >= 4, MA( C, 5 ), null );
// remember 5th bar of day is 4 bars after the first bar of the day

TodayMA = MA( C, BarsSince( newDay ) + 1 );
bigAlLookbackPeriod = BarsSince( newDay ) + 1;
//  Explore

Filter = 1;
FirstBarColor = IIf( newDay, colorLightBlue, colorDefault );
AddColumn( C, "Close" );
AddColumn( newDay, "newDay", 1.0, colorDefault, FirstBarColor );
AddColumn( BarsSince( newDay ), "BarsSince(newDay)", 1.0 );
AddColumn( TodayMA5, "TodayMA5" );
AddColumn( bigAlLookbackPeriod, "big al LB", 1.0 );
AddColumn( TodayMA, "bigal MA" );


@fxshrat Thanks, but although the original poster was not specific with regards to how he wants to manage the first few bars of the new day (i.e. not enough bars to collect a 5 bar moving average for example). What I don't understand from your example is how there is a continuous line in the Plot for the next day? If you don't have 5 bars yet (using the MA(C, 5) as an example), what is the Plot charting for those first 4 bars as a 5-bar moving average?

1 Like


I understand your point and thanks for detailing the different approaches. The original post was not clear whether we are looking for a 5 bar average of 1-minute bars, or an average of 5-minute bars over a given period.

My approach is similar to a VWAP calculation that resets each day. More bars are included as the day progresses.

1 Like

@bigalgator ah! That's kind of clever and had not occurred to me. BTW I was only using "5" bar look back as an example, and yes I was using 5 minute bars too.

Again, it is the same MA just one starting n-bars later.

Plot( C, "Price", colorDefault, styleCandle );

period = Param( "period", 5, 1, 300, 1 );

dn = DateNum();
newDay = dn != Ref( dn, -1 ); // first bar of new day
TodayMA5 = IIf( BarsSince( newDay ) >= period-1, MA( C, period ), null );
Plot( TodayMA5, "MA1", colorOrange );

Plot( MA(C, period), "MA2", colorRed );

Note that this might be shaving the system too finely.
Do all trades report on an absolutely correct time stamp sequence.

Did trade X actually happen before trade X+1?

For example, a crossed trade happens inside the "back office". This might be at a broker or even an operation like BATS which services Introducing Brokers who have no back office. Could lags in these systems can scramble the actual moment of trades?

true !! but I wanted to use it for implied volatility chart because in case of implied volatility yesterdays volatility does not matter

thanks @portfoliobuilder.. really appreciate it as i wanted to use it for implied volatility charts

@abhirajdhamija Could you please share the code for plotting Implied Volatility below the option symbol ?

you have to put the token of implied volatility from where you take your live feed.
that is done backhand in my office so do not have much idea about how to take live feed
but you can plot it simply by lets say you want to have coming month nifty expiry option on 11000 strike of july 2018 expiry
we can plot it by
nifty1100018julceiv for call option 11000 strike 2018 july expiry
nifty1100018julpeiv for put option 11000 strike 2018 july expiry

Thank you @abhirajdhamija . Which datafeed you are using for IV ? Mine is Truedata and they don't provide

I'm trying to test an intraday strategy of Moving Average crossover.
As soon as my 5 periods moving average crosses the 13 periods moving average, I want to store the High of the candle in a variable. My buy signal will get triggered if my closing is above the said variable.
But when the day ends I want to reset that variable's value to 0 so that the next day the process starts afresh
I do not want to use the previous day's variable the next day instead I want to wait for a crossover again and use the new high as my signal.
Can someone please help me code the same
Here is my code, the problem I'm facing is that my variable is being carried over the next day as well which I do not want

MAH 		= MA(H,13);
MAL 		= MA(L,13);
MA1			= MA(C,5);

H1			= ValueWhen(Ref(ma1,-1)<Ref(mah,-1) AND ma1>mah AND C>MA1, H);
Buy			= C > H1;
Sell		= C < MAL OR TimeNum()>=141500;

N 			= 1;
dn 			= DateNum();
newDay 		= dn != Ref( dn, -1 );

Buylimit 	= sum( Buy, BarsSince( newDay ) + 1 );

Buy 		= Buy AND  buylimit<= N;

Buy			= ExRem(Buy,Sell);       
Sell		= ExRem(Sell,Buy); 


As per my condition, I want to mark the high (yellow line) when the white line crosses the green line from below and buy when my closing is above that high (the yellow line)
I do not want the second trade to be initiated because the high (yellow line) is of the previous day. I want to start the process of marking the yellow line from scratch on the newday. How can I do that?


Thread closed as it attracts people without license