| / | Forum |
|
niko
2009.06.15 17:44
Hello legends of mql. I cooked up the pseudo code for you during my lunch hour at work. Here it is. let me know what's missing and i'l add it on. N&P Pseudo Code If ema7<ema14<sma50 and PriceNow is > BottomFilterLevel (eg: 1.1508 for eurusd) then: If BUY position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD. manually. (ideall we put this into OrderSend function to keep code shorter). ----GBPUSD--- ----USDJPY--- ---USDCHF--- ---AUDUSD--- **Code specifics **no money management code for this one So how does this look? |
|
niko
2009.06.15 21:57
niko wrote >>
Hello legends of mql. I cooked up the pseudo code for you during my lunch hour at work. Here it is. let me know what's missing and i'l add it on. N&P Pseudo Code If ema7<ema14<sma50 and PriceNow is > BottomFilterLevel (eg: 1.1508 for eurusd) then: If BUY position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD. manually. (ideall we put this into OrderSend function to keep code shorter). ----GBPUSD--- ----USDJPY--- ---USDCHF--- ---AUDUSD--- **Code specifics **no money management code for this one So how does this look? Hey guys, the basic tutorial on coding really helped to get my head around this (although the detailed coding part is lacking). To show you I have been busy, I attached the code below. The idea i think is there but it still returns lots of errors. You will see everything is under one big bracket in Start() the reason I did that is because mql kept coming back with error that variable can't be declared globally, and when i made it all in 1 bracket it seemed to have stopped that error (although lots of other errors persisted). I used notepad++ to check the brackets align. So did am I at least going the right way with this? //+------------------------------------------------------------------+ //| N&P 1DailyUpTrendExec.mq4 | //| Copyright Nick Lou & Pete Arh 2009 | //| 20090523 | //| | //+------------------------------------------------------------------+ extern double Lots=0.01; extern double TakeProfit=20; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ //-------------------Declaring All Variables and Conditions //--------------------declaration end int init() { return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } // Order counting code // Return: 0 = no orders // >0 = # long orders // <0 = # short orders int CalcOpenOrders() { int long=0,short=0; for(int i=0;i<OrdersTotal();i++) //i set to 0 to stop bugs from //crawling in, i<Orderstotal means if i is behind the number of orders //in market then start counting orders and make i that number { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break; //this function selects an order for further processing if(OrderType()==OP_BUY)long++; if(OrderType()==OP_SELL)short++; } if(long>0)return(long); if(short>0)return(-short); } //------------ (fingers crossed this is right). I still don't get it why we need to count orders. //--------------DECLARATION OF VARIABLES------------------------- double ema1,ema2,ema3,closeup, e1over2, e2over3, e1under2, e2under3, eurusdbuyok, eurusdshortok; //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start () { 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(Bars<75) { Print("Bars less than 100"); return(0); } //------------------EURUSD Block------------------------- //check order type, if it doesn't equal to buy already then buy //here i am setting a condition which will be //checked before a trade is opened, it shoudl state 'It's okay to enter //into long trade so long as there is either 1 order in place already or //none) // Call function. the returnvalue (output) of the function is stored into ReturnVal int ReturnVal = CalcOpenOrders(); // Check output of function if (ReturnVal >0) // >0 = long orders. See CalcOpenOrders() eurusdbuyok=1; else { eurusdbuyok=0; return(0); } if (ReturnVal<0) // <0 = short orders. See CalcOpenOrders() eurusdshortok=1; else { eurusdshortok=0; return(0); } //--------------condition ends if(eurusdshortok==1) { static int ticket; // deleted if(OrdersTotal()==0) if(e1under2 && e2under3) // short function { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"ShortOrder ",0,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SHORT order opened : ",OrderOpenPrice()); } } return(0); } // ------------------------------------------------------------------------------------------- if (eurusdbuyok==1) { static int ticket1; // deleted if(OrdersTotal()==0) if(e1over2 && e2over3 && eurusdbuyok==1) //buy function { ticket1=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(ticket1>0) { if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } } return(0); } return(0); }this is the latest version of the code. syntatically everthing seems to be ok, but when i run it through strategy tester it returns no trades. something is wrong with logic. need help. I'm becoming quite familiar with code now, well the structure of program side of it. |
|
TSWilson
2009.06.16 05:06
niko wrote >>
Hello legends of mql. I cooked up the pseudo code for you during my lunch hour at work. Here it is. let me know what's missing and i'l add it on. N&P Pseudo Code If ema7<ema14<sma50 and PriceNow is > BottomFilterLevel (eg: 1.1508 for eurusd) then: If BUY position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD. manually. (ideall we put this into OrderSend function to keep code shorter). ----GBPUSD--- ----USDJPY--- ---USDCHF--- ---AUDUSD--- **Code specifics **no money management code for this one So how does this look? Hi Niko Your pseudo code looks pretty good for a first attempt. I would however like to see it a bit more structured and there are a couple of questions that it raises for me. I was able to easily and quickly put it into the sort of format that I would typically use myself. It is attached here as a text file for you to have a look at. You will need to save this file and open it in notepad or a similar editor to see the formatting. Note that at this stage we are not writing in any specific computer language . We are just trying to specify clearly and unambiguously what we are trying to do. You may notice that the pseudo code is quite specific and "legalistic". This is how we have to talk to computers. We need to spell out very clearly what it is we want them to do. Otherwise they have a tendency to generate garbage. You will also notice the use of "Blocks" to arrange things into logical groups. These may be useful later on to help us structure the code properly. As someone pointed out earlier in this discussion, we dont do this just for fun. We do it to make the code more readible, more understandable, more maintainable and ultimately more reliable and bug free. The later is extremely important in software that is going to be used for live trading inless of course you have buckets of money that you are willing to throw away :) . Seriously, trading is difficult enough without also having to deal with buggy software. Please have a look at what I have sent you and feel free to make any changes that you think are necessary. There are a couple of important points missing from my pseudo code. Can you spot them? You may also like to deal with the two questions that I have raised in the pseudo code. A number of other questions come to mind but these can be better left until the actual coding phase. If you have not already done so you might want to try out the the Notepad++ editor that FXtrader2008 mentioned earlier in this discussion. Use any tools that you can find that make life a bit easier. Please post your revised pseudo code back to me as a text file attachment. I find trying to write structured code, pseudo or otherwise, in this HTML editor a bit tedious and messy. Regards Tim |
|
niko
2009.06.16 11:52
TSWilson wrote >>
Hi Niko Your pseudo code looks pretty good for a first attempt. I would however like to see it a bit more structured and there are a couple of questions that it raises for me. I was able to easily and quickly put it into the sort of format that I would typically use myself. It is attached here as a text file for you to have a look at. You will need to save this file and open it in notepad or a similar editor to see the formatting. Note that at this stage we are not writing in any specific computer language . We are just trying to specify clearly and unambiguously what we are trying to do. You may notice that the pseudo code is quite specific and "legalistic". This is how we have to talk to computers. We need to spell out very clearly what it is we want them to do. Otherwise they have a tendency to generate garbage. You will also notice the use of "Blocks" to arrange things into logical groups. These may be useful later on to help us structure the code properly. As someone pointed out earlier in this discussion, we dont do this just for fun. We do it to make the code more readible, more understandable, more maintainable and ultimately more reliable and bug free. The later is extremely important in software that is going to be used for live trading inless of course you have buckets of money that you are willing to throw away :) . Seriously, trading is difficult enough without also having to deal with buggy software. Please have a look at what I have sent you and feel free to make any changes that you think are necessary. There are a couple of important points missing from my pseudo code. Can you spot them? You may also like to deal with the two questions that I have raised in the pseudo code. A number of other questions come to mind but these can be better left until the actual coding phase. If you have not already done so you might want to try out the the Notepad++ editor that FXtrader2008 mentioned earlier in this discussion. Use any tools that you can find that make life a bit easier. Please post your revised pseudo code back to me as a text file attachment. I find trying to write structured code, pseudo or otherwise, in this HTML editor a bit tedious and messy. Regards Tim Hey TSW. Thank you for this. I can see you put quite a bit of time into this so I really appreciate it. I attached the updated pseudo code, but in case it didn't attach, here it is below. I tried to see what you missed out, but couldn't quite figure it out, a few things I added but they were just clarifications of things. Where do we go from here? Whilst we are creating this 'proper' code, I am still trying to solve the puzzle of the code above. It doesn't follow our pseudo code as it is an old patched up code, but I am still quite puzzled with it as it should work, at least for 1 currency. Key thing is building it in this unorganised haphazard way is teaching me the actual coding elements (like how brackets affect the script, where to declare variables, etc). Any ideas on why that code doesn't work as it is now? Program Title - N&P Pseudo Code START BLOCK - List of Currency Pairs EURUSD GBPUSD USDJPY USDCHF AUDUSD END BLOCK - List Of Currency Pairs START BLOCK - Configuration Parameters Set the value of TopFilterLevel manually on a daily basis for EURUSD Set the value of BottomFilterLevel manually on a daily basis EURUSD Set the value of TopFilterLevel manually on a daily basis for GBPUSD Set the value of BottomFilterLevel manually on a daily basis GBPUSD Set the value of TopFilterLevel manually on a daily basis for USDJPY Set the value of BottomFilterLevel manually on a daily basis USDJPY Set the value of TopFilterLevel manually on a daily basis for USDCHF Set the value of BottomFilterLevel manually on a daily basis USDCHF Set the value of TopFilterLevel manually on a daily basis for AUDUSD Set the value of BottomFilterLevel manually on a daily basis AUDUSD Set Lot size manually for All positions at once (same lot size) END BLOCK - Configuration Parameters START BLOCK - Entry Rules If the 7 Period Exponential Moving Average is greater than the 14 Period Exponential Moving Average AND the 14 Period Exponential Moving Average is greater than the 50 period Simple Moving Average AND Current Price is less than TopFilterLevel THEN Signal a BUY (Long) entry condition If the 7 Period Exponential Moving Average is less than 14 Period Exponential Moving Average AND the 14 Period Exponential Moving Average is less than the 50 period Simple Moving Average AND Current Price is less than BottomFilterLevel THEN Signal a SELL (Short) entry condition *** Question *** What time periods do you want to use for your moving averages? Are the periods Minutes, Hours, Days, Weeks, Months or do they need to be variable? ***Answer *** Excellent question. It will be 5 minute periods for all moving averages, they do not need to be variable. END BLOCK - Entry Rules START BLOCK - Manual Close **** Question *** You say "Keep being in the market until the position is closed manually. (ideall we put this into OrderSend function to keep code shorter)." What exactly do you want the program to do in this block? ***I was unclear on this before, my apologies. There should be no block as this in the code. I will exit positions manually when needed through the trade/terminal window (I assume that’s possible and won’t mess up the EA execution, right). So actually I don’t know if we need to code this flexibility or not? What do you think END BLOCK - Manual Close START BLOCK - Exit Rules If any Open Position is greater than or equal to 20 pips from the entry level THEN Close the Position END BLOCK - Exit Rules START BLOCK - Main - This block controls the whole flow of the program With each Currency Pair in the List of Currency Pairs block do all of the following Process the Exit Rules Block - (Note that in practice this would normally just be the automatic execution of a Take Profit Stop but we should mention it here in the pseudo code for completness. Of course you may have something else in mind such as using the program to close an open position—nope not for this code.) Check the Entry Rules block to see if a BUY or SELL condition exists for this Currency Pair Check to see if there is already an open BUY position for this Currency Pair Check to see if there is already an open SELL position for this Currency Pair If there is no open BUY position for this currency pair AND a Buy condition exixts for this currency pair THEN Open a BUY Position for this currency pair Size: Pre-determined Lots ELSE If there is already 1 open BUY position for this currency pair DO NOT OPEN BUY Position for this currency pair. If there is no open SELL position for this currency pair AND a SELL condition exixts for this currency pair THEN Open a SELL Position for this currency pair Size predetermined lots. ELSE If there is already 1 sell position open for this currency pair Do not open SELL Positions for this currency pair END BLOCK - Main PS: I will often use just 1 direction on a currency pair (eg: just short, for eurusd for 2 days for instance). How could we build this into the code. My assumption is I could just put ‘//’ infront of the parts of the code I don’t want to use (Eg: infront of buy or sell orders within each currency pair) and then remove them when I need to use that part of code next time. Will this work with a code structured in this way? **I can’t really think of anything you left out on purpose, to be honest. Unless it’s to check if there are sufficient funds for the trade, but that’s not necessary yet. Maybe in future versions of the strategy. |
|
niko
2009.06.16 19:31
niko wrote >>
Hey guys, the basic tutorial on coding really helped to get my head around this (although the detailed coding part is lacking). To show you I have been busy, I attached the code below. The idea i think is there but it still returns lots of errors. You will see everything is under one big bracket in Start() the reason I did that is because mql kept coming back with error that variable can't be declared globally, and when i made it all in 1 bracket it seemed to have stopped that error (although lots of other errors persisted). I used notepad++ to check the brackets align. So did am I at least going the right way with this? this is the latest version of the code. syntatically everthing seems to be ok, but when i run it through strategy tester it returns no trades. something is wrong with logic. need help. I'm becoming quite familiar with code now, well the structure of program side of it.Hey guys/gals, any help with the above bit of code (not the pseudo code but the code code) while i'm learning how to do it the proper way. I had a brainstorm today, I think maybe the error is there is one function counting the orders, and instead maybe I need 2 functions separately, one counts the buys and another sells. I programmed the code below (changed a few things to account for this), you can see below. But still it was all messed up as I am still a complete beginner. Any ideas on this one? //+------------------------------------------------------------------+
//| N&P 1DailyUpTrendExec.mq4 |
//| Copyright Nick Lou & Pete Arh 2009 |
//| 20090523 |
//| |
//+------------------------------------------------------------------+
extern double Lots=0.01;
extern double TakeProfit=20;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
//-------------------Declaring All Variables and Conditions
//--------------------declaration end
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
// Order counting code
// Return: 0 = no orders
// >0 = # long orders
// <0 = # short orders
int CalcOpenOrders()
{
int long=0,short=0;
for(int i=0;i<OrdersTotal();i++)
//i set to 0 to stop bugs from
//crawling in, i<Orderstotal means if i is behind the number of orders
//in market then start counting orders and make i that number
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
//this function selects an order for further processing
if(OrderType()==OP_BUY)long++;
if(OrderType()==OP_SELL)short++;
}
if(long>0)return(long);
if(short>0)return(-short);
}
//------------ (fingers crossed this is right). I still don't get it
why we need to count orders.
//--------------DECLARATION OF VARIABLES-------------------------
double ema1,ema2,ema3,closeup, e1over2, e2over3, e1under2, e2under3,
eurusdbuyok, eurusdshortok;
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start ()
{
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(Bars<75)
{
Print("Bars less than 100");
return(0);
}
//------------------EURUSD Block-------------------------
//check order type, if it doesn't equal to buy already then buy
//here i am setting a condition which will be
//checked before a trade is opened, it shoudl state 'It's okay to enter
//into long trade so long as there is either 1 order in place already or
//none)
// Call function. the returnvalue (output) of the function is
stored into ReturnVal
int ReturnVal = CalcOpenOrders();
// Check output of function
if (ReturnVal >0) // >0 = long orders. See CalcOpenOrders()
eurusdbuyok=1;
else
{
eurusdbuyok=0;
return(0);
}
if (ReturnVal<0) // <0 = short orders. See CalcOpenOrders()
eurusdshortok=1;
else
{
eurusdshortok=0;
return(0);
}
//--------------condition ends
if(eurusdshortok==1)
{
static int ticket;
// deleted if(OrdersTotal()==0)
if(e1under2 && e2under3) // short function
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"ShortOrder
",0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SHORT order opened : ",OrderOpenPrice());
}
}
return(0);
}
// -------------------------------------------------------------------------------------------
if (eurusdbuyok==1)
{
static int ticket1;
// deleted if(OrdersTotal()==0)
if(e1over2 && e2over3 && eurusdbuyok==1) //buy function
{
ticket1=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(ticket1>0)
{
if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
}
return(0);
}
return(0);
}
|
|
TSWilson
2009.06.17 10:05
niko wrote >>
Hey guys/gals, any help with the above bit of code (not the pseudo code but the code code) while i'm learning how to do it the proper way. I had a brainstorm today, I think maybe the error is there is one function counting the orders, and instead maybe I need 2 functions separately, one counts the buys and another sells. I programmed the code below (changed a few things to account for this), you can see below. But still it was all messed up as I am still a complete beginner. Any ideas on this one? Hi Niko With reference to the pseudo code From the code: "Use 5 Minute Time Frame for Indicators"
I will exit positions manually when needed through the trade/terminal window (I assume that’s possible and won’t mess up the EA execution, right). So actually I don’t know if we need to code this flexibility or not? What do you think
There should be no problem coding the EA so that you can use the terminal to close a position manually without messing anything up. ELSE If there is already 1 open BUY position for this currency pair DO NOT OPEN BUY Position for this currency pair. ELSE If there is already 1 sell position open for this currency pair Do not open SELL Positions for this currency pair
stated what you DO want the program to do. In my opinion the pseudo code is the place to carefully think through your logic. Otherwise you are likely to get into a mess when you start actual coding. You said: PS: I will often use just 1 direction on a currency pair (eg: just short, for eurusd for 2 days for instance). How could we build this into the code. My assumption is I could just put ‘//’ infront of the parts of the code I don’t want to use (Eg: infront of buy or sell orders within each currency pair) and then remove them when I need to use that part of code next time. Will this work with a code structured in this way?
Over a given period you may want to be long EURUSD, short GBPUSD and not trade USDCHF at all. This is quite easily accomplished but I'd like to see you have a go at trying to work out to how to describe it yourself in the pseudo code.
1. have a look at how your levels are set up and employed 2. The levels are represented as decimal numbers. For go/no go situations is is a common practice to use another type of representation known as a boolean or flag. Would it be possible to use flags to accomplish what you want to do?
**I can’t really think of anything you left out on purpose, to be honest. Unless it’s to check if there are sufficient funds for the trade, but that’s not necessary yet. Maybe in future versions of the strategy.
What items of information do you need to place an order? Where and how are each of these items described and handled in the psuedo code?
Where do we go from here? Whilst we are creating this 'proper' code, I am still trying to solve the puzzle of the code above. It doesn't follow our pseudo code as it is an old patched up code, but I am still quite puzzled with it as it should work, at least for 1 currency. Key thing is building it in this unorganised haphazard way is teaching me the actual coding elements (like how brackets affect the script, where to declare variables, etc). Any ideas on why that code doesn't work as it is now?
It will make the coding "sooo much easier" as you will soon see.
Cheers Tim |
|
niko
2009.06.17 11:36
Hey Tim, As always I really appreciate your time with me. I will go ahead and modify the pseudo code so it reflects your comments. About real code, no worries, let's do it properly (i'm just a bit impatient at times :), as learning is more powerful than anything. |
|
niko
2009.06.17 23:47
Hey Tim, I went through the pseudo code again and did as much as my newbie brain could figure out at this point. Please find it attached, I look forward to your comments! |
|
TSWilson
2009.06.18 12:20
niko wrote >>
Hey Tim, I went through the pseudo code again and did as much as my newbie brain could figure out at this point. Please find it attached, I look forward to your comments! Hi Nick That is all looking good. I went through your latest psuedo code and answered some questions etc. I think we are just about ready to start coding.
Now lets talk about the MT4 language.
You will be required to give each parameter a name, a type and an intial value
The deinit function is called every time the EA stops when you drag it off a chart or click the Expert advisor button OFF
Tim (PS we can talk on skype if needs be but lets just see how we go with the forum for the time being) |
|
niko
2009.06.18 15:39
Hey Tim, As always your help and time is highly invaluable! As a gesture of thanks I would like you to send you a very nice bottle of champaigne once we finished with the coding process. Yep, I been through the mql book quite a few times, the difficulty was putting the theory into practice. I also printed out about 10 or so EA's posted on this website, to see hwo things are coded and put together, to try and understand them. And I spend a lot of time in simulator trader on MT4 (ie backtesting and learning already existing EA's). I'l start the coding process, and make blocks very clear and separate to help both of us see what's going on. And send them to you via forum. 1 Question: You said brokerages put in things to stop aggressive pipping. 1. What do you define aggressive pipping? 2. What things do they put in place? I used to manually scalp eurusd on 1min timeframes (with a spreadbetting method), this was going really well until the brockerage clicked in and started to put delays for execution (entry and exit). So now there's no point at all doing this with this brokerage, even though it's illegal, delays will still happen and mess up the whole day (like if you typically scalp just for 1 hour, 2 delays and you lost the valuable time). I got all money back for delayed trades after aggressive threatening of court action. 2 Question: Do you use different brokers? What brokers would you recommend? (if you are allowed to mention names here) thanks, nick |