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.

Stochastics Universal price feed help
 Post subject: Stochastics Universal price feed help Post rating: 0   New post Posted: Wed 25 Jun, 2014, 10:54 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
Hi,
I am working on some code for an algo which uses stochastics. I am using the universal method for the indicator as it is easier to change the data types (I chose to test it with renko). My code compiles but it throws an error and closes the strategy then then new data comes through.
Can anybody help with solving it? I've used basically the exact same method with MACD and that works fine and I can't see why this isn't working.
Thanks
J

The error I get is: java.lang.ClassCastException: java.lang.Double cannot be cast to [D @ jforex.RenkoIndicatorTrial.onFeedData(RenkoIndicatorTrial.java:69)
So clearly a problem with type casting but whatever I try I can't seem to get it to work.

package jforex;

import java.util.*;
//Dukascopy imports=========================
import com.dukascopy.api.*;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.feed.util.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IFeedListener;
import com.dukascopy.api.feed.util.RenkoFeedDescriptor;
import com.dukascopy.api.feed.util.TimePeriodAggregationFeedDescriptor;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.util.DateUtils;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
//============================================

public class RenkoIndicatorTrial implements IStrategy, IFeedListener {
    //================================
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    //=================================
    //------FEED DESCRIPTORS---------------------
    //---This is the renko data feed descriptor (2 pip bricks)
    public IFeedDescriptor feedDescriptor = new RenkoFeedDescriptor(Instrument.EURUSD,PriceRange.TWO_PIPS,OfferSide.BID);
    //---------------------------------------------
    //-------------SET DATE AND TIME FORMATS---------
    public static SimpleDateFormat MyDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
       {
       setTimeZone(TimeZone.getTimeZone("GMT"));
       }
    };
    private Instrument selectedInstrument = Instrument.EURUSD;
    //-----------------------------------------------
   //Stochastics constants
    private int stochasticsPeriod=15;
    private int stochasticsSlowKPrd=5;
    private int stochasticsSlowDPrd=5;
    private int stochSlowKMAType = IIndicators.MaType.EMA.ordinal();
    private int stochSlowDMAType = IIndicators.MaType.EMA.ordinal();
   
    private double stochSlowKValue;
    private double stochSlowDValue;
   //------------------------
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        //--subscribe to renko feed
        context.setSubscribedInstruments(java.util.Collections.singleton(feedDescriptor.getInstrument()), true);
        context.subscribeToFeed(feedDescriptor, this);
    }
    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
    Instrument myInstrument = feedDescriptor.getInstrument();
    OfferSide myOfferSide = feedDescriptor.getOfferSide();

            //console.getOut().println("completed" + feedData);
        try {
           
        Object[] stochUniversal = indicators.calculateIndicator(selectedInstrument, feedDescriptor.getPeriod(), new  OfferSide[] {OfferSide.BID}, "STOCH", new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{stochasticsPeriod, stochasticsSlowKPrd, stochSlowKMAType, stochasticsSlowDPrd, stochSlowDMAType}, 0);
        double[][] StochFigures = {(double[])stochUniversal[0],(double[])stochUniversal[1]};
            //below is straight from the wiki - no alterations
            //Object[] stochUniversal = indicators.calculateIndicator(instrument, period, new  OfferSide[] {OfferSide.BID}, "STOCH", new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{fastKPeriod, slowKPeriod, slowKMaType.ordinal(), slowDPeriod, slowDMaType.ordinal()}, shift);

        stochSlowKValue = StochFigures[0][StochFigures[1].length-1];   
        stochSlowDValue = StochFigures[1][StochFigures[1].length-1];   
       
        console.getOut().println(String.format("The latest slow k stoch value is: %.5f and the latest slow d stoch value is: %.5f",stochSlowKValue,stochSlowDValue));
       
        } catch (JFException e) {
            console.getErr().println(e);
            e.printStackTrace();
        }
    }
    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


 
 Post subject: Re: Stochastics Universal price feed help Post rating: 0   New post Posted: Wed 25 Jun, 2014, 12:05 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
instead of calling the indicator by shift, you need to call it over candle interval, thus, change
        Object[] stochUniversal = indicators.calculateIndicator(selectedInstrument, feedDescriptor.getPeriod(), new  OfferSide[] {OfferSide.BID}, "STOCH", new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{stochasticsPeriod, stochasticsSlowKPrd, stochSlowKMAType, stochasticsSlowDPrd, stochSlowDMAType}, 0);
to
Object[] stochUniversal = indicators.calculateIndicator(
              feedDescriptor,
              new  OfferSide[] {OfferSide.BID},
              "STOCH",
              new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE},
              new Object[]{stochasticsPeriod, stochasticsSlowKPrd, stochSlowKMAType, stochasticsSlowDPrd, stochSlowDMAType},
              3,
              feedData.getTime(),
              0
);
This will return the output arrays for the last 3 candles.


 
 Post subject: Re: Stochastics Universal price feed help Post rating: 0   New post Posted: Wed 25 Jun, 2014, 12:50 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
Thanks for the fast response,
I replaced my code with what you provided but now it fails to compile:
----------
The method calculateIndicator(Instrument, Period, OfferSide[], String, IIndicators.AppliedPrice[], Object[], Filter, long, long) in the type IIndicators is not applicable for the arguments (Instrument, Period, OfferSide[], String, IIndicators.AppliedPrice[], Object[], int, long, int)
^^^^^^^^^^^^^^^^^^
Object[] stochUniversal = indicators.calculateIndicator(selectedInstrument, feedDescriptor.getPeriod(), new OfferSide[] {OfferSide.BID}, "STOCH", new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{stochasticsPeriod, stochasticsSlowKPrd, stochSlowKMAType, stochasticsSlowDPrd, stochSlowDMAType}, 3, feedData.getTime(), 0);
1. ERROR in C:\Users\xxxxx\Local Settings\JForex\xxxxx\jfxide\tmp\compile\RenkoIndicatorTrial.java (at line 68)
----------


 
 Post subject: Re: Stochastics Universal price feed help Post rating: 0   New post Posted: Wed 25 Jun, 2014, 12:58 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We updated the snippet. For solving compile-time errors and getting convenient access to our javadocs, consider using an IDE, e.g., Eclipse or NetBeans:
https://www.dukascopy.com/wiki/#Use_in_Eclipse
https://www.dukascopy.com/wiki/#Use_in_NetBeans


 
 Post subject: Re: Stochastics Universal price feed help Post rating: 0   New post Posted: Wed 25 Jun, 2014, 14:14 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
Thank you, for your fast response.
I implemented your change to the snippet and it compiles and works...
However there seems to be an problem with the output.
Every time that there is a new bar the output messages are always the same, i.e. the values are not being updated.
I tested the "feedData.getTime()" component of the stochastics indicator but that seems to update on each candle,

console.getOut().println(String.format("The time is: %s",feedData.getTime()));


yet within the indicator is it always pointing to the same point in time?

package jforex;

import java.util.*;
//Dukascopy imports=========================
import com.dukascopy.api.*;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.feed.util.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IFeedListener;
import com.dukascopy.api.feed.util.RenkoFeedDescriptor;
import com.dukascopy.api.feed.util.TimePeriodAggregationFeedDescriptor;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.util.DateUtils;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
//============================================

