iSAR giving me unexpected results

 

I'm trying to use the result from iSAR as an extra confirmation that I'm in a trend, before opening a trade. However, it is sometimes giving me results that make no sense. I saw in another thread someone suggested not using the current bar since the indicator is still being made. Okay. So I tried using the prior one. But I'm still getting weird results at times. I think there are the relevant bits of code:

double PSar;

PSar=iSAR(NULL,0,.015,.2,1);

if(PSar > Ask)
{
  Print("PSAR > Ask ", DoubleToStr(PSar,8), ">", DoubleToStr(Ask,8));
}

My EA to place a trade looks at various things, one of them is that the PSar has to be below Ask to buy (go long) or PSar above Ask to sell (go short),

On the chart below it's the first sale trade that is the issue. PSar was under the bars, so I should not have placed the sell order. My Print line above said:

2011.05.29 13:14:42 2010.07.19 05:00 Roy7 AUDUSD,M30: PSAR > Ask 0.86829000>0.86717000

Which is saying the PSar point is .86829. But the PSar on the chart is actually .86512 well under the bar where the sale was placed, and .86484 on the prior period. There's no way it should have reported as .86829...

Looking at other cases, it sometimes seems correct. But it sometimes gives me numbers that make no sense when I look at the chart itself. I'm using the same settings for the PSar indicator (.015,.20) in the chart shown below. (The buy near the end of this screenshot had the same problem. PSar is above chart, but PSar<Ask was true because the PSar value returned was much lower than what the chart is showing.)

Help? :)


 
I've searched hi and low, here and on Forex Factory, and can't find any sort of resolution to this. I've seen numerous threads asking about the numbers not matching or issues using the indicator in an EA, but no solutions or explanations. :( Surely someone has solved this before. :)
 

The code for custom PSR should be the same as the internal PSR. If the values between the custom and the internal are different then you have your answer the code between the two is not the same.

 
Ickyrus:

The code for custom PSR should be the same as the internal PSR. If the values between the custom and the internal are different then you have your answer the code between the two is not the same.


Is the code for the internal iSAR function someplace I can view it? I'd be happy to compare it to the indicator's code.

Or maybe I should try using iCustom to access the Parabolic indicator directly instead of using the built-in iSAR?

Watching a live chart, the moment a new bar opens, the next PSAR point is plotted correctly. (Based on last position and acceleration factor). So I don't know why calling iSAR with a timeshift of 0 should be an issue. While you are within a current (new) bar, the indicator version on the graph plots perfectly.

Edit- I'm assuming the indicator is right and the iSAR is wrong since the indicator always has points that make sense, but the result from iSAR is sometimes well inside the current trading range (sometimes just a point or a pip off of the Ask price) which isn't possible unless it's about to cross the signal and flip to the other side, but it's happening far from the prior bar's PSAR dot. So I think Parabolic indicator is right, but iSAR is wrong.

 

https://www.mql5.com/en/code/7892

There is a comment from 07.09.2006 that the Parabolic indicator and the build-in Parabolic function don't provide the same results. There was a bug fix added to it on Sep 6, 2006. I'm using the "fixed" version and it still doesn't sync up. Also it should be noted the latest version of MT4 that I just installed a few weeks ago doesn't have this bug fix in their Parabolic indicator. So is the bug fix even correct/valid? Or has MT just ignored it for 5 years?

Edit- Maybe the internal iSAR function has the same bug as the original indicator but was never updated with this bug fix?

Edit2- I was referring to Parabolic in the "Custom Indicators". I wasn't aware there is also a Parabolic SAR under the normal indicators. I can't recall if the Parabolic custom indicator (without above bug fix) came with MT or not.

 

Ok this is getting more and more strange. I tried using iCustom and I get the same results back as I get from iSAR. So that's good I guess.

PSarCurrentCustom=iCustom(NULL,0,"Parabolic Sub",.015,.2,0,0);
PSarCurrent=iSAR(NULL,0,.015,.2,0);

if (Volume[0]==1) { Print("First tick of new bar PSAR: ", DoubleToStr(PSarCurrent,8)); }
if (Volume[0]==2) { Print("Second tick of new bar PSAR: ", DoubleToStr(PSarCurrent,8)); }
if (Volume[0]==3) { Print("Third tick of new bar PSAR: ", DoubleToStr(PSarCurrent,8)); }

if (Volume[0]==1) { Print("First tick of new bar PSAR Custom: ", DoubleToStr(PSarCurrentCustom,8)); }
if (Volume[0]==2) { Print("Second tick of new bar PSAR Custom: ", DoubleToStr(PSarCurrentCustom,8)); }
if (Volume[0]==3) { Print("Third tick of new bar PSAR Custom: ", DoubleToStr(PSarCurrentCustom,8)); }

As I watch new bars appear while running the EA in backtesting, and check what numbers come up from these Print statements, they aren't even close to the actual PSAR dots drawn on the chart. They are, in fact, past High and Close numbers. The first, second, and third tick prints are all the same, and Custom is the same as iSar, so I'll remove all of the excess baggage from the log. Here is what is left:

2011.05.30 11:20:08 2010.07.19 07:00 Roy7 AUDUSD,M30: First tick of new bar PSAR: 0.87029000 <- High from 06:00

2011.05.30 11:20:07 2010.07.19 06:30 Roy7 AUDUSD,M30: First tick of new bar PSAR: 0.86730000 <- Low from 06:00
2011.05.30 11:19:01 2010.07.19 06:00 Roy7 AUDUSD,M30: First tick of new bar PSAR: 0.86608000 <- Low from 05:00
2011.05.30 11:19:01 2010.07.19 05:30 Roy7 AUDUSD,M30: First tick of new bar PSAR: 0.86608000 <- Low from 05:00
2011.05.30 11:17:04 2010.07.19 05:00 Roy7 AUDUSD,M30: First tick of new bar PSAR: 0.86753000 <- High from 04:00

I added to the right what that number exactly matches to when I look over the chart. The indicator has to store that sort of info I believe to calculate crossovers and such internally. But why are those numbers being returned by the function call, instead of the location the dot is drawn on the chart?

Thoughts? I'm uploading a picture showing the print log, chart, and that the iSar from current bar is the same as the High from 2 bars prior (not the dot location).

 

While posibly not relivent the values on a chart may get adjusted to what the chart can show. How many pixels are there between two horizontal lines shown on the chart? The point shown may be adjusted to cope with the scale the chart uses. This would be a ' bug' in terms there being a diference between the value shown on the chart and the calculated internal value. I can only assume that the charts approximate visually what the figures are in reality. Consider a screen of 1024x768 pixels, you can not draw one for one and get a 45degree line as there are less pixels virtically than horizontally so visually this aspect ratio has to be accounted for. The virtical scale on a chart is adjustable too just to add further complaction to chart diplay. So it becomes a question of what value do you give the dot to give it a reasonable chance to be represented. This is the other factor to be considered when considering what you see is different to what is calculated.

 

Hi Roy7

The charts are filtered for better viewing and I don't know what effect it has on something like the Psar. This leads one to a false view of the indicators but the numbers returned by the function are usually correct and you have to make an EA work with the numbers since you can't screen grab the filtered display. Definitely use the value from a closed candle since that can't change and will reduce false signals even if it is delayed. I haven't had much success with MT4 EA code and thought I was the problem but having used a different language for EAs and found my code does exactly what I want it is clear to me MT4 has fundamental issues. I think the professionals use high level languages external to MT4 and just use the MT4 client to place and close the trades. That's my tupence worth.

PS Just look at the number of builds/updates MT4 has it is a classic case of poor code. The CTL system I am using doesn't have bug fixes they barely give one update a year and only because they have added many more features each time.

 
Ruptor:

Hi Roy7

The charts are filtered for better viewing and I don't know what effect it has on something like the Psar. This leads one to a false view of the indicators but the numbers returned by the function are usually correct and you have to make an EA work with the numbers since you can't screen grab the filtered display.


The problem is that the values being returned by the iSAR function aren't correct. They are prior open/close numbers. Exactly. All the way out to the final decimal point. I listed 5 above. But the way the indicator is calculated, the PSAR dot is always above the High or below the Low. They are the dots in my screenshots above, which are above the candles when the market is falling and below the candles when the market is rising. You can point at the dot and it tells you the exact value for the indicator. That value is correct. And it is plotted on the graph correctly. But the value iSAR() returns isn't that same value. You can timeshift back for prior values on bars that are finished, and it still gives you wrong figures. (The wrong ones I listed above for example, not the prior locations of the dots).

It's like iSAR() is returning values from one of the indicator's history buffers where it tracks the low/high so it knows when a crossover happens and to change the indicator to the opposite side of the candle, instead of returning the actual indicator's location/value. I've never written an indicator so I'm not positive how it works, but isn't it the IndicatorBuffer where the value to plot comes from? And if so, isn't that the same value that is returned by iSAR() or iCustom()? I've tried both and get identically wrong results. I don't understand how iCustom() can in theory return the same value the graph is using to plot with, but the graph have the right number when I point at the dot, and my EA and Print() functions have wrong numbers (that coincidentally are exact iLow or iHigh matches from prior bars).

If I get desperate enough I guess I could copy the Parabolic indicator code into my EA and calculate my own SAR value to use. But I really shouldn't have to do that. It's a built in function...

 

I use the built in function and it works fine or should I say I haven't noticed any significant difference to what it should give. Here is my call line

sadc=iSAR(Symbol(),0,pstep,pmax,0);

The only thing I can suggest is that you put zeros in front of your real numbers because MT4 doesn't tell you if it assumes things. I have backtested EAs and they have given results even though a function required by the EA wasn't present.

bad practice and might be misinterpreted by dinky pseudo code generators like MT4
PSarCurrent=iSAR(NULL,0,.015,.2,0);
like this it is a better practice
PSarCurrent=iSAR(NULL,0,0.015,0.2,0);

 
Ruptor:

The only thing I can suggest is that you put zeros in front of your real numbers because MT4 doesn't tell you if it assumes things.


Wow. You're right. When I add a leading zero to the numbers I'm calling the functions with, it works! I wonder how many of the threads I saw asking about this issue over the last few years as I searched Google used ".2" instead of "0.2". I never saw anyone suggest this as a fix before. I realize MQL4 is not actually C, but it's C-like enough I doubted this could make any difference. I'm shocked it does. But it works now!

Thanks again!

Reason: