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 run one strategy on multiple pairs
 Post subject: How to run one strategy on multiple pairs Post rating: 0   New post Posted: Wed 13 Jan, 2010, 15:02 

User rating: 0
Joined: Mon 04 Jan, 2010, 19:56
Posts: 9
Is there a simple answer on the question "How to run one strategy on multiple pairs"? I coded a strategy which I would like to run on for example eurusd and usdcad. Of course I can make two copies of the same file, rename one and put another instrument in one of the codes. However, it would be easier to simply connect a strategy with a currency pair in the jforex platform like it is possible e.g. Metatrader. Is there such a possibility?
Thanks,
Oliver


 
 Post subject: Re: How to run one strategy on multiple pairs Post rating: 0   New post Posted: Thu 14 Jan, 2010, 11:42 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
in a JForex you could run your strategy for any instrument (currency pair), which is opened in your workspace Currencies node (see WorkspaceCurrencies_node.png). Strategy will receive data for each opened currency. Consider this example:
   public void onTick(Instrument instrument, ITick tick) throws JFException {
      if (instrument == Instrument.EURUSD){
         //here goe's your business logic for EUR/USD currency pair
      }
      if (instrument == Instrument.EURGBP){
         //here goe's your business logic for EUR/GBP currency pair
      }
   }

There is also possibility to define by user an instrument which strategy will use. Use an annotation type Configurable. Consider this example:
                      . . .
      @Configurable("instrument1")
       public Instrument instrument1;
      
      @Configurable("instrument2")
       public Instrument instrument2;
                      . . .
   
   public void onTick(Instrument instrument, ITick tick) throws JFException {
         if (instrument1 != null && instrument1==instrument){
            //here goe's your business logic for instrument1
         }
         if (instrument2 != null && instrument2==instrument){
            //here goe's your business logic for instrument2
         }
      }



Attachments:
Workspace_Currencies_node.png [6.43 KiB]
Downloaded 843 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 run one strategy on multiple pairs Post rating: 0   New post Posted: Fri 22 Jan, 2010, 00:02 

User rating: 0
Joined: Fri 15 Jan, 2010, 13:58
Posts: 8
Support wrote:
Consider this example:


Hi, I want to do something similar - gather ticks from 2 different currencies within one strategy. However I'm using a jftoolbox1.7 with my strategy, not plain java.

Would you please tell me how to benefit from mentioned code together with jftoolbox class?

package jforex.converted;
import java.awt.Color;
import jforex.mql.MQLBridge;
import com.dukascopy.api.*;
@Library("jftoolbox-1.7.jar")
public class mystrategy extends MQLBridge {
protected int init()
  {
       return(0);
  }
protected int deinit()
  {
       return(0);
  }
protected int start()
  {
  }
       return(0);
  }
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException{}/**/};


+

   public void onTick(Instrument instrument, ITick tick) throws JFException {
      if (instrument == Instrument.EURUSD){
         //here goe's your business logic for EUR/USD currency pair
      }
      if (instrument == Instrument.EURGBP){
         //here goe's your business logic for EUR/GBP currency pair
      }
   }


How to connect these two pieces of code to have it working? By working I mean to receive ticks value of 2 different currencies into variables and have possibility to use those variables inside
public class mystrategy extends MQLBridge { }


Thanks in advance.


 
 Post subject: Re: How to run one strategy on multiple pairs Post rating: 0   New post Posted: Tue 18 Dec, 2012, 06:31 
User avatar

User rating: 0
Joined: Mon 29 Aug, 2011, 04:00
Posts: 4
Location: Canada, Halifax
Hi API Support,

Is there a way to test for all instruments currently selected in the historical tester currency node?

When testing on 20+ instruments for example, instead of using - @Configurable("instrument1") public Instrument instrument1;
All the way to Instrument 20, could you use something similar to - @Configurable("Instrument") public Instrument instrument = Instrument.??????;

Where ?????? would be an input processing all instruments made available by the user in the currency node?



Thanks v much,

Nachtschwarmer


 
 Post subject: Re: How to run one strategy on multiple pairs Post rating: 0   New post Posted: Tue 18 Dec, 2012, 08:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Nachtschwarmer wrote:
Is there a way to test for all instruments currently selected in the historical tester currency node?
The strategy always works with all subscribed instruments, unless you apply specific filtering methods, see:
https://www.dukascopy.com/wiki/#Filter_Ticks_Bars
Nachtschwarmer wrote:
When testing on 20+ instruments for example, instead of using - @Configurable("instrument1") public Instrument instrument1;
All the way to Instrument 20, could you use something similar to - @Configurable("Instrument") public Instrument instrument = Instrument.??????;
This is just to make the instrument list modifiable by the user. At the moment you can't define a java.util.Collection that would appear in the parameter dialog, however, if it is not modifiable from parameters dialog, then you can use an approach similar to this, see:
https://www.dukascopy.com/wiki/#Sentiment_index/Multiple_instruments_and_currencies


 
 Post subject: Re: How to run one strategy on multiple pairs Post rating: 0   New post Posted: Thu 27 Dec, 2012, 03:15 
User avatar

User rating: 0
Joined: Mon 29 Aug, 2011, 04:00
Posts: 4
Location: Canada, Halifax
API Support wrote:
Nachtschwarmer wrote:
Is there a way to test for all instruments currently selected in the historical tester currency node?
The strategy always works with all subscribed instruments, unless you apply specific filtering methods, see:
https://www.dukascopy.com/wiki/#Filter_Ticks_Bars


In that case, would there be a simple and quick solution to un-filter the following strategy from one defined instrument to all subscribed instruments in the historical tester?

package jforex.strategies;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import java.util.Calendar;
import java.text.ParseException;


public class EURJPYTEST_H1_0100_7040 implements IStrategy {
@Configurable("Instrument")
public Instrument instrument = Instrument.EURJPY;
@Configurable("Instrument")
public Period period = Period.ONE_HOUR;
@Configurable("Amount")
public double amount = 0.01;
@Configurable("Stop loss")
public int stopLossPips = 40;
@Configurable("Take profit")
public int takeProfitPips = 70;

private SimpleDateFormat gmtSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

private IEngine engine;
private IConsole console;
private IHistory history;

private int orderCount;

@Override
public void onStart(IContext context) throws JFException {
    this.engine = context.getEngine();
    this.console = context.getConsole();
    this.history = context.getHistory();
    console.getOut().println("start");
    gmtSdf.setTimeZone(TimeZone.getTimeZone("GMT"));
}

@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
}

private boolean isValidTimeFromSdf(int fromHour, int fromMin, int toHour, int toMin) throws JFException {
    long lastTickTime = history.getLastTick(instrument).getTime();
    //you want to work with the date of the last tick - in a case you are back-testing
    String fromStr = gmtSdf.format(lastTickTime).substring(0, 11) + String.valueOf(fromHour)+":"+String.valueOf(fromMin) + ":00";
    String toStr = gmtSdf.format(lastTickTime).substring(0, 11) + String.valueOf(toHour)+":"+String.valueOf(toMin) + ":00";
    try {
        long from = gmtSdf.parse(fromStr).getTime();
        long to = gmtSdf.parse(toStr).getTime();
        //print("sdf: " + gmtSdf.format(from) + " - " + gmtSdf.format(to) + " last tick: " + gmtSdf.format(lastTickTime));
        return lastTickTime > from && lastTickTime < to;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return false;
}

     

@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    if( this.instrument != instrument || this.period != period || !isValidTimeFromSdf(1,57,2,06) ){
        return;
    }
//take previous bar from historical data
    IBar prevBar = history.getBar(instrument, period, OfferSide.BID, 2);
    OrderCommand cmd = (bidBar.getClose() > prevBar.getClose()) ? OrderCommand.BUY : OrderCommand.SELL;
    submitOrder(cmd);
}

private IOrder submitOrder(OrderCommand orderCmd) throws JFException {
double stopLossPrice, takeProfitPrice;
// Calculating stop loss and take profit prices
if (orderCmd == OrderCommand.BUY) {
stopLossPrice = history.getLastTick(this.instrument).getBid() - getPipPrice(this.stopLossPips);
takeProfitPrice = history.getLastTick(this.instrument).getBid() + getPipPrice(this.takeProfitPips);
} else {
stopLossPrice = history.getLastTick(this.instrument).getAsk() + getPipPrice(this.stopLossPips);
takeProfitPrice = history.getLastTick(this.instrument).getAsk() - getPipPrice(this.takeProfitPips);
}
// Submitting an order for the specified instrument at the current market price
return engine.submitOrder("order" + ++orderCount, this.instrument, orderCmd, this.amount, 0, 5, stopLossPrice, takeProfitPrice);
}
private double getPipPrice(int pips) {
return pips * this.instrument.getPipValue();
}

@Override
public void onMessage(IMessage message) throws JFException {
//if the message is related to order print its content
if (message.getOrder() != null){
console.getOut().println(message.getOrder().getLabel() + " " + message.getType() + " " + message.getContent());
}
}

@Override
public void onAccount(IAccount account) throws JFException {
}

@Override
public void onStop() throws JFException {


}
}


Thanks Again,

Nachtschwarmer


 
 Post subject: Re: How to run one strategy on multiple pairs Post rating: 0   New post Posted: Thu 27 Dec, 2012, 08:16 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Nachtschwarmer wrote:
In that case, would there be a simple and quick solution to un-filter the following strategy from one defined instrument to all subscribed instruments in the historical tester?
See the part about onTick and onBar:
https://www.dukascopy.com/wiki/#Strategy_API
Revisit:
https://www.dukascopy.com/wiki/#Filter_Ticks_Bars


 
 Post subject: Re: How to run one strategy on multiple pairs Post rating: 0   New post Posted: Mon 13 Jul, 2015, 07:15 

User rating: 0
Joined: Mon 17 Jun, 2013, 23:30
Posts: 48
Location: Australia, Melbourne
Hi,

I have a similar problem. I want to apply the same "Alert" strategy to 24 pairs, in order to receive an alert when the indicators are in a possible setup. The script works for one pair, however I would like to avoid running 24 different files, is it possible?

I read this post and also used the information in:

https://www.dukascopy.com/wiki/#Sentime ... currencies

The script compiles OK, no error, however when started it stops immediately with NullPointerException.

I attached the script and will appreciate if somebody can point the logic error.

Kind Regards


Attachments:
Alert.java [25.34 KiB]
Downloaded 133 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 run one strategy on multiple pairs Post rating: 0   New post Posted: Mon 13 Jul, 2015, 10:09 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Null pointer exception happens because InnerStrategy.onStart() is never called.


 

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