EA opens multiple position in the same tick

 

I don't understand why this happens 

https://imgur.com/a/qsr36lT (I don't know how to add images)

This is the code. Go to the end to see the buy programming.

The strategy uses sma, macd and parabolic sar. When the sma crosses a positive candle, the sar is below the sma and the macd is above zero, I send a buy operation.

#include <Trade\Trade.mqh>

input int smaPeriods = 180;
input int smaShift = 150;
input int macdFastPeriod = 12;
input int macdSlowPeriod = 26;
input int macdPeriod = 9;
input int lot = 10;
input int stopLossPips = 100;
input int takeProfitPips = 100;
input double parabolicSarStep = 0.02;
input double parabolicSarMaximun = 0.2;

CTrade tradingControlPanel;
MqlRates priceData[];
double smaData[];
double macdData[];
double parabolicSarData[];
int smaHandler;
int macdHandler;
int parabolicSarHandler;
int P;

void buySequence(double currentAsk);

int OnInit() {
   ArraySetAsSeries(smaData, true);
   ArraySetAsSeries(macdData, true);
   ArraySetAsSeries(parabolicSarData, true);
   ArraySetAsSeries(priceData, true);
   
   smaHandler = iMA(_Symbol, _Period, smaPeriods, smaShift, MODE_SMA, PRICE_CLOSE);
   parabolicSarHandler = iSAR(_Symbol, _Period, parabolicSarStep, parabolicSarMaximun);
   macdHandler = iMACD(_Symbol, _Period, macdFastPeriod, macdSlowPeriod, macdPeriod, PRICE_CLOSE);
   
   if(_Digits == 5 || _Digits == 3 || _Digits == 1)
      P = 10;
   else 
      P = 1;

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) {
   IndicatorRelease(smaHandler);
   IndicatorRelease(macdHandler);
   IndicatorRelease(parabolicSarHandler);
}

void OnTick() {
   double currentBid = SymbolInfoDouble(_Symbol,SYMBOL_BID); // Get latest Bid Price
   double currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get latest Ask Price

   CopyBuffer(smaHandler, 0, 0, 30, smaData);
   CopyBuffer(macdHandler, 0, 0, 30, macdData);
   CopyBuffer(parabolicSarHandler, 0, 0, 30, parabolicSarData);
   CopyRates("EURUSD", PERIOD_H4, 0, 26, priceData);
   
    if(PositionSelect(_Symbol) == false)
      buySequence(currentAsk);
      
    return;
}

void buySequence(double currentAsk) {
   double stopLoss = currentAsk - stopLossPips * _Point;
   double takeProfit = currentAsk + takeProfitPips * _Point;
   if(smaData[1] > priceData[1].open && smaData[1] < priceData[1].close && smaData[1] > parabolicSarData[1] && macdData[1] > 0) {
      tradingControlPanel.PositionOpen(_Symbol, ORDER_TYPE_BUY, lot, currentAsk, stopLoss, takeProfit, "");
   }
   return;
}
Imgur
Imgur
  • 2019.09.28
  • imgur.com
Post with 1 views.
 
#include <Trade\PositionInfo.mqh>

//--- input patameter
input ushort   InpMaxPositions = 1; // number of orders

CPositionInfo  m_position; // trade position object
//+------------------------------------------------------------------+



int total = 0;
   for(int i = PositionsTotal()-1; i >= 0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol() == _Symbol)
            total++;

   if(total > InpMaxPositions)
      return;
   else
     {
      //--- your code

     }
         
//+------------------------------------------------------------------+
 
franc_: I don't understand why this happens 
   if(smaData[1] > priceData[1].open && smaData[1] < priceData[1].close && …

Your signal exists for an entire candle. Either check for an existing position before opening a new one, or only process your signal once on a new bar.

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.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum
 

This lines doesn't check if there is an open position?

if(PositionSelect(_Symbol) == false)

The function onTick() is called everytime the price changes right? or when a new candle arrives?

 
franc_:

Tester or Demo?

 
can some body help me i want to make an EA that executes a trade when two EMA crossover and take profit at the next crossover while executing  other trades at the other crossover.