|
Detroit MI/USA
Experience: Advanced
Platform: Multicharts, Custom own
Trading: All Futures
Posts: 67 since Sep 2017
Thanks Given: 42
Thanks Received: 207
|
I have been struggling to try and code a mouse click driven anchored VWAP in Multicharts(Easylanguage) but cannot get it right.
I have tried getting various AI platforms to assist but none could get it right. The anchoring on the bar seems to be correct but it is then the plotting of the VWAP from that point forward that is a problem as it only plots on the last bar on the chart.
Any help would be much appreciated.
The code is only for 1 AVWAP but if this works I would like to be able to place as many anchored VWAPs as necessary.
Here is my code:
{ Mouse-Anchored VWAP - Simple Plot Approach }
{ CTRL + Click to set anchor point }
[ProcessMouseEvents = true];
[RecoverDrawings = false];
inputs:
Colour(Yellow);
variables:
AnchorDate(0),
AnchorTime(0),
AnchorSet(false),
VolSum(0),
PVSum(0),
VWAPValue(0),
ThisVol(0),
ThisPV(0);
// -----------------------------
// CTRL + click to set anchor point
// -----------------------------
if MouseClickCtrlPressed then begin
AnchorDate = Date[ (CurrentBar + MaxBarsBack) - MouseClickBarNumber ];
AnchorTime = Time[ (CurrentBar + MaxBarsBack) - MouseClickBarNumber ];
AnchorSet = true;
Print("VWAP anchored to: ", AnchorDate:0:0, " ", AnchorTime:0:0);
end;
// -----------------------------
// VWAP calculation - plot on every bar
// -----------------------------
// Always try to plot something, even if zero
VWAPValue = 0;
if AnchorSet then begin
// Check if current bar is at or after the anchor
if Date > AnchorDate or (Date = AnchorDate and Time >= AnchorTime) then begin
// Recalculate VWAP from anchor to current bar
VolSum = 0;
PVSum = 0;
// Find anchor bar and calculate VWAP
for Value1 = 0 to MaxBarsBack begin
if Date[Value1] = AnchorDate and Time[Value1] = AnchorTime then begin
// Found anchor, now sum from there to current
for Value2 = Value1 downto 0 begin
if Volume[Value2] > 0 then begin
ThisVol = Volume[Value2];
ThisPV = (High[Value2] + Low[Value2] + Close[Value2]) / 3 * ThisVol;
VolSum = VolSum + ThisVol;
PVSum = PVSum + ThisPV;
end;
end;
break; // Exit outer loop once we found anchor
end;
end;
if VolSum > 0 then begin
VWAPValue = PVSum / VolSum;
// Debug
if Mod(CurrentBar, 20) = 0 then
Print("Bar: ", Date:0:0, " VWAP: ", VWAPValue:0:4, " VolSum: ", VolSum:0:0);
end;
end;
end;
// Always plot - either VWAP value or 0 if before anchor
if VWAPValue > 0 then
Plot1(VWAPValue, "AVWAP", Colour)
else
NoPlot(1);
|