Blockade of placing further orders on the same currency pair

 

Hello.

I am asking for advice. What's wrong here. How to block the submission of the second and the next order on the same currency pair. Below is the code:

//+------------------------------------------------------------------+
//|                                              Sprzedaj USDJPY.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
void OnTick()
 {
      string spread="";
      int    intMarketSpreadPoints = MarketInfo( _Symbol, MODE_SPREAD );                            
      double dblMarketSpreadPrice  = intMarketSpreadPoints * _Point; 
      if (dblMarketSpreadPrice==0.060)
      {  
      spread="buy";
      }
      string trade="";
      int Orders=OrdersTotal();
      for(int pos=0;pos<Orders;pos++)
      {
      if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
            {
            if OrderSymbol()==USDJPY.;
            {
            trade="buy";
            }
      }}
      if (trade=="buy" && spread=="buy")
            {
            //--- get minimum stop level
            double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
            Print("Minimum Stop Level=",minstoplevel," points");
            double price=Ask;
            //--- calculated SL and TP prices must be normalized
            //double stoploss=Ask-75*Point;
            double stoploss=NormalizeDouble(Ask-30*Point,Digits);
            double takeprofit=NormalizeDouble(Ask+30*Point,Digits);
            // double takeprofit=Ask+75*Point;
            //--- place market order to buy 1 lot
            int ticket=OrderSend(Symbol(),OP_BUY,0.1,price,5,stoploss,takeprofit,"My Order",16384,0,clrGreen);
             }
          }
   
 
  1. CatDog: What's wrong here.
                if OrderSymbol()==USDJPY.;

    You posted code that does not compile.

  2.       if (dblMarketSpreadPrice==0.060)
    

    Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 2013.06.07

  3.          double price=Ask;
                double stoploss=NormalizeDouble(Ask-30*Point,Digits);
                double takeprofit=NormalizeDouble(Ask+30*Point,Digits);

    You buy at the Ask and sell at the Bid.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the 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 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 spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

 

The code compiles and works in my opinion. The compiler screams that I lost the variable. But the code works. I will tell you everything from the beginning. I wrote code number 1:

//+------------------------------------------------------------------+
//|                                                         Luka.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- 
int    intMarketSpreadPoints = MarketInfo( _Symbol, MODE_SPREAD ); // e.g. 15 points
double dblMarketSpreadPrice  = intMarketSpreadPoints * _Point;     // e.g. 0.00015 price delta
double dblCurrentSpreadPrice = Ask - Bid;
  
      Comment("Działa","\n",intMarketSpreadPoints,"\n",dblMarketSpreadPrice,"\n",dblCurrentSpreadPrice);                            
      if (Open[2]<Close[2] && Open[2]>Open[1])
      {  
      Comment(" /n Sell!!!");
      }
      if (Open[2]>Close[2] && Open[2]<Open[1])
      {
      Comment (" /n Buy!!!");
      }
      
  }

I wanted to turn on code 1 on Mondays on D1. On pairs where code 1 shows buy or sell, I wanted to turn on code 2 (for example, the one at the beginning).

There is a bug in my code (number 2). There should be:

//+------------------------------------------------------------------+
//|                                              Sprzedaj USDJPY.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
void OnTick()
 {
      string spread="";
      int    intMarketSpreadPoints = MarketInfo( _Symbol, MODE_SPREAD );                            
      double dblMarketSpreadPrice  = intMarketSpreadPoints * _Point; 
      if (dblMarketSpreadPrice==0.060)
      {  
      spread="buy";
      }
      string trade="";
      for(int i = OrdersTotal() - 1; i >= 0; i--)
      {
      if(!OrderSelect(i, SELECT_BY_POS))
         break;
      if(OrderSymbol()!=Symbol())
        {
        trade="buy";
        }
        }
      if (trade=="buy" && spread=="buy")
            {
            //--- get minimum stop level
            double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
            Print("Minimum Stop Level=",minstoplevel," points");
            double price=Ask;
            //--- calculated SL and TP prices must be normalized
            //double stoploss=Ask-75*Point;
            double stoploss=NormalizeDouble(Ask-180*Point,Digits);
            double takeprofit=NormalizeDouble(Ask+180*Point,Digits);
            // double takeprofit=Ask+75*Point;
            //--- place market order to buy 1 lot
            int ticket=OrderSend(Symbol(),OP_BUY,0.1,price,5,stoploss,takeprofit,"My Order",16384,0,clrGreen);
             }
          }
   

I know the code:

 string trade="";                              
      int Orders=OrdersTotal();
      if (Orders==0)
      {  
      trade="buy";
      }

It blocks any subsequent transaction. My point is that the code should block the transaction only on a given currency pair. I do not know how to do this. Please give me a suggestion.

 
CatDog:
...

It blocks any subsequent transaction. My point is that the code should block the transaction only on a given currency pair. I do not know how to do this. Please give me a suggestion.

You count the open positions (orders) only for the given currency pair.

int MyOrdersTotal()
  {
   int total=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS))
         continue;
      if(OrderSymbol()!=_Symbol)
         continue;
      if(OrderMagic()!=MyMagic)
         continue;
      if(OrderType()==OP_BUY || OrderType()==OP_SELL)
         total++;
     }
   return total;
  }

And you need to take care that only orders of your EA are counted, you do this by checking the magic number.