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.

simple EMA crossover not working constantly
 Post subject: simple EMA crossover not working constantly Post rating: 0   New post Posted: Mon 28 Sep, 2015, 12:17 
User avatar

User rating: 0
Joined: Fri 04 Sep, 2015, 12:28
Posts: 4
Location: BelgiumBelgium
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();
    }
  }



 
 Post subject: Re: simple EMA crossover not working constantly Post rating: 0   New post Posted: Fri 02 Oct, 2015, 09:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
There are two reasons:
1) Code compares 0th and 2nd bars, while 1st bar can have any value
2) Using 0th bar to detect a cross is unstable because this bar is not finished yet. Final value of the bar is going to be different than value at the beginning.

A more precise way would be to use 2nd and 1st bar to test for cross but this would introduce a delay. So it is a tradeoff between precision and speed.


 

Jump to:  

cron
  © 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