Dukascopy
 
 
Wiki JStore Search Login

DDSJFX-439 Set default data filter
 Post subject: Filter for getBar method Post rating: 0   New post Posted: Thu 21 Apr, 2011, 14:52 

User rating: 1
Joined: Sun 05 Dec, 2010, 08:44
Posts: 21
By default, the Dukascopy Historical Tester includes all bars during times that the market is closed, including Saturday bars.
To exclude these bars, we must use the Filter "WEEKENDS" in indicator method calls or getBars method calls:
double[]    rsi(Instrument instrument, Period period, OfferSide side, IIndicators.AppliedPrice appliedPrice, int timePeriod, [b]Filter filter[/b], int numberOfCandlesBefore, long time, int numberOfCandlesAfter) 

java.util.List<IBar>    getBars(Instrument instrument, Period period, OfferSide side, [b]Filter filter[/b], int numberOfCandlesBefore, long time, int numberOfCandlesAfter)


However there is only one getBar method and it does not have a filter parameter:
 IBar    getBar(Instrument instrument, Period period, OfferSide side, int shift) 


So if I wanted to get the hourly bar that was 50 hourly bars ago, if I used getBar then weekend bars would be counted. There is no way to get a single historical bar using getBar and not count weekend bars because there is no getBar method that takes a filter parameter.

I could instead use getBars, however then all 50 bars would be loaded into the List of bars, which is very inefficient if all I need was a single bar. The higher the number of bars ago, the more inefficient it would be.

Please see my code below that tests getBar and getBars with and without a filter.
package test;

import com.dukascopy.api.*;
import java.util.*;

public class FilterTest implements IStrategy {
    private static final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE");
   private IEngine engine;
   private IConsole console;
   private IHistory history;
   private IContext context;
   private IIndicators indicators;
   //private IUserInterface userInterface;
   
   long lastTickTime = 0;

   //============================================================================================================================================================
    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();

      sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        console.getOut().print("Process started.\n");
    }

   
    //============================================================================================================================================================
   public void onAccount(IAccount account) throws JFException {
   
   }


    //============================================================================================================================================================
   public void onMessage(IMessage message) throws JFException {
   }


   //============================================================================================================================================================
   public void onStop() throws JFException {
        console.getOut().print("Process stopped.\n");
    }

   
    //============================================================================================================================================================
   public void onTick(Instrument instrument, ITick tick) throws JFException {
      lastTickTime = tick.getTime();
   }
   

   //============================================================================================================================================================
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { //called on bar completion and before first onTick call
       if (period == Period.ONE_HOUR) {
            Calendar calendar1 = new GregorianCalendar();
            calendar1.setTimeZone(TimeZone.getTimeZone("UTC"));
            calendar1.setTimeInMillis(bidBar.getTime());
            if (calendar1.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {//bar open time = Monday 00:00:00
             
                console.getOut().println("Using history.getBar:");
               IBar oldBar = history.getBar(instrument, period, OfferSide.BID, 50);
               IBar newBar = history.getBar(instrument, period, OfferSide.BID, 0);

               console.getOut().println("bidBar.getTime() = " + sdf.format(new Date(bidBar.getTime())));
               console.getOut().println("newBar.getTime() = " + sdf.format(new Date(newBar.getTime())));
               console.getOut().println("oldBar.getTime() = " + sdf.format(new Date(oldBar.getTime())));
              
               long currentBarTime = bidBar.getTime();
                int numberOfCandlesBefore = 50;
                int numberOfCandlesAfter = 0;
                //long time = bidBar.getTime(); //gets bar open time of just-completed old bar of onBar time frame
                
                console.getOut().println("Using history.getBars:");
                Filter filter1 = Filter.NO_FILTER;
                List<IBar> historicalBars = history.getBars(instrument, period, OfferSide.BID, filter1, numberOfCandlesBefore, currentBarTime, numberOfCandlesAfter);
               console.getOut().println("filter = NO_FILTER, old bar time = " + sdf.format(new Date(historicalBars.get(0).getTime())));

                filter1 = Filter.WEEKENDS;
                historicalBars = history.getBars(instrument, period, OfferSide.BID, filter1, numberOfCandlesBefore, currentBarTime, numberOfCandlesAfter);
               console.getOut().println("filter = WEEKENDS, old bar time = " + sdf.format(new Date(historicalBars.get(0).getTime())));
            }
       }
    }
}


Here is the output:
Using history.getBar:

bidBar.getTime() = 2011-04-04 00:00:00 Mon

newBar.getTime() = 2011-04-04 01:00:00 Mon

oldBar.getTime() = 2011-04-01 23:00:00 Fri

Using history.getBars:

filter = NO_FILTER, old bar time = 2011-04-01 23:00:00 Fri

filter = WEEKENDS, old bar time = 2011-03-30 23:00:00 Wed


You can see that using getBar is the same as using getBars without a filter, but what I really want is the last result that does not count the weekend bars.

I request that you create a new getBar method that accepts a filter parameter for the case that a single historical bar n bars ago (without counting weekend bars) is needed.


 
 Post subject: DDSJFX-439 Set default data filter Post rating: 0   New post Posted: Fri 22 Apr, 2011, 02:55 

User rating: 1
Joined: Sun 05 Dec, 2010, 08:44
Posts: 21
The JForex platform includes Saturday bars by default when executing an indicator method or getBar or getBars method that does not take a filter parameter, for example:
double    rsi(Instrument instrument, Period period, OfferSide side, IIndicators.AppliedPrice appliedPrice, int timePeriod, int shift)
           
double[]    rsi(Instrument instrument, Period period, OfferSide side, IIndicators.AppliedPrice appliedPrice, int timePeriod, long from, long to)

IBar    getBar(Instrument instrument, Period period, OfferSide side, int shift)

java.util.List<IBar>    getBars(Instrument instrument, Period period, OfferSide side, long from, long to)


I propose a function, maybe called "setDefaultFilter", that will allow the user to set a filter (e.g. WEEKENDS) and still be able to call the above kind of methods that don't take a filter parameter.

This may be an easier alternative to adding a filter parameter to such methods as above.


 
 Post subject: Re: Filter for getBar method Post rating: 0   New post Posted: Fri 22 Apr, 2011, 02:57 

User rating: 1
Joined: Sun 05 Dec, 2010, 08:44
Posts: 21
An easier alternative may be to allow the user to set a default filter: Set default data filter


 
 Post subject: Re: DDSJFX-439 Set default data filter Post rating: 0   New post Posted: Thu 20 Oct, 2011, 16:55 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Implemented in API build 2.6.47


 

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