How to code? - page 344

 
mladen:

tkuan77

You have to find the last closed bar and to see if the day of the last closed bar is the same as the current day.

You can use something like this function to get the last closed order time :

datetime GetLastClosedOrderTime()

{

datetime CloseTime = 0;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime)

CloseTime = OrderCloseTime();

}

return(CloseTime);

}

And then you can check if the date is the same

Hi mladen,

But where do i insert this part of the code? Sorry for asking this as I am still quite new to coding myself and still can't seem to figure out how For loop works.

Regards

Ryan

 
tkuan77:

Hi mladen,

But where do i insert this part of the code? Sorry for asking this as I am still quite new to coding myself and still can't seem to figure out how For loop works.

Regards

Ryan

Ryan

Something like this :

if (TimeDayOfYear(TimeCurrent())==TimeDayOfYear(GetLastClosedOrderTime())) // no trading

 
mladen:

Ryan

Something like this :

if (TimeDayOfYear(TimeCurrent())==TimeDayOfYear(GetLastClosedOrderTime())) // no trading

Hi mladen, I have added the code in but it seems to be giving out some form of global variable error. Also, i noticed that you have a variable MagicNumber that is not define, may I ask what is that for? I have added it under int but I am not sure what is the use for that variable.

Do advise. Thanks again for your patience.

//---- input parameters

extern double TakeProfit = 1000.0;

extern double Lots = 0.1;

extern double StopLoss = 980.0;

extern int Entry_Hour_1st = 21;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int start()

{

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

//-- Trigger Trade

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

int ticket, total, MagicNumber;

double TP_Value, SL_Value;

datetime GetLastClosedOrderTime()

{

datetime CloseTime = 0;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime)

CloseTime = OrderCloseTime();

}

return(CloseTime);

}

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

if(total < 1)

{

if (Hour()==Entry_Hour_1st && ((High[0] - High[1]) > 0.00100) && ((High[1] - Low[1]) > 0.00100) && TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(GetLastClosedOrderTime()))

{

if ((Close[1] - Open[1]) > 0.00100)

{

TP_Value = (Close[1] - Open[1]);

SL_Value = (Low[1] - 0.0010);

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

if ((Open[1] - Close[1]) > 0.00100)

{

TP_Value = (Open[1] - Close[1]);

SL_Value = (Low[1] - 0.0010);

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

if ((Open[1] - Close[1]) <= 0.00100)

{

TP_Value = (High[1] - Low[1]);

SL_Value = (Low[1] - 0.0010);

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

}

if (Hour()==Entry_Hour_1st && ((Low[1] - Low[0]) > 0.00100) && ((High[1] - Low[1]) > 0.00100) && TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(GetLastClosedOrderTime()))

{

if ((Close[1] - Open[1]) > 0.00100)

{

TP_Value = (Close[1] - Open[1]);

SL_Value = (High[1] + 0.0010);

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

if ((Open[1] - Close[1]) > 0.00100)

{

TP_Value = (Open[1] - Close[1]);

SL_Value = (High[1] + 0.0010);

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

if ((Open[1] - Close[1]) <= 0.00100)

{

TP_Value = (High[1] - Low[1]);

SL_Value = (High[1] + 0.0010);

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

}

}

return(0);

}

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

 
tkuan77:

Hi mladen, I have added the code in but it seems to be giving out some form of global variable error. Also, i noticed that you have a variable MagicNumber that is not define, may I ask what is that for? I have added it under int but I am not sure what is the use for that variable.

Do advise. Thanks again for your patience.

//---- input parameters

extern double TakeProfit = 1000.0;

extern double Lots = 0.1;

extern double StopLoss = 980.0;

extern int Entry_Hour_1st = 21;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int start()

{

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

//-- Trigger Trade

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

int ticket, total, MagicNumber;

double TP_Value, SL_Value;

datetime GetLastClosedOrderTime()

{

datetime CloseTime = 0;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime)

CloseTime = OrderCloseTime();

}

return(CloseTime);

}

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

if(total < 1)

{

if (Hour()==Entry_Hour_1st && ((High[0] - High[1]) > 0.00100) && ((High[1] - Low[1]) > 0.00100) && TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(GetLastClosedOrderTime()))

{

if ((Close[1] - Open[1]) > 0.00100)

{

TP_Value = (Close[1] - Open[1]);

SL_Value = (Low[1] - 0.0010);

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

if ((Open[1] - Close[1]) > 0.00100)

{

TP_Value = (Open[1] - Close[1]);

SL_Value = (Low[1] - 0.0010);

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

if ((Open[1] - Close[1]) <= 0.00100)

{

TP_Value = (High[1] - Low[1]);

SL_Value = (Low[1] - 0.0010);

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

}

if (Hour()==Entry_Hour_1st && ((Low[1] - Low[0]) > 0.00100) && ((High[1] - Low[1]) > 0.00100) && TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(GetLastClosedOrderTime()))

{

if ((Close[1] - Open[1]) > 0.00100)

{

TP_Value = (Close[1] - Open[1]);

SL_Value = (High[1] + 0.0010);

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

if ((Open[1] - Close[1]) > 0.00100)

{

TP_Value = (Open[1] - Close[1]);

SL_Value = (High[1] + 0.0010);

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

if ((Open[1] - Close[1]) <= 0.00100)

{

TP_Value = (High[1] - Low[1]);

SL_Value = (High[1] + 0.0010);

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

}

}

return(0);

}

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

That is a function

Move it outside the body of start() function

 

mladen:

That is a function

Move it outside the body of start() function

Hi mladen, I tried moving the function out of Start function, however, my EA is still triggering more than 1 trade a day instead of limiting to only trigger 1 trade a day.

​Appreciate the enlightment.

Regards

//---- input parameters

extern double TakeProfit = 1000.0;

extern double Lots = 0.1;

extern double StopLoss = 980.0;

extern int Entry_Hour_1st = 21;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int MagicNumber;

datetime GetLastClosedOrderTime()

{

datetime CloseTime = 0;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime)

CloseTime = OrderCloseTime();

}

return(CloseTime);

}

int start()

{

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

//-- Trigger Trade

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

int ticket, total;

double TP_Value, SL_Value;

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

if(total < 1)

{

if (Hour()==Entry_Hour_1st && ((High[0] - High[1]) > 0.00100) && ((High[1] - Low[1]) > 0.00100) && TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(GetLastClosedOrderTime()))

{

if ((Close[1] - Open[1]) > 0.00100)

{

TP_Value = (Close[1] - Open[1]); // value of long body

SL_Value = (Low[1] - 0.0010); // always the same for long

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

if ((Open[1] - Close[1]) > 0.00100)

{

TP_Value = (Open[1] - Close[1]); // value of short body

SL_Value = (Low[1] - 0.0010); // always the same for long

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

if ((Open[1] - Close[1]) <= 0.00100)

{

TP_Value = (High[1] - Low[1]); // value of whole candle including head and tail

SL_Value = (Low[1] - 0.0010); // always the same for long

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Value,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

}

if (Hour()==Entry_Hour_1st && ((Low[1] - Low[0]) > 0.00100) && ((High[1] - Low[1]) > 0.00100) && TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(GetLastClosedOrderTime()))

{

if ((Close[1] - Open[1]) > 0.00100)

{

TP_Value = (Close[1] - Open[1]); // value of long body

SL_Value = (High[1] + 0.0010); // always the same for short

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

if ((Open[1] - Close[1]) > 0.00100)

{

TP_Value = (Open[1] - Close[1]); // value of short body

SL_Value = (High[1] + 0.0010); // always the same for short

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

if ((Open[1] - Close[1]) <= 0.00100)

{

TP_Value = (High[1] - Low[1]); // value of whole candle including head and tail

SL_Value = (High[1] + 0.0010); // always the same for short

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Value,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

}

}

return(0);

}

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

 
tkuan77:

Hi mladen, I tried moving the function out of Start function, however, my EA is still triggering more than 1 trade a day instead of limiting to only trigger 1 trade a day.

​Appreciate the enlightment.

Regards

//---- input parameters

extern double TakeProfit = 1000.0;

extern double Lots = 0.1;

extern double StopLoss = 980.0;

extern int Entry_Hour_1st = 21;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int MagicNumber;

datetime GetLastClosedOrderTime()

{

datetime CloseTime = 0;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()>CloseTime)

CloseTime = OrderCloseTime();

}

return(CloseTime);

}

int start()

{

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

//-- Trigger Trade

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

int ticket, total;

double TP_Value, SL_Value;

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

if(total < 1)

{

if (Hour()==Entry_Hour_1st && ((High[0] - High[1]) > 0.00100) && ((High[1] - Low[1]) > 0.00100) && TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(GetLastClosedOrderTime()))

{

if ((Close[1] - Open[1]) > 0.00100)

{

TP_Value = (Close[1] - Open[1]); // value of long body

SL_Value = (Low[1] - 0.0010); // always the same for long

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Val ue,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

if ((Open[1] - Close[1]) > 0.00100)

{

TP_Value = (Open[1] - Close[1]); // value of short body

SL_Value = (Low[1] - 0.0010); // always the same for long

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Val ue,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

if ((Open[1] - Close[1]) <= 0.00100)

{

TP_Value = (High[1] - Low[1]); // value of whole candle including head and tail

SL_Value = (Low[1] - 0.0010); // always the same for long

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,SL_Val ue,Ask+TP_Value,"My EA",200,0,Green);

return(0);

}

}

if (Hour()==Entry_Hour_1st && ((Low[1] - Low[0]) > 0.00100) && ((High[1] - Low[1]) > 0.00100) && TimeDayOfYear(TimeCurrent()) != TimeDayOfYear(GetLastClosedOrderTime()))

{

if ((Close[1] - Open[1]) > 0.00100)

{

TP_Value = (Close[1] - Open[1]); // value of long body

SL_Value = (High[1] + 0.0010); // always the same for short

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Va lue,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

if ((Open[1] - Close[1]) > 0.00100)

{

TP_Value = (Open[1] - Close[1]); // value of short body

SL_Value = (High[1] + 0.0010); // always the same for short

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Va lue,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

if ((Open[1] - Close[1]) <= 0.00100)

{

TP_Value = (High[1] - Low[1]); // value of whole candle including head and tail

SL_Value = (High[1] + 0.0010); // always the same for short

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,SL_Va lue,Bid-TP_Value,"My EA",200,0,Red);

return(0);

}

}

}

return(0);

}

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

tkuan77

Replace this :

if(total < 1)

with this :

if (total < 1 && TimeDayOfYear(GetLastClosedOrderTime()) != TimeDayOfYear(TimeCurrent())) ;

And it should not do that any more

 
mladen:

tkuan77

Replace this :

if(total < 1)

with this :

if (total < 1 && TimeDayOfYear(GetLastClosedOrderTime()) != TimeDayOfYear(TimeCurrent())) ;

And it should not do that any more

Hi mladen, after I have input the code, instead of triggering 1 trade a day, it triggered multiple trades at once till the account burst. Could it be due to some triggering mechanism that causes this?

Regards

 

Hi mladen, i have this particular code that will close all pending trades i have after i have a trade running. how do i set to to only cancel my current existing pending trade after i have triggered a trade instead of all future pending trades?

thanks.....

int new_del()

{

int i,a;

int total = OrdersTotal();

string comentario,par;

for (i=total-1; i >=0; i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if (OrderType()==OP_BUY || OrderType()==OP_SELL)

{

for (a=total-1; a >=0; a--)

{

OrderSelect(a,SELECT_BY_POS,MODE_TRADES);

comentario=OrderComment();

par=StringSubstr(comentario,0,6);

if(OrderType()==OP_SELLSTOP)// && comentario==Symbol())

{

OrderDelete(OrderTicket());

Print("Deleting SELL_STOP"," Ordertype:",OrderType());

return(1);

}

if(OrderType()==OP_BUYSTOP)// && par==Symbol())

{

OrderDelete(OrderTicket());

Print("Deleting BUY_STOP"," Ordertype:",OrderType());

return(1);

}

}

}

}

}

 
tkuan77:
Hi mladen, i have this particular code that will close all pending trades i have after i have a trade running. how do i set to to only cancel my current existing pending trade after i have triggered a trade instead of all future pending trades?

thanks.....

int new_del()

{

int i,a;

int total = OrdersTotal();

string comentario,par;

for (i=total-1; i >=0; i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if (OrderType()==OP_BUY || OrderType()==OP_SELL)

{

for (a=total-1; a >=0; a--)

{

OrderSelect(a,SELECT_BY_POS,MODE_TRADES);

comentario=OrderComment();

par=StringSubstr(comentario,0,6);

if(OrderType()==OP_SELLSTOP)// && comentario==Symbol())

{

OrderDelete(OrderTicket());

Print("Deleting SELL_STOP"," Ordertype:",OrderType());

return(1);

}

if(OrderType()==OP_BUYSTOP)// && par==Symbol())

{

OrderDelete(OrderTicket());

Print("Deleting BUY_STOP"," Ordertype:",OrderType());

return(1);

}

}

}

}

}

tkuan77

What does the "current pending trade" mean and what does the "future pending trade" mean?

 
mladen:

tkuan77

What does the "current pending trade" mean and what does the "future pending trade" mean?

Hi mladen,

Current pending trade means: once a condition has been met, the ea will set 2 pending trades (1 long and 1 short). So if say the long trade is been triggered, the ea will automatically cancel the short pending trades.

Future pending trade means: after the ea has cancel the previous pending trades and say the condition is met again, the ea by right should set another 2 pending trades (1 long and 1 short). however, if the ea currently has a running trade, the ea will automatically cancel any pending orders the ea is trying to set.

what i am trying to do is to jus let the system cancel only the current pending trade after a trade has been triggered. appreciate the help

regards