How to code? - page 341

 

Can someone be kind enough to help me solve this:

There is a problem with the code I attached below this message...,it always give the signal at the begining of a candle formation!

this has led to me entering trade late most especialy on higher time frames. Can you kindly help us so that the iny will be giving the alert only when the current price bridge MA either up for buy and down for sell, instead of waiting for the begining of another candle

Thanks Attached Files

Files:
smacd.mq4  4 kb
 

Hi Mladen, do you know what can I use to trigger my opening trade criteria?

What I want to know is how can I trigger a trade if the candle closes above or below the moving average line by 10pips.

The system will trigger the trade based on whether it closes above or below the moving average line.

I'm thinking of using iClose, but have no idea how to implement it. Appreciate your help.

Thanks.

Ryan

 
tkuan77:
Hi Mladen, do you know what can I use to trigger my opening trade criteria?

What I want to know is how can I trigger a trade if the candle closes above or below the moving average line by 10pips.

The system will trigger the trade based on whether it closes above or below the moving average line.

I'm thinking of using iClose, but have no idea how to implement it. Appreciate your help.

Thanks.

Ryan

Hi Mladen,

I have done up the code, it seems to be working, however, the trade at times does not get triggered when the criteria is met and I have no idea on how to go about it from here on.

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

//---- input parameters

extern double TakeProfit = 250.0;

extern double Lots = 0.1;

extern double StopLoss = 200.0;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

double pBid, pAsk;

datetime newbar;

int start()

{

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

if(newbar==Time[0])return(0);

else newbar=Time[0];

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

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

double Up_MA = iMA(NULL,0,10,0,MODE_LWMA,PRICE_HIGH,0);

double Dwn_MA = iMA(NULL,0,10,0,MODE_LWMA,PRICE_LOW,0);

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

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

//-- Trigger Trade

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

int ticket, total;

double CD_Close = iClose(NULL,0,1);

total = OrdersTotal(); // check for total number of trades currently open

if(total < 2)

{

if (CD_Close >= Up_MA+50*Point)//Hour()==Entry_Hour_1st && Minute()==Entry_Minute_1st)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",Entry_magicNo_1st,0,Green);

return(0);

}

if (CD_Close <= Dwn_MA-50*Point)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,Bid-TakeProfit*Point,

"My EA",Entry_magicNo_1st,0,Red);

return(0);

}

return(0);

}

return(0);

}

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

 
tkuan77:
Hi Mladen,

I have done up the code, it seems to be working, however, the trade at times does not get triggered when the criteria is met and I have no idea on how to go about it from here on.

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

//---- input parameters

extern double TakeProfit = 250.0;

extern double Lots = 0.1;

extern double StopLoss = 200.0;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

double pBid, pAsk;

datetime newbar;

int start()

{

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

if(newbar==Time[0])return(0);

else newbar=Time[0];

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

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

double Up_MA = iMA(NULL,0,10,0,MODE_LWMA,PRICE_HIGH,0);

double Dwn_MA = iMA(NULL,0,10,0,MODE_LWMA,PRICE_LOW,0);

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

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

//-- Trigger Trade

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

int ticket, total;

double CD_Close = iClose(NULL,0,1);

total = OrdersTotal(); // check for total number of trades currently open

if(total < 2)

{

if (CD_Close >= Up_MA+50*Point)//Hour()==Entry_Hour_1st && Minute()==Entry_Minute_1st)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",Entry_magicNo_1st,0,Green);

return(0);

}

if (CD_Close <= Dwn_MA-50*Point)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,Bid+StopLoss*Point,Bid-TakeProfit*Point,

"My EA",Entry_magicNo_1st,0,Red);

return(0);

}

return(0);

}

return(0);

}

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

Can you show some cases when it should have triggered a trade and it did not?

 

Hi Mladen,

Do find attached the screenshot.

Regards

Ryan

Files:
untitled.jpg  42 kb
 

Hi Mladen, could it be some form of error in some lines that is causing this error?

Regards

 
tkuan77:
Hi Mladen, could it be some form of error in some lines that is causing this error? Regards

tkuan77

As a first step check the journal and experts tab of terminal if some error message is written out there

 
mladen:
tkuan77 As a first step check the journal and experts tab of terminal if some error message is written out there

Hi mladen, thanks, the issue has already been resolved.

However, my next question is how do i sort the trades accordingly?

For example:

Total amount of trades at any point of time = 2

1 trade for each set of criteria. How do I make sure that the system does not trigger more than 1 trade for each particular set if the criteria are met more than once?

Below is my codes for reference:

total = OrdersTotal(); // check for total number of trades currently open

if(total < 2)

{

if (Hour()==Entry_Hour_1st || Hour()==Entry_Hour_2nd)

{

//If there is no trade currently open under this criteria, check to see if conditions are met, if yes, trigger a trade, if no, do not trigger a trade.

}

if (Hour()==Entry_Hour_3rd || Hour()==Entry_Hour_4th)

{

//If there is no trade currently open under this criteria, check to see if conditions are met, if yes, trigger a trade, if no, do not trigger a trade.

}

}

Do appreciate your advise. Thanks.

Regards

 

How exactly should ECN brokers orders must be handled?

 
apprentice coder:
How exactly should ECN brokers orders must be handled?

apprentice coder

The only difference is that on an ECN broker you first open an order with 0 sl an tp fields, and then modify the order (if needed) to set sl and tp, while on "regular" broker you can place sl and tp along with order opening. All the rest is exactly the same