Coding help - page 395

 

EA is not opening a counter position

Hi MQL Coders,

I have a question regarding a bug in my EA. Seems not to open a counter trend position (see attached screenshot).

It is closing the trade, but it is not open a new trade to follow the new given trend direction.

// Indicator HMA NMC

double trendc = iCustom(Symbol(),0,"Hull moving average 2 strict nmc", "", HMA_Period, HMA_Price, HMA_Speed,0,3,1);

double trendp = iCustom(Symbol(),0,"Hull moving average 2 strict nmc", "", HMA_Period, HMA_Price, HMA_Speed,0,3,2);

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

//| BUY |

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

if(trendc!=trendp)

{

if(trendc==1) // code for buy

OpenBuy();

return(0);

}

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

//| SELL |

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

if(trendc==-1) // code for sell

{

OpenSell();

return(0);

}

}

[/CODE]

[CODE]

void CheckForClose()

{

RefreshRates();

double trendc_c = iCustom(Symbol(),0,"Hull moving average 2 strict nmc", "", HMA_Period, HMA_Price, HMA_Speed,0,3,1);

double trendp_c = iCustom(Symbol(),0,"Hull moving average 2 strict nmc", "", HMA_Period, HMA_Price, HMA_Speed,0,3,2);

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

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderMagicNumber()!= MAGIC) continue;

if(OrderSymbol() != s_symbol) continue;

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

//| Close BUY |

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

if(trendc_c!=trendp_c) // Check trend

{

if(OrderType()==OP_BUY)

{

if(trendc_c==-1) //is buy?

{

bool buyClose=OrderClose(OrderTicket(),OrderLots(),MarketInfo(s_symbol,MODE_BID),Slippage*pipMultiplier,clCloseBuy);

if(buyClose==false)

{

int ErrorCode = GetLastError();

string ErrDesc = ErrorDescription(ErrorCode);

string ErrAlert= StringConcatenate("Close Buy Order - Error ",ErrorCode,": ",ErrDesc);

if(ShowAlerts == true) Alert(ErrAlert);

string ErrLog=StringConcatenate("Bid: ",MarketInfo(s_symbol,MODE_BID)," Lots: ",OrderLots()," Ticket: ",OrderTicket());

Print(ErrLog);

}

}

break;

}

}

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

//| Close SELL |

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

if(trendc_c!=trendp_c)

{

if(OrderType()==OP_SELL)

{

if(trendc_c==1) // SELL

{

bool sellClose= OrderClose(OrderTicket(),OrderLots(),MarketInfo(s_symbol,MODE_ASK),Slippage*pipMultiplier,clCloseSell);

if(sellClose == false)

{

ErrorCode = GetLastError();

ErrDesc = ErrorDescription(ErrorCode);

ErrAlert=StringConcatenate("Close Sell Order - Error ",ErrorCode,": ",ErrDesc);

if(ShowAlerts==true) Alert(ErrAlert);

ErrLog=StringConcatenate("Ask: ",MarketInfo(s_symbol,MODE_ASK)," Lots: ",OrderLots()," Ticket: ",OrderTicket());

Print(ErrLog);

}

}

break;

}

}

}

}

Thank you in advance!

 
secretcode:
Dear Mladen

Is it possible to 'add band' in attached indicator like in this post : https://www.mql5.com/en/forum/172894/page45

That indicator is from elite section but I like the way how you add band on it

Thanks for any help

secretcode

secretcode

Here is a version with bands added : ema_variation__filter_amp_bands_amp_mtf.ex4

 
mladen:
secretcode Here is a version with bands added : ema_variation__filter_amp_bands_amp_mtf.ex4

Thank You Mladen I really appreciate your expertise

Sincerely

secretcode

 
tfi_markets:
Hi MQL Coders,

I have a question regarding a bug in my EA. Seems not to open a counter trend position (see attached screenshot).

It is closing the trade, but it is not open a new trade to follow the new given trend direction.

// Indicator HMA NMC

double trendc = iCustom(Symbol(),0,"Hull moving average 2 strict nmc", "", HMA_Period, HMA_Price, HMA_Speed,0,3,1);

double trendp = iCustom(Symbol(),0,"Hull moving average 2 strict nmc", "", HMA_Period, HMA_Price, HMA_Speed,0,3,2);

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

//| BUY |

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

if(trendc!=trendp)

{

if(trendc==1) // code for buy

OpenBuy();

return(0);

}

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

//| SELL |

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

if(trendc==-1) // code for sell

{

OpenSell();

return(0);

}

}

[/CODE]

[CODE]

void CheckForClose()

{

RefreshRates();

double trendc_c = iCustom(Symbol(),0,"Hull moving average 2 strict nmc", "", HMA_Period, HMA_Price, HMA_Speed,0,3,1);

double trendp_c = iCustom(Symbol(),0,"Hull moving average 2 strict nmc", "", HMA_Period, HMA_Price, HMA_Speed,0,3,2);

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

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderMagicNumber()!= MAGIC) continue;

if(OrderSymbol() != s_symbol) continue;

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

//| Close BUY |

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

if(trendc_c!=trendp_c) // Check trend

{

if(OrderType()==OP_BUY)

{

if(trendc_c==-1) //is buy?

{

bool buyClose=OrderClose(OrderTicket(),OrderLots(),MarketInfo(s_symbol,MODE_BID),Slippage*pipMultiplier,clCloseBuy);

if(buyClose==false)

{

int ErrorCode = GetLastError();

string ErrDesc = ErrorDescription(ErrorCode);

string ErrAlert= StringConcatenate("Close Buy Order - Error ",ErrorCode,": ",ErrDesc);

if(ShowAlerts == true) Alert(ErrAlert);

string ErrLog=StringConcatenate("Bid: ",MarketInfo(s_symbol,MODE_BID)," Lots: ",OrderLots()," Ticket: ",OrderTicket());

Print(ErrLog);

}

}

break;

}

}

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

//| Close SELL |

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

if(trendc_c!=trendp_c)

{

if(OrderType()==OP_SELL)

{

if(trendc_c==1) // SELL

{

bool sellClose= OrderClose(OrderTicket(),OrderLots(),MarketInfo(s_symbol,MODE_ASK),Slippage*pipMultiplier,clCloseSell);

if(sellClose == false)

{

ErrorCode = GetLastError();

ErrDesc = ErrorDescription(ErrorCode);

ErrAlert=StringConcatenate("Close Sell Order - Error ",ErrorCode,": ",ErrDesc);

if(ShowAlerts==true) Alert(ErrAlert);

ErrLog=StringConcatenate("Ask: ",MarketInfo(s_symbol,MODE_ASK)," Lots: ",OrderLots()," Ticket: ",OrderTicket());

Print(ErrLog);

}

}

break;

}

}

}

}

Thank you in advance!

tfi_markets

Try to move both break statements one line up (to be within the "}" )

 

Hello Mladen,

would you be kind enough to tell me where I have gone wrong with this code.

Attached indicator All Pivots, I reworked it to also show the previous day open line, previous day close line and current day open line.

but the indicator is inconsistent. It always shows the pivots correctly but rarely if at all shows my added code for the daily open, prev day open and close lines.

I do not understand why, the code looks the same as pivot code so it should all show and sometimes it does but not always. I have done something wrong but not sure what.

Many thanks

PG

allpivots_daily.mq4

Files:
 
pgtips:
Hello Mladen,

would you be kind enough to tell me where I have gone wrong with this code.

Attached indicator All Pivots, I reworked it to also show the previous day open line, previous day close line and current day open line.

but the indicator is inconsistent. It always shows the pivots correctly but rarely if at all shows my added code for the daily open, prev day open and close lines.

I do not understand why, the code looks the same as pivot code so it should all show and sometimes it does but not always. I have done something wrong but not sure what.

Many thanks

PG

allpivots_daily.mq4

PG

It works OK (see the list of created objects). Check if there are overlapping prices (that could cause some lines to be invisible)

Also, do not forget to add your newly created lines in the ObjectDel() function too

Files:
objects.gif  49 kb
 

Thanks Mladen, I kept checking and it seems to be picky about names of lines inside the "" which is not what I expect.

As always thank you for your time and help,

PG

mladen:
PG

It works OK (see the list of created objects). Check if there are overlapping prices (that could cause some lines to be invisible)

 
pgtips:
Thanks Mladen, I kept checking and it seems to be picky about names of lines inside the "" which is not what I expect.

As always thank you for your time and help,

PG

The names of the object always must be unique - but as far as I saw you took care of that

 
 

Thank you, Mladen,

i have a problem and is that :

my first signal buy after last sell, i need it to be after bar is closed Above resistance or "srUp" of the code

same for sell, has to be bar closed below support or "srDown"

How could i fix this on the code?

And second, was number one previous asked, is how could i fix in my code to show the first signal buy after sell in different color??

#property strict

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_color1 clrLime

#property indicator_color2 clrRed

#property indicator_color3 clrMagenta

#property indicator_color4 clrCyan

#define BUY 1

#define SELL 2

double Buy[],

Sell[],

FirstBuy[],

FirstSell[];

int OnInit() {

SetIndexBuffer(0,FirstBuy);

SetIndexBuffer(1,FirstSell);

SetIndexBuffer(2,Buy);

SetIndexBuffer(3,Sell);

for(int i=0; i<4; i++) {

SetIndexStyle (i,DRAW_ARROW,STYLE_SOLID,2);}

SetIndexArrow (0,233);

SetIndexArrow (1,234);

SetIndexArrow (2,233);

SetIndexArrow (3,234);

return(INIT_SUCCEEDED);}

void OnDeinit(const int reason) {}

int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],

const double &open[], const double &high[], const double &low[],

const double &close[], const long &tick_volume[], const long &volume[],

const int &spread[]) {

static int lastSignal=0;

for(int i=MathMin(rates_total-prev_calculated, rates_total-1); i>=0; i--) {

FirstBuy=FirstSell=Buy=Sell=EMPTY_VALUE;

datetime when=Time;

int w=iBarShift(NULL,PERIOD_H4, when),

x=iBarShift(NULL,PERIOD_H1, when),

y=iBarShift(NULL,PERIOD_M15,when),

z=iBarShift(NULL,PERIOD_M5, when);

double srUpH4 = iCustom(NULL,PERIOD_H4,"Support and Resistance (Barry)",0,w),

srDownH4 = iCustom(NULL,PERIOD_H4,"Support and Resistance (Barry)",1,w),

MidH4 = (srUpH4+srDownH4)/2,

srUpH1 = iCustom(NULL,PERIOD_H1,"Support and Resistance (Barry)",0,x),

srDownH1 = iCustom(NULL,PERIOD_H1,"Support and Resistance (Barry)",1,x),

MidH1 = (srUpH1+srDownH1)/2,

srUp15M = iCustom(NULL,PERIOD_M15,"Support and Resistance (Barry)",0,y),

srDown15M = iCustom(NULL,PERIOD_M15,"Support and Resistance (Barry)",1,y),

Mid15M = (srUp15M+srDown15M)/2,

srUp5M = iCustom(NULL,PERIOD_M5,"Support and Resistance (Barry)",0,z),

srDown5M = iCustom(NULL,PERIOD_M5,"Support and Resistance (Barry)",1,z),

Mid5M = (srUp5M+srDown5M)/2,

srUp = iCustom(NULL,0,"Support and Resistance (Barry)",0,i),

srDown = iCustom(NULL,0,"Support and Resistance (Barry)",1,i);

if(ClosesrUp) { // first signal buy

Buy=Low-_Point; }

else {

Buy=EMPTY_VALUE; }

if(Close>Mid5M && Close>srUp) { // second signal buy

Buy=Low-_Point; }

else {

Buy=EMPTY_VALUE; }

if(Close>MidH4 && Close>Mid15M && Close<srDown) { // first signal sell

Sell=High+_Point; }

else {

Sell=EMPTY_VALUE; }

if(Close<Mid5M && Close<srDown) { // second signal sell

Sell=High+_Point; }

else {

Sell=EMPTY_VALUE; }}

return(rates_total);}

this is what i have for the alerts, shall i use it or instead use the Alert( t("text for pop up"))??

void SoundAlert(datetime i, string dir) {

static datetime lastAlert=0;

if(lastAlert!=i) {

Alert(StringFormat("%s signal on %s at %s",dir,Symbol(),TimeToStr(i)));

lastAlert=i;}}

thank you mladen

Files:
signals.png  31 kb