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.

Multiple time frames in one indicator?
 Post subject: Multiple time frames in one indicator? Post rating: 0   New post Posted: Wed 15 Jun, 2011, 00:58 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
Is it possible to use, for example, hourly data and minute data as inputs to the same indicator?


 
 Post subject: Re: Multiple time frames in one indicator? Post rating: 0   New post Posted: Wed 15 Jun, 2011, 08:01 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Yes, for instance:
        inputParameterInfos = new InputParameterInfo[] {
              new InputParameterInfo("minuteBars", InputParameterInfo.Type.BAR){{
                 setOfferSide(OfferSide.BID);
                 setInstrument(Instrument.EURUSD);
                 setPeriod(Period.ONE_MIN);
              }},
              new InputParameterInfo("hourBars", InputParameterInfo.Type.BAR){{
                 setOfferSide(OfferSide.BID);
                 setInstrument(Instrument.EURUSD);
                 setPeriod(Period.ONE_HOUR);
              }}};


 
 Post subject: Re: Multiple time frames in one indicator? Post rating: 0   New post Posted: Fri 17 Jun, 2011, 18:43 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
Thanks, that is helpful, but I have one more question.

For my indicator, I want to be able to use the 1 minute chart as the feed for the main input array but I also want to use other IBar arrays of up to 1 hour for possibly as many as 50 trading days or so. The problem is that the time (as returned by getTime) of the first bar of the hour array would be well before the time of the earliest bar of the 1 minute chart. I tried a simple code to show the hour bar close contemperaneously with the minute data. This code compiles but returns a NullPointerException (line 43) at runtime. Any ideas?

package jforex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IBar;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;

