EA Opens trades incorrectly

 

Please help if you know what's the problem.

My EA opens trades incorrectly, it opens the sell trade first instead of the buy which is the moving average. I want to open the buy trade first (Moving average) then if it goes opposite (10 Pips) direction open the sell, vice versa.

void OpenTrade()
 {
  double TPBuy=Ask+TakeProfit*Pips();
  double SLBuy=Ask-StopLoss*Pips();
  
  double TPSell=Bid-TakeProfit*Pips();
  double SLSell=Bid+StopLoss*Pips();
  
  double Range=iHigh(Symbol(),0,0)-iLow(Symbol(),0,0);

  double Filter=5*Pips();
  
  double MAClose=iMA(Symbol(),0,3,0,MODE_LWMA,PRICE_CLOSE,0);
  
  if(OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES))
  Hedge=OrderOpenPrice();
  
  if(Range>Filter&&Bid>MAClose&&Open[0]<Bid)
  {
   Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"2023",MagicNumber,0,clrBlue);
  }
  
  if(Bid-Hedge>10*Pips())
  {
   int BuyHedge=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"2023",MagicNumber,0,clrRed);
  }
   
  if(OrderSelect(Sell,SELECT_BY_POS,MODE_TRADES))
  Hedge=OrderOpenPrice();
   
  if(Range>Filter&&Bid<MAClose&&Open[0]>Bid)
  {
   Sell=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"2023",MagicNumber,0,clrRed);
  }
   
  if(Ask-Hedge>10*Pips())
  {
   int SellHedge=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"2023",MagicNumber,0,clrBlue);
  } 
  
 }
 
void OnTick()
 {
  if(CountTrades()<2){OpenTrade();}
 }


  

 
  1.   double TPBuy=Ask+TakeProfit*Pips();
      double SLBuy=Ask-StopLoss*Pips();

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  2.   if(OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES))
      Hedge=OrderOpenPrice();
      
      if(Range>Filter&&Bid>MAClose&&Open[0]<Bid)
      {
       Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"2023",MagicNumber,0,clrBlue);
      }
      
      if(Bid-Hedge>10*Pips())
      {
       int BuyHedge=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"2023",MagicNumber,0,clrRed);
      }
       
      if(OrderSelect(Sell,SELECT_BY_POS,MODE_TRADES))

    We have no idea what variables Buy or Sell are or where set and with what.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3. Your if only controlls the assignment of Hedge. What if the select fails? Should following lines also run?

  4. Scalper8: I want to open the buy trade first (Moving average) then if it goes opposite (10 Pips) direction open the sell, vice versa.
    Can not be done with US brokers.
  5. Where do you check if you already did that as not to create any more?

 
William Roeder #:
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  2. We have no idea what variables Buy or Sell are or where set and with what.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3. Your if only controlls the assignment of Hedge. What if the select fails? Should following lines also run?

  4. Can not be done with US brokers.
  5. Where do you check if you already did that as not to create any more?

My buy & sell are both integers. I will post the whole code.

I'm not using a US broker.

#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int MagicNumber;
int Buy=0;
int Sell=0;
double Hedge=0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountTrades()
 {
  int Count=0;
  for(int a=OrdersTotal()-1;a>=0;a--)
   {
    if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUY||OrderType()==OP_SELL)
    Count++;
   }
  return(Count);
 } 
 
double Pips()
 {
  double PipPoint=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==2||Digit==3){PipPoint=Point*10;}
  if(Digit==4||Digit==5){PipPoint=Point*10;}
  return(PipPoint);
 }

void OpenTrade()
 {
  double Range=iHigh(Symbol(),0,0)-iLow(Symbol(),0,0);

  double Filter=5*Pips();
  
  double MAClose=iMA(Symbol(),0,3,0,MODE_LWMA,PRICE_CLOSE,0);
  
  if(OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES))
  Hedge=OrderOpenPrice();
  
  if(Range>Filter&&Bid>MAClose&&Open[0]<Bid)
  {
   Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"2023",MagicNumber,0,clrBlue);
  }
  
  if(Bid-Hedge>10*Pips())
  {
   int BuyHedge=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"2023",MagicNumber,0,clrRed);
  }

  if(OrderSelect(Sell,SELECT_BY_POS,MODE_TRADES))
  Hedge=OrderOpenPrice();
  

  if(Range>Filter&&Bid<MAClose&&Open[0]>Bid)
  {
   Sell=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"2023",MagicNumber,0,clrRed);
  }
   
  if(Ask-Hedge>10*Pips())
  {
   int SellHedge=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"2023",MagicNumber,0,clrBlue);
  } 

 }
 
void OnTick()
 {
  if(CountTrades()<2){OpenTrade();}
 } 
 
  1. If a five (5) digit price is 1/10 PIP
    then a four (4) digit price is a PIP
      double Digit=MarketInfo(Symbol(),MODE_DIGITS);
      if(Digit==1||Digit==2||Digit==3){PipPoint=Point*10;}
      if(Digit==4||Digit==5){PipPoint=Point*10;}
    Simplify.
    PipPoint = _Digits % 2 == 0 ? Point : Point*10;
    
              How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018)

  2.   if(OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES))
      ⋮
       Buy=OrderSend(…
    OrderSend does not return a position index. And if you select by ticket, you must verify the order is still open.
 
William Roeder #:
  1. If a five (5) digit price is 1/10 PIP
    then a four (4) digit price is a PIP
    Simplify.
              How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018)

  2. OrderSend does not return a position index. And if you select by ticket, you must verify the order is still open.

Thanks for heads up 

double Pips()
 {
  double PipPoint=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==3||Digit==5){PipPoint2=Point*10;}
  return(PipPoint);
 }

I used the selecting of ticket on , changed My if statement and added another void

void Buy()
 {
  double Range=iHigh(Symbol(),0,0)-iLow(Symbol(),0,0);

  double Filter=5*Pips2();
  
  double MAClose=iMA(Symbol(),0,3,0,MODE_LWMA,PRICE_CLOSE,0);
  
  if(Range>Filter&&Bid>MAClose&&Open[0]<Bid)
  {
   BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"2023",MagicNumber,0,clrBlue);
  } 
  
 }
 
void BuyHedge()
 {
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   if(OrderSelect(BuyTrade,SELECT_BY_TICKET,MODE_TRADES))
   Hedge=OrderOpenPrice();
 
   if(Hedge-Bid>5*Pips2())
   {
    int BuyHedge=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"2023",MagicNumber,0,clrRed);
   }
   
  }
  
 }

Changes to the on tick function

void OnTick()
 {
  if(CountTrades()<1){Buy();}
  else if(CountTrades()<2){BuyHedge();}
}

Everything works How how I want my EA to open trades