adventures of a newbie - page 10

 
niko:
Your wish is my command (i was gonna say 'high flyer' but then checked dictionary online and apparently its a rude comment, nevermind)

Ok. I've had only just a quick look as you are now being a bit too random for my liking.

You have not based your changes on the latest version of my code.

Therefore, I have no idea whether the latest version of my code did what you needed, before you started to add further stuff to the previous version.

But what I did notice was that right at the start of the start() function your opening curly brace is missing. I thought we'd been through this.

You need to exercise some control. If you are going to change something - make sure you add your change to the latest version. And let us know what your change is and why you're making it.


CB.

 
niko wrote >>

Tim,

I got the code to point where there are no errors reported when it is compiled (it was just fixing a few bugs you highlighted), even though in strategy tester it does not make any trades yet. But I am puzzled at something: ...

Hi Nick,

Looking at your comments here and some of the changes you have made to the code, I suspect your 'puzzlement' may be coming about because of confusion between variables and function parameters. They look and act rather similarly but in fact they are quite different beasts.


Perhaps the commented code example below will make things a bit clearer.


I have taken the liberty of tidying up the development code a bit more and changing a few function parameter names a a little bit to try and differentiate them from other variables. I also moved all the superfluous notes, comments etc to an "Appendum" at the end of the file for you to do with as you wish.

The code is looking good . There are still a few little odds and ends to do which I have marked in the file AS TODO's, but the main task ahead of you now is debugging and testing.


Testing is a very important part of the development cycle. With trading systems, failure to test properly can result in subtle problems that can play havoc with your trading strategy and ultimately your account. The main task with testing is to make ensure that the code is doing what you expect it to do. Effectively you have to either write down or say to youself, "In this situation, I expect this outcome". You then devise and carry out your test to see if this is actually what happens. If it doesn't you have to rectify the problem. You repeat this process for all the likely situations that you can envisage and perhaps some unlikley ones as well . You are also of course looking for runtime errors or bugs that may or may not show up with MetaTrader system error messages.


With MT4, your main testing tool will be the Strategy Tester and log files (under the Journal Tab) as well as the Results, Graph and Report faciities.


With the log file I often find it easier to open the whole file in Microsoft Notepad rather than trying to look at part of it through the Journal viewer. Dont forget to clear the log file/journal before each run or you will finish up looking through a lot of old, and perhaps no longer relevant information. After each test run in the strategy tester, you examine the log file for both debug statements messages as well as metatrader system messages . You can use your MetaTrader onine documentation to look ap error codes for additional information. Also use the Results, Graphs and Report facilities to see what the program is doing in any particular set of conditions.


Hint - When you do your initial test runs, keep the test period short, to minimise the amount of data in the log file.


We have already built some debug facilities into the code but you will probably need to expand on these. Disable individual debug statements as required to keep the information in the log file condensed and relevant. There is an example in the code of how to do this without deleting or commenting out debug statements which can be quite time consuming. I leave my main debug statemants in the program file indefinetly and just turn them all off with the DEBUG_ON flag in the program once I have finished testing.


Managing the log files and debug statements efficiently is, I believe, the key to good and efficient testing and debugging in the MetaTrader environment. I have seen people on this forum grumbing about the lack of debugging facilities in MetaTrader. Millions of lines of commercial code have been successfully degbugged and tested using facilities no more sophisticated than what you find in MetaTrader. These facilities may be basic but they work fine, providing you know how to use them properly.


Another point worth making is that trying to debug messy or poorly organised code can be very time consuming and frustrating. It is from this point onwards that you will really start to reap the benefits of your well structured and well commented code. If anyone else ever needs to work on your code, they will be very grateful to you for your efforts in this respect.


Nick, this journey stil has a way to go yet. For me, the testing, debugging and optimizing - the "getting it all working stuff" is the most interesting part of the job, and often the most challenging. I'll pass the code back to to you to get started with testing and wait for you to get back to me with questions and/or problems that you encounter.


Regards

Tim



A simple code example to try and demonstrate the difference between a global variable and a function parameter

//+------------------------------------------------------------------+
//| Hello Trader.mq4 |
//| Copyright © 2009, Your Name |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Your Name"
#property link ""


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
stringMyGlobalVariable; // This global variable which holds a string i.e text value has been declared
// but as yet has no value in it. It is a 'container' which at this point in time holds
// only an empty or null string.

int init()

{

MyGlobalVariable = "Nick"; // Now we assign i.e put a value in the variable. Our 'container' which is named
// MyGlobalVariable now holds the string value of "Nick"

return(0);
}


//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{

return(0);
}


//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{

MyFunction(MyGlobalVariable); // This calls the function named MyFunction which is declared below
// This function requires one string parameter.

