Dukascopy
 
 
Wiki JStore Search Login

HELP: Why my strategy does not generate orders?
 Post subject: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Mon 02 Apr, 2012, 17:09 
User avatar

User rating: 0
Joined: Thu 19 May, 2011, 11:48
Posts: 26
Location: Hungary, Budapest
Dear Traders and Support,

Can anyone help me, why my strategy doesn't generate orders? I don't see any exception in strategy log. As I see there were at least two signs on the View only platform, when strategy had to generate orders.

Thanks for your help!

package jfxstrategies;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

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;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Library;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.RequiresFullAccess;


public class JFROCStrategy implements IStrategy {
   

    private int rocperiod = 14;
    private double rr = 3.0;
    private double amount = 3.0;
   
     private transient IEngine engine;
    private transient IConsole console;
    private transient IHistory history;
    private transient IContext context;
    private transient IIndicators indicators;
   
    private final Instrument[] instrumentsa = new Instrument[]{
            Instrument.EURUSD,
//            Instrument.EURJPY,
//            Instrument.EURCAD,
//            Instrument.EURNZD,
//            Instrument.EURAUD,
//            Instrument.GBPUSD,
              Instrument.NZDUSD,
//            Instrument.NZDJPY,
//            Instrument.AUDUSD,
//            Instrument.USDJPY,
//            Instrument.USDCHF 
            };
   
    private final Period[] periodsa = new Period[]{ Period.ONE_HOUR };
    private transient SortedSet<Instrument> instruments;
    private transient final SortedSet<Period> periods = new TreeSet<Period>(Collections.reverseOrder());
   
    private static boolean isTradeAllowed = true;


    public void onAccount(IAccount account) throws JFException {
    }

    public void onBar(Instrument instrument, Period period, IBar bidBar, IBar askBar)
            throws JFException {
        if(instruments.contains(instrument) && periods.contains(period)) {
           
            double[] roc = indicators.roc(
                    instrument,
                    period,
                    OfferSide.BID,
                    AppliedPrice.CLOSE,
                    this.rocperiod,
                    Filter.ALL_FLATS,
                    this.rocperiod,
                    bidBar.getTime(),
                    0 );
           
            double[] atr = indicators.atr(
                    instrument,
                    period,
                    OfferSide.BID,
                    this.rocperiod,
                    Filter.ALL_FLATS,
                    this.rocperiod,
                    bidBar.getTime(),
                    0 );
           
            if(isTradeAllowed) {
                double currRoc = roc[roc.length-1];
                double prevRoc = roc[roc.length-3];
                double sl = round(instrument.getPipScale(), 2*atr[atr.length-1]);
               
                // Buy
                if( prevRoc < 0 && currRoc > 0.15) {
                   
                    println(String.format("Lasr ROC: %1$.5f Prev ROC: %2$.5f 2Prev ROC: %3$.5f", roc[roc.length-1], roc[roc.length-2], roc[roc.length-3]));
                    IOrder o = engine.submitOrder(
                            OrderCommand.BUY.name(),
                            instrument,
                            OrderCommand.BUY,
                            amount,
                            0,
                            5,
                            bidBar.getClose() - sl,
                            bidBar.getClose() + rr*sl
                            );
                }
               
                // Sell
                if( prevRoc > 0 && currRoc < -0.15) {
                    println(String.format("Lasr ROC: %1$.5f Prev ROC: %2$.5f 2Prev ROC: %3$.5f", roc[roc.length-1], roc[roc.length-2], roc[roc.length-3]));
                    IOrder o = engine.submitOrder(
                            OrderCommand.SELL.name(),
                            instrument,
                            OrderCommand.SELL,
                            amount,
                            0,
                            5,
                            bidBar.getClose() + sl,
                            bidBar.getClose() - rr*sl
                            );
                }
            }
        }
    }

    public void onMessage(IMessage message) throws JFException {
   
        printOrderInfo(message);
        IOrder o = message.getOrder();
       
        switch(message.getType()){
       
            case ORDER_FILL_OK :
                double m = 10;
                if(o.getOrderCommand().equals(OrderCommand.BUY)) m = m*-1;
                o.setStopLossPrice(
                        o.getStopLossPrice() + m*o.getInstrument().getPipValue(),
                        OfferSide.BID,
                        10 );
                isTradeAllowed = false;
                break;
               
            case ORDER_CLOSE_OK:
                if(o.getProfitLossInPips() < 0){
                    isTradeAllowed = true;
                }else{
                    double bs = 1;
                    double price = o.getClosePrice();
                    if(o.getOrderCommand().equals(OrderCommand.SELL)) bs = -1;
                    double tp = price + bs * 60 * o.getInstrument().getPipValue();
                    double sl = price - bs * 25 * o.getInstrument().getPipValue();
                   
                    engine.submitOrder(
                            o.getOrderCommand().name(),
                            o.getInstrument(),
                            o.getOrderCommand(),
                            amount,
                            0,
                            5,
                            sl,
                            tp
                            );
                    isTradeAllowed = false;                   
                }
                break;
               
            case ORDER_SUBMIT_REJECTED:
                isTradeAllowed = true;
                break;
               
            case ORDER_FILL_REJECTED:
                isTradeAllowed = true;
                break;
               
            case ORDER_CLOSE_REJECTED:
                isTradeAllowed = false;
                break;
        }
    }

    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();
       
