volume versus average

Post Reply
Dewey McG
Trader
Posts: 435
Joined: Sat Nov 26, 2011 4:20 pm
Location: Tampa FL

volume versus average

Post by Dewey McG »

My coding skills don't even qualify for beginner level so please forgive any stupid questions about a task that anyone else could do easily.

All I want to do is add a condition to an EA that the volume must be above the session average volume before taking a signal. The only volume indicator I found that has this is the Better Volume with Session Averages. The only problem is there are several different types of volume bars. I was able to make it work but the EA runs incredibly slow. Is there a more efficient way to do this?

Here is the code I came up with and the indicator I used below:

And (((CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1] > CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1])
Or ((CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1] > CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1])
Or ((CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1] > CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1])
Or ((CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1] > CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1])
Or ((CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1] > CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1])
Or (CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1] > CustomIndicator( "BetterVolume 1.4 plus Session Average" , 500 , "NumberOfBars must be greater than MAPeriod" , 20 , 40 , True , 17 , 5 , 3 , 12 , 8 , 17 )[1]))))))
You do not have the required permissions to view the files attached to this post.
Lowphat
Trader
Posts: 121
Joined: Fri Jun 27, 2014 12:20 am

volume versus average

Post by Lowphat »

you could prolly just call the built in vol and then put a ma array on it. ill post some code when i get time unless someone beats me to it
Lowphat
Trader
Posts: 121
Joined: Fri Jun 27, 2014 12:20 am

volume versus average

Post by Lowphat »

hmm going try to embed the session into the EA so we don't have to use the indicator at all
could limit the check to every few mins instead of tick also.
you will prolly have to tweak this abit but its a start

add externs to the EA

Code: Select all

extern int     NumberOfBars = 200;
extern string  Note  = "NumberOfBars must be greater than MAPeriod";
extern int     MAPeriod   = 20;
extern int     AsiaBegin  = 17;   // Start time of Asian session in brokers time 
extern int     AsiaEnd    = 5;   // End time of Asian session in brokers time
extern int     EurBegin   = 3;   // Start time of European session in brokers time
extern int     EurEnd     = 12;   // End time of European session in brokers time
extern int     USABegin   = 8;   // Start time of New York session in brokers time
extern int     USAEnd     = 17;   // Endtime of New York session in brokers time
checking if vol is above session av

Code: Select all

   if (VolIsAboveAverage()) Comment ("Vol Is AboveAverage");//change comments to logic u need:P
   if (!VolIsAboveAverage())Comment ("Vol Is BelowAverage");


functions to add outside of start/tick loop

Code: Select all

bool VolIsAboveAverage()
{
  double v4[500];
   for(int i=0; i<499; i++) 
   {
      v4[i] = NormalizeDouble(CalculateSessionAverageVolume(i),0);
   }
  int vol=iVolume(NULL,PERIOD_CURRENT,0);
  if (vol>=v4[0]) return(true);
  if (vol< v4[0]) return(false);
}

Code: Select all

double CalculateSessionAverageVolume(int bar)
{
   double av, v = 0;
   int i, c = 0;
   i = bar;
   while (c<MAPeriod)
   {
      if ( Session(iTime(NULL,Period(),i)) == Session(iTime(NULL,Period(),bar)) )
      {
         v = iVolume(NULL,Period(),i);
         av = av+v;
         c++;
      }
      i++;
      if (i>(2*NumberOfBars)) { break;}
   }   
   return(av/MAPeriod);
}

Code: Select all

int Session(datetime dt )
{
   int session = 0;
   int hour = TimeHour(dt);
   if ( (hour >=AsiaBegin)  || ( (AsiaBegin>AsiaEnd) && (hour <= AsiaEnd)) )
   { 
      session = 1;
   }
   if ( (hour >=EurBegin) || ( (EurBegin>EurEnd) && ( hour <= EurEnd)) )
   {
      session = 2;
   }
   if ( (hour>=USABegin && hour>EurEnd)  || ( (USABegin>USAEnd) && (hour <= USAEnd)) )
   { 
      session = 3;
   }
   if (session==0) { Comment("Error - CurentSession()"); }
   return (session);
Dewey McG
Trader
Posts: 435
Joined: Sat Nov 26, 2011 4:20 pm
Location: Tampa FL

volume versus average

Post by Dewey McG »

Thanks. I will try to embed this as you suggested and see if I can make it work.
Post Reply

Return to “Coders Hangout”