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.

timing of tick, bar, indicator
 Post subject: timing of tick, bar, indicator Post rating: 0   Post Posted: Tue 20 Apr, 2010, 03:23 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
I wrote a simple moving average cross-over strategy and backtested it. But the entries/exits don't correspond with an overlay of the same moving averages on the backtest chart. So I am suspecting that I have some confusion with the timing of the methods. In particular, here are a few questions.

1) When is onBar( ) called? Is it called when the bar is completed or started?
2) When is onTick( ) called? Same as above.
3) If I call indicators.sma(inst, period, bid, ap, 20, 0) does it calculate the sma of the last completed period bar? or the current incomplete bar?

For example, if I call indicators.sma(Period.FIVE_MINS) within onTick( ), does it use the last completed FIVE_MINS bar or does it use the current, but probably incomplete, FIVE_MINS bar?


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Wed 21 Apr, 2010, 08:43 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
Quote:
When is onBar( ) called? Is it called when the bar is completed or started?
Function onBar() is called when a first tick of the next candle has arrived. If there isn't new ticks, then the onBar() is called after 1 second when the last tick came for the last candle.
Quote:
When is onTick( ) called? Same as above.
The onTick() is called when a tick has arrived.
Quote:
If I call indicators.sma(inst, period, bid, ap, 20, 0) does it calculate the sma of the last completed period bar? or the current incomplete bar?
It will calculate current incomplete bar. In this case, the last tick data will be used for getting a close price.
Quote:
...does it use the current, but probably incomplete, FIVE_MINS bar?
Yes


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Wed 21 Apr, 2010, 13:54 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
What if I call indicator.sma(inst, FIVE_MINS, bid, ap, 20, 0) inside onBar( period = FIVE_MINS )?

Does the indicator use that tick in the next, new and incomplete FIVE_MINS bar or the last tick from the just completed FIVE_MINS bar?

Thank you for your help


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Thu 22 Apr, 2010, 08:30 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could you please clarify your question?
If you call your indicator(whit shift parameter 0) in the onBar function, then the indicator to calculate a value, will use candles starting from current (just started) candle.


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Thu 22 Apr, 2010, 16:52 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
so if I only want to use complete candle data in the indicator, I would use indicator(shift=1) in either onBar( ) or onTick( )?

Also, onBar( ) works the same way right? The current bar in onBar( ) is incomplete because it is called at the start of the bar? I would need to use the previous bar if I only want to use a completed bar?


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Fri 23 Apr, 2010, 08:41 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Quote:
so if I only want to use complete candle data in the indicator, I would use indicator(shift=1) in either onBar( ) or onTick( )?
Yes
Quote:
The current bar in onBar( ) is incomplete because it is called at the start of the bar?
Yes
Quote:
I would need to use the previous bar if I only want to use a completed bar?
Yes


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Mon 26 Apr, 2010, 00:38 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
I just ran a live test and my findings seem to be contrary to what you're saying...

bidBar and askBar in onBar() is returning the last completed bar. And indicators(shift=0) is returning the output up to the last completed bar too.

So that means I don't need to use IHistory to call the latest completed bar. And that indicators(shift = 0) is sufficient for calculating complete bars.

What could be causing the difference between our findings?


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Tue 27 Apr, 2010, 14:10 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Quote:
bidBar and askBar in onBar() is returning the last completed bar.
This statement is true.
Quote:
And indicators(shift=0) is returning the output up to the last completed bar too.
This statement is false. Consider this code
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
      if (Instrument.EURUSD == instrument && Period.ONE_MIN == period){
         
         double sma = context.getIndicators().sma(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 20, 0);
         double sma2 = context.getIndicators().sma(instrument, period, OfferSide.BID, AppliedPrice.CLOSE, 20, 1);
         
         context.getConsole().getOut().println( " === sma shift 0 === "+sma+" === sma shift 1 === "+sma2);
}

As you see from output, an indicator values have slight difference. It mean that, for a sma indicator, shift=1 and shift=0 isn't the same.
What do you mean with this question
Quote:
What could be causing the difference between our findings?


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Wed 28 Apr, 2010, 14:44 

User rating: 1
Joined: Fri 26 Mar, 2010, 19:19
Posts: 116
Location: Canada
Comparing between shift=1 and shift=0 is exactly what I did. But I find that the [previous bar's indicator(shift=0)] = [current bar's indicator(shift=1)]. Doesn't that mean indicator(shift=0) is a complete bar because it is the same as shift=1 for the next bar?

e.g.

IHistory.getBar(shift = 1) {indicator.sma(shift=0)} == onBar() {indicator.sma(shift=1)}


 
 Post subject: Re: timing of tick, bar, indicator Post rating: 0   Post Posted: Fri 30 Apr, 2010, 08:08 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could please clarify, from our example on your PC, does a sma value exactly match the sma2 value? There should be small difference.


 

Jump to:  

  © 1998-2026 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