How to code? - page 166

 

thanks for your reply.

but this is not working if there was slippage when the position was closed, right?

 
codersview:
Hi All,

I got an issue with the EA of RSI and MA...

I got the indicator working properly... All I want is to convert it into EA...

I tried a couple of things. IndicatorCounted() is not working in EA, so I tried to hard-code the values of the for loop (bar=0; bar<15; bar++), I was getting correct RSI, but SMA is not giving corect values...

I'm attaching the code for SMA crossing the RSI...

So could someone please help me to convert this Indicator into EA.

***************************************************************************************************************************************

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 LawnGreen

#property indicator_color2 DarkBlue

double ExtMapBuffer1[];

double ExtMapBuffer2[];

int init()

{

IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,ExtMapBuffer1);

SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);

SetIndexBuffer(1,ExtMapBuffer2);

return(0);

}

int deinit()

{

return(0);

}

int start()

{

int bar, limit;

int counted_bars=IndicatorCounted();

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit=Bars-IndicatorCounted();

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

ExtMapBuffer1 = iRSI(NULL,0,14,PRICE_TYPICAL,bar);

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

ExtMapBuffer2=iMAOnArray(ExtMapBuffer1,Bars,14,0,MODE_SMA,bar);

return(0);

}

***************************************************************************************************************************************

Thanks & Regards,

Ganesh

Ganesh,

Try doing the bars in time order.

Instead of for(bar=0; bar<limit; bar++)

Use for(bar=limit-1; bar>=0; bar--)

Chazzmoney

 

Hi

Thanks for the quick response... I tried it, but doesnt make any difference...

That too if we use Bars in the loop, it has to calculate entire values (for all bars) for each and every tick... and there could be delay.. Also SMA value would be zero.

In the Indicator we would be calculating RSI and EMA only for the new bar by using IndicatorCounted(), but we cant use it in the EA..

Exact point of the problem is the 2nd for loop and the values for the iMAOnArray..

First for loop works perfectly even when we hard-code it...

 
jan100:
thanks for your reply. but this is not working if there was slippage when the position was closed, right?

Some brokers write [sl] and [tp] in the comment field. You can use it as well.

 

thanks. i'll try that.

how is it possible to select the last closed order with the right magic number?

 
jan100:
thanks. i'll try that. how is it possible to select the last closed order with the right magic number?

If you find it in the history list, this is a closed order, if in total list - opened yet.

 

Ea code request: Close previous order when open new position

Hi, i would like to make request for code for when the new position is open,automatically the previous order will close.Mean i dont want to use take profit or trailling.

Example:

When order buy is open,previous order sell will close

When order sell is open,previous order buy will close

i found the code is OrderCloseBy() but i dont know exactly where to put this code

thanks

 
darkkiller:
Hi, i would like to make request for code for when the new position is open,automatically the previous order will close.Mean i dont want to use take profit or trailling.

Example:

When order buy is open,previous order sell will close

When order sell is open,previous order buy will close

i found the code is OrderCloseBy() but i dont know exactly where to put this code

thanks

Problem solve

put after OrderSend BUY

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position

}

put after OrderSend SELL

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position

}

Other code i get from ForexFactory thanks to(Fulltime247,magnumfreak)

extern int MAGICTerminal=101;

extern double LOTSize=0.10;

extern string ORDERComment="Order Comment Text";

//TOTAL Count..

int btotal=0; int stotal=0; int ticket=0; double closelots=0; bool xbool;

for( int cnt=OrdersTotal()-1;cnt>=0;cnt--)

{

xbool=OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()==OP_BUY && OrderMagicNumber()==MAGICTerminal && OrderSymbol()==Symbol()){btotal++; ticket=OrderTicket(); closelots=OrderLots();}

if(OrderType()==OP_SELL && OrderMagicNumber()==MAGICTerminal && OrderSymbol()==Symbol()){stotal++; ticket=OrderTicket(); closelots=OrderLots();}

}// END TOTAL count..

if(WHATEVER YOUR CONDITION FOR A BUY)

{

if(btotal>0){OrderClose(ticket,closelots,Bid,3,CLR_NONE);}

ticket=OrderSend(Symbol(),OP_BUY,LOTSize,Ask,3,0,0,ORDERComment,MAGICTerminal,0,Magenta);

}

if(WHATEVER YOUR CONDITION FOR A SELL)

{

if(stotal>0){OrderClose(ticket,closelots,Ask,3,CLR_NONE);}

ticket=OrderSend(Symbol(),OP_SELL,LOTSize,Bid,3,0,0,ORDERComment,MAGICTerminal,0,Lime);

}
 
 

I am new to MT4.

I want to calculate the number of bars after shortMA has crossed longMA, anyone know how to code this function?

Thanks in advance!