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.

TickCount indicator
 Post subject: TickCount indicator Post rating: 0   New post Posted: Wed 18 Jul, 2012, 18:08 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
Hello,

I want to create an indicator which shows the number of ticks during a period as a histogram.
Can you please provide a solution how I can have access to tick data (means counting ticks) in a time based chart (e.g. 5 minutes) ?

Regards
AbsoluteReturner


 
 Post subject: Re: TickCount indicator Post rating: 0   New post Posted: Thu 19 Jul, 2012, 10:01 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
AbsoluteReturner wrote:
Can you please provide a solution how I can have access to tick data (means counting ticks) in a time based chart (e.g. 5 minutes) ?
  1. Select bars as input (You can use this as example),
  2. take ticks from history, i.e. in onStart fetch IHistory from the context and then in calculate just call ticks over the bar's period.
Let us know if you need any more details on this.


 
 Post subject: Re: TickCount indicator Post rating: 0   New post Posted: Tue 24 Jul, 2012, 11:37 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
I encoded the following TickCount indicator:
package jForex;

import com.dukascopy.api.indicators.*;
import com.dukascopy.api.JFException;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IBar;
import com.dukascopy.api.ITick;
import java.util.List;
import java.awt.Color;

public class TCNT implements IIndicator
{
    private IHistory clsHistory;
    private IndicatorInfo clsIndicatorInfo;
    private IIndicatorContext clsIndicatorContext;
    private InputParameterInfo[] clsInputParameterInfos;
    private OutputParameterInfo[] clsOutputParameterInfos;
    private IBar[][] clsBarInputs = new IBar[1][];
    private double[][] clsdOutputs = new double[1][];
   
    public void onStart(IIndicatorContext context)
    {
        clsIndicatorContext = context;
        clsHistory = context.getHistory();
        clsIndicatorInfo = new IndicatorInfo("TCNT", "Tick Count", "My Indicators", false, false, false, 1, 0, 1);
        clsIndicatorInfo.setRecalculateAll(false);
        clsIndicatorInfo.setRecalculateOnNewCandleOnly(false);
        clsInputParameterInfos = new InputParameterInfo[] { new InputParameterInfo("BarInputs", InputParameterInfo.Type.BAR) };
        clsOutputParameterInfos = new OutputParameterInfo[] { new OutputParameterInfo("TickCount", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.HISTOGRAM) {{ setColor(Color.darkGray); }} };
    }
   
    public IndicatorResult calculate(int iStartIndex, int iEndIndex)
    {
        int iInIndex = 0, iOutIndex = 0, iTickCount = 0, iIndex = 0;
        long lInterval = clsIndicatorContext.getFeedDescriptor().getPeriod().getInterval(),
             lTickTime = 0L, lPeriodStartTime = 0L;
        List<ITick> listTicks;
        ITick[] arrayTicks;
       
        if (iStartIndex > iEndIndex)
            return new IndicatorResult(0, 0);
       
        try
        {
            listTicks = clsHistory.getTicks(clsIndicatorContext.getFeedDescriptor().getInstrument(), ((IBar)clsBarInputs[0][iStartIndex]).getTime(), ((IBar)clsBarInputs[0][iEndIndex]).getTime());
            arrayTicks = (ITick[])listTicks.toArray(new ITick[0]); 
        }
        catch (JFException jFExc)
        {
            clsIndicatorContext.getConsole().getErr().println("getTicks Error: " + jFExc.getMessage());
            listTicks = null;
            arrayTicks = null;
        }
       
        for (iInIndex = iStartIndex, iOutIndex = 0 ; iInIndex <= iEndIndex ; iInIndex++, iOutIndex++)
        {
            iTickCount = -1;
            if (arrayTicks != null)
            {
                iTickCount = 0;
                for (iIndex = 0 ; iIndex < arrayTicks.length ; iIndex++)
                {
                    lTickTime = arrayTicks[iIndex].getTime();
                    lPeriodStartTime = ((IBar)clsBarInputs[0][iInIndex]).getTime();
                    if (lTickTime >= lPeriodStartTime && lTickTime < lPeriodStartTime + lInterval)
                        iTickCount++;
                }
            }
            clsdOutputs[0][iOutIndex] = iTickCount;
        }
       
        return new IndicatorResult(iStartIndex, iOutIndex);
    }
   
    public int getLookback()
    {
        return 0;
    }
   
    public int getLookforward()
    {
        return 0;
    }
   
    public IndicatorInfo getIndicatorInfo()
    {
        return clsIndicatorInfo;
    }
   
    public InputParameterInfo getInputParameterInfo(int iIndex)
    {
        return (iIndex <= clsInputParameterInfos.length ? clsInputParameterInfos[iIndex] : null);
    }
   
    public OptInputParameterInfo getOptInputParameterInfo(int iIndex)
    {
        return null;
    }
   
    public OutputParameterInfo getOutputParameterInfo(int iIndex)
    {
        return (iIndex <= clsOutputParameterInfos.length ? clsOutputParameterInfos[iIndex] : null);
    }
   
    public void setInputParameter(int iIndex, Object oArray)
    {
        clsBarInputs[iIndex] = (IBar[]) oArray;
    }
   
    public void setOptInputParameter(int iIndex, Object oValue)
    {
        return;
    }
   
    public void setOutputParameter(int iIndex, Object oArray)
    {
        clsdOutputs[iIndex] = (double[]) oArray;
    }
}

I can start the indicator, but I have several problems / questions:

I don't get histogram values for periods after I started the indicator.
Should I use readTicks() instead of getTicks(), as you suggested in the following thread: viewtopic.php?f=65&t=47579&p=65212 ?
But this solution is for a strategy, not for an indicator, and I'm not sure if it's recommended to use asynchronous methods inside indicators.

Also I got an ArrayIndexOutOfBoundsException while indicator was running, but until now only once, and I don't know exactly at which code line - I assume between line 55 and 70 in the calculate() method.

After start of indicator, all histogram values are 0 about a half minute. Only after this waiting time plausible values greater then 0 are shown.

In addition, I'm not sure if the ticks inside the list I get from getTicks() are sorted by timestamp or not, therefore I coded a double nested for(....) loop which is not performance optimised.


Regards
AbsoluteReturner


 
 Post subject: Re: TickCount indicator Post rating: 0   New post Posted: Tue 24 Jul, 2012, 13:53 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Try the following:
Attachment:
TicksPerBar.java [5.1 KiB]
Downloaded 372 times
Uncheck the "Recalculate on new bar only" to calculate on every tick.


 
 Post subject: Re: TickCount indicator Post rating: 0   New post Posted: Tue 24 Jul, 2012, 16:02 

User rating: 1
Joined: Tue 12 Jul, 2011, 20:43
Posts: 51
Location: Germany,
This solution works, thanks.

Is it possible to eliminate the optional input parameter "Bar count" (or change it to a maximum bar count) by obtaining the number of periods which are currently visible on chart ?


 
 Post subject: Re: TickCount indicator Post rating: 0   New post Posted: Wed 25 Jul, 2012, 07:46 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
AbsoluteReturner wrote:
Is it possible to eliminate the optional input parameter "Bar count" (or change it to a maximum bar count) by obtaining the number of periods which are currently visible on chart ?
This is possible, but you would need to wait quite a while for all the on-chart values to recalculate. Assume you took off "bar count" parameter:
  1. On first calculate the indicator calculates values for last ~4000 bars (which takes a while given the algorithm).
  2. On every next calculate the indicator calculates the value only for the last bar.
Now assume that you were calculating over visible bar count on chart, assume it's 100. At 1) you actually don't have the bar count available yet (You can obtain it by adding an extra output with setDrawnByIndicator(true) in drawOutput method, see HeikenAshiIndicator for examaple), hence, you still need some constant at the beginning. And at 2) You would calculate over 100 bars instead of just the last bar.

Also readTicks is not an option here, because you would need to store the calculated values in some sort of a barTime->tickCount feed once they finish to calculate, but you could tell the indicator to pick those values only by selecting the racalculateAll option.

To summarize, there is no performant way how an indicator could calculate tick count over big bar count. Strategy, however, could do this.


 

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