How to code? - page 306

 

Hi,

I wasnt really getting anywhere with the functions, I checked them countless times to no avail so decided to write lots of Print() messages at each stage of my code to see how far it gets before clonking out. It actually doesnt get as far as the GetLots function and fails when checking my buy and sell criteria. Weirdly I havent changed this part of the code but something is a miss- but at least I now know where to focus on!

 

Hello all,

I tested my code earlier today and it worked relatively well. Without changing the code or backtest criteria (like dates etc) I did a backtest an hour later and the results are completely different, as in no results what so ever. So im just wondering why this happened? For instance MT4 connectivity?

 

...

If you did not change any parameters, than check the following : when you run the back-test open the journal tab and check if some errors are written out there. Errors in orders execution could be the one cause erratic results. Also check if you buy and sell criteria are :solid" : ie - they will enter aways on a same point.

crsnape@btinternet.com:
Hello all, I tested my code earlier today and it worked relatively well. Without changing the code or backtest criteria (like dates etc) I did a backtest an hour later and the results are completely different, as in no results what so ever. So im just wondering why this happened? For instance MT4 connectivity?
 

I dont get any errors in my journal. First entry is this:

2012.07.22 15:35:37 H4 Period EA EURUSD,H4: loaded successfully

Followed by a huge list of these:

2012.07.22 15:47:07 TestGenerator: unmatched data error (volume limit 56304 at 2012.07.19 12:00 exceeded)

and then finally:

2012.07.22 15:47:07 H4 Period EA inputs: MagicNumber=42; RiskRewardRatio=3; D1SlowMAPeriod=200; D1FastMAPeriod=50; FastMACDPeriod=12; SlowMACDPeriod=26; SignalPeriod=9; DNCPeriod=120; SL_Period=10;

But then nothing afterwards.

My test was done from 04.01.2012 - 21.07.2012.

 

...

Then you have a problem with entry conditions (your EA never tried to open an order)

crsnape@btinternet.com:
I dont get any errors in my journal. First entry is this:

2012.07.22 15:35:37 H4 Period EA EURUSD,H4: loaded successfully

Followed by a huge list of these:

2012.07.22 15:47:07 TestGenerator: unmatched data error (volume limit 56304 at 2012.07.19 12:00 exceeded)

and then finally:

2012.07.22 15:47:07 H4 Period EA inputs: MagicNumber=42; RiskRewardRatio=3; D1SlowMAPeriod=200; D1FastMAPeriod=50; FastMACDPeriod=12; SlowMACDPeriod=26; SignalPeriod=9; DNCPeriod=120; SL_Period=10;

But then nothing afterwards.

My test was done from 04.01.2012 - 21.07.2012.
 

Yes its strange because before it was entering positions. I have some print() written in my code from beginning to end and nothing gets written to the journal at all to record even the very beginnings of my code. Not even this one to check if a previous position has been opened already on the current bar:

if ((GlobalVariableGet (barsGV) == 0) || (GlobalVariableGet (barsGV) < Bars))

{

GlobalVariableSet(HasOrderedGV,false);

GlobalVariableSet(barsGV,Bars);

}

//--- Check for long position (BUY) possibility

if (GlobalVariableGet (HasOrderedGV) == false)

{

Print("TEST");

if (... entry conditions...

 

...

If your EA tried at any stage to open an order it would have been written in the journal (error or not, does not matter - in case if there was an error, an error would have been there, if it opened an order that would be written there)

But since nothing is written there it means that your EA never got to the stage to actually try to open an order and that can happen only if your set of conditions that have to be fulfilled in EA is never getting to the step of actually open an order. So you have to clarify your conditions. Try from the simplest condition that you know that will execute, and then build up adding conditions. That way you will be able to identify the step that is preventing you from opening orders

crsnape@btinternet.com:
Yes its strange because before it was entering positions. I have some print() written in my code from beginning to end and nothing gets written to the journal at all to record even the very beginnings of my code. Not even this one to check if a previous position has been opened already on the current bar:

if ((GlobalVariableGet (barsGV) == 0) || (GlobalVariableGet (barsGV) < Bars))

{

GlobalVariableSet(HasOrderedGV,false);

GlobalVariableSet(barsGV,Bars);

}

//--- Check for long position (BUY) possibility

if (GlobalVariableGet (HasOrderedGV) == false)

{

Print("TEST");

if (... entry conditions...
 

I've put a few more Print()s after the if commands and I think its failing at this point, particularly the red line as my journal stops print after this point.

//--- GLOBAL VARIABLES

string HasOrderedGV = "has_ordered_GV";

string barsGV = "bars_GV";

//--- Determine if order already placed on H4 time block

if ((GlobalVariableGet (barsGV) == 0) || (GlobalVariableGet (barsGV) < Bars))

{

GlobalVariableSet(HasOrderedGV,false);

GlobalVariableSet(barsGV,Bars);

}

Print("Determine if order already placed for current bar ", HasOrderedGV);

//--- Check for long position (BUY) possibility

if (GlobalVariableGet (HasOrderedGV) == false)

{

I found this bit of code online and used it in mine. But just wondering now whether it will work. The global variable barsGV is declared as a string, however it references it to 0 in the first line. Is this right?

 

...

That code works OK when it runs for the first time

0 is tested for the following reason :

double GlobalVariableGet([/TD] [TD]string name)

"Returns the value of an existing global variable or 0 if an error occurs"

Error when retrieving global variable value would be when the global variable is not created yet. But, as I said, that code is OK when run for the first time. The problem is the following : imagine that you run a test and the barsGV is set to 5000 at the end of the test. Now you run the EA again and the bars are always going to be less than the barsGV value (which is still 5000). It (the code) has to be written differently. Use something like this

GlobalVariableSet(barsGV,0);

in the init(), and then it should work OK for consecutive tests too. But it will also, artificially, "pretend" that there was no opened order at the current bar when you start the EA which may be an error. It is much better to use the function to count the orders opened on a current bar than to use global varibles for that purpose (for multiple reasons)

________________________________________________

PS: using global variable(s) like that prevents you from using multiple instances of an EA (each global variable is visible from another code - any code : EA,script, indicator ..., so if the name is not unique, you risk a mix-up of EAs reading and setting all the same global variable at a same time)

crsnape@btinternet.com:
I've put a few more Print()s after the if commands and I think its failing at this point, particularly the red line as my journal stops print after this point.

//--- GLOBAL VARIABLES

string HasOrderedGV = "has_ordered_GV";

string barsGV = "bars_GV";

//--- Determine if order already placed on H4 time block

if ((GlobalVariableGet (barsGV) == 0) || (GlobalVariableGet (barsGV) < Bars))

{

GlobalVariableSet(HasOrderedGV,false);

GlobalVariableSet(barsGV,Bars);

}

Print("Determine if order already placed for current bar ", HasOrderedGV);

//--- Check for long position (BUY) possibility

if (GlobalVariableGet (HasOrderedGV) == false)

{

I found this bit of code online and used it in mine. But just wondering now whether it will work. The global variable barsGV is declared as a string, however it references it to 0 in the first line. Is this right?
 

Ahh.. I see. Because Global Variables save their values in memory. So as you say when it finishes backtesting and its at 5000 say, this is saved to memory even if the backtesting is re-run so it will never return false because BarsGV isnt below 5000? Jeez this is deep.

And also, if it generates an error, it sets HasOrderedGV to false so it can continue with the rest of the code?

Have I understood you correctly?

BTW- is this allowed?

GlobalVariableSet(HasOrderedGV, false);

Because in MQL4 Book it says:

datetime GlobalVariableSet([/TD] [TD]string name, double value)

The second value in my instance is bool not double?