How to code? - page 197

 

Can anyone to help coding this EA?

Hello,

I'm currently trading with a martingale EA which will open max 4 levels of orders. I just think of a hedging strategy that may reduce the risk of using this EA. Below is a description of my strategy:

1. The hedging EA will monitor the drawdown of the opening orders. If the total floating losses is greater than a pre-determined amount, it will trigger the EA to open a hedged order in opposite to the opening orders.

2. The lot size of the hedged order is calculated based on a multiple (user can set the multiple) of the lot size of the last opening order (e.g. if the lot size of the last level order is 1.6 and the multiple is set at 2, then the hedged order will be open with lot size at 3.2.

3. When the hedged order returned to its opening level, the hedged order will be closed automatically.

4. When the overall basket (the opening orders + the hedged order) reaches a pre-determined net profit amount, all orders will be closed.

I wonder if some good programmer can code this hedging EA for me. I think it will be very useful for other martingale EA as well. Thanks a lot!

Best regards,

Wallace

 

whoa..

thanks guys..

how about buy stop and sell stop?

just change "BUY" over there?

just want 2 positions only for 1 pairs, not more than that..

sorry, still newbie..

 

How to code this indis into EA ?

Hi, i'm a new learner and interested to learn mql, can someone tell me how to code this into EA ?

extern int Fast.MA.Period = 5;

extern int Slow.MA.Period = 34;

extern int Signal.period = 5;

//---- buffers

double Buffer1[],

Buffer2[],

b2[],

b3[];

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

// two additional buffers used for counting

IndicatorBuffers(4);

IndicatorShortName("MA_5_34");

SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,3);

SetIndexArrow(0,242); // down 226 234 242

SetIndexBuffer(0,b2);

SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,3);

SetIndexArrow(1,241); //UP 225 233 241

SetIndexBuffer(1,b3);

// These buffers are not plotted, just used to determine arrows

SetIndexBuffer (2,Buffer1);

SetIndexBuffer (3,Buffer2);

//----

return(0);

}

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

//| Custor indicator deinitialization function |

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

int deinit()

{

//----

return(0);

}

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

//| Custom indicator iteration function |

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

int start()

{

int i, counted_bars=IndicatorCounted();

double MA5,MA34;

int limit=Bars-counted_bars;

Print(" print limit = ", limit);

if(counted_bars>0) limit++; <----can someone explain to me what it means ?

// Main line

for(i=0; i<limit; i++) <----can someone explain to me what it means ?

{

MA5=iMA(NULL,0,Fast.MA.Period,0,MODE_SMA,PRICE_MEDIAN,i);

MA34=iMA(NULL,0,Slow.MA.Period,0,MODE_SMA,PRICE_MEDIAN,i);

Buffer1=MA5-MA34;

}

// Signal line

for(i=0; i<limit; i++) <----can someone explain to me what it means ?

{

Buffer2=iMAOnArray(Buffer1,Bars,Signal.period,0,MODE_LWMA,i);

} <----can someone explain to me what it means ?

// Arrows

for(i=0; i<limit; i++)

{

if(Buffer1 > Buffer2 && Buffer1 < Buffer2)

b2 = High+10*Point;

if(Buffer1 Buffer2)

b3 = Low-10*Point; <----can someone explain to me what it means ?

}

//----

return(0);

}

Thanks guys =^_^=

 
liew_stanley:
if(counted_bars>0) limit++; <----can someone explain to me what it means ?

// Main line

for(i=0; i<limit; i++) <----can someone explain to me what it means ?

// Signal line

for(i=0; i<limit; i++) <----can someone explain to me what it means ?

{

Buffer2=iMAOnArray(Buffer1,Bars,Signal.period,0,MODE_LWMA,i);

} <----can someone explain to me what it means ?

// Arrows

for(i=0; i<limit; i++)

{

if(Buffer1 > Buffer2 && Buffer1 < Buffer2)

b2 = High+10*Point;

if(Buffer1 Buffer2)

b3 = Low-10*Point; <----can someone explain to me what it means ?

Thanks guys =^_^=

It's easy.

When you start your indi, Bars=1000 (for example) and counted_bars=0. So limit=1000 and your indi calculates all 1000 bars. After this counted_bars=1000, and every new tick indi calculates only last (zero) bar. When next bar comes, limit=1 and indi recalculates two last bars.

b3 = Low-10*Point; - it's just a line 10 points lower then bar's minimum.

 
liew_stanley:
Hi, i'm a new learner and interested to learn mql, can someone tell me how to code this into EA ?

extern int Fast.MA.Period = 5;

extern int Slow.MA.Period = 34;

extern int Signal.period = 5;

//---- buffers

double Buffer1[],

Buffer2[],

b2[],

b3[];

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

//| Custom indicator initialization function |

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

int init()

{

//---- indicators

// two additional buffers used for counting

IndicatorBuffers(4);

IndicatorShortName("MA_5_34");

SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,3);

SetIndexArrow(0,242); // down 226 234 242

SetIndexBuffer(0,b2);

SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,3);

