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 use multiple instruments and feeds?
 Post subject: How to use multiple instruments and feeds? Post rating: 0   New post Posted: Wed 22 Aug, 2012, 08:37 
User avatar

User rating: 8
Joined: Tue 25 Oct, 2011, 23:02
Posts: 74
Location: Australia, Melbourne
I'd like to create a strategy that applies the same trading rules to multiple instruments. What is the suggested approach for handling multiple instruments with a feed? I have some code that implements the new approach for API 2.7 with a feed but I'm not sure how to go about handling multiple instruments...

for example - within the IStrategy onStart() method
 ....
        //subscribe to instruments
        _instSet = new HashSet<Instrument>();
        _instSet.add(Instrument.GBPUSD);
 //       _instSet.add(Instrument.EURUSD);

        context.setSubscribedInstruments(_instSet);   

      // create feed
        _feed = new FeedDescriptor();
        _feed.setInstrument(Instrument.GBPUSD);  // TODO: How to create multiple instruments for a feed?
        _feed.setDataType(DataType.RENKO);
        _feed.setFilter(Filter.WEEKENDS);
        _feed.setOfferSide(OfferSide.BID);
        _feed.setPriceRange(PriceRange.valueOf(_renkoBrickSize));
       
        debugPrint (INFO, "Feed = " + _feed.toString());

        //subscribing to custom Period
        _context.subscribeToFeed(_feed, new IFeedListener()
        {
            @Override
            public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData)
            {
                Instrument instrument = feedDescriptor.getInstrument();
                IRenkoBar renkoBar = (IRenkoBar) feedData;
                int numberBars = 2;
               
                if (_instSet.contains(instrument))
                {                     
                    try
                    {
                       // update indicator values
                       IRenkoBar lastRenkoBar = _history.getRenkoBar(instrument, OfferSide.BID, PriceRange.valueOf(_renkoBrickSize), 0);
                       debugPrint(INFO, "LastRenkoBar start time: " + sdf.format(lastRenkoBar.getTime()));
                       debugPrint(INFO, "feedData: RenkBar start time: " + sdf.format(renkoBar.getTime()));
                        long currentBarTime = lastRenkoBar.getTime();             
                       
                        // ema indicator
                        Object[] Values = _indicators.calculateIndicator(_feed,
                                          new  OfferSide[] {OfferSide.BID},
                                          "EMA",
                                          new IIndicators.AppliedPrice[] { IIndicators.AppliedPrice.CLOSE },
                                          new Object[] { _emaPeriod },
                                          numberBars,
                                          currentBarTime,
                                          0);

                        double[] emaValues = (double[]) Values[0];
                        ema = emaValues[0];     

                    }
                    catch (JFException e)
                    {
                        console.getOut().println(instrument.toString() + " " + "Exception: " + e.toString());
                        e.printStackTrace();
                    }
                }
               
            }
        });



 
 Post subject: Re: How to use multiple instruments and feeds? Post rating: 1   New post Posted: Thu 23 Aug, 2012, 14:45 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You need to subscribe to each indicator separately using different FeedDescriptor instances.

import com.dukascopy.api.*;
import com.dukascopy.api.feed.FeedDescriptor;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IFeedListener;
import com.dukascopy.api.feed.IRenkoBar;

import java.util.HashSet;

public class StrategyFeeds implements IStrategy, IFeedListener {
    private IEngine engine;
    private IConsole console;
    private IHistory _history;
    private IContext context;
    private IIndicators _indicators;
    private IUserInterface userInterface;
   private IContext _context;

   int _renkoBrickSize = 0;
   int _emaPeriod = 5;
   private HashSet<Instrument> _instSet;
   
    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();
      this._context = context;

      //subscribe to instruments
      _instSet = new HashSet<Instrument>();
      _instSet.add(Instrument.GBPUSD);
      _instSet.add(Instrument.EURUSD);

      context.setSubscribedInstruments(_instSet);

      // create feed


      for(Instrument instr : _instSet) {

         FeedDescriptor _feed = new FeedDescriptor();
         _feed.setInstrument(instr);  // TODO: How to create multiple instruments for a feed?
         _feed.setDataType(DataType.RENKO);
         _feed.setFilter(Filter.WEEKENDS);
         _feed.setOfferSide(OfferSide.BID);
         _feed.setPriceRange(PriceRange.valueOf(_renkoBrickSize));

         //debugPrint (INFO, "Feed = " + _feed.toString());

         //subscribing to custom Period


         _context.subscribeToFeed(_feed, this);

      }
    }


   @Override
   public void onFeedData(IFeedDescriptor feedDescriptor, ITimedData feedData)
   {
      Instrument instrument = feedDescriptor.getInstrument();
      IRenkoBar renkoBar = (IRenkoBar) feedData;
      int numberBars = 2;

      if (_instSet.contains(instrument))
      {
         try
         {
            // update indicator values
            IRenkoBar lastRenkoBar = _history.getRenkoBar(instrument, OfferSide.BID, PriceRange.valueOf(_renkoBrickSize), 0);
            //debugPrint(INFO, "LastRenkoBar start time: " + sdf.format(lastRenkoBar.getTime()));
            //debugPrint(INFO, "feedData: RenkBar start time: " + sdf.format(renkoBar.getTime()));
            long currentBarTime = lastRenkoBar.getTime();

            // ema indicator
            Object[] Values = _indicators.calculateIndicator(feedDescriptor,
                  new  OfferSide[] {OfferSide.BID},
                  "EMA",
                  new IIndicators.AppliedPrice[] { IIndicators.AppliedPrice.CLOSE },
                  new Object[] { _emaPeriod },
                  numberBars,
                  currentBarTime,
                  0);

            double[] emaValues = (double[]) Values[0];
            //ema = emaValues[0];

         }
         catch (JFException e)
         {
            console.getOut().println(instrument.toString() + " " + "Exception: " + e.toString());
            e.printStackTrace();
         }
      }

   }


    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 {
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
}


Attachments:
StrategyFeeds.java [3.23 KiB]
Downloaded 300 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.
 

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