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

Can strategy be OnPriceChange and embedded Indicator OnBarClose?

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

    Can strategy be OnPriceChange and embedded Indicator OnBarClose?

    I have a strategy set to OnPriceChange that has an embedded indicator which I am currently using by shifting the value index to 0 in historic and 1 in live tape. This works but what's throwing me off is that the indicator is plotting its internal state via its ToString() method based on its setting. Which of course is LIVE as the strategy is set to OnPriceChange.

    I recall that with Ninja7 setting an indicator in code to OnBarClose would have no effect if that strategy would execute against live ticks. Has that changed in NT8?

    Otherwise how do you suggest I produce log output that reflects the embedded indicator's last bar close values?

    Thanks in advance.

    #2
    Hello,

    Thank you for the post.

    For this question:

    I recall that with Ninja7 setting an indicator in code to OnBarClose would have no effect if that strategy would execute against live ticks. Has that changed in NT8?
    This has not changed between NinjaTrader 7 and 8. Indicators will inherit the calculation mode of the Strategy they are used in.

    Please take a look at this reference sample which describes how you would calculate your strategy on each tick/ price change and still only calculate your indicators at the first tick of a new bar.



    You will have a condition using IsFirstTickOfBar, which will return a boolean that tells you if the OnBarUpdate() being called is for the first tick of the new bar. In this sample, they check if IsFirstTickOfBar == true and if the position is flat, then they enter long.

    Code:
    if (State == State.Historical)
    		return;
    
    if (IsFirstTickOfBar && Position.MarketPosition == MarketPosition.Flat)
    	
         if (LinReg(Median, 10)[1] > LinReg(Median, 10)[2] && LinReg(Median, 10)[1] > 
         SMA(Median, 10)[1])
         EnterLong("long entry");
    }

    Once the strategy is long, it will continue to check the position on every tick, along with exit conditions:

    Code:
    if (Position.MarketPosition == MarketPosition.Long)
    {
    	if (CrossBelow(LinReg(Median, 10), SMA(Median, 10), 1))
    		ExitLong("LinReg cross SMA exit", "long entry");
    }
    Below is a link to IsFirstTickOfBar:



    Please let us know if we may be of any further assistance.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick response, Chris.

      Well, looking at your example, that is what I'm doing. I know how to use the proper index based on historical [0] or live tape [1]. My issue arises as I'm printing log statements directly from the indicator and it's printing LIVE values, which isn't what I want.

      I can of course reproduce the log output in my strategy but some values I do not have access to. So the only other recourse would be to add a special ToStringHistorical() method that shifts its output based on live or historical, similar to what I'm doing in the strategy.

      I hope that makes sense. If you can think of an easier/better solution, I'm all ears

      Originally posted by NinjaTrader_ChrisL View Post
      Hello,

      Thank you for the post.

      For this question:



      This has not changed between NinjaTrader 7 and 8. Indicators will inherit the calculation mode of the Strategy they are used in.

      Please take a look at this reference sample which describes how you would calculate your strategy on each tick/ price change and still only calculate your indicators at the first tick of a new bar.



      You will have a condition using IsFirstTickOfBar, which will return a boolean that tells you if the OnBarUpdate() being called is for the first tick of the new bar. In this sample, they check if IsFirstTickOfBar == true and if the position is flat, then they enter long.

      Code:
      if (State == State.Historical)
      		return;
      
      if (IsFirstTickOfBar && Position.MarketPosition == MarketPosition.Flat)
      	
           if (LinReg(Median, 10)[1] > LinReg(Median, 10)[2] && LinReg(Median, 10)[1] > 
           SMA(Median, 10)[1])
           EnterLong("long entry");
      }

      Once the strategy is long, it will continue to check the position on every tick, along with exit conditions:

      Code:
      if (Position.MarketPosition == MarketPosition.Long)
      {
      	if (CrossBelow(LinReg(Median, 10), SMA(Median, 10), 1))
      		ExitLong("LinReg cross SMA exit", "long entry");
      }
      Below is a link to IsFirstTickOfBar:



      Please let us know if we may be of any further assistance.

      Comment


        #4
        Hello,

        Thank you for the reply.

        ...the proper index based on historical [0] or live tape [1].
        No index will dictate whether data is live or historical. NinjaTrader will run through all available historical data and apply the logic historically. Once NinjaTrader gets to the data coming in, it will be in the real-time state. Could you please provide a sample of the issue you are experiencing?

        I look forward to your reply.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hello,
          No index will dictate whether data is live or historical. NinjaTrader will run through all available historical data and apply the logic historically. Once NinjaTrader gets to the data coming in, it will be in the real-time state. Could you please provide a sample of the issue you are experiencing?

          I look forward to your reply.
          I don't think that you understood my last question. But never mind - I hacked myself a solution by passing the indicator a loggingIndex which I can increment during runtime, thus producing adjusted (i.e. previous historical) candle logging.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Jon17, Today, 04:33 PM
          0 responses
          1 view
          0 likes
          Last Post Jon17
          by Jon17
           
          Started by Javierw.ok, Today, 04:12 PM
          0 responses
          4 views
          0 likes
          Last Post Javierw.ok  
          Started by timmbbo, Today, 08:59 AM
          2 responses
          10 views
          0 likes
          Last Post bltdavid  
          Started by alifarahani, Today, 09:40 AM
          6 responses
          40 views
          0 likes
          Last Post alifarahani  
          Started by Waxavi, Today, 02:10 AM
          1 response
          19 views
          0 likes
          Last Post NinjaTrader_LuisH  
          Working...
          X