# Why is this not returning true ? - page 2

39

Keith Watford:

You would code it exactly as you would say it

if  calclong1==true Or  calclong2==true And  calclong3 ==true  And  calclong4 ==true  And  calclong5 ==true

now that is a bit ambiguous as it often can be when mixing || and &&

so we put in some extra brackets to make it clear

As William has pointed out with bools or boolean results the ==true is not really necessary

```if((calcLong1()||calcLong2()) && calcLong3() && calcLong4() && calcLong5() && CountPositions()<1)
```

so would it be like this? ^^^

`if((calcLong1||calcLong2) && calcLong3 && calcLong4 && calcLong5 && CountPositions<1)`

or like this? ^^

39

Should this work ?

```void OnTick()

{
if((calcLong1()||calcLong2()) && calcLong3() && calcLong4() && calcLong5() && CountPositions()<1)
{
Print ("LONG");

Symbol(),
LNumcontracts,
0,
StopLossLong,
TakeProfitLong,
NULL,
0,
0,
Green);
}
```
Moderator
30420

George Johnson:

so would it be like this? ^^^

or like this? ^^

They are both correct, assuming that the variables or functions return a bool result.

George Johnson:

Should this work ?

I don't know as I cannot see where the variables are calculated.

39

Keith Watford:

They are both correct, assuming that the variables or functions return a bool result.

I don't know as I cannot see where the variables are calculated.

```double YH = iHigh(_Symbol, PERIOD_D1, 1);  // yesterday's high
double YL = iLow(_Symbol, PERIOD_D1, 1);  // yesterday's low
double YC = iClose(_Symbol, PERIOD_D1, 1);  // yesterday's close

double PP = (YH+YL+YC)/3;
double R3 = PP+2*(YH-YL);
double R2 = PP+(YH-YL);
double R1 = PP*2-YL;
double S1 = PP*2-YH;
double S2 = PP-YH-YL;
double S3 = PP+2*(YH-YL);

//O
double O = iOpen (_Symbol, _Period, 0);
//H
double H = iHigh (_Symbol, _Period, 0);
//L
double L = iLow (_Symbol, _Period, 0);
//C
double C = iClose (_Symbol, _Period, 0);

//O1
double O1 = iOpen (_Symbol, _Period, 1);
//H1
double H1 = iHigh (_Symbol, _Period, 1);
//L1
double L1 = iLow (_Symbol, _Period, 1);
//C1
double C1 = iClose (_Symbol, _Period, 1);

//O2
double O2 = iOpen (_Symbol, _Period, 2);
//H2
double H2 = iHigh (_Symbol, _Period, 2);
//L2
double L2 = iLow (_Symbol, _Period, 2);
//C2
double C2 = iClose (_Symbol, _Period, 2);

//O3
double O3 = iOpen (_Symbol, _Period, 3);
//H3
double H3 = iHigh (_Symbol, _Period, 3);
//L3
double L3 = iLow (_Symbol, _Period, 3);
//C3
double C3 = iClose (_Symbol, _Period, 3);

double UnitCost = MarketInfo(Symbol(),MODE_LOTSIZE)/ MarketInfo(Symbol(),MODE_TICKSIZE);
double Ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);

double Lstoplossbig = ((C-L)+(5*Ticksize))*100000;
double Sstoplossbig = ((H-C)+(5*Ticksize))*100000;
double StopLossLong = L - 5*Ticksize;
double StopLossShort = H + 5*Ticksize;
double TakeProfitLong = C+(2*(C-L+(5*Ticksize)));
double TakeProfitShort = C-(2*(H-C+(5*Ticksize)));

extern double RISKPERCENTAGE = 3;
double AB = AccountBalance();
double Risk = (AB/100)*RISKPERCENTAGE;

double LRPP = Risk / Lstoplossbig;
double SRPP = Risk / Sstoplossbig;
double LNumcontracts = (LRPP)/ UnitCost;
double SNumcontracts = (SRPP)/ UnitCost;

bool calcLong1()
{
bool GHammer = 2*(C-O)<=O-L && 2*(H-C)<=C-O;
return GHammer;
}

bool calcLong2()
{
bool RHammer = 2*(O-C)<=C-L && 2*(H-O)<=O-C;
return RHammer;
}

bool calcShort1()
{
bool GSStar = 2*(C-O)<=(H-C) && 2*(O-L)<=(C-O);
return GSStar;
}

bool calcShort2()
{
bool RSStar = 2*(O-C)<=(H-O) && 2*(C-L)<=(O-C);
}

bool calcLong3()
{
double RSI = iRSI(_Symbol,_Period,14,PRICE_CLOSE,0);
return RSI<70;
}

bool calcShort3()
{
double RSI = iRSI(_Symbol,_Period,14,PRICE_CLOSE,0);
return RSI>30;
}

void OnTick()

{
if((calcLong1()||calcLong2()) && calcLong3() && CountPositions()<1)
{
Print ("LONG");

Symbol(),
LNumcontracts,
0,
StopLossLong,
TakeProfitLong,
NULL,
0,
0,
Green);
}

if((calcShort1()||calcShort2()) && calcShort3() && CountPositions()<1)
{
Print ("SHORT");

//sell
int sellticket = OrderSend(
Symbol(),
OP_SELL,
SNumcontracts,
0,
StopLossLong,
TakeProfitShort,
NULL,
0,
0,
Green);
}

};

int CountPositions()
{
int NumberOfPositions=0;

for(int i=OrdersTotal()-1; i>= 0; i--)
{

string CurrencyPair=OrderSymbol();

if(_Symbol==CurrencyPair)

{
NumberOfPositions=NumberOfPositions+1;
}
}
return NumberOfPositions;
};
```