// In this case we have passed it our global variable as a parameter.

// MyFunction("Fred"); // We could also pass this function the literal string "Fred"

// MyFunction(12.739); // This would not work. The passed value must be a string because that is what we
// have declared the parameter as in the function declaration below.

// MyFunction(DoubleToStr(12.739,3)); // This would work because we have used an inbuilt MT4 function to convert the decimal
// number to a string i.e "12.739").
// We can also pass other functions as parameters as we have shown here, provided the
// function that we are using as a parameter returns the correct data type which in
// this case is a string.

return(0);
}


//+------------------------------------------------------------------+
//| A function with one parameter |
//+------------------------------------------------------------------+


void MyFunction(string MyFunctionParameter) // The function is declared with a parameter in the same way that a varible is declared
// but it is a function parameter not a variable. It can be used just like a variable
// but only inside the function. The big difference is that it can be passed a different value
// from outside the function whenever the function is called. This enables functions to
// be used as general purpose blocks or modules.
// Function parameters are also used in some circumstances to return values from the
// function to the outside world but that is another more complex story for another day!

{
Comment(MyFunctionParameter); // The value passed to 'MyFunctionParameter' will be displayed at the top, left hand
// corner of the screen

}

 
hey CB, that's strange as I always save the mq4 file as new, with latest date to separate the other files. confusion must have slipped through. the bracket i wanted to include, but i thought as some point you asked me to remove it (it was the last one, but without the last one, the start() would be unbalanced. so that's why I removed the first one as well, but i'l put it back).
 
niko:
hey CB, that's strange as I always save the mq4 file as new, with latest date to separate the other files. confusion must have slipped through. the bracket i wanted to include, but i thought as some point you asked me to remove it (it was the last one, but without the last one, the start() would be unbalanced. so that's why I removed the first one as well, but i'l put it back).

Let's not start adding and removing braces willy-nilly. I had previously pointed out that you had a completely superfluous set, both of which could be removed.

Here is the latest version. Please do the following:

- Compile it first and fix any errors - that way you will know that any subsequent errors are due to changes you may make

- Let me know whether or not there were any errors you had to fix

- Give it a try and see if it does what you need

- Let me know how that goes - and let me know what changes you think you need to make


CB

Files:
 
Thank's CB. I'l get onto it, sorry if I frustrated you a bit
 

Thanks Tim,

I really appreciate the length to which you go to explain these things to me, it is very encouraging. I seem to get 'flashes' of getting it and then not getting it at all (regarding functions and calls) even though I must have read the documentation on functions twenty times already. I'l spend more time on it. About 'testing and debugging' I have done this in a different package before (going through every trade that was made, ensuring everything fitted together) as I think it's essential, even for manual trading (strategy testing). I shall proceed with your code and let you know.

 
niko:
Thank's CB. I'l get onto it, sorry if I frustrated you a bit

No worries mate. Just trying to take things one step at a time.


CB

 

Hey CB,


I fixed a few bugs in the code, it was mainly missing parenthesis, it's highlighted in the code. No more syntax errors.

I run the code on strategy tester, but it only does BUY trades. No short trades at all. I looked through logs, it producers error 130 (to do with stops) so that doesn't shed any light on our problem.

I tried to see if I can figure out where the error is (I assume it is in fnSHouldWeTrade or fnOrderDuplicate).

Latest version of code attached, I added values to TopFilter and BotFilter to enable the strategy to make trades.

ps: i figured out why i pasted the old code for you before, i reinstalled mt4 into a different folder (no connection errors) and code was an old duplicate from old folder

 
niko:

Hey CB,


I fixed a few bugs in the code, it was mainly missing parenthesis, it's highlighted in the code. No more syntax errors.

I run the code on strategy tester, but it only does BUY trades. No short trades at all. I looked through logs, it producers error 130 (to do with stops) so that doesn't shed any light on our problem.

I tried to see if I can figure out where the error is (I assume it is in fnSHouldWeTrade or fnOrderDuplicate).

Latest version of code attached, I added values to TopFilter and BotFilter to enable the strategy to make trades.

ps: i figured out why i pasted the old code for you before, i reinstalled mt4 into a different folder (no connection errors) and code was an old duplicate from old folder


Change:

OrderSend(Symbol(),OP_SELL,Lots,Bid,5,0,Bid+TakeProfit*Point,"Nikos First EA",0,0,Red);

to:

OrderSend(Symbol(),OP_SELL,Lots,Bid,5,0,Ask-TakeProfit*Point,"Nikos First EA",0,0,Red);


CB

 
thank CB, i should have noticed that, so the error was right in the end (130).
Reason: