"int" not "void"
int OnCalculate( const int rates_total, // size of input time series const int prev_calculated, // number of handled bars at the previous call const datetime& time{}, // Time array const double& open[], // Open array const double& high[], // High array const double& low[], // Low array const double& close[], // Close array const long& tick_volume[], // Tick Volume array const long& volume[], // Real Volume array const int& spread[] // Spread array );
And you have to return a value, an int type value to be passed as the prev_calculated parameter during the next function call.
"int" not "void"
And you have to return a value!
Return Value
int type value to be passed as the prev_calculated parameter during the next function call.
I have already tried this as well before the post but same it didnt work, compilation warning "no indicator window property is defined, indicator_chart_window is applied".
and when run with the EA, i have the following error "'TestOnCalculate' is not expert and cannot be executed"
input double ATR_Multiplier = 1.5; // ATR multiplier for stop loss input double RiskRewardRatio = 2; // Risk to reward ratio input int MovingAveragePeriod = 14; // Moving average period for crossover input int TrailingStop = 100; // Trailing stop in points double ATRValue; double StopLossLevel; double TrailStopLossLevel; double LotSize; double TrailingStopLevel; //+------------------------------------------------------------------+ //| Expert OnCalculate function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { ATRValue = iATR(NULL, 0, MovingAveragePeriod, PRICE_CLOSE); //Print("ATR value calculated: ", ATRValue); StopLossLevel = close[0] - ATR_Multiplier * ATRValue; //Print("Stop loss level calculated: ", StopLossLevel); TrailStopLossLevel = close[0] - ATRValue; //Print("Trail stop loss level calculated: ",TrailStopLossLevel); // Calculate lot size based on 2:1 reward to risk ratio LotSize = AccountFreeMargin() * RiskRewardRatio / (ATRValue * Point); //Print("LotSize calculated: ", LotSize); return rates_total; } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- Print("ATR value calculated: ", ATRValue); Print("Stop loss level calculated: ", StopLossLevel);; Print("Trail stop loss level calculated: ",TrailStopLossLevel); Print("LotSize calculated: ", LotSize); }
You can't use OnCalculate and OnTick at the same time.
If it is an Indicator then ONLY use OnCalculate (indicators cannot trade).
If it is an Expert Advisor then ONLY use OnTick.
ATRValue = iATR(NULL, 0, MovingAveragePeriod, PRICE_CLOSE);Do not post code that will not even compile.
It does compile. The user is just coding it incorrectly.
It does compile, but you are using it incorrectly ...
ATRValue = iATR( _Symbol, _Period, MovingAveragePeriod, 0 );
double iATR( string symbol, // symbol int timeframe, // timeframe int period, // averaging period int shift // shift );

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello experts,
I need your advise to understand why OnCalculate doesnt work in my EA.
I have the following compilation warning "OnCalculate function declared with wrong type or/and parameters" and the print statements always return 0 when EA is running.
2023.02.08 15:10:20.296 TestOnCalculate ADAUSDm,M30: LotSize calculated: 0.0
All I need here is to use OnCalculate to get the updated value for LotSize and TrailingStopLossLevel based on ATR
Maybe I don't really understand how it works so appreciate if you can explain it.