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.