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.

tester - subscribe instruments from strategy for internal use
 Post subject: tester - subscribe instruments from strategy for internal use Post rating: 0   Post Posted: Sun 26 May, 2013, 05:23 

User rating: 1
Joined: Mon 27 Feb, 2012, 22:49
Posts: 119
Location: Czech Republic,
My new strategy use currency strength, which need a lot of instruments.

I want to test only one instrument, but I must to select a lot of instruments. Tester in visual mode open a lot of charts, but I need only one.

I calculate position size and risk to equity in account currency (USD) and if I test cross pair I need quote from instrument primary currency and USD. And I have this problem too.

If strategy could subscribe instruments for internal use this problem will be not. Could you allow this subscribing?

Thanks very much.


 
 Post subject: Re: tester - subscribe instruments from strategy for internal use Post rating: 2   Post Posted: Mon 27 May, 2013, 03:37 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
Maybe I don't really understand your question, but... what do you mean "internal usage" ?

Each strategy must/should subscribe in onStart to the Set of instruments it is interested in.

It should also reject the possibility in any relevant callback of instruments which it does not want.

You must be new to authoring IStrategies? But also I haven't used the Historical Tester for multiple feed instruments, just to look at a single instrument. Maybe Historical Tester does something like open a bunch of charts, but I only see it opening the single chart on the instrument I'm interested in... so this may not help you.

You should be able to subscribe to all of the instrument feeds you need, but open a chart on only the one you're interested to test visually. I'm pretty sure that is possible, but I may be wrong. Very simple strategies work like this, and you subscribe WITHIN THE STRATEGY to the instrument feeds that you're interested in.

/// global variables
   @Configurable("Instrument")
   public Instrument selectedInstrument = Instrument.EURUSD; // just one
///
   final Set<Instrument> instruments = new HashSet<Instrument>();
///
/// the next two lines are within the onStart method
      instruments.add(selectedInstrument);
      // more than one *could* be placed in the Set
      context.setSubscribedInstruments(instruments, true);
///
/// within onTick (for example) we reject instrument(s) we do not want
      // reject/return unless the single instrument we want
      if ((false==instrumentArg.equals(selectedInstrument))) return;



In standalone mode, if we get the next API update, we should be able to open charts in historical testing mode, but I imagine you want to do this within JForex platform's historical tester. Standalone involves lots of other complexities you'd do best to avoid.

HyperScalper


 
 Post subject: Re: tester - subscribe instruments from strategy for internal use Post rating: 0   Post Posted: Mon 27 May, 2013, 05:59 

User rating: 1
Joined: Mon 27 Feb, 2012, 22:49
Posts: 119
Location: Czech Republic,
HyperScalper thanks for answer, but:

My new strategy use currency strength, which need a lot of instruments.

I want to test only one instrument, but I must to select a lot of instruments. Tester in visual mode open a lot of charts, but I need only one.

....


 
 Post subject: Re: tester - subscribe instruments from strategy for internal use Post rating: 2   Post Posted: Mon 27 May, 2013, 16:46 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
lifon wrote:
HyperScalper thanks for answer, but:

My new strategy use currency strength, which need a lot of instruments.

I want to test only one instrument, but I must to select a lot of instruments. Tester in visual mode open a lot of charts, but I need only one.

....


Can't you select just the single instrument you want in the Historical Tester, but then have the Strategy module subscribe to all the other instruments you need "internally" to the calculations? Doesn't that work? I admit I've not tried to do this, but isn't that what you want to do?

So you can get the Strategy "internally" to receive Ticks, Bars, whatever, from the other instruments (from the various other instruments), do your currency "strength" calculations, but then generate signals for the single instrument which you are backtesting?

Again, I haven't done this, but wouldn't that work?

HyperScalper


 
 Post subject: Re: tester - subscribe instruments from strategy for internal use Post rating: 2   Post Posted: Mon 27 May, 2013, 17:52 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
OK, your claims were driving me crazy so I hacked this strategy together.

In the Visual tester, I selected only instrument EUR/USD and that was the ONLY chart which was created. If you use this approach, I think you'll get the "internal" data you want, but only the chart you are interested in.

But, when running this, as you can see, if all are selected, then the Strategy subscribes to all of the specified instruments. (This is only a hacked together example, so you would have to do whatever you need in your own processing, maybe not on ticks, but bars or whatever you do.)

ONE REQUIREMENT may be to make sure the
context.setSubscribedInstruments(subscribedInstruments, true); // lock or wait flag
use the "lock" flag is true.

As you can see further down, all of the subscribed instruments provide mixed onTick data,
but only the single chart instrument selected in the Historical Tester is displayed.

In the code below, the "tradedInstrument" variable should be the ONLY chart you ask
the Historical Tester to show. That's the only instrument you need to check when starting
the backtesting run.

IT IS NOT NECESSARY TO SPECIFY ANY INSTRUMENT TO THE HISTORICAL TESTER
UNLESS YOU WANT IT TO DISPLAY THAT CHART. The "internal" instruments' data you want
are requested inside the Strategy code, as you can see.

I ran this under the JForex Live platform historical tester
FXDD ver2.24.17.2 API ver2.7.9.5, while it was doing other things like
trading live, etc. I almost never use DEMO, so I really haven't tested it
in the DEMO version.

