# HELP: zigzag indicator based martingale

21

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

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 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;

{

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 sell=false;

{

int H;

int h;

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

h=iHigh(NULL,0,0);

if(H>h)

{

}

//---Sell condition

int L;

int l;

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

l=iLow(NULL,0,0);

if(L<l)

{

sell=true;

}

//---

}

int res=0;

{

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

if(OrderType()==OP_SELL &&

OrderSymbol()==Symbol() &&

OrderMagicNumber()==MAGICNUMBER

)

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

}

if(sell=true)

{

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

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()

{

//---

}

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

1052

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 ;```
21

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 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;

{

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 sell=false;

{

int res=0;

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

{

return(0);

}

/*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++)

{

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

OrderMagicNumber()==MAGICNUMBER

)

{

{

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

{

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

}

}

if(OrderType()==OP_SELL)

{

{

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

}

}

}}

return(0);

}

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

Moderator
10065

Looks to me that you have commented out the code that places sells with /*  */
1052

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])
{
ticket  = ordersend(...OP_Sell... ) ;
}

```

to be read at the beginning

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

for(int cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICNUMBER )
{
if(OrderType()==OP_SELL) nb_sell++;
}}
return(0);
}```
21

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 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;

{

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;

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

{

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

{

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

}

}

return(0);

}

//---Start

int start()

{

{

int ticket=0;

int nb_sell;

int cnt;

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

{

if(nb_sell>0)

{

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

OrderMagicNumber()==MAGICNUMBER &&

OrderType()==OP_SELL)

{

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

}

}

return(0);

}

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

{

{

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

OrderMagicNumber()==MAGICNUMBER &&

{

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

}

}

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

return(0);

}

}

return(0);

}

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

Moderator
10065

```   static int nb_buy=0; static int 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

1052

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();