This is how the variables are created, but something is definitely still going wrong

24800

```double YH = iHigh(_Symbol, PERIOD_D1, 1);  // yesterday's high
double YL = iLow(_Symbol, PERIOD_D1, 1);  // yesterday's low
⋮
double LNumcontracts = (LRPP)/ UnitCost;
double SNumcontracts = (SRPP)/ UnitCost;

bool calcLong1()…```

Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

1. They are initialized once on program load.

2. They don't update unless you assign to them.

3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

1. Terminal starts.
2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
3. OnInit is called.
4. For indicators OnCalculate is called with any existing history.
5. Human may have to enter password, connection to server begins.
6. New history is received, OnCalculate called again.
7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
external static variable - MQL4 programming forum #2 2013.02.10

39

William Roeder:

Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

1. They are initialized once on program load.

2. They don't update unless you assign to them.

3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

1. Terminal starts.
2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
3. OnInit is called.
4. For indicators OnCalculate is called with any existing history.
5. Human may have to enter password, connection to server begins.
6. New history is received, OnCalculate called again.
7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
external static variable - MQL4 programming forum #2 2013.02.10

Thanks, this makes more sense. I had a look at the static variable forum you linked but I cant understand how it will work with my code?

i do need exactly what your saying, for it to reset on every tick.

whats the best way for me to do it?

24800

George Johnson: i do need exactly what your saying, for it to reset on every tick.

What part of “They don't update unless you assign to them” was unclear?

39

William Roeder:

What part of “They don't update unless you assign to them” was unclear?

sorry im still really new to coding, how do i assign to them ?

24800

George Johnson: sorry im still really new to coding, how do i assign to them ?
`variable = value;`
39

William Roeder:

```//O
double O()
{
return iOpen (_Symbol, _Period, 0);
}
//H
double H()
{
return iHigh (_Symbol, _Period, 0);
}
//L
double L()
{
return = iLow (_Symbol, _Period, 0);
}
//C
double C()
{
return iClose (_Symbol, _Period, 0);
}
```
```bool calcLong1()
{
O=O()
H=H()
L=L()
C=C()
bool GHammer = 2*(C-O)<=O-L && 2*(H-C)<=C-O;
return GHammer;
}```

would this mean they update ?

or is there an easier way ?