Convert inicator into Strategy

 

Hi 

I'm trying out some simple strategies however I'm struggeling to understand how to produce calculations to use in trade conditions.

Anyway if you're able answer this that should be resolved:

I'm trying to get this simple calculation/indicator into a strategy where I can use it to create a condition for a trading strategy, here is the indicator:

double Array_MA10[];


int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Array_MA10,INDICATOR_DATA);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration 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[])
  {
//---
   for(int i=39; i<rates_total; i++){ 
     double ma_value = 0;
     
     for(int ii=0; ii< 40; ii++){
         double a=close[i-ii];
         ma_value += a;
         }
      Array_MA10[i]= (ma_value/40)*0.995;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

In my attempt I though adding int OnCalculate() would work however I can't make it return values to use in void OnTick():

int MA14 = 0;
int MA60 = 0;

double Array_MA10[];

double Array_MA14[], Array_MA60[];


int OnInit(void){
   MA14 = iMA(_Symbol,_Period,14,0,MODE_SMA,PRICE_CLOSE);
   MA60 = iMA(_Symbol,_Period,60,0,MODE_SMA,PRICE_CLOSE);
   
   SetIndexBuffer(0,Array_MA10,INDICATOR_DATA);
   
   return(INIT_SUCCEEDED);
   
}

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[])
  {
//---
   for(int i=9; i<rates_total; i++){ 
     double ma_value = 0;
     
     for(int ii=0; ii< 10; ii++){
         double a=close[i-ii];
         ma_value += a;
         }
      Array_MA10[i]= (ma_value/10)*0.995;
     }
     double MA10_previous = Array_MA10[1];
     double MA10_current = Array_MA10[0];
//--- return value of prev_calculated for next call
   return(MA10_previous);
   return(MA10_current);
  }
//+------------------------------------------------------------------+
void OnTick()
  {  
   CopyBuffer(MA14,0,1,3,Array_MA14);
   CopyBuffer(MA60,0,1,3,Array_MA60);
      
   double lastClose = iClose(_Symbol,PERIOD_CURRENT,0);
   double previousClose = iClose(_Symbol,PERIOD_CURRENT,1);
     
   if (previousClose < MA10_previous && (lastClose > MA10_current) && (PositionsTotal()==0)){
   
      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      ask = NormalizeDouble(ask,_Digits);
         
      double sl = ask - (ask*0.004*100) *_Point;
      sl = NormalizeDouble(sl,_Digits);
         
      double tp = ask + (ask*0.01*100) *_Point;
      tp = NormalizeDouble(tp,_Digits);
      
      trade.Buy(0.1,_Symbol,ask,sl,tp);
      
   } 
   else if((Array_MA14[1] < Array_MA60[1]) && (Array_MA14[0] > Array_MA60[0]) && (PositionsTotal()==1)){
   
      double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      trade.PositionClose(1);            
           }
   
   }

there is likely a very easy fix but I've been researching and testing for a while and new problems just keep occuring so if you have any answers that would be amazing!!!

 

 OnCalculate() is used for indicators not strategy. 

 MA14 = iMA(_Symbol,_Period,14,0,MODE_SMA,PRICE_CLOSE);
is called into EA but more detailed in articles  section  of Mql5.com like 'Step by Step  Guide to writing an Expert Advisor in MQl5  for Beginners '  in 
 
Tango.Trader:

Hi 

I'm trying out some simple strategies however I'm struggeling to understand how to produce calculations to use in trade conditions.

Anyway if you're able answer this that should be resolved:

I'm trying to get this simple calculation/indicator into a strategy where I can use it to create a condition for a trading strategy, here is the indicator:

In my attempt I though adding int OnCalculate() would work however I can't make it return values to use in void OnTick():

there is likely a very easy fix but I've been researching and testing for a while and new problems just keep occuring so if you have any answers that would be amazing!!!


the only way to do it is by:

1) Creating a MQLRates array (like: MqlRates rates[]; )

2) copying the values of such timeframe to that array (this will provide you with an array identical to the array which is passed via OnCalculate() [but there is no OnCalculate on Experts, so you have to create it your own, on some specific new array, by using an Array of TYPE:  MqlRates )

3) copy the indicator function wich makes the calculations, and change it to use the MqlRates array you just created

4) call the indicator function inside the Expert under either function Timer() or via OnTick() function

Reason: