Help with logic in my code? - page 2

 
You have been abusing our help, such as 181 posts for Closing out half lots. - MQL4 forum. I suspect their all tired of you and your incessant questions.
 
How am I "abusing" your help? I have thanked everyone 100 times over for their kindness. Are you insinuating that we are restricted to the number of posts or threads allowed to start?

I did not write 181 posts on there? That was a joint effort, some of which, from people who did not know how to do it themselves. That thread is now resolved on the final post I have put up... All thanks to everyone's help.

You have a funny way of deciding who is eligible and worthy enough for your help. Either way, I am sorry if you feel tired of threads where people are asking for questions to be answered. Maybe you should take a break from helping on here for a while?
 
DomGilberto:

Does anyone else have any time to share?

First, there are many coding styles, but the choice of a particular style (or combination of styles) is largely up to the individual programmer. However, I think there are three things that are important. In order of importance (at least in my opinion), they are:

  1. The code must be syntactically and logically correct;
  2. The code should be easily comprehensible by the person(s) who wrote it; and
  3. The code should be easy to read by people who didn't write it.
The first two are critically important. The third may not be as important as the previous two, but it helps allot when the programmer shows his/her code to others. Just some food for thought.

Second, if you have:

triggerBarTime=Time[1];
PullBack_Bar=Time[1];

why are you doing the following (the highlighted portion)?

if(PullBack_Bar - triggerBarTime > Point / 2.) // If PullBack_Bar is greater in time than the triggerBarTime, then place the order.

If you want to determine whether the PullBack_Bar is greater in time than the triggerBarTime, which is what your comments suggest, you need to simply test whether the PullBack_Bar is greater than triggerBarTime.

if (PullBack_Bar > triggerBarTime) {
   H1_Buy_Touch = "H1 Buy Touch";
   OrderEntry(0); // Pending order Buy Stop function is called.
}
 

Hey Thirteen, thanks for your kind post and stopping by!

I was under the impression that doing a straight ">" comparison, is not the most accurate? Then again, I think what I have done there is really stupid relative to datetime as I am not comparing price ("Point"). Thanks for pointing that out :)

What I have written does work. I did actually post that back to let everyone know that I think I had it sorted. Just didn't understand the other persons post saying what I have written is overly complex? Am curious to understand how else I would do it?

 
DomGilberto:



I was under the impression that doing a straight ">" comparison, is not the most accurate?


Hi this double comparison issue can get confusing. Generally I would advise the following (Others may have their own approach :) ).

- Avoid comparing doubles for equality or inequality, use < or > where feasible/logical in your code.

- However, Sometimes you do want >= or <= (rather than < or > ) to maximise pips etc, in which case ...

In the rare cases you specifically care about equality ( =, !=, >=, <= ) then compare using a tolerance, either

- round both numbers to nearest point and then compare with a small tolerance (eg Point/2 or even smaller)

- OR compare unrounded numbers with a bigger tolerance (which would be almost equal to your rounding size - eg just under 'Point' for prices) (ala Thirteen :) )

Eg if waiting for price to meet a MA, the MA is likely some arbitrary 'fluffy' number and unlikely to be a strict price (number of pips), so I would just use

if (price >= MA) ...

However if waiting for price to hit another specific price, eg previous candle OHLC or a Pivot line, then I think the Pivot Price is a better defined value than a MA (in terms of precise Pips/Points), I guess a lot of people may have pending orders sitting on them etc. and market may react at that price point, and in that case you may want to compare with a tolerance to be sure you dont miss the match, so in that case I might use:

if (ge(price,Pivot))...

Where ge() is a user defined 'greater or equal' function to compare price and Pivot within a tolerance. But then again I might still not care than much and just use (price >= Pivot). I would (in order of preference)

  • whenever possible, try to use < and > without a tolerance,
  • maybe a tolerance for ( >= . <= ) depending on what I'm comparing and if it might mean losing pips - eg if two very specific price points (eg other OHLC, Pivots?) I'd use a tolerance, if comparing a price against some fluffy number from an indicator series (eg MA) then probably not...
  • if strict equality is important then always use a tolerance for direct equality(or inequality) ( = / != ),
 
DomGilberto:

What I have written does work. I did actually post that back to let everyone know that I think I had it sorted.

If your code works for you, that is what is most important. Just have a few suggestions for your future consideration:

First, you have:

if(ema21 - H1_low  > -Point)

. . . given that ema21 and H1_low are doubles that represent price, I think you meant:

if(ema21 - H1_low  > Point / 2.)

See WHRoeder's post here.

Second, you might consider using boolean or integer values for your "bias" variables, instead of string values. Using strings invites too many unacceptable typo errors (such as an extra space). Additionally, your current use of strings won't allow you to relate/compare one "bias" variable to another, if the need arises. See the following example (code compiles but not tested):

#define UP      1
#define NEUTRAL 0
#define DOWN    -1

// Lets assume:
int D1_Bias = UP, H4_Bias = UP, H1_Bias = NEUTRAL;

// Determine if D1_Bias is UP and, if so, print "D1 bias is UP" in the journal/log
if (D1_Bias == UP)
   Print ("D1 bias is UP");
        
// Determine if D1_Bias and H4_Bias are the same and, if so, print "D1 and H4 bias is the same" in the journal/log
if (D1_Bias == H4_Bias)
   Print ("D1 and H4 bias is the same");
        
// Determine if H4_Bias is greater than the H1_Bias and, if so, print "H4 bias is more than H1 bias" in the journal/log
if (H4_Bias > H1_Bias)
   Print ("H4 bias is more than H1 bias");

Third, you might consider grouping your current "SmallFish" and "BigFish" data into a more usable collection, such as an array, so you can possibly avoid such heavily nested IF statements. See an example here.

 

Great advice! Thank you very much for both of your help :)

Reason: