Need help with MQL4

Hello,

I have two scripts to buy or sell two orders with the same stop loss and different take profit. The only problem is my account has to comply with US FIFO rules and when the market is very volatile these two orders will not get filled because it violates these rules at the Stop Loss level. What happens is as the market moves fast up and down the second order might get filled a couple of pippets later, let’s say if I’m going short on EURUSD and if I put a stop of 20 pips on the sell script and I get filled with the first order at 1.14000 and filled at 1.13998 for my second order on the script it will violate because it will try to put a stop for the second order at 1.14198 because the first order is already at 1.14200 for the stop loss and the second order can’t close first than the second. So I was wondering if someone could help me fixing this problem by forcing the second order to have the same stop loss as the first order.

Code for the buy script:


#property show_inputs

#include <stderror.mqh>
#include <stdlib.mqh>

//parameternya ini, input-nya ini
extern double LOT =   0.30;
extern int    TP  =   20;
extern int    TP2 =   60;
extern int    SL  =   20;
//---------------------------
string        MySym    =     "";
int       MyDigits     =      5;
double    MyPoints     = 0.0001;


//+------------------------------------------------------------------+
//| Custom initialization function |
//+------------------------------------------------------------------+
int init()
  {
       MySym = Symbol();   
   MyPoints  = MarketInfo( MySym, MODE_POINT  );
   MyDigits  = MarketInfo( MySym, MODE_DIGITS );
   //---
        if( MyPoints == 0.001   ) { MyPoints = 0.01;   MyDigits = 3; }
   else if( MyPoints == 0.00001 ) { MyPoints = 0.0001; MyDigits = 5; }
   
   return(0);
  }
//+------------------------------------------------------------------+
// +
//+------------------------------------------------------------------+
int start()
{
    RefreshRates();
    while( IsTradeContextBusy() ) { Sleep(100); }
//----
     double Stop  = NormalizeDouble(Ask - (SL  * MyPoints),MyDigits);
     double Prof1 = NormalizeDouble(Ask + (TP  * MyPoints),MyDigits);
     double Prof2 = NormalizeDouble(Ask + (TP2 * MyPoints),MyDigits);
      
     int ticket = OrderSend(MySym,OP_BUY,LOT,Ask,NormalizeDouble(3 * MyPoints,MyDigits),Stop,Prof1,"contest",1,0,CLR_NONE);
                  OrderSend(MySym,OP_BUY,LOT,Ask,NormalizeDouble(3 * MyPoints,MyDigits),Stop,Prof2,"contest",2,0,CLR_NONE);
         if(ticket<1)
            {
             int error=GetLastError();
             Print("Error = ",ErrorDescription(error));
             return;
            }
//----
   OrderPrint();
return(0);
}

Code for the sell script:


#property show_inputs

#include <stderror.mqh>
#include <stdlib.mqh>

//parameternya ini, input-nya ini
extern double LOT = 0.30;
extern int    TP  =   20;
extern int    TP2 =   60;
extern int    SL  =   20;
//---------------------------
string        MySym    =     "";
int       MyDigits     =      5;
double    MyPoints     = 0.0001;


//+------------------------------------------------------------------+
//| Custom initialization function |
//+------------------------------------------------------------------+
int init()
  {
       MySym = Symbol();   
   MyPoints  = MarketInfo( MySym, MODE_POINT  );
   MyDigits  = MarketInfo( MySym, MODE_DIGITS );
   //---
        if( MyPoints == 0.001   ) { MyPoints = 0.01;   MyDigits = 3; }
   else if( MyPoints == 0.00001 ) { MyPoints = 0.0001; MyDigits = 5; }
   
   return(0);
  }
//+------------------------------------------------------------------+
// +
//+------------------------------------------------------------------+
int start()
{
    RefreshRates();
    while( IsTradeContextBusy() ) { Sleep(100); }
//----
     double Stop  = NormalizeDouble(Bid + (SL  * MyPoints),MyDigits);
     double Prof1 = NormalizeDouble(Bid - (TP  * MyPoints),MyDigits);
     double Prof2 = NormalizeDouble(Bid - (TP2 * MyPoints),MyDigits);
      
     int ticket = OrderSend(MySym,OP_SELL,LOT,Bid,NormalizeDouble(3 * MyPoints,MyDigits),Stop,Prof1,"contest",1,0,CLR_NONE);
                  OrderSend(MySym,OP_SELL,LOT,Bid,NormalizeDouble(3 * MyPoints,MyDigits),Stop,Prof2,"contest",2,0,CLR_NONE);
         if(ticket<1)
            {
             int error=GetLastError();
             Print("Error = ",ErrorDescription(error));
             return;
            }
//----
   OrderPrint();
return(0);
}

Thanks

Did you test it? Because your stop is calculated as the same value “Stop” and it should take this value and not something else. I mean that SL is calculated by you and not by the server so you sent the same Stop to both orders.

Should "int ticket = " also not be in front of the second order?
or ticket2 and checked for error?

Do you have some kind of error report?

TC

An other option is to sent the second order with a smaller SL and change it after both orders are filled.

TC

On the script I only have one option to put a stop which is supposed to be same for both orders, the second order would have to have a bigger stop to make sure that the sl for the second order would be above the first order’s sl in case you were selling and under in case you were buying.

It will put the S/L there but when it gets to it you get this:

Your trade could not be closed because you have an older trade on the same currency pair. To comply with NFA FIFO regulations, you must first close your older trade. For more information please refer to: MetaTrader 4 - Frequently Asked Questions | OANDA fxTrade

Sorry for not mentioning that I am not a programmer, someone had written this code for me in the past and I was hoping for someone to change the code so as to behave the way it doesn’t violate fifo rules.

Making the SL for the second order a little bit bigger or smaller should solve the problem.

I will try to change the code.


For this result :
I added this line:
double Stop2 = NormalizeDouble(Ask - ((SL+1) * MyPoints),MyDigits);
and changed this line:
int ticket2 = OrderSend(MySym,OP_BUY,LOT,Ask,NormalizeDouble(3 * MyPoints,MyDigits),Stop2,Prof2,“contest”,2,0,CLR_NONE);

The code was not running without problems Things are missing or different platform


After fooling around with your new code for a little bit I was able to make it work without errors or warnings.

Thanks for the help :53:


#property show_inputs

#include <stderror.mqh>
#include <stdlib.mqh>

//parameternya ini, input-nya ini
extern double LOT =   0.10;
extern int    TP  =   14;
extern int    TP2 =   38;
extern int    SL  =   12;
//---------------------------
string        MySym    =     "";
int       MyDigits     =      5;
double    MyPoints     = 0.0001;


//+------------------------------------------------------------------+
//| Custom initialization function |
//+------------------------------------------------------------------+
int init()
  {
       MySym = Symbol();   
   MyPoints  = MarketInfo( MySym, MODE_POINT  );
   MyDigits  = MarketInfo( MySym, MODE_DIGITS );
   //---
        if( MyPoints == 0.001   ) { MyPoints = 0.01;   MyDigits = 3; }
   else if( MyPoints == 0.00001 ) { MyPoints = 0.0001; MyDigits = 5; }
   
   return(0);
  }
//+------------------------------------------------------------------+
// +
//+------------------------------------------------------------------+
int start()
{
    RefreshRates();
    while( IsTradeContextBusy() ) { Sleep(100); }
//----
     double Stop  = NormalizeDouble(Ask - (SL  * MyPoints),MyDigits);
     double Stop2 = NormalizeDouble(Ask - ((SL+1) * MyPoints),MyDigits);
     double Prof1 = NormalizeDouble(Ask + (TP  * MyPoints),MyDigits);
     double Prof2 = NormalizeDouble(Ask + (TP2 * MyPoints),MyDigits);
      
     int ticket = OrderSend(MySym,OP_BUY,LOT,Ask,NormalizeDouble(3 * MyPoints,MyDigits),Stop,Prof1,"contest",1,0,CLR_NONE);
     int ticket2 = OrderSend(MySym,OP_BUY,LOT,Ask,NormalizeDouble(3 * MyPoints,MyDigits),Stop2,Prof2,"contest",2,0,CLR_NONE);
         if(ticket<1)
            {
             int error=GetLastError();
             Print("Error = ",ErrorDescription(error));
             
            }

        
//----
   OrderPrint();
return(0);
}

Hi,
I am glad to hear you get it working. Most Errors that I got where from my platform and not copy the code correct.

You can make the difference between the SL even smaller then 1 pip like 0.1 pip

TC