Help Needed On MQL Code

 
Hello,

I need help on this EA. Actually I already got system in Amibroker TA, also I am trying to code same system as MetaTrader EA.

Below is amibroker code(AFL) for system that I want to code as EA.
Quote:
no= Param("period",7,1,100,1);
res=EMA(HHV(H,no),3);
sup=EMA(LLV(L,no),3);



a = Cross(H,res);
b = Cross(sup,L);
eq=Param("0 equity",1,0,1,1);

state = IIf(BarsSince(a)<BarsSince(b),1,0); //(<<this return 1 or 0 based on condition)

buy=cover=state>Ref(state,-1);
sell=short=state<Ref(state,-1);

As you can see there is external parameter ("no") which can be selected by user is an integer value.

Calculation on lines/variables RES & SUP (Resistance & Support)

“res” can be calculated using EMA of period 3 & PRICE will be highest high price of last 7 bars (Including Current bar). This 7 bars is integer value which set by external parameter by user “no”.
Same goes for “sup” which is EMA of period 3, But only difference is that price of EMA calculation is lowest low price of last 7 bars(Including Current bar).

Now variable “a” will set to “1” or “0” based on the crossing of current bar high & variable “res”.
For variable “b” set to “1” or “0” based on the crossing of variable “sup” & current bar low.
Here is definition of this “CROSS” afl code.

Quote:
SYNTAX Cross( ARRAY1, ARRAY2 )
RETURNS ARRAY
FUNCTION Gives a "1" or true on the day that ARRAY1 crosses above ARRAY2. Otherwise the result is "0".
To find out when ARRAY1 crosses below ARRAY2, use the formula cross(ARRAY2, ARRAY1)
EXAMPLE cross( close, ema(close,9) )
Now the variable “state” which also can be set to “1” or “0” based on following condition.

BarsSince(a) : Calculates the number of bars (time periods) that have passed since ARRAY was true (or 1) in this case array was variable “a”

BarsSince(b) : Calculates the number of bars (time periods) that have passed since ARRAY was true (or 1) in this case array was variable “b”.


SYNTAX BarsSince( ARRAY )
RETURNS ARRAY
FUNCTION Calculates the number of bars (time periods) that have passed since ARRAY was true (or 1)
EXAMPLE barssince( macd() < 0 )

state = IIf(BarsSince(a)<BarsSince(b),1,0);

Quote:
SYNTAX iif( EXPRESSION, TRUE_PART, FALSE_PART )
RETURNS ARRAY
FUNCTION "Immediate-IF" - a conditional function that returns the value of the second parameter (TRUE_PART) if the conditional expression defined by the first parameter (EXPRESSION) is true; otherwise, the value of third parameter is returned (FALSE_PART). Please note that IIF is a function - so the result of evaluation is returned by that function and should be assigned to some variable. IIf always evaluates both TRUE_PART and FALSE_PART, even though it returns only one of them. Because of this, you should watch for undesirable side effects. The following example shows one common error made with IIF function: IIF( condition, result = 7, result = 9 ); // THIS IS WRONG Correct usage is: result = IIF( condition, 7, 9 ); /* 7 or 9 is *returned* and assigned to a variable depending on condition */
EXAMPLE The formula result = iif( macd()<signal(), Volume, -Volume) will assign positive volume values to the result variable on days when macd was below its signal line, and negative volume values on theother days.


Note: This system generates buy/sell without vanishing. When buy/sell comes at some point it stays there so no fake signals.


Now I tried to create same system using above logic in MQL. Here is my step by step code conversion ;


bool a;
val_high=High[iHighest(NULL,60,MODE_HIGH,6,0)];
// highest high which is HHV in afl code
val_Low=Low[iLowest(NULL,60,MODE_LOW,6,0)];
// lowest Low which is LLV in afl code
res=iMA(NULL,0,Period_EMA,0,MODE_EMA,val_high,0);
// variable res which is EMA of period 3 & Price as HHV of period 7.
sup=iMA(NULL,0,Period_EMA,0,MODE_EMA,val_Low,0);
// variale sup which is EMA of period 3 & Price as LLV of period 7.
if (iHigh(NULL,60,0)>res) // Current high crosses res, I don't know whether I have coded this right compare to "a = Cross(H,res);" afl code.
{
a=true;
}
if( sup < iLow(NULL,60,0) )
// Current Low crosses below sup, I don't know whether I have coded this right compare to "b = Cross(sup,L);" afl code.
{
b=true;
}

// After this I didn't know how to calculate above state = IIf(BarsSince(a)<BarsSince(b),1,0); condition in MQL.//(<<this return 1 or 0 based on condition)

// its actually Calculates the number of bars (time periods) that have passed since condition a & b was true (or 1) . When Number Of bars since first condition "a" is true less than Number Of bars since second condition "b" is true, then set "state" variable to 1 otherwise set it 0.

//Based on this when buy=state>Ref(state,-1); I need to execute line 1 in MQL

//Based on this when sell=state<Ref(state,-1); I need to execute line 2 in MQL

// Here "ref" syntax definition

//

// SYNTAX ref( ARRAY, period )
// RETURNS ARRAY
// FUNCTION References a previous or subsequent element in a ARRAY. A positive period references "n" periods in the future; a negative period references "n" periods ago.
// EXAMPLE The formula "ref( CLOSE, -14 )" returns the closing price 14 periods ago. Thus, you could write the 14-day price rate-of-change (expressed in points) as "C - ref( C, -14 )." The formula "ref( C, 12 )" returns the closing price 12 periods ahead (this means looking up the


if(buy==true) // LINE 1
{
Opn_B=true;
// Opn_B=True --- Opening New Buy order
Cls_S=true; // Cls_S=True --- Closing Previous opened Sell order
}
if(sell==true) // LINE 2
{
Opn_S=true; // Opn_S=True --- Opening New Sell order
Cls_B=true; // Cls_B=True --- Closing Previous opened Buy order
}



Please guide me whether first part of conversion is done properly by me like variables val_high(HHV of 7), val_low(LLV of 7), res, sup & suggest me correct MQL code for "state" variable so I can proceed to LINE 1 & LINE 2 condition.


Thank You

 

Right now here is my final code


val_high=High[iHighest(NULL,60,MODE_HIGH,6,0)]; // highest high
val_low=Low[iLowest(NULL,60,MODE_LOW,6,0)]; // lowest Low
res=iMA(NULL,0,Period_EMA,0,MODE_EMA,val_high,0); // variable res
sup=iMA(NULL,0,Period_EMA,0,MODE_EMA,val_low,0); // variale sup


if (iHigh(NULL,60,0)>res) // Current bar high crosses above res
{
a=1;
}
else
{
a=0;
}
if(sup>iLow(NULL,60,0)) // sup crosses above current bar Low
{
b=1;
}
else
{
b=0;
}

if (condition for buy) //
{ //
Opn_B=true; //
Cls_S=true; //
}
if (condition for sell) //
{ //
Opn_S=true; //
Cls_B=true; //
}


I want to know how to calculate or start counting number of bars since certain condition true for sometime till false value set. Say current close is greater than moving average close for say 5 bars in 1 hour chart including current bar when condition found to be true. How to calculate this number of "5" bars. I think this will solve my last code of part if I am able to count this numbers of bars after condition true till condition to be false .

Reason: