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.

indicators.calculateIndicator() sometimes returns empty array
 Post subject: indicators.calculateIndicator() sometimes returns empty array Post rating: 0   New post Posted: Mon 01 Dec, 2014, 14:12 

User rating: 0
Joined: Fri 28 Nov, 2014, 10:37
Posts: 3
Location: Alkmaar
Hi,

I’m working with rangebars in combination with EMA and MACD indicators. Below is a snippet of my code:

IFeedDescriptor feedDescriptor = new RangeBarFeedDescriptor(instrument, getPriceRange(), OfferSide.BID);

Object[] minMaxFeed = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "MinMax”, new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { getMinMax() }, 20, selectedRangeBar.getTime(), 0);


This code is run when a IRangeBarFeedListener receives data (from 10 pip bars), and most of the time this code will return an array with data, but sometimes the array is empty. I’ve seen occasions where this occurs on multiple consecutive rangebars, causing my strategy to miss trade entry points.

How can I gracefully handle this situation without missing entry points?


 
 Post subject: Re: indicators.calculateIndicator() sometimes returns empty array Post rating: 0   New post Posted: Mon 01 Dec, 2014, 17:11 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please provide the strategy that replicates the case. It will help us a lot.


 
 Post subject: Re: indicators.calculateIndicator() sometimes returns empty array Post rating: 0   New post Posted: Fri 05 Dec, 2014, 21:34 

User rating: 0
Joined: Fri 28 Nov, 2014, 10:37
Posts: 3
Location: Alkmaar
Please see code below (forum gave me error regarding attachment quota). All logic related to positions has been cleared, but if I run this strategy on my LIVE account, I frequently get "Incomplete data" in the log files.



package forex.strategies;

import java.util.Date;

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.IHistory;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
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.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.PriceRange;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.feed.IRangeBar;
import com.dukascopy.api.feed.IRangeBarFeedListener;
import com.dukascopy.api.feed.util.RangeBarFeedDescriptor;


public class DukascopySampleStrategy implements IStrategy {
      
   protected IEngine engine;
   protected IIndicators indicators;
   protected IHistory history;
   protected IConsole console;
   protected IContext context;

   private Instrument instrument = Instrument.EURCAD;

   private int minmax = 30;
   private int ema = 100;

      
   private IRangeBarFeedListener listener = null;
   private PriceRange priceRange = PriceRange.valueOf(10);
   
   
   public String getDescription() {
      return "";
   }
   
   protected Instrument getInstrument() {
      return instrument;
   }
   
   protected long getLogTime() {
      try {
         return history.getLastTick(getInstrument()).getTime();
      } catch (Exception e) {
         return new Date().getTime();
      }
   }

   // ** onBar used for entries **
   public void onBar(Instrument instrument, Period period, IBar askBar,
         IBar bidBar) {

   }
   
   public void onStart(IContext context) throws JFException {
      this.engine = context.getEngine();
      this.indicators = context.getIndicators();
      this.history = context.getHistory();
      this.console = context.getConsole(); // allows printing to console
      this.context = context;
      
      subscribe();
   }
   
   private void subscribe() {
      
      if(listener != null) {
         context.unsubscribeFromRangeBarFeed(listener);
      }
            
      listener = new IRangeBarFeedListener() {
         
         @Override
         public void onBar(Instrument arg0, OfferSide arg1, PriceRange arg2,
               IRangeBar arg3) {
            // TODO Auto-generated method stub
            
            try {
               DukascopySampleStrategy.this.setSentiment(arg0, arg3);
            } catch (Exception e) {
               log("Got exception in setSentiment(): " + e.getMessage());
               e.printStackTrace();
            }
         }
      };

      
      context.subscribeToRangeBarFeed(getInstrument(), OfferSide.BID, priceRange, listener);
      
      log("Subscribed to " + priceRange + " feed");
   }
   

   // ** onTICK: for open position management **
   public void onTick(Instrument instrument, ITick tick) {

      
   }


   private void setSentiment(Instrument instrument, IRangeBar selectedRangeBar)
         throws JFException {
      
     
      IFeedDescriptor feedDescriptor = new RangeBarFeedDescriptor(instrument, priceRange, OfferSide.BID);   

      Object[] minMaxFeed = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "MinMax",
             new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { minmax }, 20, selectedRangeBar.getTime(), 0);
      
        double[] minFeed = (double[]) minMaxFeed[0];
        double[] maxFeed = (double[]) minMaxFeed[1];
       
        Object[] macdFeedObj = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "MACD",
             new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { 12, 26, 9 }, 3, selectedRangeBar.getTime(), 0);
   
      double[] macdFeed = (double[]) macdFeedObj[0];
      double[] macdHistFeed = (double[]) macdFeedObj[2];

      Object[] maLngFeed = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { OfferSide.BID }, "EMA",
             new AppliedPrice[] { AppliedPrice.CLOSE }, new Object[] { ema }, 2, selectedRangeBar.getTime(), 0);
      
       
        double[] emaLng = (double[]) maLngFeed[0];
       
       
        if(minFeed.length < 1 || maxFeed.length < 1 || emaLng.length < 2 || macdFeed.length < 2) {
            ///// <---- THIS HAPPENS A LOT ----->
           log("Incomplete data!");
           return;
        }

   }
   

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

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

   @Override
   public void onStop() throws JFException {
      // TODO Auto-generated method stub
      
   }
   
   protected void log(String string) {

      this.console.getOut().println(string);
   }


}


 
 Post subject: Re: indicators.calculateIndicator() sometimes returns empty array Post rating: 0   New post Posted: Mon 08 Dec, 2014, 08:33 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We will investigate.


 
 Post subject: Re: indicators.calculateIndicator() sometimes returns empty array Post rating: 0   New post Posted: Fri 19 Dec, 2014, 14:33 

User rating: 0
Joined: Fri 28 Nov, 2014, 10:37
Posts: 3
Location: Alkmaar
Any news on this? A workaround would be useful.


 

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