Hello,
I'm practice with a simple strategy, that sends me an email if the EMA20 crosses the EMA80 for example.
Sometimes, it is correct, but most of the time not
I have no idea why? Can someone help me with this?
Here is the code:
private Period period = Period.TEN_MINS;
public IFeedDescriptor feedDescriptor = new TimePeriodAggregationFeedDescriptor(Instrument.EURUSD, period,
OfferSide.BID, Filter.NO_FILTER);
@Override
public void onStart(IContext context) throws JFException {
this.console = context.getConsole();
this.indicators = context.getIndicators();
this.context = context;
context.setSubscribedInstruments(java.util.Collections.singleton(feedDescriptor.getInstrument()), true);
context.subscribeToFeed(feedDescriptor, this);
}
@Override
public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
try {
double[] ema80 = indicators.ema(feedDescriptor, AppliedPrice.CLOSE, feedDescriptor.getOfferSide(), 80).calculate(
3, feedData.getTime(), 0);
double[] ema20 = indicators.ema(feedDescriptor, AppliedPrice.CLOSE, feedDescriptor.getOfferSide(), 20).calculate(
3, feedData.getTime(), 0);
this.ema20pre = ema20[0];
this.ema80pre = ema80[0];
this.ema20current = ema20[2];
this.ema80current = ema80[2];
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
if (ema20pre < ema80pre && ema20current > ema80current) {
// cross up
sendMail("Stijgend :" + dateFormat.format(new Date(feedData.getTime())) + "\r\n");
} else if (ema20pre > ema80pre && ema20current < ema80current) {
// cross down
sendMail("Dalend :" + dateFormat.format(new Date(feedData.getTime())) + "\r\n");
}
} catch (JFException e) {
e.printStackTrace();
}
}