Please Help for a Great EA ...for scalping .

 
I have a large EA that works wonderfully on EURUSD 1 minute in virtual, in Mql4.
My problem is that as soon as I get into reality, he loses money.
I turned in all directions and I cannot correct the error.
I think it should be fairly easy to fix.

Thank you in advance for your help.

//+------------------------------------------------------------------+
//|                                     eaxtwoependingaorders .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
extern int MagicNumber=20100807; //magic
extern string Name_EA = "eaxtwoependingaorders V2" ;
extern double LotFactor =50; //lotsize factor
double lot;
extern int TakeProfit = 120;
extern int StopLoss   = 0;
extern  int ecartask=20;
extern  int ecartbid=0;
int last_bar  = 0;
extern bool trail=true;
extern double TrailingStop=0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{ 
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
    new_del () ;
   if (last_bar == Bars) return(0);
   last_bar = Bars;
   if ( OrdersTotal () == 0 ) 
   { 
   int buystop =  OrderSend(Symbol(),OP_BUYSTOP, (AccountEquity() * 0.01 /LotFactor),Ask+ecartask*Point,5,(Ask+ecartask*Point)-StopLoss*Point,(Ask+ecartask*Point)+TakeProfit*Point,"eaxtwoependingaorders V2",MagicNumber,0,clrBlue);
   int sellstop = OrderSend(Symbol(),OP_SELLSTOP, (AccountEquity() * 0.01 /LotFactor),Bid-ecartbid*Point,5,(Bid-ecartbid*Point)+StopLoss*Point,(Bid-ecartbid*Point)-TakeProfit*Point,"eaxtwoependingaorders V2",MagicNumber,0,clrBlue);
   } 
   return ( 0 );
}
//-------------------- ( to close pending order) ---------------
int new_del()
{
      if(trail) trail();
int i,a;
int total = OrdersTotal();
string comentario,par;
for (i=total-1; i >=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)
{
for (a=total-1; a >=0; a--)
{
if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))

if(OrderType()==OP_SELLSTOP)
{
 bool modify1= OrderDelete(OrderTicket());
Print("Deleting SELL_STOP"," Ordertype:",OrderType());
return(1);
}
if(OrderType()==OP_BUYSTOP)
{
 bool modify2= OrderDelete(OrderTicket());
Print("Deleting BUY_STOP"," Ordertype:",OrderType());
return(1);
}
}
}
}
   return ( 0 );
}
//+------------------------------------------------------------------+
//| Trailing Stoploss après Breakeven                            |
//+------------------------------------------------------------------+
void trail()
  {
for (int i = OrdersTotal()-1; i >= 0; i --)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY) 

    {
               if(Bid - OrderOpenPrice() > TrailingStop *10* MarketInfo(OrderSymbol(),MODE_POINT)) 
                 {
             
                  if(((OrderStopLoss() < Bid - TrailingStop *10* MarketInfo(OrderSymbol(), MODE_POINT))&& ((Bid-OrderOpenPrice())>(Point*TrailingStop)))|| 
                     (OrderStopLoss()==0))                                                              
                    {
                      bool modify1=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),clrGreenYellow);                    
                    }
                 }
                 } else if(OrderType()==OP_SELL) {
               if(OrderOpenPrice()-Ask>TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT)) 
                 {
              
                  if(((OrderStopLoss()>Ask+TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT))&& ((OrderOpenPrice()-Ask)>(Point*TrailingStop)))|| 
                     (OrderStopLoss()==0)) 
                    {
                     bool modify2=OrderModify(OrderTicket(),OrderOpenPrice(),
                                              Ask+TrailingStop*10*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),clrGreenYellow);
                    }
                 }
              }
           }
     }
  } 



 

So you want help to converting a looser EA  toa winner in the real account?

Sorry I lost my magic wand.

if it works on the demo it will work also real account.

if not it is because dynamic spread 

 
7413276: I turned in all directions and I cannot correct the error.I think it should be fairly easy to fix.
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  3. You don't know how to code, but the changes are easy? That's like saying "I don't know how to build engines but adding another cylinder should be easy; it's just one more."
              Is it easy (or difficult) to …? - General - MQL5 programming forum #2 2017.08.31

  4. Rerun your test and this time use a realistic spread.

  5.     if (last_bar == Bars) return (0);
    
       last_bar = Bars; 

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

  6. int total = OrdersTotal ();
    verser (i = total-1; i> = 0; i--){
       if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES))
       if (OrderType () == OP_BUYSTOP || OrderType () == OP_SELLSTOP) 
    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              PositionClose is not working - MQL5 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles

  7. OrderSend (Symbol (), OP_BUYSTOP, (AccountEquity () * 0.01 / LotFactor), Ask + ecartask * Point, 5, (Ask + ecartask * Point) -StopLoss * Point, (Ask + ecartask * Point) + TakeProfit * Point, "eaxtwoependingaorders V2", MagicNumber, 0, clrBlue); 
    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.

  8. Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.
    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
                Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
 

Thank you very much for your quick responses.

I will take your comments into account

Gerard