Hi, I would really appreciate some help as I am completely new to mql. I programmed a basic strategy, but I need to include a time based exit (eg: Close order if time from entry exceeds or equals to 2 days). Like an inactivity stop in a way. I spent ages trying to figure out the time thing but no luck so far.
Could someone who's wise enough share their knowledge?
Thank you!
Build a function which does the following:
- Notes the current year, and day index number within year (eg. today is approximately 160) using the DayOfYear() function.
- Notes the current time - see date and time functions in the documentation.
- Loops through the order list checking first whether each order is at least 2 days older and if it is exactly 2 days older, then also that the OrderOpenTime() is earlier than the current time.
- Closes any order which matches the above criteria.
Prefer to give you pseudocode rather than coding it up for you, as I'm sure you'd prefer to learn.
Hope this helps,
CB
Build a function which does the following:
- Notes the current year, and day index number within year (eg. today is approximately 160) using the DayOfYear() function.
- Notes the current time - see date and time functions in the documentation.
- Loops through the order list checking first whether each order is at least 2 days older and if it is exactly 2 days older, then also that the OrderOpenTime() is earlier than the current time.
- Closes any order which matches the above criteria.
Prefer to give you pseudocode rather than coding it up for you, as I'm sure you'd prefer to learn.
Hope this helps,
CB
I think it can be slightly less complex for a newbie...
1. Build a loop for all your open orders
2. Inside the loop, after successfully selected an order, then just check
if (TimeCurrent()-OrderOpenTime() > 60*60*48) // current time - order open time greater than 2 days in seconds.
{
Close that order // (hint: use OrderClose function)
}
I think it can be slightly less complex for a newbie...
1. Build a loop for all your open orders
2. Inside the loop, after successfully selected an order, then just check
if (TimeCurrent()-OrderOpenTime() > 60*60*48) // current time - order open time greater than 2 days in seconds.
{
Close that order // (hint: use OrderClose function)
}
Thank you guys! You are great, its an unknown land for a newbie mql, and guides like you help to cross it!
I think it can be slightly less complex for a newbie...
1. Build a loop for all your open orders
2. Inside the loop, after successfully selected an order, then just check
if (TimeCurrent()-OrderOpenTime() > 60*60*48) // current time - order open time greater than 2 days in seconds.
{
Close that order // (hint: use OrderClose function)
}
Hey guys/gals, I'm stuck again. Tried to include this edit, compiler didn't notice any errors, but it still doesn't work. All the trades that lasted many days before, are still there. I think I included it incorrectly as im totally new to mql (and proper coding as well) Here's the code below, please help:
//+------------------------------------------------------------------+
//| N&P 1DailyUpTrendExec.mq4 |
//| Copyright Nick Lou & Pete Arh 2009 |
//| 20090523 |
//| |
//+------------------------------------------------------------------+
extern double Lots=0.01;
extern double TakeProfit=20;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(Bars<75)
{
Print("Bars less than 100");
return(0);
}
//Declaration
double ema1,ema2,ema3,closeup, e1over2, e2over3,range;
ema1= iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0);
ema2= iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
ema3= iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
e1over2=ema1>ema2;
e2over3=ema2>ema3;
if(OrdersTotal()==0) // one order at the time
{
// Long Entry
static int ticket;
if(e1over2 && e2over3) //Basic Condition
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if (TimeCurrent()-OrderOpenTime() > 60*60*48) // I need long position to be closed if it exceeds 2 days being in the market
{
OrderClose(ticket,Lots,Bid,0,Purple); // ***NEW ADDITION 11 June 09, but this doesn't work blin!
}
else Print("Error closing DELAY order: ",GetLastError());
return(0);
}
return(0);
}
***please adjust the code as it should look and then i'l learn from the change, otherwise i will get confused again
Hey guys/gals, I'm stuck again. Tried to include this edit, compiler didn't notice any errors, but it still doesn't work. All the trades that lasted many days before, are still there. I think I included it incorrectly as im totally new to mql (and proper coding as well) Here's the code below, please help:
//+------------------------------------------------------------------+
//| N&P 1DailyUpTrendExec.mq4 |
//| Copyright Nick Lou & Pete Arh 2009 |
//| 20090523 |
//| |
//+------------------------------------------------------------------+
extern double Lots=0.01;
extern double TakeProfit=20;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(Bars<75)
{
Print("Bars less than 100");
return(0);
}
//Declaration
double ema1,ema2,ema3,closeup, e1over2, e2over3,range;
ema1= iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0);
ema2= iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
ema3= iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
e1over2=ema1>ema2;
e2over3=ema2>ema3;
if(OrdersTotal()==0) // one order at the time
{
// Long Entry
static int ticket;
if(e1over2 && e2over3) //Basic Condition
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if (TimeCurrent()-OrderOpenTime() > 60*60*48) // I need long position to be closed if it exceeds 2 days being in the market
{
OrderClose(ticket,Lots,Bid,0,Purple); // ***NEW ADDITION 11 June 09, but this doesn't work blin!
}
else Print("Error closing DELAY order: ",GetLastError());
return(0);
}
return(0);
}
***please adjust the code as it should look and then i'l learn from the change, otherwise i will get confused again
Have you checked for any errors in the journal?
It may be good to set slippage value greater than 0.
Have you checked for any errors in the journal?
It may be good to set slippage value greater than 0.
Hey robofx, thanks for repying! Yep, the journal produces the same result as without the below code
if (TimeCurrent()-OrderOpenTime() > 60*60*48) // I need long position to be closed if it exceeds 2 days being in the market
{
OrderClose(ticket,Lots,Bid,0,Purple); // ***NEW ADDITION 11 June 09, but this doesn't work blin!
But the key is I need to make this code work. Because I'm only learning coding, I don't think I inputted it correctly into the main code. Does that make sense?
I can set the slippage to greater than 0, and will do when the strategy is finished, but at the moment that is not the problem. The problem is that the mt4 doesn't recognise the new addition to the code (ie the bit above) for some reason. Do you or anyone else have any ideas why?
Hey robofx, thanks for repying! Yep, the journal produces the same result as without the below code
if (TimeCurrent()-OrderOpenTime() > 60*60*48) // I need long position to be closed if it exceeds 2 days being in the market
{
OrderClose(ticket,Lots,Bid,0,Purple); // ***NEW ADDITION 11 June 09, but this doesn't work blin!
But the key is I need to make this code work. Because I'm only learning coding, I don't think I inputted it correctly into the main code. Does that make sense?
I can set the slippage to greater than 0, and will do when the strategy is finished, but at the moment that is not the problem. The problem is that the mt4 doesn't recognise the new addition to the code (ie the bit above) for some reason. Do you or anyone else have any ideas why?
This is because you put that piece of code (force close trades after 2 days) under the if(OrdersTotal()==0),
Try to think yourself, if you successfully open a trade, the OrdersTotal() will be 1, so it will never run your code below that if ... Got it?
Some other minor bugs too, anyway, I fixed the code for you. Here is a working one...
This is because you put that piece of code (force close trades after 2 days) under the if(OrdersTotal()==0),
Try to think yourself, if you successfully open a trade, the OrdersTotal() will be 1, so it will never run your code below that if ... Got it?
Some other minor bugs too, anyway, I fixed the code for you. Here is a working one...
Hi migmof, I think I see what you mean (the code should be outside the function so it is always checked for). Sorry for the silly mistake, mql is not just a new language but a whole new planet for me (i'm a psychologist by background :)
Hi migmof, I think I see what you mean (the code should be outside the function so it is always checked for). Sorry for the silly mistake, mql is not just a new language but a whole new planet for me (i'm a psychologist by background :)
Haha, a psychologist stepping into programming world.. Good luck to your adventure.... Some background information for you.
MQL4 is based on C programming language, and the upcoming release of MQL5 will be C++ based, an Object Oriented Programming model, ie using classes, inheritance and Polymorphism). Perhaps some introductory C/C++ programming books will be able to help you...
Haha, a psychologist stepping into programming world.. Good luck to your adventure.... Some background information for you.
MQL4 is based on C programming language, and the upcoming release of MQL5 will be C++ based, an Object Oriented Programming model, ie using classes, inheritance and Polymorphism). Perhaps some introductory C/C++ programming books will be able to help you...
Oh my god, will I now need to relearn mql5!!!?!?!! It's tough enough as it is you know! haha
Yep i knew that mql is based on C, to be honest best learning source is you guys, you point me in the right direction then I dig out mql book and go through bits by bits. Otherwise I can spend a month going through books by myself and not get anywhere (this is what happened in the past :)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, I would really appreciate some help as I am completely new to mql. I programmed a basic strategy, but I need to include a time based exit (eg: Close order if time from entry exceeds or equals to 2 days). Like an inactivity stop in a way. I spent ages trying to figure out the time thing but no luck so far.
Could someone who's wise enough share their knowledge?
Thank you!