Open another trade

 

I want to open a 2nd trade (if its in profit or a loss within 5 pips), when I execute my 1st trade I would like to open another order. The code below will open when it hits the TP Which is incorrect, please help if you know the problem.

bool OpenTradeInProfitOrLoss()
 {
  bool Trade=false;
  for(int v=OrdersHistoryTotal()-1;v>=0;v--)
  {
   if(OrderSelect(v,SELECT_BY_POS,MODE_HISTORY))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(OrderType()==OP_BUY)
    {
     if((OrderOpenPrice()-Bid)<5*Pips())Trade=true;
    }
    
    if(OrderType()==OP_SELL)
    {
     if((OrderOpenPrice()+Ask)>5*Pips())Trade=true;
    }
   }
  }
  
  return(Trade);
 } 

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(CountSymbol(Symbol())==0)
  {
   if(Range>Filter&&Bid>MAClose&&Open[0]<Bid)
   {
    int BuyStop=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,SLBuy,TPBuy,"2023",MagicNumber,0,clrBlue);//1.5
   }
  
   if(OpenTradeInProfitOrLoss())
   {
    BuyHedge=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,SLSell,TPSell,"2023",MagicNumber,0,clrRed);
   } 
   
  }
  
  if(CountSymbol(Symbol())==0)
  {
   if(Range>Filter&&Bid<MAClose&&Open[0]>Bid)
   {
    int SellStop=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,SLSell,TPSell,"2023",MagicNumber,0,clrRed);
   }
   
   if(OpenTradeInProfitOrLoss())
   {
    SellHedge=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,SLBuy,TPBuy,"2023",MagicNumber,0,clrBlue);
   }
   
  } 
  
 }
 
  1. Scalper8: (if its in profit or a loss within 5 pips),
         if((OrderOpenPrice()-Bid)<5*Pips())Trade=true;

    Your code: A) has nothing to do with profit or loss. B) doesn't open “within 5 pips.” It opens when market is less than 5 pips below any OOP. Within (±) would be using absolute value.

  2.   double TPBuy=Ask+TakeProfit*Pips();
      double SLBuy=Ask-StopLoss*Pips();
      
      double TPSell=Bid-TakeProfit*Pips();
      double SLSell=Bid+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)

 
William Roeder #:
  1. Your code: A) has nothing to do with profit or loss. B) doesn't open “within 5 pips.” It opens when market is less than 5 pips below any OOP. Within (±) would be using absolute value.

  2. 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)

 I tried not using +/- but It still opens at Take profit.

bool OpenTradeInProfitOrLoss()
 {
  bool Trade=false;
  double BuyPrice=0;
  double SellPrice=0;
  for(int v=OrdersHistoryTotal()-1;v>=0;v--)
  {
   if(OrderSelect(v,SELECT_BY_POS,MODE_HISTORY))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   BuyPrice=BuyStop>5*Pips();
   SellPrice=SellStop>5*Pips();
   {
    if(OrderType()==OP_BUY)
    {
     if(BuyPrice)Trade=true;
    }
    
    if(OrderType()==OP_SELL)
    {
     if(SellPrice)Trade=true;
    }
   }
  }
  
  return(Trade);
 }
 
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber) <<<<<< This If only applies
   BuyPrice=BuyStop>5*Pips();                                   <<<<<< to this line of code.
   SellPrice=SellStop>5*Pips();
   {
    if(OrderType()==OP_BUY)
 
William Roeder #:

My bad, removed it but still encounter the same problem

bool OpenTradeInProfitOrLoss()
 {
  bool Trade=false;
  double BuyPrice=BuyStop>5*Pips();
  double SellPrice=SellStop>5*Pips();
  for(int v=OrdersHistoryTotal()-1;v>=0;v--)
  {
   if(OrderSelect(v,SELECT_BY_POS,MODE_HISTORY))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(OrderType()==OP_BUY)
    {
     if(BuyPrice)Trade=true;
    }
    
    if(OrderType()==OP_SELL)
    {
     if(SellPrice)Trade=true;
    }
   }
  }
  
  return(Trade);
 }
 
William Roeder #:

I assumed it might work changing it to OrdersTotal and not using OrdersHistoryTotal, still no luck. Instead the 2nd buy order opens a order sell at the time, still incorrect. 

bool OppositeTrade()
 {
  bool Trade=false;
  double BuyTrade=BuyStop>Bid-5*Pips();
  double SellTrade=SellStop<Ask+5*Pips();
  
  for(int w=OrdersTotal()-1;w>=0;w--)
  {
   if(OrderSelect(w,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(OrderType()==OP_BUY)
    {
     if(BuyTrade)Trade=true;
    }
    
    if(OrderType()==OP_SELL)
    {
     if(SellTrade)Trade=true;
    } 
   }
  }
  
  return(Trade);
 } 
 
Scalper8 #: I assumed it might work changing it to OrdersTotal and not using OrdersHistoryTotal, still no luck. Instead the 2nd buy order opens a order sell at the time, still incorrect. 

Luck is not involved. Learning to code is.

Your code now only looks at open orders.

 
William Roeder #:

Luck is not involved. Learning to code is.

Your code now only looks at open orders.

I removed the orderopenprice as you mentioned, removed the buyprice and sellprice from the loop. I'll revert back to the  OrdersHistoryTotal. 

double BuyPrice=BuyStop>5*Pips();
double SellPrice=SellStop>5*Pips();

The problem might these 2 not opening within 5 pips.

 

When removing the Buyprice & sellprice and selecting by ticket It still opens buy and sell at the same time.

int Direction()
 {
  int LastDirection=0;
  
  for(int u=OrdersTotal()-1;u>=0;u--)
  {
   if(OrderSelect(u,SELECT_BY_TICKET,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(OrderType()==OP_BUY){LastDirection=1;}
    else if(OrderType()==OP_SELL){LastDirection=-1;}
   }
  }
  
  return(LastDirection);
 }

With both if statements they open at the same time, when I change it to else if 2 buys open and no sell within 5 pips

void OpenTrade()
 {
  double TPBuy=Ask+TakeProfit*Pips();
  double SLBuy=Ask-StopLoss*Pips();
  
  double TPSell=Bid-TakeProfit*Pips();
  double SLSell=Bid+StopLoss*Pips();
  
  BuyPrice=Bid+5*Pips();
  SellPrice=Bid-5*Pips();
  
  if(BuyPrice>0&&SellPrice>0)
  {
   if(BuyPrice)
   {
    Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,SLBuy,0,"2023",MagicNumber,0,clrBlue);
   } 
   
   else if(SellPrice)
   {
    Sell=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,SLSell,0,"2023",MagicNumber,0,clrRed);
   }
  } 
}

Reason: