Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Submit JForex API bug reports in this forum only.
    Submit Converter issues in Converter Issues.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Different values between chart and consol get out
 Post subject: Different values between chart and consol get out Post rating: 0   New post Posted: Mon 16 Mar, 2015, 18:56 
User avatar

User rating: 0
Joined: Wed 24 Oct, 2012, 17:12
Posts: 20
Location: France, Paris
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 {
    }

}



Image


Attachments:
nzdusd.png [129.7 KiB]
Downloaded 313 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Different values between chart and consol get out Post rating: 0   New post Posted: Tue 17 Mar, 2015, 09:34 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Indicator value with index 0 represents currently forming bar. Therefore this value will move up and down as the new bar gets formed.
And the conditions like "emaFast0 <= emaSlow0" can be different when the new bar opens and when it closes.

Also, please check
https://www.dukascopy.com/wiki/#Indicat ... _checklist


 

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