Sell Order won't open

 

Below is my code, sell order won't open (if statement) is the same as buy. I would like to arbitrage buy & sell. Are my Price1 & Price2 functions correct for arbitrage?

Your help will be appreciated!

extern double TakeProfit=5;
extern double StopLoss=5;

int MagicNumber=687346587;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountSymbol(string Pair)
 {
  int NoSymbol=0;
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   if(OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Pair)
   NoSymbol++;
  }
  return(NoSymbol);
 }

int CountPenOrders()
 {
  int Pending=0;
  for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
  {
   if(OrderSelect(Loop2,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP)
   Pending++;
  }
   
  return(Pending);
 } 
 
double Pips()
 {
  double PipPoint=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==2||Digit==3){PipPoint=Point*10;}
  if(Digit==1||Digit==2){PipPoint=Point*100;}
  if(Digit==4||Digit==5){PipPoint=Point*10;}
  return(PipPoint);
 } 

void OpenTrade()
 {
  double TPBuy=Ask+TakeProfit*Pips();
  double TPSell=Bid-TakeProfit*Pips();
  
  double SLBuy=Ask-StopLoss*Pips();
  double SLSell=Bid+StopLoss*Pips();
  
  datetime Expire=TimeCurrent()+(60*15);
  
  double Price1=(MarketInfo(Symbol(),MODE_BID)+MarketInfo(Symbol(),MODE_ASK))/2;
  double Price2=(MarketInfo(Symbol(),MODE_BID)+MarketInfo(Symbol(),MODE_ASK))/2;
  
  double FinalPrice=1.0/(Price1*Price2);
  
  if(FinalPrice)
  {
   if(CountSymbol(Symbol())<1)
   {
    int Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"Scalper8",MagicNumber,Expire,clrBlue);
    bool SelectBuy=OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES);
    if(Buy>0)
    {
     bool BuyMod=OrderModify(Buy,OrderOpenPrice(),SLBuy,TPBuy,0,clrNONE);
    }
   }
   
   if(CountSymbol(Symbol())<1)
   {
    int Sell=OrderSend(Symbol(),OP_SELL,0.01,Bid-0.5*Pips(),0,SLSell,TPSell,"Scalper8",MagicNumber,Expire,clrRed);
    bool SelectSell=OrderSelect(Sell,SELECT_BY_POS,MODE_TRADES);
    if(Sell>0)
    {
     bool SellMod=OrderModify(Sell,OrderOpenPrice(),SLSell,TPSell,0,clrNONE);
    }
   }
   
  }
  
 } 

