VWAP Based EA?

 

Hello all.

With VWAP reversion mentioned in more and more financial blogs/publications I was curious if anyone has made/knows of an EA that trades based off of a VWAP indicator.

I did a search and came up with very little.

I know that the guys at Dailyfx made one for marketscope.. It paints 3 StDevs of bands. Can this be adapted for MT4?

-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
-- TODO: Add minimal and maximal value of numeric parameters and default color of the streams
function Init()
    indicator:name("Volume Weighted Average Price (VWAP)");
    indicator:description("Volume Weighted Average Price (VWAP)");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);
   indicator:setTag("group", "Volume Indicators");
   
   indicator.parameters:addGroup("Band Parameters");
   indicator.parameters:addBoolean("BAND", "Show Bands", "" , true);   
   indicator.parameters:addDouble("ONE", "First Multiplier", "" , 1);
   indicator.parameters:addDouble("TWO", "Second Multiplier", "" , 2);
   indicator.parameters:addDouble("THREE", "Third Multiplier", "" , 3);
   indicator.parameters:addGroup("VWAP Parameters");
   indicator.parameters:addString("Type", "CLOSE", "", "C");
    indicator.parameters:addStringAlternative("Type", "OPEN", "", "O");
    indicator.parameters:addStringAlternative("Type", "HIGH", "", "H");
    indicator.parameters:addStringAlternative("Type", "LOW", "", "L");
    indicator.parameters:addStringAlternative("Type","CLOSE", "", "C");
    indicator.parameters:addStringAlternative("Type", "MEDIAN", "", "M");
    indicator.parameters:addStringAlternative("Type", "TYPICAL", "", "T");
    indicator.parameters:addStringAlternative("Type", "WEIGHTED", "", "W");
   
   
    indicator.parameters:addInteger("width","Width", "", 1, 1, 5);
    indicator.parameters:addInteger("style", "Style", "", core.LINE_SOLID);
    indicator.parameters:setFlag("style", core.FLAG_LINE_STYLE);
    indicator.parameters:addColor("VAMA_color", "Color of VWAP Line", "", core.rgb(0, 0, 255));
   
   indicator.parameters:addGroup("First band Line Style");
   indicator.parameters:addInteger("widthONE","Width", "", 1, 1, 5);
    indicator.parameters:addInteger("styleONE", "Style", "", core.LINE_SOLID);
    indicator.parameters:setFlag("styleONE", core.FLAG_LINE_STYLE);
    indicator.parameters:addColor("ONE_color", "Band Color", "", core.rgb(255, 0, 0));
   
   indicator.parameters:addGroup("Second band Line Style");
   indicator.parameters:addInteger("widthTWO","Width", "", 1, 1, 5);
    indicator.parameters:addInteger("styleTWO", "Style", "", core.LINE_SOLID);
    indicator.parameters:setFlag("styleTWO", core.FLAG_LINE_STYLE);
    indicator.parameters:addColor("TWO_color", "Band Color", "", core.rgb(0, 255, 0));
   
   indicator.parameters:addGroup("Third band Line Style");
   indicator.parameters:addInteger("widthTHREE","Width", "", 1, 1, 5);
    indicator.parameters:addInteger("styleTHREE", "Style", "", core.LINE_SOLID);
    indicator.parameters:setFlag("styleTHREE", core.FLAG_LINE_STYLE);
    indicator.parameters:addColor("THREE_color", "Band Color", "", core.rgb(255, 0, 0));
   
end

-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- TODO: Refine the first period calculation for each of the output streams.
-- TODO: Calculate all constants, create instances all subsequent indicators and load all required libraries

local first;
local source = nil;
local PriceTimesVolume;
local VAMA = nil;
local host;
local offset;
local weekoffset;
local s=nil
local e=nil;
local START=nil;
local Type;
local p;
local RAW;
local DEV;
local UP={};
local DOWN={};
local BAND;
local ONE;
local TWO;
local THREE;

function Prepare()
   ONE=instance.parameters.ONE;
   TWO=instance.parameters.TWO;
    THREE=instance.parameters.THREE
   Type=instance.parameters.Type;
   BAND=instance.parameters.BAND;
    source = instance.source;
    first = source:first();
   
    if Type == "O" then
        p = source.open;
    elseif Type == "H" then
        p = source.high;
    elseif Type == "L" then
        p = source.low;
    elseif Type == "M" then
        p = source.median;
    elseif Type == "T" then
        p = source.typical;
    elseif Type == "W" then
        p = source.weighted;
    else
        p = source.close;
    end
   
   host = core.host;
   offset = host:execute("getTradingDayOffset");
    weekoffset = host:execute("getTradingWeekOffset");
   
   PriceTimesVolume = instance:addInternalStream(first,0);   

    local name = profile:id() .. "(" .. source:name() .. ")";
    instance:name(name);
    VAMA = instance:addStream("VAMA", core.Line, name, "VAMA", instance.parameters.VAMA_color, first);
   VAMA:setWidth(instance.parameters.width);
    VAMA:setStyle(instance.parameters.style);
   
   if BAND then
   
   RAW=instance:addInternalStream (first, 0)
   UP[1] = instance:addStream("UP"..1, core.Line, name, "UP 1", instance.parameters.ONE_color, first);
   UP[1]:setWidth(instance.parameters.widthONE);
    UP[1]:setStyle(instance.parameters.styleONE);
   DOWN[1] = instance:addStream("DOWN"..1, core.Line, name, "DOWN 1", instance.parameters.ONE_color, first);
   DOWN[1]:setWidth(instance.parameters.widthONE);
    DOWN[1]:setStyle(instance.parameters.styleONE);
   UP[2] = instance:addStream("UP"..2, core.Line, name, "UP 2", instance.parameters.TWO_color, first);
   UP[2]:setWidth(instance.parameters.widthTWO);
    UP[2]:setStyle(instance.parameters.styleTWO);
   DOWN[2] = instance:addStream("DOWN"..2, core.Line, name, "DOWN 2", instance.parameters.TWO_color, first);
   DOWN[2]:setWidth(instance.parameters.widthTWO);
    DOWN[2]:setStyle(instance.parameters.styleTWO);
   UP[3] = instance:addStream("UP"..3, core.Line, name, "UP 3", instance.parameters.THREE_color, first);
   UP[3]:setWidth(instance.parameters.widthTHREE);
    UP[3]:setStyle(instance.parameters.styleTHREE);
   DOWN[3] = instance:addStream("DOWN"..3, core.Line, name, "DOWN 3", instance.parameters.THREE_color, first);
   DOWN[3]:setWidth(instance.parameters.widthTHREE);
    DOWN[3]:setStyle(instance.parameters.styleTHREE);
   end

end

-- Indicator calculation routine
-- TODO: Add your code for calculation output values
function Update(period)
    if period >= first and source:hasData(period) then
   
             if core.getcandle("D1", source:date(period),0, 0) ~= s then
             s, e = core.getcandle("D1", source:date(period),0, 0);
             START = findDateFast(source, s, false);    
             end
    
        PriceTimesVolume[period]=p[period] *source.volume[period];
      
       if START ~= -1 then      
            VAMA[period] = core.sum(PriceTimesVolume,core.range(START,period))/core.sum(source.volume,core.range(START, period));   
            
            if BAND then
            RAW[period] = p[period]-VAMA[period];
            DEV=core.stdev (RAW, core.range(START, period));
            UP[1][period]=  VAMA[period] + DEV*ONE;
            DOWN[1][period]=VAMA[period] - DEV*ONE;
            UP[2][period]=  VAMA[period] + DEV*TWO;
            DOWN[2][period]=VAMA[period] - DEV*TWO;
            UP[3][period]=  VAMA[period] + DEV*THREE;
            DOWN[3][period]=VAMA[period] - DEV*THREE;
            end
      end
      
    end
   
end

function findDateFast(stream, date, precise)
    local datesec = nil;
    local periodsec = nil;
    local min, max, mid;

    datesec = math.floor(date * 86400 + 0.5)

    min = 0;
    max = stream:size() - 1;

    while true do
        mid = math.floor((min + max) / 2);
        periodsec = math.floor(stream:date(mid) * 86400 + 0.5);
        if datesec == periodsec then
            return mid;
        elseif datesec > periodsec then
            min = mid + 1;
        else
            max = mid - 1;
        end
        if min > max then
            if precise then
                return -1;
            else
                return min - 1;
            end
        end
    end
end

Thanks

Russ

 

You're asking someone to make an EA for free, right?

Do you think it's your responsibility to introduce how trades based off that indicator may make good profit in the first place?

 

Nick(y),

I'm not asking anyone to do anything. I'm asking if anyone has seen or has made an indicator like that in the past.... what anyone decides to do after that is up to them.

From my understanding this is a community of people all striving for the same thing. As soon as I have the wherewithal to contribute my own indicators experts and advice i will do so but until then I am merely concerned with accumulating as much knowledge as possible. And rather than just post a 2 line question I decided to provide the only code that i know of pertaining to this indicator. Is it profitable? Who knows that's pretty difficult to determine if nothing like that exists on MT4... If, however, you graph it against etfs and indices, it's pretty clear someone or something is looking at it.

Russ

 

This is what it looks like on Marketscope btw... for what its worth.

 
russellb:


I know that the guys at Dailyfx made one for marketscope.. It paints 3 StDevs of bands. Can this be adapted for MT4?

Probably, there are plenty of people that will try and code it for you, all you need to do is ask in the right place: MT4 & MT5 coding by request
 
Will do thanks Raptor.
 

how can I use it in MT4??? I mean,, may I past it to into expert/indicator?? or does it work

Reason: