HELP: zigzag indicator based martingale

Back to topics list To post a new topic, please log in or register
avatar
4
Constantino 2015.07.23 22:31
 

I never seen an EA trade like this so forgive me if this has already been made

 

I'm planning a ea based on the zigzag indicator, which opens a sell + closes buys after the last highest position and opens a buy + closes salls at lowest. If there is a new lowest after the first, it will open a new buy with double the size of the first, and so on.

 

I have a rough idea on how to code this but I can't find any specific guides for everything I need. Something like keeping track of open orders and using Basic Lot Size*2^(Number of Open Orders) to determine the size of the next order to be opened. If anyone could help I'd greatly appreciate.

 

Thanks 

avatar
4
Constantino 2015.07.24 00:21 #
 

So far I managed to come up with this code

 

//+------------------------------------------------------------------+

//|                                                ZigZag Hedger.mq4 |

//|                                              Rodrigo Constantino |

//|                                                             1.0v |

//+------------------------------------------------------------------+


//+------------------------------------------------------------------+

//| Defining variables                                               |

//+------------------------------------------------------------------+

extern int MAGICNUMBER              =314159265;

extern double BaseLotSize           =0.01;  //starting lot size

extern double Multiplier            =2;     //multipliyng factor for lotsize

extern int Depth=12;

extern int lotdigits=2;

extern double Slippage=0;

int ThisBarTrade        =      0;

int Position            =      0;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+


//---Defining Lot Size

double lots()


  {

   double minlot = MarketInfo(Symbol(), MODE_MINLOT);

   double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);

   double leverage= AccountLeverage();

   double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE);

   double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);


   double MinLots=0.01; double MaximalLots=50.0;


   double lots=NormalizeDouble(BaseLotSize*MathPow(Multiplier,OrdersTotal()),2);

   if(lots<minlot) lots=minlot;

   if(lots>maxlot) lots=maxlot;

   if(AccountFreeMargin()<Ask*lots*lotsize/leverage)

     {

      Print("We have no money. Lots = ",lots," , Free Margin = ",AccountFreeMargin());

      Comment("We have no money. Lots = ",lots," , Free Margin = ",AccountFreeMargin());

     }

   return(lots);

  }//def ends


//---Start

int start()

  {

   bool buy=false;

   bool sell=false;

   if(ThisBarTrade!=Bars)

     {

      ThisBarTrade=Bars;

      //---Buy condition

      int H;

      int h;

      H=iHighest(NULL,0,MODE_HIGH,Depth,0);

      h=iHigh(NULL,0,0);

      if(H>h)

        {

         buy=true;

        }

      //---Sell condition

      int L;

      int l;

      L=iLowest(NULL,0,MODE_HIGH,Depth,0);

      l=iLow(NULL,0,0);

        if(L<l)

        {

         sell=true;

        }

      //---

     }


//---opening buy and closing sells

   int res=0;

   if(buy=true)

     {

      res=OrderSend(Symbol(),OP_BUY,lots(),Ask,Slippage,0,0," ",MAGICNUMBER,0,Blue);

      for(int cnt=0;cnt<OrdersTotal();cnt++)

      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_SELL && 

         OrderSymbol()==Symbol() && 

         OrderMagicNumber()==MAGICNUMBER

         )

        {OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);}

     }

     

     //---opening sell and closing buys

     if(sell=true)

     {

      res=OrderSend(Symbol(),OP_SELL,lots(),Ask,Slippage,0,0," ",MAGICNUMBER,0,Blue);

      for(int cnt=0;cnt<OrdersTotal();cnt++)

      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_BUY && 

         OrderSymbol()==Symbol() && 

         OrderMagicNumber()==MAGICNUMBER

         )

        {OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);}

     }

     return(0);

     }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

   void OnDeinit(const int reason)

     {

      //---


     }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

   void OnTick()

     {

      //---


     }

//+------------------------------------------------------------------+


 

avatar
250
ffoorr 2015.07.24 13:16 #
 

This is not a ZigZag indicator


How will you know, that this is the last higher ZigZag, you can never know until you get the next higher or lower


 if( iCustom(NULL,0,"ZigZAg", ext1,ext2,ext3,0,1) = Low[1] )  

this is the way to get the last zigzag low, but maybe there will be a new low instead


You can store the lot size and the loss or profit of the last trade into an array, and before each new trade go and see what was the previous one


double last_lot[5];

double last_profit[5];

if( open_new_order ) last_lott = last_lot[0] ; new_lot = ?? to calculate

 

ticket =  OrderSend(..new_lot ..)  
  {
   if( ticket >0 )  last_lot[0] = new_lot ;


avatar
4
Constantino 2015.07.25 08:43 #
 

Thanks for the light brother. Go it running but it won't open any seel order: it keeps opening buy orders whenever buy or sell conditions are reached. could you take a look at it?

 

//+------------------------------------------------------------------+

//|                                                ZigZag Hedger.mq4 |

//|                                              Rodrigo Constantino |

//|                                                             1.0v |

//|Works safe on GBPJPY H1

//+------------------------------------------------------------------+


//+------------------------------------------------------------------+

//| Defining variables                                               |

//+------------------------------------------------------------------+

extern int MAGICNUMBER              =314159265;

extern double BaseLotSize           =0.01;  //starting lot size

extern double Multiplier            =   2;  //multipliyng factor for lotsize

extern int Depth                    =  12;

extern int Deviation=5;  // Deviation

extern int Backstep=3;   // Backstep

extern double maxlot=5;

extern int lotdigits                =   2;

extern double Slippage              =   0;

int ThisBarTrade                    =   0;

int Position                        =   0;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+


//---Defining Lot Size

double lots()


  {

   double minlot=MarketInfo(Symbol(),MODE_MINLOT);


   double leverage= AccountLeverage();

   double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE);

   double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);


   double MinLots=0.01; double MaximalLots=50.0;


   double lots=NormalizeDouble(BaseLotSize*MathPow(Multiplier,OrdersTotal()),2);

   if(lots<minlot) lots=minlot;

   if(lots>maxlot) lots=maxlot;

   if(AccountFreeMargin()<Ask*lots*lotsize/leverage)

     {

      Print("We have no money. Lots = ",lots," , Free Margin = ",AccountFreeMargin());

      Comment("We have no money. Lots = ",lots," , Free Margin = ",AccountFreeMargin());

     }

   return(lots);

  }//def ends


//---Start

int start()

  {

   bool buy=false;

   bool sell=false;

   if(ThisBarTrade!=Bars)

     {

      ThisBarTrade=Bars;


      //---opening buy 

      int res=0;

      if(iCustom(NULL,0,"ZigZAg",Depth,Deviation,Backstep,0,1)==Low[1])

        {

         res=OrderSend(Symbol(),OP_BUY,lots(),Ask,Slippage,0,0," ",MAGICNUMBER,0,Blue);

         return(0);

        }


      //---opening sell and closing buys

      /*if(iCustom(NULL,0,"ZigZAg",Depth,Deviation,Backstep,0,1)==High[1])

        {

         res=OrderSend(Symbol(),OP_SELL,lots(),Bid,Slippage,0,0," ",MAGICNUMBER,0,Blue);

         return(0);

        }*/

     }

   for(int cnt=0;cnt<OrdersTotal();cnt++)

     {

      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

      if(OrderSymbol()==Symbol() && 

         OrderMagicNumber()==MAGICNUMBER

         )

        {

         if(OrderType()==OP_BUY)

           {

            if(iCustom(NULL,0,"ZigZAg",Depth,Deviation,Backstep,0,1)==High[1]/* && Bid > OrderOpenPrice()*/) 

              {

               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

              }

              }

            if(OrderType()==OP_SELL)

              {

               if(iCustom(NULL,0,"ZigZAg",Depth,Deviation,Backstep,0,1)==Low[1]/* && Ask < OrderOpenPrice()*/) 

                 {

                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);

                 }

                 }

}}

               return(0);

              }

//+------------------------------------------------------------------+

 

avatar
Moderator
3416
GumRai 2015.07.25 09:16 #
 
Looks to me that you have commented out the code that places sells with /*  */
avatar
250
ffoorr 2015.07.25 12:05 #
 

to make it simpler, you will have to create a fonction to count order, count buy order, sell order, to close order at the same moment you get the signal

   if(iCustom(NULL,0,"ZigZAg",Depth,Deviation,Backstep,0,1)==Low[1])
      {
      if( nb_buy > 0 ) close_buy();// or OrderClose(ticket_buy );
      ticket  = ordersend(...OP_Sell... ) ;
      }


to be read at the beginning

 int count_order()
 { 
  static int nb_buy=0; static int nb_sell = 0;

   nb_buy =0; nb_sell =0;

  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      if( ! OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) ) continue;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICNUMBER )
         {
         if(OrderType()==OP_BUY) nb_buy++;
         if(OrderType()==OP_SELL) nb_sell++;
     }}
 return(0);
}


avatar
4
Constantino 2015.07.25 23:18 #
 

