Thank you for looking at this thread. I'm writing some basic indicators in MQL5, and I am currently writing one for a simple average true range with no smoothing. Compiling the code leads to no errors or warnings. For some reason when I apply the EA to a chart it loads on fine but then on the next tick it disappears with no warning! I attached my code here, maybe there is something wrong with it?
Thank you for looking!
//+------------------------------------------------------------------+//| avg_true_range_function.mq5 |//| Copyright 2018, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property library#property copyright"Copyright 2018, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.00"//+------------------------------------------------------------------+//| My function |//+------------------------------------------------------------------+// int MyCalculator(int value,int value2) export// {// return(value+value2);// }//+------------------------------------------------------------------+double avg_true_range_function(int no_candles) export
{
//Creates a struct where the information about the candles (e.g. price) will be stored.MqlRates PriceInfo[];
//Sorts it from the current candle to the oldest candleArraySetAsSeries(PriceInfo,true);
//Copies the price information into the arraydouble Data=CopyRates(Symbol(),PERIOD_CURRENT,0,Bars(Symbol(),PERIOD_CURRENT),PriceInfo);
//Creating 'buy' arraysdouble high_price[]; //Store the high pricedouble low_price[]; //Store the low pricedouble close_price[]; //Store the close priceArrayResize(high_price,no_candles);
ArrayResize(low_price,no_candles);
ArrayResize(close_price,no_candles);
for(int i=0; i<no_candles+1; i=i+1)
{
high_price[i]=PriceInfo[i+1].high;
low_price[i]=PriceInfo[i+1].low;
close_price[i]=PriceInfo[i+1].close;
}
double tr[]; //Array to store the value of the true rangedouble tr_values_store[],avg_tr=0,high_minus_low=0,high_minus_close=0,low_minus_close=0;
ArrayResize(tr,no_candles);
ArrayResize(tr_values_store,3);
for(int i=no_candles;i>0;i=i-1)
{
high_minus_low = high_price[i-1]-low_price[i-1];
high_minus_close = high_price[i-1]-close_price[i];
low_minus_close = low_price[i-1]-close_price[i];
tr_values_store[0] = high_minus_low;
tr_values_store[1] = high_minus_close;
tr_values_store[2] = low_minus_close;
tr[i-1] = ArrayMaximum(tr_values_store,WHOLE_ARRAY,0);
}
double tr_sum=0;
for(int i=0;i<no_candles;i=i+1)
{
tr_sum = tr_sum + tr[i];
}
avg_tr = tr_sum/no_candles;
return avg_tr;
}
//+------------------------------------------------------------------+
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.
Hi all!
Thank you for looking at this thread. I'm writing some basic indicators in MQL5, and I am currently writing one for a simple average true range with no smoothing. Compiling the code leads to no errors or warnings. For some reason when I apply the EA to a chart it loads on fine but then on the next tick it disappears with no warning! I attached my code here, maybe there is something wrong with it?
Thank you for looking!