How to scan for stocks having current day volume greater than average of past few days?

I'm trying to write code which can return some results if the total volume of current day in percent is greater than average volume of past 5 days.

Is it correct to get the average volume of past 5 days?

AverageVolume = MA(Volume, 5);

Let's say the average volume of past 5 days is 10000 then what I want is if at any moment, the volume of current trading day exceeds the average, then it should give me an alert.

Now I want the total volume of the current day for something like this to work:

if(TodayTotalVolume > AverageVolume) {
   // Do something here
}

How to get total value of current trading day in real time so that the alert gets generated the moment the condition is satisfied?

You need to read this part of manual first: http://www.amibroker.com/guide/h_exploration.html

AverageVolume = MA(Volume, 5);

Filter = Volume > AverageVolume;
AddColumn( Volume, "Volume", 1.0 );
AddColumn( AverageVolume, "AvgVolume", 1.0 );

Yeah I had read that manual before and know how to add custom columns.

In the code you gave, you have used Volume but won't that give me the volume of the most recent candle formed? If I select daily timeframe, then it will give me volume only after the trading session is over, if I select 15 minute candle then it will only give me volume of the most recent 15 minute candle?

I'm looking for the total traded volume of running trading session at any given point of time when the market is open.

I did look at this article about getting real time values: https://www.amibroker.com/guide/afl/getrtdata.html

But the data feed I have is not a plugin, it's a software which feeds data into Amibroker so I checked the realtime quote but that was empty. So I can't use the variables given on the above link.

Is there any alternative way to get the total traded volume since market got open in real time?

AverageVolume = MA(Volume, 5);
Filter = Volume > Ref(AverageVolume,-1);
AddColumn( Volume, "Volume", 1.0 );
AddColumn( AverageVolume, "AvgVolume", 1.0 );

@sarupria to get a good answer you need to learn how to ask a good question. Be specific about your needs. In your first question you asked

This will obviously be interpreted by users as Daily bar data that you are using.

But in your later post you go on with

So now you are looking at intraday data. Because your question is so vague and poorly worded you have not received the answer you are looking for.

Perhaps this,

 Bar_today = BarsSince( Day() != Ref( Day(), -1 ) ) + 1;
TodayVolume = Sum( V, Bar_today );

TimeFrameSet( inDaily ); // switch to Daily minute frame
/* MA now operates on Daily data, ma5 holds time-compressed 5 bar MA of Daily bars */
ma5 = MA( V, 5 );
TimeFrameRestore(); // restore time frame to original

FiveDayAve = TimeFrameExpand( ma5, inDaily );

/////////////////////
// Explore
/////////////////////
Filter = 1;
dynamic_colour = IIf( TodayVolume > FiveDayAve, colorGreen, colorDefault );
AddColumn( Bar_today, "Bar Today", 1.0 );
AddColumn( V, "Volume this bar", 1.0 );
AddColumn( TodayVolume, "Cum Volume Today", 1.0, colorDefault, dynamic_colour );
AddColumn( FiveDayAve, "FiveDayAve", 1.0 );

/////////////////////
// Chart
/////////////////////
Plot( TodayVolume , "Today Cum Vol", colorRed, styleline );
Plot( FiveDayAve, "FiveDayAve", colorbrightgreen, styleDashed );

Producing this Exploration for debugging

image

2 Likes

@portfoliobuilder is spot on.

@sarupria you need to learn how to ask questions AND thank for responses. Your response is unceremonious and does not encourage anyone to help you any further.

2 Likes

Thanks. Your answer is exactly what I wanted!

Not sure why thought my reply was "unceremonious", it's a normal conversation but if saying thanks is your criteria to provide help then I'll make sure to say thanks whenever I'm in touch with you. Thanks!

It is sad, that there are users who treat saying "thank you" for the help they were offered, as something strange and unnecessary. Very sad ...

1 Like

Here is version not being dependent on timeframe functions.
It is around three times faster on 100k bars (according to code check and profile).
Results are the same ones (see picture) (while in below version results are also inline between EOD and intraday).

/// @link http://forum.amibroker.com/t/how-to-scan-for-stocks-having-current-day-volume-greater-than-average-of-past-few-days/4612/11
/// code not being dependent on time frame functions
/// instead using sparse functions
/// alternative calculation by fxshrat
dn = DateNum();
newday = dn != Ref(dn , -1);

if( Interval() < inDaily ) { // intraday
	SetBarsRequired( 1500 );
	TodayVolume = SumSince(newday, V) + ValueWhen(newday, V);
	Vcomp = SparseCompress(newday, Ref(TodayVolume, -1));
	// Avg. of previous past days
	FiveDayAvg = ValueWhen(newday, SparseExpand(newday, MA(Vcomp, period = 5)), 1);
} else {// if EOD
	TodayVolume = V;
	// Avg. of previous past daily bars
	FiveDayAvg = Ref(MA(V, period = 5), -1);
}

/// @link http://forum.amibroker.com/t/how-to-scan-for-stocks-having-current-day-volume-greater-than-average-of-past-few-days/4612/5
/// example output by portfoliobuilder
/////////////////////
// Explore
/////////////////////
Filter = 1;
dynamic_colour = IIf( TodayVolume > FiveDayAvg, colorGreen, colorDefault );
AddColumn( V, "Volume this bar", 1.0 );
AddColumn( TodayVolume, "Cum Volume Today", 1.0, colorDefault, dynamic_colour );
AddColumn( FiveDayAvg, "FiveDayAvg", 1.0 );

/////////////////////
// Chart
/////////////////////
Plot( TodayVolume , "Today Cum Vol", colorRed, styleline );
Plot( FiveDayAvg, "FiveDayAvg", colorbrightgreen, styleDashed );

161844

3 Likes

I think that @portfoliobuilder explained it well enough in his post. Tomasz provided you with ready to use solution (meeting the criteria of your first post), but you didn't say "thank you". Instead you have just changed your criteria and expected another try ...

Tomasz should really appreciate that :wink:

Maybe you don't know this yet (because you are a newcomer - you have joined a week ago) but "He" --> Tomasz is the owner and chief software architect of AmiBroker, so I would be really cautious with such remarks... Tomasz is a little bit more experienced in this field, than you :wink:

in general, that's a pity, that there is no short info (some kind of short statistics) below the profile picture/pictogram of each user, allowing everyone (especially new users) to quickly recognize more experienced users. In my opinion it would be helpful, because most users don't visit other members' profiles.