This is so far the progress I made and it looks working except when you change the chart period, then is when it crashes. Some guidance will be appreciated.
//+------------------------------------------------------------------+//| ChartScale.mq4 |//| Copyright 2016, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2016, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.06"#property strict#property indicator_chart_window//--- input parametersinputdouble HeadMargin=20.0;
inputdouble TailMargin=20.0;
//input double PipScale=0;double pip1=1/pow(10,_Digits-1);
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+enum onoff
{
ON, // On
OFF // Off
};
input onoff ascr=ON; // "Smart" Auto-Scrollinput onoff atrR=OFF; // ATR Range//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- indicator buffers mapping
IndicatorShortName(NULL);
ChartSetInteger(0,CHART_SCALEFIX,true);
//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
return(0);
}
//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+voidOnChartEvent(constint id, // Event IDconstlong& lparam, // Parameter of type long eventconstdouble& dparam, // Parameter of type double eventconststring& sparam // Parameter of type string events
)
{
int pos,xpos;
double priceHigh,priceLow,priceMax,priceMin,priceRange,headClose,midRange;
bool cshift=ChartGetInteger(0,CHART_SHIFT,0);
xpos=(int)(ChartGetInteger(0,CHART_VISIBLE_BARS,0)*ChartShiftSizeGet()/100);
if(cshift==true) pos=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0)+xpos-ChartGetInteger(0,CHART_VISIBLE_BARS,0); // Chart position for AutoScroll On/Offelse pos=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0)-ChartGetInteger(0,CHART_VISIBLE_BARS,0); // Check for chart shiftif(ascr==ON)
{
if(pos>0) ChartSetInteger(0,CHART_AUTOSCROLL,false); // If chart scrolled backwards switch off AutoscrollelseChartSetInteger(0,CHART_AUTOSCROLL,true); // if chart at front switch Autoscroll on
}
//--- int bars_count=ChartGetInteger(0,CHART_VISIBLE_BARS,0);
int bar=ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);
priceHigh= iHigh(Symbol(),PERIOD_CURRENT,bar);
priceLow = iLow(Symbol(),PERIOD_CURRENT,bar);
for(int i=0; i<bars_count; i++,bar--) // Find High/Low price on visible chart
{
if(bar>=0)
{
int highLoop = iHigh(Symbol(),PERIOD_CURRENT,bar);
int lowLoop = iLow(Symbol(),PERIOD_CURRENT,bar);
if(highLoop>priceHigh) priceHigh=highLoop;
if(lowLoop<priceLow) priceLow=lowLoop;
}
}
if(pos<1) pos=1;
if(atrR==ON) priceRange=iATR(Symbol(),PERIOD_D1,14);
else priceRange=0;
//double Rmul=1/sqrt(Period()+5);//Rmul=Rmul<1?1:Rmul; // Visible price range//priceRange*=Rmul;if(priceHigh-priceLow>priceRange) priceRange=priceHigh-priceLow;
headClose=(iHigh(Symbol(),PERIOD_CURRENT,bar)+iMA(Symbol(),0,3,0,MODE_SMMA,PRICE_MEDIAN))/2;
midRange=(priceHigh+priceLow)/2;
if(headClose>=midRange)
{
priceMax=priceHigh+priceRange*(HeadMargin/100.0); // Calculate top margin
priceMin=priceLow-priceRange*(TailMargin/100.0); // Calculate bottom margin
}
else
{
priceMax=priceHigh+priceRange*(TailMargin/100.0); // Calculate top margin
priceMin=priceLow-priceRange*(HeadMargin/100.0); // Calculate bottom margin
}
//priceMax=priceHigh+Rmul*50*pip1;//priceMin=priceLow-Rmul*50*pip1;ChartSetDouble(0,CHART_FIXED_MAX,priceMax); // Set top marginChartSetDouble(0,CHART_FIXED_MIN,priceMin); // Set bottom marginChartRedraw();
}
//+------------------------------------------------------------------+//| The function receives shift size of the zero bar from the right |//| border of the chart in percentage values (from 10% up to 50%). |//+------------------------------------------------------------------+double ChartShiftSizeGet(constlong chart_ID=0)
{
//--- prepare the variable to get the resultdouble result=EMPTY_VALUE;
//--- reset the error valueResetLastError();
//--- receive the property valueif(!ChartGetDouble(chart_ID,CHART_SHIFT_SIZE,0,result))
{
//--- display the error message in Experts journalPrint(__FUNCTION__+", Error Code = ",GetLastError());
}
//--- return the value of the chart propertyreturn(result);
}
//+------------------------------------------------------------------+void IndicatorShortName(string name)
{
IndicatorSetString(INDICATOR_SHORTNAME, name);
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
I'm really new to mq programming and I'm currently trying to migrate the following script to mq5.
https://www.mql5.com/es/code/17200
This is so far the progress I made and it looks working except when you change the chart period, then is when it crashes. Some guidance will be appreciated.