Ask Anti Martingale Script

 

Hi,

I want to make an EA with a strategy like the following.

Lets say I have a sell order. If the price then drops by xxx pips, it will open another order like averaging. And if the price continues to fall it will open the next order. Etc.


I made the following script, is there something wrong, why it open multiple sell order at the same time?

Maybe some one here can help me

if(TotalOrdersCountSell()>=1)

   {
      GoAverage();
           
   }  

void GoAverage()
{
         int      iCount      =  0;
         double   LastOP      =  0;
         double   LastLots    =  0;
         bool     LastIsSellPir = FALSE;
         int      iTotalBuyPir   =  0;
         int      iTotalSellPir  =  0;
         int      Spread      =  0;
         int      Level       =  0;
   
         Spread= MarketInfo(Symbol(), MODE_SPREAD);
                                 
         for(iCount=0;iCount<OrdersTotal();iCount++)
         {
                    
            OrderSelect(iCount,SELECT_BY_POS,MODE_TRADES);
            
            if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==MagicNumberSell)
           {
               if(LastOP==0) {LastOP=OrderOpenPrice();}
               if(LastOP>OrderOpenPrice()) {LastOP=OrderOpenPrice();}         
               if(LastLots<OrderLots()) {LastLots=OrderLots();}
               LastIsSellPir=TRUE;
               iTotalSellPir++;
               
               if(iTotalSellPir==MaxTrade)
               return;
           }
      
        }      
         
         /* If Price DOWNTREND...., Ceck Bid (*/

         if(LastIsSellPir)
         {
             if(Bid<=LastOP-(Spread*Point)-(PipStepAveraging*SetPoint))
            {
               OrderSend(Symbol(), OP_SELL,NormalizeDouble(LastLots,2), Bid, Slippage,NormalizeDouble((Bid+StopLoss*SetPoint),Digits),0, EAComment, MagicNumberSell);  //AVERAGING MODE
               LastIsSellPir=FALSE;
               return;
            }
              
         } 
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
Trade Orders in DOM - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Agus Wahyu Pratomo:

I stopped at what could be your first problem:

if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==MagicNumberSell)

Don't depend on OrderComment at all. Some brokers don't allow it or override it altogether.

Remove that condition and try again.
 
Alexander Martinez #:

I stopped at what could be your first problem:

Don't depend on OrderComment at all. Some brokers don't allow it or override it altogether.

Remove that condition and try again.

I tried your suggestion. Still doesnt work

 

But When im doing martingale script. First we buy then if price goes down it will open order buy again.

It work with same script .

void GoMartingale()
{
         int      iCount      =  0;
         double   LastOP      =  0;
         double   LastLots    =  0;
         bool     LastIsBuy   =  FALSE;
         int      iTotalBuy   =  0;
         int      iTotalSell  =  0;
         int      Spread      =  0;
         int      Level       =  0;
   
         Spread= MarketInfo(Symbol(), MODE_SPREAD);
         
         
                
         for(iCount=0;iCount<OrdersTotal();iCount++)
         {
                    
           OrderSelect(iCount,SELECT_BY_POS,MODE_TRADES);
           
           if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderComment()==EAComment && OrderMagicNumber()==MagicNumberBuy)
           {
               if(LastOP==0) {LastOP=OrderOpenPrice();}
               if(LastOP>OrderOpenPrice()) {LastOP=OrderOpenPrice();}
               if(LastLots<OrderLots()) {LastLots=OrderLots();}
               LastIsBuy=TRUE;
               iTotalBuy++;
               
               if(iTotalBuy==MaxTrade) 
               return;
           }
         }

         
         /* If Price DOWNTREND...., Check Bid (*/
        if(LastIsBuy)
         {
            if(Bid<=LastOP-(Spread*Point)-(PipStepMarti*SetPoint))
            {
               OrderSend(Symbol(), OP_BUY,NormalizeDouble((Multiply*LastLots),2), Ask, Slippage,0,0, EAComment, MagicNumberBuy);   
               //ModifyTP();
               LastIsBuy=FALSE;
               return;
            }
            
         }