So you have all of your "internal" instruments available for processing, but only the
single chart you want which presumably would be where your trading stuff appears.

In my opinion, this is exactly what the Historical Tester should do, and it does it correctly.

HyperScalper

package com.fs.strategy;

import java.util.HashSet;
import java.util.Set;

import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;

public class MultiCurrencyInternalCalcs implements IStrategy {
   
   final Set<Instrument> subscribedInstruments = new HashSet<Instrument>();
   
   @Configurable("Single Instrument to Trade")
   public Instrument tradedInstrument = Instrument.EURUSD;
   
   @Configurable("also Use EUR/GBP")
   public boolean useEURGBP = false;

   @Configurable("also Use EUR/USD")
   public boolean useEURUSD = false;

   @Configurable("also Use AUD/USD")
   public boolean useAUDUSD = false;

   @Configurable("also Use NZD/USD")
   public boolean useNZDUSD = false;
   
   IContext context;

   @Override
   public void onStart(IContext context) throws JFException {
      this.context = context;
      subscribedInstruments.add(tradedInstrument); // always this one
      
      if (useEURGBP) subscribedInstruments.add(Instrument.EURGBP);
      if (useEURUSD) subscribedInstruments.add(Instrument.EURUSD);
      if (useAUDUSD) subscribedInstruments.add(Instrument.AUDUSD);
      if (useNZDUSD) subscribedInstruments.add(Instrument.NZDUSD);
      
      int count = subscribedInstruments.size();
      try {
         context.setSubscribedInstruments(subscribedInstruments, true); // wait
      }
      catch(Exception e) {
         println(e.getMessage());
      }
      println("Subscribed instruments contains "+count+" instruments.");
      for (Instrument inst : subscribedInstruments) {
         println("Instrument: "+inst.toString());
      }
   }
   
   private void println(final String msg) {
      if (context==null) return;
      context.getConsole().getOut().println(msg);
   }
   
   int dataCount = 0;
   // arbitrarily stop this after receiving 1000 ticks..... demo purposes only

   @Override
   public void onTick(Instrument instrument, ITick tick) throws JFException {
      if (!subscribedInstruments.contains(instrument)) return; // ignore
      println("Tick for instrument: "+instrument.toString()+" received.");
      // ok so you'd use maybe a HashMap to keep calculations for each of
      // the instruments which comes through, and whatever your calculations
      // are..... etc.
      // BUT in the Visual Tester, only a single symbol (the one traded)
      // is needed for charting purposes.
      dataCount++;
      if (dataCount>1000) context.stop();
      return;
   }

   @Override
   public void onBar(Instrument instrument, Period period, IBar askBar,
         IBar bidBar) throws JFException {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void onMessage(IMessage message) throws JFException {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void onAccount(IAccount account) throws JFException {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void onStop() throws JFException {
      // TODO Auto-generated method stub
      println("Strategy stopped.");
   }

}



16:43:01 Strategy stopped.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: EUR/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: EUR/GBP received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: EUR/USD received.
16:43:01 Tick for instrument: EUR/GBP received.
16:43:01 Tick for instrument: EUR/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: NZD/USD received.
16:43:01 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: EUR/GBP received.
16:43:00 Tick for instrument: EUR/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: NZD/USD received.
16:43:00 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: EUR/USD received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: EUR/USD received.
16:42:59 Tick for instrument: EUR/USD received.
16:42:59 Tick for instrument: EUR/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: EUR/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: EUR/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: EUR/USD received.
16:42:59 Tick for instrument: EUR/GBP received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: NZD/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:59 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: NZD/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: NZD/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: NZD/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: NZD/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: NZD/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: NZD/USD received.
16:42:58 Tick for instrument: AUD/USD received.
16:42:58 Tick for instrument: EUR/GBP received.
16:42:58 Tick for instrument: NZD/USD received.
16:42:58 Tick for instrument: EUR/USD received.
16:42:57 Tick for instrument: EUR/GBP received.
16:42:57 Tick for instrument: AUD/USD received.
16:42:57 Tick for instrument: NZD/USD received.
16:42:57 Tick for instrument: EUR/USD received.
16:42:57 Tick for instrument: AUD/USD received.
16:42:57 Tick for instrument: EUR/USD received.
16:42:57 Tick for instrument: EUR/USD received.
16:42:57 Tick for instrument: EUR/GBP received.
16:42:57 Tick for instrument: AUD/USD received.
16:42:57 Tick for instrument: EUR/USD received.
16:42:57 Tick for instrument: EUR/GBP received.
16:42:57 Tick for instrument: NZD/USD received.
16:42:57 Tick for instrument: EUR/USD received.
16:42:57 Tick for instrument: NZD/USD received.
16:42:57 Tick for instrument: EUR/USD received.
16:42:57 Tick for instrument: AUD/USD received.
16:42:57 Tick for instrument: NZD/USD received.



 
 Post subject: Re: tester - subscribe instruments from strategy for internal use Post rating: 0   Post Posted: Mon 27 May, 2013, 18:22 

User rating: 1
Joined: Mon 27 Feb, 2012, 22:49
Posts: 119
Location: Czech Republic,
You are right. It's embarrassing. I had blocked subscribing in test mode in my code. Thanks very much. Lifon


 

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