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

How to create my own version of IsFirstTickOfBar ?

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

    How to create my own version of IsFirstTickOfBar ?

    I'd like to create my own version of the built-in bool named IsFirstTickOfBar, but I would name it "IsFirstTickOfBarMine" etc., so I can call it from Indicators, Strategies, AddOns, etc.

    the code would have a private int

    a GET method inside the class would return a bool


    I found this forum post, https://ninjatrader.com/support/foru...ing#post770073 but I'm not sure if it is what I'm needing.


    I think I need to place the code file in the Custom/AddOns folder.

    Do I create it as an indicator, so it has access to global vars like CurrentBar ?

    Do I need to use my own namespace?



    or should I be trying to extend the NinjaScriptBase, to add a named bool var to it, and have a GET method which returns the bool, when this var is called from inside an OnMarketData event?




    anyway, i'm pretty lost how to do this.
    any help is appreciated.
    thanks

    #2
    Hello balltrader,

    Thanks for the post.

    From the given details its not really clear what the goal is here for this property. Are you trying to add logic to the existing property or do something with its value?

    A strategy or indicator can already get the value of IsFirstTickOfBar so you can just re use that property in your logic. You would not need to create anything else or use an addon.

    You mentioned calling the variable from OnMarketData, IsFirstTickOfBar can be called from OnMarketData however it would not be relevant there like it would be from OnBarUpdate.



    You can create your own version of any property by just making another property:

    Code:
    public bool IsFirstTickOfBarMine {get;set;}
    
    protected override void OnBarUpdate ()
    {
        IsFirstTickOfBarMine = IsFirstTickOfBar;
    }

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      thanks Jesse, sorry for my lack of performance in describing what I wanted to do.

      I already have my own indicator, inside that indicator I override the OMData event

      protected override void OnMarketData(MarketDataEventArgs e)

      and in there I call a method in my indicator class, which references a class variable int BarIndex_inprocess, which keeps track of the value of the index of the current bar in process, and when that is different than the built-in variable named CurrentBar, then I recognize that a new bar has started, and my method updates the value of the var BarIndex_inprocess, and returns true. Repeated calls to this method will detect the same value between my variable and the currentBar value, and return false, indicating it is NOT the first tick.
      I needed to do this because I was getting the IsFirstTickOfBar triggered multiple times at the start of a new bar inside the OMData, i.e. OMData had 7 events in the same timestamp, and it kept resetting counters to zero and messing up my logic.


      My thinking was: I don't want to copy that method to all my other indicator script that need this logic, so how could I make my own script, which had it's own getters and setters, then I could access it from anywhere, the same as I would access the built-in IsFirstBar...
      but if I can't "Add" a property to the Ninjascript instance(?)
      I guess I need to use a method?

      and do I create an indicator, with a class and a constructor and a public property of type bool?
      and is it an indicator, or an AddOn ( I mean which folder does the code file belong in?)

      I hope that made more sense.

      Comment


        #4
        Hello balltrader,

        Thank you for the reply.

        What you had seen with IsFirstTickOfBar is a bar would be expected. If you need to use IsFirstTickOfBar for some calculation that logic should be in OnBarUpdate because it relates to the bar events.

        What you described about using the CurrentBar is a normal approach, you can see that in many system indicators like the BuySellPressure.

        You could possibly use an indicator to re plot the value your logic finds and then read that plot value. Using public properties from indicators is very expensive compared to a plot because public properties need the Update() method called.

        I look forward to being of further assistance.



        JesseNinjaTrader Customer Service

        Comment


          #5
          I'm afraid this got off on a tangent about the FirstTickOfBar.

          I'm more interested in how to make a function / method which can be called from any other code (indicaotr / strategy / AddOn)

          this doesn't need a class, just a function, which would be in some global namespace.

          I think this must be an AddOn, but it doesn't need a window.

          Is there a webpage which lists all your sample code? I found it before, but I can't find it now. Maybe it was a topic in this forum where all the sample code is?

          Comment


            #6
            Hello balltrader,

            You can make a public function but you will need to call Update() in that function which is expensive. If at all possible avoid using a function and instead use a plot. Functions or custom public properties would generally be reserved for cases where you are detached from bar processing, for example in the zig zag it loops over the series data when you call one of its methods. Calling a method or public properties from a different script is out of context so if you don't use Update the data returned would not be relevant.

            There is a general sample of exposing data that is not a plot here: https://ninjatrader.com/support/help...alues_that.htm

            You can otherwise see the zig zag indicator and its public methods for more examples.

            Please let me know if I may be of additional assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              ok, thank you for your reply.
              I looked at code for both ZigZag & SampleBoolSeries.

              Now I understand the Update() function / method, (reference: https://ninjatrader.com/support/help...t8/?update.htm) will force an OnBarUpdate event trigger if the bar is not up to date. So that is why it can be expensive.
              and the ZigZag indicator has two public methods: LowBar and HighBar , which coudl be called at any time, so there is no guarantee that the OBU has been run, so you are required to forvce the OBU check using Update()
              thankyou


              back to my original question about creating my own functions / methods, it seems like I need to create an indicator, bcuz they can be called from anywhere, and the caller needs to access my method using the naming convention MyClassName.MyClassMethodName(inputArguments)

              in otherwords, I can't create a method named "BallsMethod()" and have it be a property of the main NinjaScript instance...
              probably bcuz it is protected or final, etc.
              Last edited by balltrader; 03-10-2021, 06:10 PM.

              Comment


                #8
                Hello balltrader,

                Where the code needs to go depends on the use case. If you need to use a strategy you could place that code in an indicator or just within the strategy its self. An addon will not be required for any part of this concept.

                Making an indicator with a public plot method or property like the zig zag would generally be the suggestion if you needed to expose data to other indicators or strategies. That would enable the method to use the bar data and existing NinjaScript features.


                Please let me know if I may be of additional assistance.

                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hi again,

                  I have a new question about IsFirstTickOfBar and CurrentBar

                  In the event driven processing of NT8, can there be events from OnMarketDepth and/or OnMarketData arriving BEFORE OnBarUpdate?
                  my debugging seems to indicate that happens frequently.

                  My question is, (without asking you to reveal the secret inner workings of NinjaScript)

                  "is the int value of CurrentBar set during any event, or only during the OnBarUpdate event?"


                  During debugging, it seems when I recv an event from OnMarketData, and the BarsPeriod is 1 minute, and the event argument .Time is HH.mm.ss

                  i.e. the timestamp of the event data is 1 second in a new minute, ie 08:01:01 - this is the first data recvd in the 8 am minute 01,
                  does NinjaScript detect a new bar has started, and it changes the int value of CurrentBar at that very moment, or does it wait until it is in the event OnBarUpdate?

                  Comment


                    #10
                    Hello balltrader,

                    The OnMarketData override is documented as The OnMarketData() method is expected to be called after OnBarUpdate(). OnMarketDepth does not have the same documented stipulation so I don't believe you would be able to have a specific order with that and OnBarUpdate.

                    I cannot see into the source code of the platform to know how CurrentBar is assigned, for our use we wouldn't ever need to know that. We would only need to know the CurrentBar represents the bar in processing right now when OnBarUpdate is called, it would be incremented when a new bar is formed. CurrentBar is only relevant to OBU so if you are printing that in other overrides that won't be relevant there.

                    The OnBarUpdate override is called based on how the BarsType builds bars and also dependent on the incoming market data. As mentioned the CurrentBar is only relevant in OnBarUpdate so printing the value when OnBarUpdate is called would represent the current processing bars index. If you are printing it from other places you may see that it is on the current bar or possibly moving into the next bar. If you are are using a non NinjaScript method like OnRender then CurrentBar is meaningless in that context and would represent the first/random bar on the chart.


                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Shai Samuel, 07-02-2022, 02:46 PM
                    4 responses
                    94 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by DJ888, Yesterday, 10:57 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by MacDad, 02-25-2024, 11:48 PM
                    7 responses
                    158 views
                    0 likes
                    Last Post loganjarosz123  
                    Started by Belfortbucks, Yesterday, 09:29 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post Belfortbucks  
                    Started by zstheorist, Yesterday, 07:52 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post zstheorist  
                    Working...
                    X