Thanks once againg for the help. I've managed to opne both sell and open orders. HOwever I can't close them now, if I use the count orders function. If i remove that, it will only close one order at a time. How can I close all sell or buy orders at once when the conditions are met?

 

//+------------------------------------------------------------------+

//|                                                ZigZag Hedger.mq4 |

//|                                              Rodrigo Constantino |

//|                                                             1.0v |

//|Works safe on GBPJPY H1

//+------------------------------------------------------------------+


//+------------------------------------------------------------------+

//| Defining variables                                               |

//+------------------------------------------------------------------+

extern int MAGICNUMBER              =314159265;

extern double BaseLotSize           =0.01;  //starting lot size

extern double Multiplier            =   2;  //multipliyng factor for lotsize

extern int Depth                    =  12;

extern int Deviation                =   5;

extern int Backstep                 =   3;

extern double maxlot                =   5;

extern int lotdigits                =   2;

extern double Slippage              =   0;

int ThisBarTrade                    =   0;

int Position                        =   0;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+


//---Defining Lot Size

double lots()


  {

   double minlot=MarketInfo(Symbol(),MODE_MINLOT);


   double leverage= AccountLeverage();

   double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE);

   double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);


   double MinLots=0.01; double MaximalLots=50.0;


   double lots=NormalizeDouble(BaseLotSize*MathPow(Multiplier,OrdersTotal()),2);

   if(lots<minlot) lots=minlot;

   if(lots>maxlot) lots=maxlot;

   if(AccountFreeMargin()<Ask*lots*lotsize/leverage)

     {

      Print("We have no money. Lots = ",lots," , Free Margin = ",AccountFreeMargin());

      Comment("We have no money. Lots = ",lots," , Free Margin = ",AccountFreeMargin());

     }

   return(lots);

  }//def ends

//---count open orders

int count_order()

  {

   static int nb_buy=0; static int nb_sell=0;


   nb_buy=0; nb_sell=0;


   for(int cnt=0;cnt<OrdersTotal();cnt++)

     {

      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) continue;

      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICNUMBER)

        {

         if(OrderType()==OP_BUY) nb_buy++;

         if(OrderType()==OP_SELL) nb_sell++;

        }

     }

   return(0);

  }

//---Start

int start()

  {

   if(ThisBarTrade!=Bars)

     {

      ThisBarTrade=Bars;


      //---opening buy 

      int ticket=0;

      int nb_sell;

      int nb_buy;

      int cnt;

      if(iCustom(NULL,0,"ZigZAg",Depth,Deviation,Backstep,0,1)==Low[1])

        {

         if(nb_sell>0)

           {

            OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

            if(OrderSymbol()==Symbol() && 

               OrderMagicNumber()==MAGICNUMBER && 

               OrderType()==OP_SELL) 

               {

               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Green);

           }

           }

         ticket=OrderSend(Symbol(),OP_BUY,lots(),Ask,Slippage,0,0," ",MAGICNUMBER,0,Blue);

         return(0);

        }

      //---opening sell and closing buy

      if(iCustom(NULL,0,"ZigZAg",Depth,Deviation,Backstep,0,1)==High[1])

        {

         if(nb_buy>0)

           {

            OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

            if(OrderSymbol()==Symbol() && 

               OrderMagicNumber()==MAGICNUMBER && 

               OrderType()==OP_BUY) 

               {

               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Green);

               }

           }

         ticket=OrderSend(Symbol(),OP_SELL,lots(),Bid,Slippage,0,0," ",MAGICNUMBER,0,Red);

         return(0);

        }


     }


   return(0);

  }

//+------------------------------------------------------------------+

 

avatar
Moderator
3416
GumRai 2015.07.25 23:53 #
 

In your function you have

   static int nb_buy=0; static int nb_sell=0;



   nb_buy=0; nb_sell=0;

 Your function does nothing useful as it returns 0.

If you want to use the variable values in your main code, declare nb_buy and nb_sell globally

In you main code, they are not given any value.

Create a loop to close the orders, at the moment, it will only close the order at position cnt and cnt is declared, but not given a value

avatar
250
ffoorr 2015.07.26 07:55 #
 

It should not compile : nb_buy not declared

  static int nb_buy=0; static int nb_sell=0;

to be declared at global scope, not inside the fonction.

check compiler errors :


 count_order();

if(nb_buy>0)
     {
     for(int cnt=0;cnt<OrdersTotal();cnt++)
         {
         if(! OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) ) continue;
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICNUMBER && OrderType()==OP_BUY) 
               {
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Green);
               }
Back to topics list  

To add comments, please log in or register