public class RenkoIndicatorTrial implements IStrategy, IFeedListener {
    //================================
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    //=================================
    //------FEED DESCRIPTORS---------------------
    //---This is the renko data feed descriptor (2 pip bricks)
    public IFeedDescriptor feedDescriptor = new RenkoFeedDescriptor(Instrument.EURUSD,PriceRange.TWO_PIPS,OfferSide.BID);
    //---------------------------------------------
    //-------------SET DATE AND TIME FORMATS---------
    public static SimpleDateFormat MyDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
       {
       setTimeZone(TimeZone.getTimeZone("GMT"));
       }
    };
    private Instrument selectedInstrument = Instrument.EURUSD;
    //-----------------------------------------------
   //Stochastics constants
    private int stochasticsPeriod=15;
    private int stochasticsSlowKPrd=5;
    private int stochasticsSlowDPrd=5;
    private int stochSlowKMAType = IIndicators.MaType.EMA.ordinal();
    private int stochSlowDMAType = IIndicators.MaType.EMA.ordinal();
   
    private double stochSlowKValue;
    private double stochSlowDValue;
   //------------------------
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        //--subscribe to renko feed
        context.setSubscribedInstruments(java.util.Collections.singleton(feedDescriptor.getInstrument()), true);
        context.subscribeToFeed(feedDescriptor, this);
    }
    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {
    Instrument myInstrument = feedDescriptor.getInstrument();
    OfferSide myOfferSide = feedDescriptor.getOfferSide();

            //console.getOut().println("completed" + feedData);
        try {
           
        Object[] stochUniversal = indicators.calculateIndicator(
              selectedInstrument,
              feedDescriptor.getPeriod(),
              new  OfferSide[] {OfferSide.BID},
              "STOCH",
              new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE},
              new Object[]{stochasticsPeriod, stochasticsSlowKPrd, stochSlowKMAType, stochasticsSlowDPrd, stochSlowDMAType},
              feedDescriptor.getFilter(),
              3,
              feedData.getTime(),
              0
);
        double[][] StochFigures = {(double[])stochUniversal[0],(double[])stochUniversal[1]};
            //below is straight from the wiki - no alterations
            //Object[] stochUniversal = indicators.calculateIndicator(instrument, period, new  OfferSide[] {OfferSide.BID}, "STOCH", new IIndicators.AppliedPrice[] {AppliedPrice.CLOSE}, new Object[]{fastKPeriod, slowKPeriod, slowKMaType.ordinal(), slowDPeriod, slowDMaType.ordinal()}, shift);

        stochSlowKValue = StochFigures[0][StochFigures[0].length-1];   
        stochSlowDValue = StochFigures[1][StochFigures[1].length-1];
   
       
        console.getOut().println(String.format("The slow k stoch value is: %.5f and the slow d stoch value is: %.5f",stochSlowKValue,stochSlowDValue));
       
        } catch (JFException e) {
            console.getErr().println(e);
            e.printStackTrace();
        }
    }
    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


 
 Post subject: Re: Stochastics Universal price feed help Post rating: 0   New post Posted: Wed 25 Jun, 2014, 15:21 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We did not notice that you have got the first two arguments wrong to the IIndicators.calculateIndicator method - you should pass the feedDescriptor argument instead of instrument and period.
    private static final int K = 0;
    private static final int D = 1;
    private Object[] params;

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.indicators = context.getIndicators();

        context.setSubscribedInstruments(java.util.Collections.singleton(feedDescriptor.getInstrument()), true);
        context.subscribeToFeed(feedDescriptor, this);
        params = new Object[] {
                stochasticsPeriod,
                stochasticsSlowKPrd,
                stochSlowKMAType.ordinal(),
                stochasticsSlowDPrd,
                stochSlowDMAType.ordinal()
        };
    }

    public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData) {

        try {
            Object[] stochUniversal = indicators.calculateIndicator(
                    feedDescriptor,
                    new OfferSide[] { OfferSide.BID },
                    "STOCH",
                    new IIndicators.AppliedPrice[] { AppliedPrice.CLOSE },
                    params,
                    3,
                    feedData.getTime(),
                    0
            );
            double[][] stoch = { (double[]) stochUniversal[0], (double[]) stochUniversal[1] };           
            console.getOut().format("Last 3 STOCH k= %s slow d=%s time=%s",
                  toString(stoch[K]),
                  toString(stoch[D]),
                  DateUtils.format(feedData.getTime())
            ).println();
           
            Object[] stochUniversalShift = indicators.calculateIndicator(
                    feedDescriptor,
                    new OfferSide[] { OfferSide.BID },
                    "STOCH",
                    new IIndicators.AppliedPrice[] { AppliedPrice.CLOSE },
                    params,
                    1
            );
           
            double[] stochLast = { (Double) stochUniversalShift[0], (Double) stochUniversalShift[1] };
            console.getOut().format("Last STOCH slow k=%.5f slow d=%.5f \n-------",
                  stochLast[K],
                  stochLast[D]
            ).println();

        } catch (JFException e) {
            console.getErr().println(e);
            e.printStackTrace();
        }
    }
We added the full strategy at the end of the respective wiki section, see RenkoStochShiftAndCandleInterval.java:
https://www.dukascopy.com/wiki/#Indicator_Calculation/Calculate_on_price_feed_by_candle_interval


 
 Post subject: Re: Stochastics Universal price feed help Post rating: 0   New post Posted: Wed 25 Jun, 2014, 16:07 

User rating: 0
Joined: Fri 04 Apr, 2014, 16:35
Posts: 50
Location: Monaco, Monte-Carlo
Great, thank you very much, even more so for the multiple fast responses!


 

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