problem with moving averages (noob inside)

 

Hello everyone and sorry for my English, I'm starting to program in mql4 and to do a simple and on the crossover moving averages. The problem is that the ea does not open positions, opens them only if I delete Lastfastmovingaverages, lastmediummoving etc. .. and I leave only those on the current candle.


//+------------------------------------------------------------------+
//|                                                         MM 2.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double lotto=0.1;
extern int stop_loss=70;
extern int take_profit=140;
extern bool Compounding=true;
extern double Rischio=5.0;
extern int PeriodoMediaVeloce=50;
extern int PeriodoMediaMedium=100;
extern int PeriodoMediaLenta=200;
double TP=take_profit*10*Point;
double SL=stop_loss*10*Point;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
     if(NewBar()){
     calcolasize(Rischio);
     if(condizionelong()) EseguiOrdineLong();
     if(condizioneshort())EseguiOrdineShort();
     }
 }

  
//+------------------------------------------------------------------+

double FastMovingAverage = iMA(Symbol(),0,PeriodoMediaVeloce,0,MODE_EMA,PRICE_CLOSE,0);
double LastFastMovingAverage = iMA(Symbol(),0,PeriodoMediaVeloce,0,MODE_EMA,PRICE_CLOSE,1);
double MediumMovingAverage = iMA(Symbol(),0,PeriodoMediaMedium,0,MODE_EMA,PRICE_CLOSE,0);
double LastMediumMovingAverage = iMA(Symbol(),0,PeriodoMediaMedium,0,MODE_EMA,PRICE_CLOSE,1);
double SlowMovingAverage = iMA(Symbol(),0,PeriodoMediaLenta,0,MODE_EMA,PRICE_CLOSE,0);
double LastSlowMovingAverage = iMA(Symbol(),0,PeriodoMediaLenta,0,MODE_EMA,PRICE_CLOSE,1);

bool condizionelong () {
     if ((LastFastMovingAverage < LastSlowMovingAverage) && (FastMovingAverage > SlowMovingAverage)) return true;
     else return false;
     }
     
bool condizioneshort() {
     if ((LastFastMovingAverage > LastSlowMovingAverage) && (FastMovingAverage < SlowMovingAverage)) return true;
     else return false; 
     }

double calcolasize (double Rischioins) {
 if (Compounding){
  double diff=stop_loss*10*Point;
  diff=NormalizeDouble(diff,5);
  Print(MarketInfo(Symbol(),MODE_TICKVALUE));
  lotto=(AccountBalance()*Rischioins/100)/(MarketInfo(Symbol(),MODE_TICKVALUE)*diff*MathPow(10,MarketInfo(Symbol(),MODE_DIGITS)));
  return lotto;
 }
 else return lotto=0.1;

}

void EseguiOrdineLong() {
     OrderSend(Symbol(),OP_BUY,lotto,Ask,0,Ask-SL,Ask+TP,"Ordine Long eseguito");
     }
     
void EseguiOrdineShort() {
     OrderSend(Symbol(),OP_SELL,lotto,Bid,0,Bid+SL,Bid-TP,"Ordine Short eseguito");
     }
     
bool NewBar(){

  static datetime lastbar;
  datetime curbar = Time[0];
  
   if(lastbar!=curbar){
      lastbar=curbar;
      return (true);
   }
   else{
     return(false);
   }
   }


 
extern int PeriodoMediaVeloce=50;
extern int PeriodoMediaMedium=100;
extern int PeriodoMediaLenta=200;
input ENUM_TIMEFRAMES PeriodoMediaVeloce = PERIOD_CURRENT,
                      PeriodoMediaMedium = PERIOD_CURRENT,
                      PeriodoMediaLenta  = PERIOD_CURRENT;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

double FastMovingAverage, LastFastMovingAverage, MediumMovingAverage, LastMediumMovingAverage, SlowMovingAverage, LastSlowMovingAverage;
void OnTick()
  {
     FastMovingAverage       = iMA(Symbol(),0,PeriodoMediaVeloce,0,MODE_EMA,PRICE_CLOSE,0);
     LastFastMovingAverage   = iMA(Symbol(),0,PeriodoMediaVeloce,0,MODE_EMA,PRICE_CLOSE,1);
     MediumMovingAverage     = iMA(Symbol(),0,PeriodoMediaMedium,0,MODE_EMA,PRICE_CLOSE,0);
     LastMediumMovingAverage = iMA(Symbol(),0,PeriodoMediaMedium,0,MODE_EMA,PRICE_CLOSE,1);
     SlowMovingAverage       = iMA(Symbol(),0,PeriodoMediaLenta,0,MODE_EMA,PRICE_CLOSE,0);
     LastSlowMovingAverage   = iMA(Symbol(),0,PeriodoMediaLenta,0,MODE_EMA,PRICE_CLOSE,1);

     if(NewBar()){
     calcolasize(Rischio);
     if(condizionelong()) EseguiOrdineLong();
     if(condizioneshort())EseguiOrdineShort();
     }
 }
 
Konstantin Nikitin:
Why are you using time frames for average period parameter instead of average period? 
 
Mladen Rakic :
Why are you using time frames for average period parameter instead of average period? 

I dont have anything. Simply corrected the code. And that's all.
iMA

[in]  Timeframe. It can be any of ENUM_TIMEFRAMES enumeration values. 0 means the current chart timeframe.

 
Konstantin Nikitin:

I dont have anything. Simply corrected the code. And that's all.
iMA

[in]  Timeframe. It can be any of ENUM_TIMEFRAMES enumeration values. 0 means the current chart timeframe.

This is a correction?

input ENUM_TIMEFRAMES PeriodoMediaVeloce = PERIOD_CURRENT,
                      PeriodoMediaMedium = PERIOD_CURRENT,
                      PeriodoMediaLenta  = PERIOD_CURRENT;
 
Konstantin Nikitin:
thank you very much, but by making these changes, the periods of moving averages are determined by those that imposed on the chart?
 
Mladen Rakic :

This is a correction?

And this ...
You can specify only minutes. But it's easier to use the finished enumeration.

 
Konstantin Nikitin:

And this ...
You can specify just seconds. But it's easier to use the finished structure.

yes, but in this way, the candles taken into consideration by moving averages are determined by what?

 
Konstantin Nikitin:

And this ...
You can specify just seconds. But it's easier to use the finished structure.

Please revise what you have posted regarding this issue (before it is too late)

  • 2nd parameter of iMa() is the time frame
  • 3rd parameter of iMA() is the average period
 

I solved my problem, it was enough to move the code where I declared the moving averages in the ontick function

thank you all

Reason: