Indicators: What You See Is (not) What You Get in the code

 

Foreword: I am quite new to THIS programming language.

I have the impression that, for most indicators, what I see on the graph is not what I am getting in the code. So, before I might move to complex logic, I went back to the simple cases as I’d need to understand the basis with simple examples I guess.

Can someone cast some light over that? Am I getting a wrog interpretation somewhere?

I will clarify them with the simple example I am experimenting now:

To put it simple, I consider two Moving Averages, open trade (SELL) when Orange one is less than the Blue one and DX- is greater than 23.

CLOSE trade when a bar crosses the Orange line.

The (extract of) code is:

gMovingAverage_Orange = NormalizeDouble(iMA (Symbol(), 0, 10, 0, MODE_SMA, PRICE_TYPICAL, 0), Digits);gMovingAverage_Blue = NormalizeDouble(iMA (Symbol(), 0, 7, 0, MODE_SMMA, PRICE_MEDIAN, 5), Digits);gADXplus = iADX(Symbol(), PERIOD_CURRENT, 14, PRICE_CLOSE, MODE_PLUSDI, 0);gADXminus = iADX(Symbol(), PERIOD_CURRENT, 14, PRICE_CLOSE, MODE_MINUSDI, 0);// IF no order openif ( (gMovingAverage_Orange gADXplus) && (gADXminus > GA_DXminus_Threshold) ){ SELL operation; tradeOpen = 1;}// IF sell order openif (gMovingAverage < gLastTick.ask) // [DEBUG: deve prendere last bar!]{ CLOSE trade; tradeOpen = 0;}

Result is in the screenshot, which makes me wonder what the hell is going on... according to what I see it should open the trade where the bar is!

Thanks

Files:
ma_cross.png  37 kb
 

Hi DadeM,

One question I have is...the Blue MA appears to be 5 bars back. Is that intentional...?

Other than that...I am not able to see the ADX scale on the right...but it looks like they all satisfy the Trade Conditions for SELL.

It's possible that ADXMinus popped above 23 and dropped back immediately...but enough to trigger the Sell Trade...?

Three tests you can do to see how the Trade works:

1) Watch that Sell Trade in super slow motion to see when it triggers... Carefully watch the ADXMinus scale.

2) Run the same code without the Threshold...and you will see more exactly where the trades should happen. Then add back your ADX Threshold.

3) Add plenty of Comments...they are your best friend in coding. See your MA and ADX values immediately on the chart. Should leave you no question as to if the values are different or not on every bar.

Hope this helps you.

Robert

 

Hi CosmicLifeForm,

I don't know what happened to the beautifully formatted code: I will add it at the bottom of this message since I don't seem to be able to edit my former post.

About the ADX, the threshold is definitely visible as is the green line (sorry for cutting the scale).

Same happens without ADX in fact (even if to a lesser amount: combining indicators multiplies the mess, that is why I am trying to understand the logic with simple ones).

About the blue MA, yes, it's a sort of variation of the Alligator indicator.

My approach is to get (and learn) something from the theory, while running simulations with different indicators alongside and see if something "talks" to me.

The diagram (posted above) shows what I mean: enter the trade when the orange line crosses the blue one downwards. Leave aside the other openings, my point is why it seems to get different values in the code as from what we can see.

This is NOT the only example: this is just the last (easy) one: how can I "visually" find a strategy if when I implement it the result is considerably different?

gMovingAverage_Orange = NormalizeDouble(iMA (Symbol(), 0, 10, 0, MODE_SMA, PRICE_TYPICAL, 0), Digits);gMovingAverage_Blue = NormalizeDouble(iMA (Symbol(), 0, 7, 0, MODE_SMMA, PRICE_MEDIAN, 5), Digits);

gADXplus = iADX(Symbol(), PERIOD_CURRENT, 14, PRICE_CLOSE, MODE_PLUSDI, 0);

gADXminus = iADX(Symbol(), PERIOD_CURRENT, 14, PRICE_CLOSE, MODE_MINUSDI, 0);

// IF no order open

if ( (gMovingAverage_Orange gADXplus) && (gADXminus > GA_DXminus_Threshold) )

{

SELL operation;

tradeOpen = 1;

}

// IF sell order open

if (gMovingAverage < gLastTick.ask) // [DEBUG: deve prendere last bar!]

{

CLOSE trade;

tradeOpen = 0;

}
 
DadeM:
Hi CosmicLifeForm,

I don't know what happened to the beautifully formatted code: I will add it at the bottom of this message since I don't seem to be able to edit my former post.

About the ADX, the threshold is definitely visible as is the green line (sorry for cutting the scale).

Same happens without ADX in fact (even if to a lesser amount: combining indicators multiplies the mess, that is why I am trying to understand the logic with simple ones).

About the blue MA, yes, it's a sort of variation of the Alligator indicator.

My approach is to get (and learn) something from the theory, while running simulations with different indicators alongside and see if something "talks" to me.

The diagram (posted above) shows what I mean: enter the trade when the orange line crosses the blue one downwards. Leave aside the other openings, my point is why it seems to get different values in the code as from what we can see.

This is NOT the only example: this is just the last (easy) one: how can I "visually" find a strategy if when I implement it the result is considerably different?

gMovingAverage_Orange = NormalizeDouble(iMA (Symbol(), 0, 10, 0, MODE_SMA, PRICE_TYPICAL, 0), Digits);gMovingAverage_Blue = NormalizeDouble(iMA (Symbol(), 0, 7, 0, MODE_SMMA, PRICE_MEDIAN, 5), Digits);

gADXplus = iADX(Symbol(), PERIOD_CURRENT, 14, PRICE_CLOSE, MODE_PLUSDI, 0);

gADXminus = iADX(Symbol(), PERIOD_CURRENT, 14, PRICE_CLOSE, MODE_MINUSDI, 0);

// IF no order open

if ( (gMovingAverage_Orange gADXplus) && (gADXminus > GA_DXminus_Threshold) )

{

SELL operation;

tradeOpen = 1;

}

// IF sell order open

if (gMovingAverage < gLastTick.ask) // [DEBUG: deve prendere last bar!]

{

CLOSE trade;

tradeOpen = 0;

}

is gLastTick a class?

 

gLastTick is simply taken from:

SymbolInfoTick(Symbol(), gLastTick)

Could it be a reason that many indicators need 2 bars to be consolidated?

 
DadeM:
gLastTick is simply taken from:
SymbolInfoTick(Symbol(), gLastTick)
Could it be a reason that many indicators need 2 bars to be consolidated?

Yep, that is a structure

In your conditions you do not check if there is a cross of two averages. You only check if one average is smaller than the other average - and that is not the same as when checking for crosses

 
mladen:
Yep, that is a structure In your conditions you do not check if there is a cross of two averages. You only check if one average is smaller than the other average - and that is not the same as when checking for crosses

You are totally right, and I might want to go for a more elegant solution eventually.

Still, the condition where one line is smaller than the other should be even easier, I don't get the rationale why it apparently fails...

Reason: