OnCalculate not working for me

 

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:22.523 TestOnCalculate ADAUSDm,M30: LotSize calculated: 0.0
2023.02.08 15:10:22.523 TestOnCalculate ADAUSDm,M30: Trail stop loss level calculated: 0.0
2023.02.08 15:10:22.523 TestOnCalculate ADAUSDm,M30: Stop loss level calculated: 0.0
2023.02.08 15:10:22.523 TestOnCalculate ADAUSDm,M30: ATR value calculated: 0.0

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.

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                                      |
//+------------------------------------------------------------------+
void 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);
       StopLossLevel = close[0] - ATR_Multiplier * ATRValue;
       TrailStopLossLevel = close[0] - ATRValue;

   
       // Calculate lot size based on 2:1 reward to risk ratio
       LotSize = AccountFreeMargin() * RiskRewardRatio / (ATRValue * Point);
       
   }

//+------------------------------------------------------------------+
//| 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);
   
  }
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Event Handling Functions - Functions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

"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.

 
Fernando Carreiro #:

"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);
   
  }
 
ahmedelmidany #: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"

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.

 
Fernando Carreiro #:

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.

Thanks for the explanation Fernando.

 
       ATRValue = iATR(NULL, 0, MovingAveragePeriod, PRICE_CLOSE);
Do not post code that will not even compile.
 
William Roeder #:
Do not post code that will not even compile.

You can elaborate more on what do you mean by "will not even compile" rather than pasting same comment.

 
@William Roeder #: Do not post code that will not even compile.

It does compile. The user is just coding it incorrectly.

ahmedelmidany #: You can elaborate more on what do you mean by "will not even compile" rather than pasting same comment.

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 
   );
 
Fernando Carreiro #: It does compile. The user is just coding it incorrectly.
Sorry. Coffee hadn't yet kicked in; I was thinking MQL5 (handle).
 
@William Roeder #: Sorry. Coffee hadn't yet kicked in; I was thinking MQL5 (handle).
No problem!
Reason: