//+------------------------------------------------------------------+ //| 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,e1under2,e2under3; 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); e1under2=ema1<ema2; e2under3=ema2<ema3; e1over2=ema1>ema2; e2over3=ema2>ema3; if(OrdersTotal()==0) // one order at the time { // Short Entry static int ticket; if(e1under2 && e2under3) // short function { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"Short Order ",0,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SHORT order opened : ",OrderOpenPrice()); } if(OrdersTotal()==0) // one order at the time if(e1over2 && e2over3) //this may be messy cos old version had ==1 for all variables { if(e1over2 && e2over3) //buy function ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green); //What's 12345 for? I ADDED ASk-30*Point for stop loss 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); } return(0); } return(0); } }that's the code guys!
Wow! Pretty good job of coding for a "newbie". You can write your code much more concisely than I am able.
I notice that you have two identical conditional-if lines for the BUY section, whereas the SELL section only has one.
The second identical condition-if statements for the BUY function is probably harmless, but is it necessary?
if(OrdersTotal()==0) // one order at the time
if(e1over2 && e2over3) //this may be messy cos old version had ==1 for all variables <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<here's the first one
{
if(e1over2 && e2over3) //buy function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<here's the second one
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green); //What's 12345 for? I ADDED ASk-30*Point for stop loss
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);
}
Also, it appear you have both Sell and Buy functions contained inside the same Conditional-if:
if(OrdersTotal()==0) // one order at the time
{
// Short Entry
static int ticket;
if(e1under2 && e2under3) // short function
{ // The matching bracket for this is way below the SELL and BUY function<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<see below
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"Short Order ",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SHORT order opened : ",OrderOpenPrice());
}
if(OrdersTotal()==0) // one order at the time
if(e1over2 && e2over3) //this may be messy cos old version had ==1 for all variables
{
if(e1over2 && e2over3) //buy function
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green); //What's 12345 for? I ADDED ASk-30*Point for stop loss
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);
}
return(0);
} // this is the matching bracket for the one shown above<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<see above
return(0);
}
}
The BUY order is subject to the SELL criteria the way you have the noted conditional-if arranged.
that's the code guys!
The reason that it doesn't ever make a long order is that you have not closed the code block which performs the short order with a closing curly bracket. This means that essentially you are only executing a long order if (e1under2 && e2under3) AND (e1over2 && e2over3) which I imagine won't happen.
You say that you wish to open an order even if there is already an order open in the other direction. From this I assume that you want to limit the number of orders to a max of one in each direction. By including the check that OrdersTotal() is equal to zero, this limits you effectively to 1 order total, not one in each direction. What you need to do is to check the number and type of existing orders using a loop and counters (loads of examples in this site), then use the result to determine whether an order should be made.
I noticed in your other post, how you mentioned you wanted to learn to write MQL so I'm not going to write this for you. This example is exactly the right sort of simple code & logic which is essential in the learning process.
Please do this for yourself and let me know how you get on.
Hey guys, you are legends !!!
Thank you for your comments FXTrader2008 and cloudbreaker. I didn't notice there was a repeated condition for buy, il cut that out, and it makes sense regarding the bracket not being closed. Good idea not to give me the coded answer, as I do want to learn the code myself. I'l have a look online for loop/counters.
Oh the reason the code is so nice and compact is because a friend of mine helped me put it together (who is a programmer).
I'l proceed with my side of things and if I get stuck i'l let you know!
Do you guys trade live markets independently or do you work for metaquotes?
I didn't looked deep in your code, but one first tip:
Try to structure your code more clearly - we don't do this for fun but for better reading and understanding! It has no influence on the "sense" or meaning of the code...
Lets see, how your code SHOULD look (only the essential part):
if(OrdersTotal()==0) // one order at the time
{
// Short Entry
static int ticket;
if(e1under2 && e2under3) // short function
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"Short Order ",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SHORT order opened : ",OrderOpenPrice());
}
if(OrdersTotal()==0) // one order at the time
if(e1over2 && e2over3) //this may be messy cos old version had ==1 for all variables
{
if(e1over2 && e2over3) //buy function
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green); //What's 12345 for? I ADDED ASk-30*Point for stop loss
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);
}
return(0);
}
return(0);
}
As you can see now: You only test for your "short"-condition
// Short Entry
static int ticket;
if(e1under2 && e2under3) // short function
and inside this you test for the "long"-condition, twice - for security ; ) - this can't work.
if(e1over2 && e2over3) //this may be messy cos old version had ==1 for all variables
{
if(e1over2 && e2over3) //buy function
Better: (but far away from perfect)
if(OrdersTotal()==0) // one order at the time
{
// Short Entry
static int ticket;
if (e1under2 && e2under3) // short function
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"Short Order ",0,0,Red);
if (ticket>0)
{
if (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SHORT order opened : ",OrderOpenPrice());
} }
if(e1over2 && e2over3) //buy function {
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green); //What's 12345 for? I ADDED ASk-30*Point for stop loss
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);
}
Now it should work for short and long trades.
I highly recommend to read this book here about MQL4 - especially the section Creation of a normal program - it is very good written and you can easily implement your strategy. I also started so...
If you download the files there and have problems with the comments (since they are cyrillic) open "word", copy&paste the source of each file from the internet-page (since the comments there are english) to word and finally copy&paste from word to Metaeditor. From now on you can study the programs in english.
I hope, I could help...
(By the way: I'm also very new to MQL4 but I work as a programmer and so syntax and structure are no problem for me...)
Greetings TuRRiCAN
Hey Tourrican this is very helpful thank you! Cyrillic no problem, I am Russian, from siberia actually.
Yeah I studied the mql book, but it is still outside of practical learning, its a whole different thing to study it and then to try and write a program yourself (becasue they don't clearly explain with real life examples in each part of the book, why and how those things are used you know.
But i'l get there, the burning desire inside me to make this work is greater than any obstacle.
Do you guys trade live markets independently or do you work for metaquotes?
In answer to your question, I normally work as a chopper pilot. However, given the economic situation, the company I work for do not want to fly at the moment. So, in the interim, I've been developing EAs for a company which is owned by my best mate. We are live trading on behalf of some fairly well known funds.
I'm pleased that you are trying to learn to code and will help when ever I can. Its refreshing to find someone join the forum and want to learn to be self-sufficient.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey guys, thank you for helping me with my code, it's 2.11am in London, and I been trying to decypher this mql enigma for a while now. By the way CodesGuru thank you for tutorials, they are clearer than the standard stuff offered, nice effort!
I am on a newbie quest to coding a strategy and as always need the pro's help in this.
Below is a code I put together, but for some reason it executes only short trades.
I'm stuck not knowing why. Additionally I also need to add this to it: 1. Code must execute both Long and Short trades regardless if there is a trade open in the opposite direction already (eg: If there is a long trade that's open, the code should still enter a short position if the conditions are met). I guess I need to play aroun with 'OrdersTotal()==0; function but I don't know what to do.
Please help. the code is attached. (i tried to attach it but for some reason it does not, any idea what's going on?)
I promise a good bottle of champaigne for the person who helps me most to bring this code to live fruition :-)
ps: I test this in mt4 simulator/strategy tester.
look forwar to your help guys!
nick