Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Synchronization problem in multi-time strats OR multithreading affecting Print()?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Synchronization problem in multi-time strats OR multithreading affecting Print()?

    Hi, I've been trying to do some multi-time frame analysis and have been seeing some strange things.

    I have set up a strategy with a primary time frame of 60 seconds, and I add a second time frame of 120 seconds in Strategy.Initialize(). Also in Strategy.Initialize() I add two instances of my custom indicator (I use a different parameter value to get two instances; the parameter value also serves is an instance id: i.e. 0 for the primary time frame, and 1 for the secondary). Then in Strategy.OnBarUpdate(), I Print() the bar number as well as the instance id. Intuitively, for every 2 prints from time frame 0, there should be 1 print from time frame 1. But after a while it seems as though the two time frames get out of sync as the following clip from the Output window seems to demonstrate:

    Code:
    Bar: 375. Computing AdamaDiff for DataSeries: 1   <-- Shouldn't this bar
    Bar: 727. Computing AdamaDiff for DataSeries: 0
    Bar: 376. Computing AdamaDiff for DataSeries: 1
    Bar: 728. Computing AdamaDiff for DataSeries: 0
    Bar: 377. Computing AdamaDiff for DataSeries: 1
    Bar: 729. Computing AdamaDiff for DataSeries: 0
    Bar: 378. Computing AdamaDiff for DataSeries: 1
    Bar: 730. Computing AdamaDiff for DataSeries: 0
    Bar: 379. Computing AdamaDiff for DataSeries: 1
    Bar: 731. Computing AdamaDiff for DataSeries: 0
    Bar: 380. Computing AdamaDiff for DataSeries: 1
    Bar: 732. Computing AdamaDiff for DataSeries: 0
    Bar: 733. Computing AdamaDiff for DataSeries: 0
    Bar: 381. Computing AdamaDiff for DataSeries: 1
    Bar: 734. Computing AdamaDiff for DataSeries: 0
    Bar: 735. Computing AdamaDiff for DataSeries: 0
    Bar: 382. Computing AdamaDiff for DataSeries: 1
    Bar: 736. Computing AdamaDiff for DataSeries: 0
    Bar: 383. Computing AdamaDiff for DataSeries: 1
    Bar: 737. Computing AdamaDiff for DataSeries: 0
    Bar: 738. Computing AdamaDiff for DataSeries: 0
    Bar: 384. Computing AdamaDiff for DataSeries: 1
    Bar: 739. Computing AdamaDiff for DataSeries: 0
    Bar: 740. Computing AdamaDiff for DataSeries: 0
    Bar: 385. Computing AdamaDiff for DataSeries: 1
    Bar: 741. Computing AdamaDiff for DataSeries: 0
    Bar: 386. Computing AdamaDiff for DataSeries: 1
    Bar: 742. Computing AdamaDiff for DataSeries: 0
    Bar: 387. Computing AdamaDiff for DataSeries: 1
    Bar: 743. Computing AdamaDiff for DataSeries: 0
    Bar: 744. Computing AdamaDiff for DataSeries: 0
    Bar: 388. Computing AdamaDiff for DataSeries: 1
    Bar: 745. Computing AdamaDiff for DataSeries: 0
    Bar: 746. Computing AdamaDiff for DataSeries: 0
    Bar: 389. Computing AdamaDiff for DataSeries: 1
    Bar: 747. Computing AdamaDiff for DataSeries: 0
    Bar: 748. Computing AdamaDiff for DataSeries: 0
    Bar: 390. Computing AdamaDiff for DataSeries: 1
    Bar: 749. Computing AdamaDiff for DataSeries: 0
    Bar: 750. Computing AdamaDiff for DataSeries: 0 <-- be in this vicinity?
    Is this because NT's UI operates in a separate thread such that outputs from Print() are somehow being blocked so it only LOOKS like there is a synchronization problem? Or could there ACTUALLY be a synchronization problem?

    #2
    That's just how NT builds the 60 and 120 bars.

    Say there are these ticks:
    02:10:30
    02:11:30
    02:12:30
    02:14:30 <- "missing" tick at 02:13:30
    02:15:30

    Bars build on 60 secs series (A):
    02:11:00
    02:12:00
    02:13:00
    02:15:00
    02:16:00

    Bars build on 120 secs series (B):
    02:12:00
    02:14:00
    02:16:00

    This will yield:
    A 02:11:00
    A 02:12:00
    B 02:12:00
    A 02:13:00
    B 02:14:00 <- problem
    A 02:15:00
    A 02:16:00
    B 02:16:00

    Comment


      #3
      So are you saying that if there isn't a tick during a bar, NT just skips the bar all together? So say I wanted to access a bar that was 5 seconds ago on a 1 second chart, but because no tick came in for a bar 3 seconds ago, if I access High[5], am I actually accessing the high value of 6 seconds ago?

      I'm inferring this because it seems you're suggesting that the bar index numbers actually get out of sync for different time frames.

      Code:
       missing
         tick
            |
            v
      1 2 3   4 5 6 7 8
      | | |   | | | | |
        1   2   3   4   5
        |   |   |   |   |
      Shouldn't NT just store previous or NULL values for bars where no ticks came in so that the data series for different time frames stay in sync. I mean I would expect that if I had a 60 second time frame and a 120 second time frame, accessing 10 bars back on the 60 second time frame and 5 bars back on the 120 second time frame would give me data from the same time stamp (give or take 60 seconds).

      Comment


        #4
        The DataSeries class currently is bar driven vs time driven. We will introduce a time driven DataSeries class for NT7 due out later in 2008. If you are dealing with 1 and 5 second bars, the limitation will certainly be magnified.
        RayNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Ray View Post
          The DataSeries class currently is bar driven vs time driven. We will introduce a time driven DataSeries class for NT7 due out later in 2008. If you are dealing with 1 and 5 second bars, the limitation will certainly be magnified.
          Icic. Does the Plot class behave differently? If instead of creating custom DataSeries objects I add a Plot that I use to store values, are they bar driven or time driven? So say I have a 60 second time frame and a 120 second time frame. If I write an indicator, MyIndicator, like this:

          Code:
          protected override void Initialize()
          {
            Add(Plot(Color.Transparent, PlotStyle.Line, "Am_I_Time_Driven"));
            PriceTypeSupported = true;
          }
          
          protected override void OnBarUpdate()
          {
            double tSomeValue = DoSomeCalculations();  // Do some calculations
            Am_I_Time_Driven.Value.Set(tSomeValue);  // Store for later use
          }
          
          #region Properties
          [Browsable(false)]
          [XmlIgnore()]
          public DataSeries Am_I_Time_Driven
          {
            get { return Values[0]; }
          }
          #endregion
          And then in MyStrategy:

          Code:
          #region Variables
          private MyIndicator ind0;
          private bool ind0Valid = false;
          private MyIndicator ind1;
          private bool ind1Valid = false;
          #endregion
          
          private override void Initialize()
          {
            Add(PeriodType.Second, 120);
          }
          
          private override void OnBarUpdate()
          {
            switch(BarsInProgress) {
              case 0:
                ind0 = MyIndicator(BarsArray[0]);
                if(!ind0Valid) ind0Valid = true;
                break;
              case 1:
                ind1 = MyIndicator(BarsArray[1]);
                if(!ind1Valid) ind1Valid = true;
                break;
            }
            if(ind0Valid && ind1Valid) {
              double tValue0 = ind0.Am_I_Time_Driven[10];
              double tValue1 = ind1.Am_I_Time_Driven[5];
            }
          }
          Can I then be sure that tValue0 and tValue1 are from the same "proximate" time period, i.e. that the two DataSeries are in no danger of "drifting apart" due to missing data points?

          And am I right in assuming that NT will create separate instances of MyIndicator for BarsArray[0] and BarsArray[1]?

          Comment


            #6
            Same behaviour.

            A plot itself is just an object that manages the visuals on the chart, the actual values themselves are stored in DataSeries objects.

            NT does internally create separate instances of indicator objects if you pass in different parameters.
            RayNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cre8able, Today, 03:20 PM
            1 response
            9 views
            0 likes
            Last Post cre8able  
            Started by fiddich, Today, 05:25 PM
            0 responses
            3 views
            0 likes
            Last Post fiddich
            by fiddich
             
            Started by gemify, 11-11-2022, 11:52 AM
            6 responses
            804 views
            2 likes
            Last Post ultls
            by ultls
             
            Started by ScottWalsh, Today, 04:52 PM
            0 responses
            4 views
            0 likes
            Last Post ScottWalsh  
            Started by ScottWalsh, Today, 04:29 PM
            0 responses
            9 views
            0 likes
            Last Post ScottWalsh  
            Working...
            X