SetIndexArrow(1,241); //UP 225 233 241

SetIndexBuffer(1,b3);

// These buffers are not plotted, just used to determine arrows

SetIndexBuffer (2,Buffer1);

SetIndexBuffer (3,Buffer2);

//----

return(0);

}

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

//| Custor indicator deinitialization function |

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

int deinit()

{

//----

return(0);

}

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

//| Custom indicator iteration function |

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

int start()

{

int i, counted_bars=IndicatorCounted();

double MA5,MA34;

int limit=Bars-counted_bars;

Print(" print limit = ", limit);

if(counted_bars>0) limit++; <----can someone explain to me what it means ?

// Main line

for(i=0; i<limit; i++) <----can someone explain to me what it means ?

{

MA5=iMA(NULL,0,Fast.MA.Period,0,MODE_SMA,PRICE_MEDIAN,i);

MA34=iMA(NULL,0,Slow.MA.Period,0,MODE_SMA,PRICE_MEDIAN,i);

Buffer1=MA5-MA34;

}

// Signal line

for(i=0; i<limit; i++) <----can someone explain to me what it means ?

{

Buffer2=iMAOnArray(Buffer1,Bars,Signal.period,0,MODE_LWMA,i);

} <----can someone explain to me what it means ?

// Arrows

for(i=0; i<limit; i++)

{

if(Buffer1 > Buffer2 && Buffer1 < Buffer2)

b2 = High+10*Point;

if(Buffer1 Buffer2)

b3 = Low-10*Point; <----can someone explain to me what it means ?

}

//----

return(0);

}

Thanks guys =^_^=

If you want to use this indicator in an ea you can use the icustom() function to access it. you dont really need to understand any of the code of the indicator, just the data that it spits out. At least thats the easy way to do it.

 
fxcourt:
If you want to use this indicator in an ea you can use the icustom() function to access it. you dont really need to understand any of the code of the indicator, just the data that it spits out. At least thats the easy way to do it.

Meaning to say, use the function icustom() in the EA to recall the indis ? how should the coding to be written in the EA ? can give me some guide ?

Many Thanks

 
toiii:
whoa..

thanks guys..

how about buy stop and sell stop?

just change "BUY" over there?

just want 2 positions only for 1 pairs, not more than that..

sorry, still newbie..

Read this

MQL4 Tutorial

 

Is this correct coding ?

extern double TakeProfit=100;

extern double Lots=1;

extern double TrailingStop=35;

extern int ShortEma = 5;

extern int LongEma = 34;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

int Crossed (double line1 , double line2)

{

static int last_direction = 0;

static int current_direction = 0;

if(line1>line2)current_direction = 1; //up

if(line1<line2)current_direction = 2; //down

if(current_direction != last_direction) //changed

{

last_direction = current_direction;

return (last_direction);

}

else

{

return (0);

}

}

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

//| expert start function |

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

int start()

{

//----

int total;

double shortEma, longEma;

shortEma = iCustom(NULL,0,"Test_Demo",13,0,0);

longEma = iCustom(NULL,0,"Test_Demo",54,0,0);

Print("shortEma = " + shortEma + " : longEma = " + longEma);

int isCrossed = 0;

isCrossed = Crossed (LongEma,ShortEma);

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1)

{

1=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA",12345,0,Green);

if(1>0)

{

if(OrderSelect(1,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if(isCrossed == 2)

{

2=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);

if(2>0)

{

if(OrderSelect(2,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

return(0);

}

}

i've got an error saying 1 & 2 unexpected token, what is that mean ? can guide me ?

Thanks

 

I guess you deleted something and forgot:

2=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red); if(2>0)

it's nonsense.

 
liew_stanley:
extern double TakeProfit=100;

extern double Lots=1;

extern double TrailingStop=35;

i've got an error saying 1 & 2 unexpected token, what is that mean ? can guide me ?

Thanks

Delete 1=, and 2= before OrderSend(....