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.

Problem with JForex API ema?
 Post subject: Problem with JForex API ema? Post rating: 0   Post Posted: Thu 17 Oct, 2013, 08:55 

User rating: 0
Joined: Thu 17 Oct, 2013, 08:50
Posts: 1
Location: India, Bangalore
Hi,

I am new to coding on JForex; but am an experienced programmer on Java.

This is what I do -

double ema20n = indicators.ema(instrument, timeframe, OfferSide.ASK,
AppliedPrice.CLOSE, period20, shift);
; where timeframe = THIRTY_MINUTES, period20 = 20, shift = 1.

The function is supposed to give me the 20 period ema at the last fully completed on a thirty minute chart. I also apply the ema indicator on the chart with same params.

I find that the two ema values are different. This shouldn't ideally happen. Any idea what I am doing wrong? Or is this a bug on JForex API?


 
 Post subject: Re: Problem with JForex API ema? Post rating: 0   Post Posted: Thu 17 Oct, 2013, 09:31 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
See:
https://www.dukascopy.com/wiki/#Indicator_usage_checklist
and for chart-to-strategy value comparison:
https://www.dukascopy.com/wiki/#Add_indicators_on_chart/Include_in_OHLC


 
 Post subject: Re: Problem with JForex API ema? Post rating: 1   Post Posted: Thu 17 Oct, 2013, 09:33 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Very much this is the filter.
Probably you use filter on the chart (Filter.WEEKENDS, or Filter.ALL_FLATS), but not in the given code.

If that is the case, and you really use flat filter on the chart, you have to use the same filter in the code as well.

There is another overloaded version of the indicators.ema() function, where you can define a filter. But that function requires more parameters. From javadoc:
double[] ema(Instrument instrument,
             Period period,
             OfferSide side,
             IIndicators.AppliedPrice appliedPrice,
             int timePeriod,
             Filter filter,
             int numberOfCandlesBefore,
             long time,
             int numberOfCandlesAfter)
             throws JFException

    Calculates the Exponential Moving Average for bars specified with numberOfCandlesBefore, time and numberOfCandlesAfter parameters.

    Parameters:
        instrument - instrument of the bar
        period - period of the bar
        side - Bid or Ask side of the bar
        appliedPrice - type of input data
        timePeriod - time period value
        filter - filter
        numberOfCandlesBefore - how much candles to load before and including the candle with time specified in the time parameter
        time - time of the last candle in the period specified with the numberOfCandlesBefore parameter or/and time of the candle prior to the first candle in the period specified with the numberOfCandlesAfter parameter
        numberOfCandlesAfter - how much candles to load after (and not including) the candle with time specified in the time parameter
    Returns:
        values for the specified bars
    Throws:
        JFException - when parameters are not valid
    See Also:
        Indicator calculation, Exponential Moving Average


You need to define a time parameter, which can be for example the time of the tick that triggered the onTick() method:
public void onTick(Instrument instrument, ITick tick) throws JFException {
    ...
    double ema20n_array[] = indicators.ema(instrument, timeframe, OfferSide.ASK, AppliedPrice.CLOSE, period20, Filter.WEEKENDS, 1, tick.getTime(), 0);
    double ema20n = ema20n_array[0];
    ...
}


Or if you want calculate it during bar creation, you can use the bar's time:
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    ...
    double ema20n_array[] = indicators.ema(instrument, timeframe, OfferSide.ASK, AppliedPrice.CLOSE, period20, Filter.WEEKENDS, 1, askBar.getTime().getTime(), 0);
    double ema20n = ema20n_array[0];
    ...
}



Keep in mind also, that now you calculate EMA with OfferSide.ASK. This means that you have to select on the chart "Ask" as well. (Next to the period and chart type on the toolbar). This way you will see the same values.


 

Jump to:  

  © 1998-2026 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