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.

Trading not happening every hour - for custom strategy
 Post subject: Trading not happening every hour - for custom strategy Post rating: 0   Post Posted: Mon 03 Jun, 2013, 04:42 

User rating: 0
Joined: Mon 03 Jun, 2013, 04:27
Posts: 1
Hi everyone,

----------
First of all, I could not see a sub-forum specifically for programming, so I thought I would post my query in this section. If there is a programming section, please let me know for future reference.
----------

I have been asked to help a user with his strategy - by looking at the Java code he implemented and make some minor tweaks to suit his needs. Basically, he wants to make a trade for every 1 hour period. At the moment, the code I do have is running, but it is not making any trades whatsoever. Because I have very limited knowledge on this particular program, I thought someone here might be able to see what is wrong. Here is the code of this particular strategy:

package jforex;

import com.dukascopy.api.*;
import com.dukascopy.api.Configurable;
import com.dukascopy.api.Filter;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IFillOrder.*;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IIndicators.MaType;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.IUserInterface;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import java.util.*;

public class Strategy implements IStrategy {

    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    private int counter = 0;
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Period")
    public Period selectedPeriod = Period.ONE_HOUR;
    @Configurable("G")
    public double g = 0.1;
    @Configurable("H")
    public double h = 0.1;
    @Configurable("Fast K period")
    public int fastKPeriod = 3;
    @Configurable("Slow K Period")
    public int slowKPeriod = 5;
    @Configurable("K MA Type")
    public MaType slowKMaType = MaType.SMA;
    @Configurable("Slow D Period")
    public int slowDPeriod = 5;
    @Configurable("D MA Type")
    public MaType slowDMaType = MaType.SMA;
    @Configurable("DMI")
    public int DMI = 14;
    @Configurable("Amount")
    public double amount = 0.1;
    @Configurable("Stop loss")
    public int stopLossPips = 30;
    @Configurable("Take profit")
    public int takeProfitPips = 90;
    private IOrder order;
    private Filter filter = Filter.ALL_FLATS;

    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();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        if (order != null) {
            order.close();
        }
    }

    public void onTick(Instrument barInstrument, ITick tick) throws JFException {
    }

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

        if (order != null && order.getState() == IOrder.State.CLOSED) {
            order = null;
        }

        if (!instrument.equals(barInstrument) || (period != this.selectedPeriod)) {
            return;
        }
        IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);

        double[] dPlusDI = indicators.plusDi(instrument, selectedPeriod, OfferSide.BID, DMI, filter, 2, bidBar.getTime(), 0);

        double[] dMinusDI = indicators.minusDi(instrument, selectedPeriod, OfferSide.BID, DMI, filter, 2, bidBar.getTime(), 0);

        double[] dStoch = indicators.stoch(instrument, period, OfferSide.BID, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, 0);

       

        if (period != selectedPeriod) {

            //GO LONG
            if (order == null && dPlusDI[1] > dPlusDI[0] * (1 + g) && dMinusDI[1] < dMinusDI[0] * (1 - g) && dStoch[0] > dStoch[1] * (1 + h)) {
                //if all conditions are satisfied, make a trade (buy currency using specified instrument)
                order = engine.submitOrder("EUR/USD", instrument, IEngine.OrderCommand.BUY, amount);
                console.getOut().println("BUY");
            } else {
                //if any condition fails, close the trade
                order.close();
                console.getOut().println("CLOSE");
            }

            //GO SHORT
            if (order == null && dPlusDI[1] < dPlusDI[0] * (1 - g) && dMinusDI[1] < dMinusDI[0] * (1 + g) && dStoch[0] < dStoch[1] * (1 - h)) {
                //if all conditions are satisfied, make a trade (sell currency using specified instrument)
                order = engine.submitOrder("EUR/USD", instrument, IEngine.OrderCommand.SELL, amount);
                console.getOut().println("SELL");
            } else {
                //if any condition fails, close the trade
                order.close();
                console.getOut().println("CLOSE");
            }

            if (order != null && order.getState() == IOrder.State.FILLED) {
                if (order.isLong() == true && (dPlusDI[1] < dPlusDI[0] || dMinusDI[1] > dMinusDI[0] || dStoch[0] < dStoch[1])) {
                    order.close();
                    console.getOut().println("CLOSE");
                    order = null;
                } else if (order.isLong() == false && (dPlusDI[1] > dPlusDI[0] || dPlusDI[1] < dMinusDI[0] || dStoch[0] > dStoch[1])) {
                    order.close();
                    console.getOut().println("CLOSE");
                    order = null;
                }
            }

    }
    }
}


I have enclosed the executing code in an if block (which begins with if (period != selectedPeriod) {), assuming that a trade will be made only every hour. I think an else block should accompany this if block, but I wouldn't know what to put in it.

I will be grateful for any help received.


Attachments:
Robs-Strategy.java [4.75 KiB]
Downloaded 308 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: Trading not happening every hour - for custom strategy Post rating: 0   Post Posted: Mon 03 Jun, 2013, 07:30 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider logging relevant values (i.e. indicators, bars, orders etc.) in the decision points in the code such that you see which execution paths get taken and why, see:
https://www.dukascopy.com/wiki/#IConsole/Logging_values


 
 Post subject: Re: Trading not happening every hour - for custom strategy Post rating: 2   Post Posted: Mon 03 Jun, 2013, 11:02 
User avatar

User rating: 70
Joined: Sat 22 Sep, 2012, 17:43
Posts: 118
Location: Brazil, Fortaleza, Ceará
if the current bar refers to another instrument or a period we are not interested in - return.
if (!instrument.equals(barInstrument) || (period != this.selectedPeriod))
{
   return;
}


if the current bar refers to a period we are not interested in - attempt to trade :!:
second block never reached due to the logic in the first so change '!=' to '==' in the first instance.
if (period != selectedPeriod)
{
}


if order==null && [trading signal criteria is true] placeorder else closeorder :!:
as soon as you place an order - order is no longer null. if open trade lasts at least one hour, it will close at next bar since order !=null
closing logic below not relevant therefore. also, all trades labelled "EUR/USD" ???
if (order == null && dPlusDI[1] > dPlusDI[0] * (1 + g) && dMinusDI[1] < dMinusDI[0] * (1 - g) && dStoch[0] > dStoch[1] * (1 + h))
{
   //if all conditions are satisfied, make a trade (buy currency using specified instrument)
   order = engine.submitOrder("EUR/USD", instrument, IEngine.OrderCommand.BUY, amount);
   console.getOut().println("BUY");
}
else
{
   //if any condition fails, close the trade
   order.close();
   console.getOut().println("CLOSE");
}
...
...


if you follow the advice of Support you will see from your output statements that the execution path is all wrong :!:


 
 Post subject: Re: Trading not happening every hour - for custom strategy Post rating: 2   Post Posted: Tue 04 Jun, 2013, 12:50 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
You can also setup an Eclipse project (or NetBeans if you like), and test the code in that IDE, with debug facilities (breakpoints, watches, etc.).

Take a look here: https://www.dukascopy.com/wiki/#Use_in_Eclipse


 

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