public class Test implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private IBar[][] inputs = new IBar[2][];
    private int timePeriod = 2;
    //timePeriod is bars per day * number of days or 288(for 5 min bars) * 10
    private double[][] outputs = new double[1][];
   
    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("XXXXX", "XXXXX", "My indicators",
              false, false, false, 1, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {   
            new InputParameterInfo("Price", InputParameterInfo.Type.BAR),
            new InputParameterInfo("Hour", InputParameterInfo.Type.BAR){{
                setOfferSide(OfferSide.BID);
                setInstrument(Instrument.EURUSD);
                setPeriod(Period.ONE_HOUR);
            }}};
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(2, 2, 100000, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int i, j;
        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
            int timeIndex = getTimeIndex((inputs[0][i].getTime()), inputs[1]);
            outputs[0][j] = inputs[1][timeIndex].getClose();
            }
        return new IndicatorResult(startIndex, j);
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }

    public void setInputParameter(int index, Object array) {
        inputs[index] = (IBar[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }

    private int getTimeIndex(long time, IBar[] target) {
        if (target == null) {
            return -1;
        }

        int first = 0;
        int upto = target.length;
       
        while (first < upto) {
            int mid = (first + upto) / 2;
           
            IBar data = target[mid];
           
            if (data.getTime() == time) {
                return mid;
            }
            else if (time < data.getTime()) {
                upto = mid;
            }
            else if (time > data.getTime()) {
                first = mid + 1;
            }
        }                               
        return -1;
    }
}


 
 Post subject: Re: Multiple time frames in one indicator? Post rating: 0   New post Posted: Tue 21 Jun, 2011, 08:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You have to set the right number of inputs in IndicatorInfo:
indicatorInfo = new IndicatorInfo("XXXXX", "XXXXX", "My indicators", false, false, false, 2, 1, 1);


 
 Post subject: Re: Multiple time frames in one indicator? Post rating: 0   New post Posted: Wed 22 Jun, 2011, 15:24 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
I have two more questions. I'll ask them in two posts.

I was able to get the secondary IBar arrays (arrays other than the "Main" input) to work but when I tried to use a secondary array with a period that was less than the main period, nothing appeared in the indicator window. There were no errors in compilation and no exceptions at runtime but nothing appeared. It didn't matter if I referenced the array or not. Simply referencing any other IBar array other than the Main one caused the exception. Is it possible to use, for example, an inputParameterInfo object of period "ONE_MIN" on a five minute chart?

This is the faulty code.

package jforex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IBar;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;

public class Test implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private IBar[][] inputs = new IBar[3][];
    private int timePeriod = 2;
    //timePeriod is bars per day * number of days or 288(for 5 min bars) * 10
    private double[][] outputs = new double[1][];
   
    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("XXXXX)", "XXXX", "My indicators",
                false, false, false, 3, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {   
            new InputParameterInfo("Main", InputParameterInfo.Type.BAR),
            new InputParameterInfo("One Second", InputParameterInfo.Type.BAR){{
                setOfferSide(OfferSide.BID);
                setInstrument(Instrument.EURUSD);
                setPeriod(Period.ONE_SEC);
            }},
            new InputParameterInfo("One Minute", InputParameterInfo.Type.BAR){{
                setOfferSide(OfferSide.BID);
                setInstrument(Instrument.EURUSD);
                setPeriod(Period.ONE_MIN);
            }}};
           
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(2, 2, 100000, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int i, j;

        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
             double currentChange;
             int timeIndex = getTimeIndex(inputs[0][i].getTime(), inputs[2]);
             currentChange = (timeIndex == -1)?
                Double.NaN : ((IBar)inputs[2][timeIndex]).getClose();
             outputs[0][j] = currentChange;
             }
        return new IndicatorResult(startIndex, j);
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }

    public void setInputParameter(int index, Object array) {
        inputs[index] = (IBar[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
   
    private int getTimeIndex(long time, IBar[] target) {
        if (target == null) {
            return -1;
        }

        int first = 0;
        int upto = target.length;
       
        while (first < upto) {
            int mid = (first + upto) / 2;
           
            IBar data = target[mid];
           
            if (data.getTime() == time) {
                return mid;
            }
            else if (time < data.getTime()) {
                upto = mid;
            }
            else if (time > data.getTime()) {
                first = mid + 1;
            }
        }                               
        return -1;
    }
}



This code compiles and runs as expected. Notice that I only changed the number of inputs (lines 14, 21), the input array references (lines 50, 52) and I omitted the code in lines 24-28.

package jforex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.IBar;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;

public class Test implements IIndicator {
    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private IBar[][] inputs = new IBar[2][];
    private int timePeriod = 2;
    //timePeriod is bars per day * number of days or 288(for 5 min bars) * 10
    private double[][] outputs = new double[1][];
   
    public void onStart(IIndicatorContext context) {
        indicatorInfo = new IndicatorInfo("XXXXX)", "XXXX", "My indicators",
                false, false, false, 2, 1, 1);
        inputParameterInfos = new InputParameterInfo[] {   
            new InputParameterInfo("Main", InputParameterInfo.Type.BAR),
            /*new InputParameterInfo("One Second", InputParameterInfo.Type.BAR){{
                setOfferSide(OfferSide.BID);
                setInstrument(Instrument.EURUSD);
                setPeriod(Period.ONE_SEC);
            }},*/
            new InputParameterInfo("One Minute", InputParameterInfo.Type.BAR){{
                setOfferSide(OfferSide.BID);
                setInstrument(Instrument.EURUSD);
                setPeriod(Period.ONE_MIN);
            }}};
           
        optInputParameterInfos = new OptInputParameterInfo[] {new OptInputParameterInfo("Time period", OptInputParameterInfo.Type.OTHER,
                new IntegerRangeDescription(2, 2, 100000, 1))};
        outputParameterInfos = new OutputParameterInfo[] {new OutputParameterInfo("out", OutputParameterInfo.Type.DOUBLE,
                OutputParameterInfo.DrawingStyle.LINE)};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
        //calculating startIndex taking into account lookback value
        if (startIndex - getLookback() < 0) {
            startIndex -= startIndex - getLookback();
        }
        int i, j;

        for (i = startIndex, j = 0; i <= endIndex; i++, j++) {
             double currentChange;
             int timeIndex = getTimeIndex(inputs[0][i].getTime(), inputs[1]);
             currentChange = (timeIndex == -1)?
                Double.NaN : ((IBar)inputs[1][timeIndex]).getClose();
             outputs[0][j] = currentChange;
             }
        return new IndicatorResult(startIndex, j);
    }

    public IndicatorInfo getIndicatorInfo() {
        return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
        if (index <= inputParameterInfos.length) {
            return inputParameterInfos[index];
        }
        return null;
    }

    public int getLookback() {
        return timePeriod;
    }

    public int getLookforward() {
        return 0;
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
        if (index <= optInputParameterInfos.length) {
            return optInputParameterInfos[index];
        }
        return null;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
        if (index <= outputParameterInfos.length) {
            return outputParameterInfos[index];
        }
        return null;
    }

    public void setInputParameter(int index, Object array) {
        inputs[index] = (IBar[]) array;
    }

    public void setOptInputParameter(int index, Object value) {
        timePeriod = (Integer) value;
    }

    public void setOutputParameter(int index, Object array) {
        outputs[index] = (double[]) array;
    }
   
    private int getTimeIndex(long time, IBar[] target) {
        if (target == null) {
            return -1;
        }

        int first = 0;
        int upto = target.length;
       
        while (first < upto) {
            int mid = (first + upto) / 2;
           
            IBar data = target[mid];
           
            if (data.getTime() == time) {
                return mid;
            }
            else if (time < data.getTime()) {
                upto = mid;
            }
            else if (time > data.getTime()) {
                first = mid + 1;
            }
        }                               
        return -1;
    }
}


 
 Post subject: Re: Multiple time frames in one indicator? Post rating: 0   New post Posted: Wed 22 Jun, 2011, 20:43 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
Second question: is it possible to loop directly through the secondary array without having to reference the "Main" array? For example, you could find the index of the first value in the secondary array and loop to the end of that array. I've tried several different ways and I've repeatedly gotten out of bounds exceptions.

The problem that I am running into, is that I need multiple days of data, but I also short interval bars, about 10 seconds would work but even less would be optimal. If I use, for example, 10 second bars, I can't access data that is 20 days in the past. If I use hour bars, I can access data that is several weeks old, but I don't have the specificity to access 10 second data.

If either of these two issues can be resolved, I'll have what I need.

Thanks.


 
 Post subject: Re: Multiple time frames in one indicator? Post rating: 0   New post Posted: Sun 26 Jun, 2011, 17:46 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
Just to clarify on the previous post. I only need a couple hours of short interval data, e.g. 10 second data. If I had several weeks of hour data, that would satisfy my requirements on the top end. For intervals in between, I would prefer to have several weeks of data, but I can make do with less.

I just want to know if this is workable on JForex. If anyone can help me, I can share my ideas. Here's a preview of an extremely dumbed-down version of what I'm trying to develop. The buy/sell signals come at the end of the bars that the arrows are above. The indicator at the bottom is based on the closing price for the bar.


Attachments:
jankindicator.JPG [181.7 KiB]
Downloaded 519 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: Multiple time frames in one indicator? Post rating: 0   New post Posted: Tue 28 Jun, 2011, 11:43 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Please have a look at the following: https://www.dukascopy.com/wiki/index.php ... nt_periods


 
 Post subject: Re: Multiple time frames in one indicator? Post rating: 0   New post Posted: Tue 05 Jul, 2011, 15:14 

User rating: 0
Joined: Fri 10 Jun, 2011, 14:54
Posts: 11
Location: United States,
Thanks. Since I'm not great with Java, it's going to take me a while to dig through this and make sure I can make it work for what I'm trying to do, but it looks like it will do the trick.


 

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