Does this mean it is not possible to plot a range chart with the bid/ask midpoint or does this just mean I need to write different code, possibly using the PlotOHLC function?
I tried using the code below, but I had received guidance to use arrays instead. While the code below is producing valid values for the open, high, low, and close, I'm only getting vertical lines that don't reflect the values in the arrays being plotted.
_SECTION_BEGIN("Price");
// Plot RANGE bars based on the midpoint between the bid/ask (instead of using Close values)
RequestTimedRefresh(3,false);
CurrentInterval=-(Interval(1)); // get the range interval
BarSize=CurrentInterval*TickSize;
// The 4 variables below will replace the standard Open, High, Low, and Close
MPO=0; // Initialize Mid Point Open array
MPH=0; // Initialize Mid Point High array
MPL=10000; // Initialize Mid Point Low array This is set to a high value initially to ensure the first tick will be lower, so the code will update it going forward.
MPC=0; // Initialize Mid Point Close array
LastTick=0; // Track the last tick during each bar because the last tick determines when a new bar needs to be started
Bid=GetRTData("Bid");
Ask=GetRTData("Ask");
CurrentMidPoint = (Bid+Ask)/2; // Get the current midpoint
LastTick[0]=CurrentMidPoint; // Initialize the very first LastTick value
MPO[0]=CurrentMidPoint; // Initialize the very first Open value
_N(Title = StrFormat("CustomPrice5 {{NAME}} - {{INTERVAL}} {{DATE}} Bid %g, Ask %g, Mid %g, Open %g, High %g, Low %g, Close %g {{VALUES}}", Bid[BarCount-1], Ask[BarCount-1], CurrentMidPoint, MPO, MPH, MPL, MPC ));
NextBar=False; // NextBar tracks whether or not a new bar is needed based on the current midpoint value (CurrentMidPoint)
LastTimeNum=LastValue(TimeNum()); //LastTimeNum is used to track number of seconds since chart has been redrawn
for( i = 1; i < BarCount; i++ )
{ if (i < (BarCount -1) AND MPC[i] == 0) // if the bar beeing processed is not the latest bar, and if no existing values exist, use the standard O, H, L, C values to initialize them
{ // I believe using standard O, H, L, C may help when backfilling since the historical intraday bid and ask values are not available
MPO[i] = O[i];
MPH[i] = H[i];
MPL[i] = L[i];
MPC[i] = C[i];
}
else
if (i == (BarCount -1)) // Processing the latest bar
{
x=0; //x is a counter to track the number of repetitions of the "while" loop. This is too try to avoid the "endless loop" error.
while (NextBar==False AND (x < 1000000)) // NextBar tracks whether or not a new bar is needed based on the current midpoint value (CurrentMidPoint)
{
Bid=GetRTData("Bid");
Ask=GetRTData("Ask");
CurrentMidPoint = (Bid+Ask)/2;
// If price goes below or above current bar range, set NextBar to True and capture the price value that is outside the bar range.
if ((MPH[BarCount-1] - CurrentMidPoint) > (BarSize) OR (CurrentMidPoint - MPL[BarCount-1]) > (BarSize))
{
NextBar=True;
LastTick[BarCount-1] = CurrentMidPoint;
}
else // else the CurrentMidPoint is within the range of the current bar
{
if (MPO[i]==0)
MPO[i]=LastTick[BarCount-2]; // set the Open (MPO) to the LastTick from the prior bar
if (MPL[i] > CurrentMidPoint)
MPL[i] = CurrentMidPoint; // adjust the low value when the current mid point is lower
if (MPH[i] < CurrentMidPoint)
MPH[i] = CurrentMidPoint; // adjust the high value when the current mid point is higher
MPC[i] = CurrentMidPoint; // The midpoint close value is always set to the current midpoint
}
x=x+1; //x is a counter to track the number of repetitions of the "while" loop. This is too try to avoid the "endless loop" error.
//Redraw the chart every 10 seconds
if ((LastValue(TimeNum()) - LastTimeNum) > 10) //LastTimeNum is used to track number of seconds since chart has been redrawn
{
PlotOHLC(MPO,MPH,MPL,MPC,"Range Chart",0,styleCandle);
LastTimeNum=LastValue(TimeNum());
}
}
}
}
_SECTION_END();