void OnTick()
 {
  if(CountPenOrders()<1){OpenTrade();}
 }
 
  1. On a five digit broker (5 digits/3 on JPY), a PIP is 10 points. On a four digit broker, a PIP equals a point.
    This is MT4, why not use the Predefined Variables Digits?
      double PipPoint=0;
      double Digit=MarketInfo(Symbol(),MODE_DIGITS);
      if(Digit==1||Digit==2||Digit==3){PipPoint=Point*10;}
      if(Digit==1||Digit==2){PipPoint=Point*100;}
      if(Digit==4||Digit==5){PipPoint=Point*10;}
      return(PipPoint);
    Simplified
      return Digits % 2 = 0 ? Point : 10* Point;
  2.   double TPBuy=Ask+TakeProfit*Pips();
      double TPSell=Bid-TakeProfit*Pips();
      
      double SLBuy=Ask-StopLoss*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 to 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)

  3.   double FinalPrice=1.0/(Price1*Price2);
      
      if(FinalPrice)

    False is zero, non-zero is true. Always true.

  4.     int Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"Scalper8",MagicNumber,Expire,clrBlue);
        bool SelectBuy=OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES);
    

    A ticket is not a position index.

  5. int Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"Scalper8",MagicNumber,Expire,clrBlue);
        bool SelectBuy=OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES);
        if(Buy>0)
        {
         bool BuyMod=OrderModify(Buy,OrderOpenPrice(),SLBuy,TPBuy,0,clrNONE);

    There is no need to open an order and then set the stops. Simplify your code - do it in one step. TP/SL on OrderSend has been fine for years.
              Build 500 № 9 (2013)
              Is OrderSend() function now able to send ECN trades (with SL and TP) by itself? - MQL4 programming forum (2013)
              OP_BUY the same on Instant Execution and Market Execution? - MQL4 programming forum #2 (2014)
              Need help me mql4 guru add take profit to this EA - MQL4 programming forum #8 (2017)

  6. Scalper8: Below is my code, sell order won't open (if statement) is the same as buy. 
       if(CountSymbol(Symbol())<1)
       {
        int Buy=OrderSend(…
       }
       
       if(CountSymbol(Symbol())<1)
       {
        int Sell=OrderSend(…
    

    If the count is zero, you open a buy. The count is now one and you won't ever open a sell. Why does this confuse you?

 
William Roeder #:
  1. On a five digit broker (5 digits/3 on JPY), a PIP is 10 points. On a four digit broker, a PIP equals a point.
    This is MT4, why not use the Predefined Variables Digits?
    Simplified
  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 to 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)

  3. False is zero, non-zero is true. Always true.

  4. A ticket is not a position index.

  5. There is no need to open an order and then set the stops. Simplify your code - do it in one step. TP/SL on OrderSend has been fine for years.
              Build 500 № 9 (2013)
              Is OrderSend() function now able to send ECN trades (with SL and TP) by itself? - MQL4 programming forum (2013)
              OP_BUY the same on Instant Execution and Market Execution? - MQL4 programming forum #2 (2014)
              Need help me mql4 guru add take profit to this EA - MQL4 programming forum #8 (2017)

  6. If the count is zero, you open a buy. The count is now one and you won't ever open a sell. Why does this confuse you?

double FinalPrice=1.0/(Price1*Price2);
  
 if(FinalPrice)

I'm confused, is it incorrect? Should I change it?

int Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"Scalper8",MagicNumber,Expire,clrBlue);
    bool SelectBuy=OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES);
    if(Buy>0)
    {
     bool BuyMod=OrderModify(Buy,OrderOpenPrice(),SLBuy,TPBuy,0,clrNONE);

I'm using the following to avoid send error 130

 
William Roeder #:
  1. On a five digit broker (5 digits/3 on JPY), a PIP is 10 points. On a four digit broker, a PIP equals a point.
    This is MT4, why not use the Predefined Variables Digits?
    Simplified
  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 to 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)

  3. False is zero, non-zero is true. Always true.

  4. A ticket is not a position index.

  5. There is no need to open an order and then set the stops. Simplify your code - do it in one step. TP/SL on OrderSend has been fine for years.
              Build 500 № 9 (2013)
              Is OrderSend() function now able to send ECN trades (with SL and TP) by itself? - MQL4 programming forum (2013)
              OP_BUY the same on Instant Execution and Market Execution? - MQL4 programming forum #2 (2014)
              Need help me mql4 guru add take profit to this EA - MQL4 programming forum #8 (2017)

  6. If the count is zero, you open a buy. The count is now one and you won't ever open a sell. Why does this confuse you?

if(CountSymbol(Symbol())<1)
   {
    int Buy=OrderSend(…
   }
   
   if(CountSymbol(Symbol())<1)
   {
    int Sell=OrderSend(…

Thank you for the heads up minor mistakes..

if(FinalPrice)
  {
   if(CountSymbol(Symbol())<1)
   {
    int Buy=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"Scalper8",MagicNumber,Expire,clrBlue);
    bool SelectBuy=OrderSelect(Buy,SELECT_BY_POS,MODE_TRADES);
    if(Buy>0)
    {
     bool BuyMod=OrderModify(Buy,OrderOpenPrice(),SLBuy,TPBuy,0,clrNONE);
    }
   }
   
  if(FinalPrice)
  {
   if(CountSymbol(Symbol())<2)
   {
    int Sell=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"Scalper8",MagicNumber,Expire,clrRed);
    bool SelectSell=OrderSelect(Sell,SELECT_BY_POS,MODE_TRADES);
    if(Sell>0)
    {
     bool SellMod=OrderModify(Sell,OrderOpenPrice(),SLSell,TPSell,0,clrNONE);
    }
   }
  }
 }
Reason: