Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MySharedMethods: pass Highs/Lows,..., Instrument

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

    #16
    Hello Jesse,
    I have a last unsolved issue on this.
    Applying your code as described I am able to pass a value from my custom BarType to the strategy. The issue I have is that running a backtest, "get;" takes only the value of the first bar and does not load anymore the changes happening on the following bars.
    I did the following:

    - in my custom BarType:

    namespace NinjaTrader.NinjaScript.BarsTypes
    {
    public double MySharedDouble
    { get; set; }
    .....

    protected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) //on every tick
    ​{
    MySharedDouble = CalculateMySD(bars, time);

    - in my Strategy

    ​if (barsType == null)
    {
    return;
    }
    else
    {​
    double MySD = barsType.MySharedDouble;
    Print(Time[0] + " " + MySD );
    }​

    The CalculateMySD(bars, time) method correctly calculates a new value on every incoming tick in the BarType, but this updated value is not passing to the Strategy, that takes and keeps only the value calculated on the first bar. How do I get the public double passing an updated value on each bar?

    Thank you for your support

    Martin


    Last edited by martin70; 05-23-2023, 01:43 AM.

    Comment


      #17
      To better explain the issue above, I attach an example of custom BarType and Strategy.

      Running a backtest with these, on Print Output 1 you get all different values calculated in BarType of the variable to be shared; this is not happening in Print Output 2 where I get only the value passed to the strategy on the first bar and then nothing else.

      Could you please give me a hint about how getting shared the updated values calculated in BarType to the Strategy during backtesting?


      Thank you
      Martin


      Attached Files

      Comment


        #18
        Hello martin70,

        That is due to the cast failing and the barsType variable being null. You would need to use dynamic in this use case. Also keep in mind that your double value will have the same value for every bar, the bars type resets that for each data point you process. The bar series is processed before you apply the strategy so the strategy is only going to see the most recent value. In realtime as new bars are made it would be able to see a different value but would never see a different value in historical.

        if you intended that to be in a series where each bar has a unique value you would have to come up with an indexing system similar to what you see being used with the volumetric bars. Unfortunately that code is not available for review so I wouldn't be able to make a suggestion on how to accomplish that specific type of logic in a bars type.


        Code:
        if (Bars == null)
        return;
        
        dynamic barsType = Bars.BarsType as dynamic;
        if(barsType == null)
        {
            Print("barsType null");
        }
        
        Print("Print MySharedValue from Strategy: " + barsType.MySharedDouble + " " + Time[0]);

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        368 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        571 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X