        instruments = new TreeSet<Instrument>(Arrays.asList(instrumentsa));
        periods.addAll(Arrays.asList(periodsa));
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    private <T> void println(T t){
        this.console.getOut().println(t.toString());
    }
   
    private static double round(double scale, double num){
        double i = Math.pow(10, scale);
        return Math.round(num*i)/i;
    }
   
    private void printOrderInfo(IMessage m){
        IOrder o = m.getOrder();
        println(
                m.getType().name() +
                " " + o.getInstrument() +
                " " + o.getOrderCommand().name() +
                " Open: " + o.getOpenPrice() +
                " StopLoss: " + o.getStopLossPrice() +
                " TakeProfit: " + o.getTakeProfitPrice() +
                " Trailing: " + o.getTrailingStep()
                );
    }
}


Best regards,
Kirilla


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Mon 02 Apr, 2012, 19:16 
User avatar

User rating: 1
Joined: Mon 25 Jul, 2011, 11:46
Posts: 29
Location: United Kingdom, Burnley
Hard to tell with no log, maybe the messaging system works a little differently through the remote strategies, so isTradeAllowed is being left at false? It's a complete guess though, so needs an answer from support or someone else :)


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Mon 02 Apr, 2012, 19:24 
User avatar

User rating: 0
Joined: Thu 19 May, 2011, 11:48
Posts: 26
Location: Hungary, Budapest
Thank you for you answer. I don't have any idea what's wrong. No error in strategy log and this worked well on backtest. isTradeAllowed initialized with 'true', so I think at least one order should be done.


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Mon 02 Apr, 2012, 19:46 
User avatar

User rating: 1
Joined: Mon 25 Jul, 2011, 11:46
Posts: 29
Location: United Kingdom, Burnley
Oh I think I know what it is. You're not subscribing to the instruments. You need to call context.setSubscribedInstruments in your onStart.


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Mon 02 Apr, 2012, 19:56 
User avatar

User rating: 0
Joined: Thu 19 May, 2011, 11:48
Posts: 26
Location: Hungary, Budapest
Thank you friend!!! I'll check it.


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Tue 03 Apr, 2012, 21:09 
User avatar

User rating: 8
Joined: Tue 31 Jan, 2012, 11:24
Posts: 72
Location: India, Gurgaon
Antilochus wrote:
Oh I think I know what it is. You're not subscribing to the instruments. You need to call context.setSubscribedInstruments in your onStart.



Exactly Antilochus,

You are right.

I have checked it, and it is not subscribing any instrument.

@ Kirilla,

Kirilla,

You need to add on start method to run this strategy, apart from this, you strategy is stopping due to null point exception. If you can tell me, what you want to do, then I can restructure your code and then give it back to you.


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Mon 16 Apr, 2012, 22:41 
User avatar

User rating: 0
Joined: Thu 19 May, 2011, 11:48
Posts: 26
Location: Hungary, Budapest
Hi drishti!

Why do you think this strategy will stop? I've tested it in Historical tester and worked well. Btw, the strategy is very simple, it uses Rate of Change indicator for entering new trade and atr to define sl and tp. sl = 2 * atr and tp = 3*sl. enter if curr val of ROC is above/below 0.15/-0.15 on Hourly and the value of ROC 2 before is below/above 0. In addition it sets trailing stop in onMessage if order is filled or open a new position in the same direction if position is closed by profit.

best regards,
Kirilla


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Thu 19 Apr, 2012, 14:50 
User avatar

User rating: 8
Joined: Tue 31 Jan, 2012, 11:24
Posts: 72
Location: India, Gurgaon
Kirilla wrote:
Hi drishti!

Why do you think this strategy will stop? I've tested it in Historical tester and worked well. Btw, the strategy is very simple, it uses Rate of Change indicator for entering new trade and atr to define sl and tp. sl = 2 * atr and tp = 3*sl. enter if curr val of ROC is above/below 0.15/-0.15 on Hourly and the value of ROC 2 before is below/above 0. In addition it sets trailing stop in onMessage if order is filled or open a new position in the same direction if position is closed by profit.

best regards,
Kirilla


I will look soon and provide you details.

Thanks.


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Fri 20 Apr, 2012, 06:41 
User avatar

User rating: 0
Joined: Wed 21 Mar, 2012, 18:15
Posts: 3
Location: Switzerland, Zurich
Hi all,
I also have the same problem. My first strategy had a mistake which I now corrected and it runs nicely on a local run. However, I believe the problem might be that I have the same name for both of my files (classes). This probably causes an error at compilation in their workspace, as it would on mine. I didnt think of that I just modified the original file and resent it, but they have two classes with the same name. Could that be the problem?


 
 Post subject: Re: HELP: Why my strategy does not generate orders? Post rating: 0   Post Posted: Sat 21 Apr, 2012, 10:04 
User avatar

User rating: 0
Joined: Wed 21 Mar, 2012, 18:15
Posts: 3
Location: Switzerland, Zurich
I have the same problem, I believe its just because I only modified my original class (strategy runs fine on local run), and sent it again with the same file name. There are two files with the same names on the server and maybe this is the problem...


 

Jump to:  

  © 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