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.

How to calculate a simple derivative ?
 Post subject: How to calculate a simple derivative ? Post rating: 0   New post Posted: Wed 21 Dec, 2011, 10:22 
User avatar

User rating: 4
Joined: Wed 21 Dec, 2011, 10:21
Posts: 30
Location: France, Sallanches
Hello everybody,
I’m a new user of JForex. I have already code something in java, but the JForex API is’nt very clear for me at this time (I need to exercise myself).
So sorry if my question seems to be a little bit simple or stupid.

I would like to calculate the derivative of a SMA with the simple formula:

Derivative = math.abs(sma[i]-sma[i-1]);

Where sma[i] is the value of sma of the lastest bar and sma[i-1] is the value os sma of the lastest bar-1.

However, I have misunderstood how to the indicators works. Anyone can give me a little exemple of code ? It will be great.

By advance, thanks !


 
 Post subject: Re: How to calculate a simple derivative ? Post rating: 0   New post Posted: Wed 21 Dec, 2011, 15:12 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please consider the following code.
It computes derivative and prints it to console when a new bar is created.

package jforex.strategies;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
import java.text.DecimalFormat;

public class MAStrategySample implements IStrategy {

    private IConsole console;
    private IIndicators indicators;
   
    @Configurable("Instrument")
    public Instrument selectedInstrument = Instrument.EURUSD;
    @Configurable("Applied price")
    public AppliedPrice appliedPrice = AppliedPrice.CLOSE;   
    @Configurable("MA type")
    public MaType maType = MaType.SMA;
    @Configurable("MA time period")
    public int maPeriod = 5;
   
    private OfferSide selectedOfferSide = OfferSide.BID;
    private Period selectedPeriod = Period.TEN_SECS;

    public void onStart(IContext context) throws JFException {
        this.indicators = context.getIndicators();
        this.console = context.getConsole();
       
        // add MA indicator to chart from strategy
        IChart chart = context.getChart(selectedInstrument);       
        if (chart != null) {
            selectedOfferSide = chart.getSelectedOfferSide();           
            selectedPeriod = chart.getSelectedPeriod();
            chart.addIndicator(indicators.getIndicator("MA"), new Object[]{maPeriod, maType.ordinal()});
        }
    }

    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 {
        if (!instrument.equals(this.selectedInstrument)) {
            return;
        }
    }

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

        if (!instrument.equals(this.selectedInstrument)
                || !period.equals(this.selectedPeriod)) {
            return;
        }
       
        // open order signals with MA
        int barCount = 2;
        double[] ma = indicators.ma(instrument, this.selectedPeriod, selectedOfferSide, appliedPrice, maPeriod, maType, Filter.ALL_FLATS, barCount, askBar.getTime(), 0);
        // print derivative of last two bars
        printDouble(ma[1] - ma[0]);
    }

    private void printDouble(double d) {
        console.getOut().println((new DecimalFormat("#.#######")).format(d));
    }
}


Attachments:
MAStrategySample.java [2.36 KiB]
Downloaded 254 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: How to calculate a simple derivative ? Post rating: 0   New post Posted: Thu 22 Dec, 2011, 14:01 
User avatar

User rating: 4
Joined: Wed 21 Dec, 2011, 10:21
Posts: 30
Location: France, Sallanches
Thanks a lot !
That's exactly what i wanted.


 

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