Question from beginner

 

Hello Everyone,


I am a beginner with MQL4 but I do have some programming experience. I am working on my first EA and it compiles fine without any errors or warnings. Nevertheless, I am stuck. I tried to structure my program similar to ones I see online and etc... In my start() function I check the various setup conditions and make orders based on these conditions. However, my program does not make any trades (even when tested over as long as six months). I cannot understand why this is so. I tried checking conditions in a regular function and then call it in the start() function, along with other things, but nothing helps. I attached my file - if someone could explain to me what I am doing wrong.


Thank You,

denrus

Files:
fpa3z4.mq4  5 kb
 
denrus:

[...] I attached my file - if someone could explain to me what I am doing wrong.

From literally 10 seconds of inspection - so apologies if this is nonsense - the problem looks as though it's because you're trying to enter positions by placing a buy/sell stop at the current ask/bid price. This isn't going to work. A stop is a pending order which needs to be at least the broker's stop-level - MarketInfo(Symbol(), MODE_STOPLEVEL) pips - away from the current price. If you simply want to enter at the current market price, use OP_BUY or OP_SELL rather than OP_BUYSTOP or OP_SELLSTOP.


There may be other problems in the code; this is just the one which leapt out at me. The perennial recommendation in this forum is to use Print() to log what your code is trying to do, and where problems are occurring.

 
jjc:

From literally 10 seconds of inspection [...]

There may be other problems in the code; this is just the one which leapt out at me.

A further 10 seconds says that I don't like the look of your OrderClose() condition either. You seem to be closing if the position is in profit to the tune of exactly the ATR. If the price gaps around, you could exceed your desired profit target without closing the position, or miss a potential win if the price gaps beyond the ATR and then reverses.

 
jjc:

From literally 10 seconds of inspection - so apologies if this is nonsense - the problem looks as though it's because you're trying to enter positions by placing a buy/sell stop at the current ask/bid price. This isn't going to work. A stop is a pending order which needs to be at least the broker's stop-level - MarketInfo(Symbol(), MODE_STOPLEVEL) pips - away from the current price. If you simply want to enter at the current market price, use OP_BUY or OP_SELL rather than OP_BUYSTOP or OP_SELLSTOP.


There may be other problems in the code; this is just the one which leapt out at me. The perennial recommendation in this forum is to use Print() to log what your code is trying to do, and where problems are occurring.


Ok, so if I use the OP_BUYSTOP then I need to specify a price that is higher than the current Ask price, something like this:

new=OrderSend(Symbol(), OP_BUYSTOP, lots, HH3, 4, 0, 0, "", MAGICMA, 2, Red); return;

And, also I need to check that HH3 is actually higher than current Ask by the minimum amount so:

if (HH3-Ask >=(MarketInfo(Symbl(), MODE_STOPLEVEL)))

{new=OrderSend(Symbol(), OP_BUYSTOP, lots, HH3, 4, 0, 0, "", MAGICMA, 2, Red); return;}


I am sorry if this is a stupid question, but I am confused about using the print() to debug. It says it prints a message to the experts log - Where is the experts log and how do I view it?


Thanks,

denrus

 
jjc:

A further 10 seconds says that I don't like the look of your OrderClose() condition either. You seem to be closing if the position is in profit to the tune of exactly the ATR. If the price gaps around, you could exceed your desired profit target without closing the position, or miss a potential win if the price gaps beyond the ATR and then reverses.

understood

 
denrus:

And, also I need to check that HH3 is actually higher than current Ask by the minimum amount so:

if (HH3-Ask >=(MarketInfo(Symbl(), MODE_STOPLEVEL)))

{new=OrderSend(Symbol(), OP_BUYSTOP, lots, HH3, 4, 0, 0, "", MAGICMA, 2, Red); return;}

Close, but not quite. MarketInfo(..., MODE_STOPLEVEL) returns a number of pips, such as 30. The above code is therefore doing a test such as (1.4203 - 1.4197 >= 30). For this sort of comparison you need to multiply MODE_STOPLEVEL by MODE_TICKSIZE, giving you a value such as 0.0003.


BTW, in case it leads to questions later, posts by Metaquotes staff on this forum indicate that the slippage parameter (4, in your example) is ignored on pending orders.


I am sorry if this is a stupid question, but I am confused about using the print() to debug. It says it prints a message to the experts log - Where is the experts log and how do I view it?

In the Terminal pane (which has tabs for Trade, Account History etc), have a look at the Experts tab. There's a corresponding file on disk in the experts\logs subdirectory of the MT4 installation.

 
jjc wrote >>

A further 10 seconds says that I don't like the look of your OrderClose() condition either. You seem to be closing if the position is in profit to the tune of exactly the ATR [...]

denrus wrote >>

understood

To forestall further questions later on, there are two other classes of problem lurking in the sort of comparison which you're currently doing against the ATR.


Firstly, the ATR is an average and therefore - like many indicators - it's capable of returning sub-pip values. For example, the 20-bar H1 ATR on EURGBP is currently 0.0012435. A subtraction of one price from another like you're currently doing is obviously never going to equal such a sub-pip value. Even if the price didn't gap around, your code would potentially never fulfil its condition for closing orders based on a simple equality check. You need to round values as appropriate so that you're using the same degree of precision on both sides of a comparison.


Secondly, there's a more general need to watch out for precision issues when doing arithmetic involving double values (as discussed at extreme length in topics such as 'MQL4 MathCeil() intermittantly returns wrong result'). Taking an example from elsewhere, 1.3635 / 0.0001 = 13634.999999999998 in MQL (or C++), not 13635. It's worth checking out the posts on this forum about NormalizeDouble() and CompareDoubles().

 
jjc:

...In the Terminal pane (which has tabs for Trade, Account History etc), have a look at the Experts tab. There's a corresponding file on disk in the experts\logs subdirectory of the MT4 installation.


I cannot figure this out. I right click onto the message and select "open" but this error message comes up. Also, I have tried navigating to experts/logs in the MT4 installation and the subfolder logs within the folder experts does not show up at all. Also, I have noticed that any EA's I create do not show up in the experts folder, but the default EA's and any EA's I download do. Are the missing files on a network because they do not seem on my disk, or are they hidden or what? Any help much appreciated.

denrus

 

If you are using Vista, and have UAC (User Account Control) turned on, then check here for your missing files:

C:\Users\<your account name>\AppData\Local\VirtualStore\Program Files\<MT4 install directory>\experts


CB

 
cloudbreaker:

If you are using Vista, and have UAC (User Account Control) turned on, then check here for your missing files:

C:\Users\<your account name>\AppData\Local\VirtualStore\Program Files\<MT4 install directory>\experts


CB

thank you


denrus

 

Hello everyone,


Another small question:


In my EA my start() function calls certain user-defined functions which check for conditions upon which an order (SL and TP) is modified. I cannot find a way to program my EA such that it checks conditions to modify an order once per bar, as suppossed to at every tick. When checking at every tick, I get many "OrderModify Error 1" messages which unneccesarily fill up my journal. I know why this occurs - unchanged parameters within the OrderModify() function. What I need is a method to call my OrderModify() functions only at specific intervals - such as at the beginning or middle of each bar. I have been stuck with this issue for a while so I turned to the forum.


Thanks,

Denrus

Reason: