Several positions in crossing EMAs EA problem

 

Hello,


I am starting to code in mql5 and I am devloping a code that opens one positions every time there is a crossover of daily EMA8 and daily EMA21 and price is above daily SMA200.

I do get to open 2 positions, but there are not any more positions but conditions are being met... I am not getting what is my mistake.

I would aprreciate some help here!


#include <Trade\Trade.mqh>

CTrade trade;

input ENUM_TIMEFRAMES time_frame_ea = PERIOD_D1; // Time frame EA


// Create array for several prices

double EMA8array[], EMA21array[], SMA200array[];

int EMA8 = 0;

int EMA21 = 0;

int SMA200 = 0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

// Define properties of the EMA

   EMA8 = iMA(_Symbol, time_frame_ea, 8, 0, MODE_EMA, PRICE_CLOSE);

   EMA21 = iMA(_Symbol, time_frame_ea, 21, 0, MODE_EMA, PRICE_CLOSE);

   SMA200 = iMA(_Symbol, time_frame_ea, 200, 0, MODE_SMA, PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
// Get ask price

   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK), _Digits);

// Get bid price

   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID), _Digits);
   string order = "";
   int positions_total = PositionsTotal();
   datetime newtime = iTime(Symbol(), time_frame_ea, 0);

// Sort the price array from the current candle downwards

   ArraySetAsSeries(EMA8array, true);

   ArraySetAsSeries(EMA21array, true);

   ArraySetAsSeries(SMA200array, true);

   CopyBuffer(EMA8, 0, 0, 3, EMA8array);

   CopyBuffer(EMA21, 0, 0, 3, EMA21array);

   CopyBuffer(SMA200, 0, 0, 3, SMA200array);

// Conditions for buy

   if(newtime == TimeCurrent())
     {
      
      if(EMA8array[0] > EMA21array[0] && EMA8array[1] < EMA21array[1] && Ask > SMA200array[1])
        {
         order = "buy";
        }
      if(order == "buy")
        {
         Newbuy();

         Comment(newtime + " " + TimeCurrent());

        }
     }
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Newbuy()
  {

   int positions_total = PositionsTotal();
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK), _Digits);

   for(int i=0; i<=positions_total; i++)
     {
      if(positions_total == i)
        {
         trade.Buy(0.01, NULL, Ask, 0, NULL, NULL);
         break;
        }
     }

   return;

  }


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



//+------------------------------------------------------------------+
 
  1. You are looking at D1 bar zero. You will get multiple cross and uncross as the candle forms.

    You should wait for a new D1 bar to start and check bars one and two.

  2.    if(newtime == TimeCurrent())
    That will almost always be false, unless a tick happens at the first second of the start of the candle.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014.04.04)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011.05.06)

    detecting a new bar without removing ability to detect tick in multiple timeframe - Easy Trading Strategy - MQL4 programming forum #8 (2021.08.24)

 
William Roeder:
  1. You are looking at D1 bar zero. You will get multiple cross and uncross as the candle forms.

    You should wait for a new D1 bar to start and check bars one and two.

  2. That will almost always be false, unless a tick happens at the first second of the start of the candle.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014.04.04)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011.05.06)

    detecting a new bar without removing ability to detect tick in multiple timeframe - Easy Trading Strategy - MQL4 programming forum #8 (2021.08.24)

Thank you William!

I am struggling with point No. 2. You send me to some mql4 forums and I am familiar with mql5. I believe the logic still the same... I just have to sort out how to code it...

 
Camilo Andres Acevedo Corzo:

Thank you William!

I am struggling with point No. 2. You send me to some mql4 forums and I am familiar with mql5. I believe the logic still the same... I just have to sort out how to code it...

Found it for MQL5!:


https://www.mql5.com/en/forum/363472

detecting a new bar without removing ability to detect tick in multiple timeframe
detecting a new bar without removing ability to detect tick in multiple timeframe
  • 2021.02.23
  • www.mql5.com
Hi I was created a thread about static datetime in here: https://www.mql5.com/en/forum/362972 and successfully working on it...