Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

SAR indicator chart value and calculated value mismatch
 Post subject: SAR indicator chart value and calculated value mismatch Post rating: 0   New post Posted: Sun 17 Mar, 2013, 19:30 
User avatar

User rating: 0
Joined: Thu 14 Jun, 2012, 16:58
Posts: 5
Location: Portugal, Lisboa
Hello,

I'm trying to move from other forex trading API's to jforex API, but i'm always stuck in this problem that I can't figure out so i'm writing this topic so anybody could help me.

Every time i calculate, for example, a sar indicator value for my strategy, this value never match the value from the chart.

For example in the picture below, the calculated value (outputed in the 'Messages' table inside the blue circle) it's 1.27661843....
but in the chart, the value it's 1.22492 (indicated with a yellow arrow, green dots).

In the chart it's also some red dots. That's a custom indicator that outputs the calculated sar. Before the 'test start' bar this indicator is under the green indicator (has the exact same value that the default sar indicator), but when the test starts the custom indicator jumps to the 1.27.

What am i doing wrong?
How can i get the sar value plotted in the chart by the default sar indicator, to use in the strategy?

Thanks in advance.


Image

Code taken from the strategy (complete code in attachment):
public class SARWorking implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
   
    private final double SAR_ACCELERATION = 0.001;
    private final double SAR_MAXIMUM = 0.003;
    Object[] params = new Object[]{SAR_ACCELERATION, SAR_MAXIMUM};

    private IChart chart;
    private IIndicator sar;
    private IIndicator mySar;   
    private IChartPanel chPanel;

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.indicators = context.getIndicators();
        this.chart = context.getChart(Instrument.EURUSD);
       
        mySar = indicators.getIndicator("SARTEST");
        this.chPanel = this.chart.add(mySar, params, new Color[]{Color.RED},new DrawingStyle[]{DrawingStyle.DOTS}, new int[]{1});
        sar = indicators.getIndicator("SAR");
        this.chPanel = this.chart.add(sar, params, new Color[]{Color.GREEN},new DrawingStyle[]{DrawingStyle.DOTS}, new int[]{1});

        Object[] firstIndicatorValues =  indicators.calculateIndicator(Instrument.EURUSD,
            Period.DAILY, new  OfferSide[] {OfferSide.ASK}, "SAR",
            new AppliedPrice[]{AppliedPrice.CLOSE}, params, 0);
                   
        console.getOut().println("first indicator value: " + ((Object[])firstIndicatorValues)[0]);

    }


Code taken from the custom indicator (Complete code in attachment):
    public void onStart(IIndicatorContext context) {
        IIndicatorsProvider indicatorsProvider = context.getIndicatorsProvider();
        sar = indicatorsProvider.getIndicator("SAR");
                this.console = context.getConsole();

        indicatorInfo = new IndicatorInfo("SARTEST", "pSAR test", "My indicators",
                        true, false, true, 1, 2, 1);
        inputParameterInfos = new InputParameterInfo[] {new InputParameterInfo("Price HL", InputParameterInfo.Type.PRICE)};
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Acceleration", OptInputParameterInfo.Type.OTHER,
                new DoubleRangeDescription(0.001, 0.001, 1, 0.001, 3)), new OptInputParameterInfo("Maximum", OptInputParameterInfo.Type.OTHER,
                new DoubleRangeDescription(0.003, 0.001, 1, 0.001, 3))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("sar", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.DOTS)};
    }
 
    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
 
        sar.setInputParameter(0, inputs[0]);
        sar.setOptInputParameter(0, sarParams[0]);
        sar.setOptInputParameter(1, sarParams[1]);
        sar.setOutputParameter(0, outputs[0]);
        sar.calculate(startIndex, endIndex);
 
        return sar.calculate(startIndex, endIndex);
    }



Attachments:
File comment: Simple strategy to demonstrate the problem
SARWorking.java [2.82 KiB]
Downloaded 353 times
File comment: Custom indicator to test calculated SAR values
IndicatorTest1.java [4.92 KiB]
Downloaded 309 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: SAR indicator chart value and calculated value mismatch Post rating: 0   New post Posted: Mon 18 Mar, 2013, 08:29 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See:
https://www.dukascopy.com/wiki/#Indicator_usage_checklist


 
 Post subject: Re: SAR indicator chart value and calculated value mismatch Post rating: 0   New post Posted: Thu 21 Mar, 2013, 01:38 
User avatar

User rating: 0
Joined: Thu 14 Jun, 2012, 16:58
Posts: 5
Location: Portugal, Lisboa
Hello,

Thanks for the link.
I've follow the list.
I've set in 'Tools > Preferences > Chart > Flats Filtration' to "Filter all Flats" and in the code, the offer sides are the same.
The GMT time zone its used in the strategy.
And I've plotted the OHLC info object on chart, as described in the example code (Include in OHLC).

But the value mismatch still persist as the image below shows (yellow arrow, chart value=1.21867 and in the blue circle the calculated value=1.27907).
What am i doing wrong in the code?

Thanks in advance.

Image

Code taken from strategy (I've put in attachment the complete code of this strategy for reference):
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if(instrument != this.instrument || period != this.period){
            return;
        }
        //calculate for the bar that just finished
        double sar = indicators.sar(instrument, period, side, SAR_ACCELERATION, SAR_MAXIMUM, 1);
        print("%s -> sar=%.5f", sdf.format(bidBar.getTime()), sar);
    }


Attachments:
File comment: Strategy code for reference
SARWorking.java [4.76 KiB]
Downloaded 322 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com