Hi, please consider the following jforex strategy. This strategy identify 2 EMA cross and send a message to the console. Two pairs are processed : eurodol and yen.
The 2 EMA are calculate on the basis of a 4 hours timeframe.
I want to be warmed every 5 minutes. Every alert gives me the ema values.
The console identify EMA cross today :
17:50:01 Signal Long, cross EMA sur NZD/USD. emaFast0/Fast1/emaSlow0/Slow1 0.7357725795549369 0.7351857758433036 0.7355645783856398 0.7354184268219457
17:45:00 Signal Long, cross EMA sur NZD/USD. emaFast0/Fast1/emaSlow0/Slow1 0.7357402718626291 0.7351857758433036 0.7355560069570685 0.7354184268219457
17:40:00 Signal Long, cross EMA sur NZD/USD. emaFast0/Fast1/emaSlow0/Slow1 0.735786425708783 0.7351857758433036 0.7355682518550276 0.7354184268219457
As you can see, the strategy identify a EMA cross. But when you see the chart, there is no cross on the nzd/usd 4 hours.
Please find attached the chart (nzd/usd today UT H4)
Please find below the code :
package jforex.strategies.sdk;
import java.awt.Color;
import java.awt.Font;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Library;
import com.dukascopy.api.Period;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.drawings.IChartDependentChartObject;
import com.dukascopy.api.drawings.IChartObjectFactory;
import com.dukascopy.api.drawings.IOhlcChartObject;
import com.dukascopy.api.indicators.OutputParameterInfo.DrawingStyle;
public class DK_CrossEMAv1 implements IStrategy {
@Configurable("Periode de calcul indicateur")
public Period selectedPeriod = Period.FOUR_HOURS;
@Configurable("Intervalle de scan")
public Period scanPeriod = Period.FIVE_MINS;
@Configurable("")
public int emaTimePeriodFast = 21;
@Configurable("")
public int emaTimePeriodSlow = 30;
private Set<Instrument> selectedInstruments = new HashSet<Instrument>();
private IEngine engine;
private IHistory history;
private IConsole console;
private IContext context;
private IOrder order;
private IIndicators indicators;
private IChart chart;
@Override
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.history = context.getHistory();
this.console = context.getConsole();
this.indicators = context.getIndicators();
this.context = context;
selectInstrument(Instrument.EURUSD);
selectInstrument(Instrument.USDJPY);
Set<Instrument> instruments = (Set<Instrument>)((HashSet<Instrument>)selectedInstruments).clone();
context.setSubscribedInstruments(instruments);
// subscribe the instrument that we are going to work with
context.setSubscribedInstruments(selectedInstruments, true);
}
@Override
public void onMessage(IMessage message) throws JFException {}
private void selectInstrument(Instrument instrument)
{
selectedInstruments.add(instrument);
}
@Override
public void onStop() throws JFException {
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (selectedInstruments.contains(instrument)&&(period == scanPeriod)) {
try{
double emaFast0 = indicators.ema(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, emaTimePeriodFast,0);
double emaFast1 = indicators.ema(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, emaTimePeriodFast,1);
double emaSlow0 = indicators.ema(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, emaTimePeriodSlow,0);
double emaSlow1 = indicators.ema(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, emaTimePeriodSlow,1);
if (emaSlow1 > emaFast1
&& emaFast0 >= emaSlow0
) { // smaFast falls below smaSlow
console.getOut().println("Signal Long, cross EMA sur " + instrument + ". emaFast0/Fast1/emaSlow0/Slow1 " + emaFast0 + " "+ emaFast1 + " "+ emaSlow0 + " "+ emaSlow1);
} else if (emaSlow1 < emaFast1
&& emaFast0 <= emaSlow0
) { // smaFast overtakes smaSlow
console.getOut().println("Signal Short, cross EMA sur " + instrument + ". emaFast0/Fast1/emaSlow0/Slow1 " + emaFast0 + " "+ emaFast1 + " "+ emaSlow0 + " "+ emaSlow1);
}
} catch (Exception e) {
console.getErr().println(e);
e.printStackTrace();
}
}
}
@Override
public void onAccount(IAccount account) throws JFException {
}
}
