1. ## A Good Trading System

Hello funyoo,

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

Below is amibroker code(AFL) for system that I want to code as EA.
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);

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

As you can see there is external parameter ("no") which can be selected by user is in 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. 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.

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 current bar low & variable sup.
Here is definition of this CROSS afl code.

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 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.
Now I created EA based on above system. But my knowledge with MQL is not good.
I try to create some MQL code but I am not able to get same results in Metatrader which was I am getting in Amibroker.

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

Thank You

2. Right now I have coded this MQL code for above system. Only last part of calculating number of bars rremianed.

val_high=High[iHighest(NULL,60,MODE_HIG H,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;
}

{ //
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 false .