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

Coding question

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

    Coding question

    Allow me to describe what I am trying to solve.

    I have an indicator that draws a short horizontal line when a specific condition is met.

    I am using (a)
    DrawLine("Rising0", false, barsForward, EMA(20)[0], barsBack0, EMA(20)[0], up0, DashStyle.Solid, wline0);

    and (b)
    if (pasthlines_On) DrawLine("Rising0b" + CurrentBar, false, barsForward, EMA(20)[0], barsBack1, EMA(20)[0], up0, DashStyle.Solid, wline0);

    The reason I have both is that sometimes I want to see the past ones, sometimes not.

    a) behaves nicely in that the line appears and disappears when the condition happens to change within the same bar.

    But when I have (b) also activated, then what happens is that
    (a) draws the line and (b) also. And while when the condition changes within the same bar, the line drawn from (a) disppears but the one drawn by (b) remains (unless I push F5) when I would want it to also disappear.

    It's kind of like I need to run (a) on BarClose False while (b) needs to run on BarClose True. I could achieve that by running the indicator twice. But is there perhaps another way to achieve the same?

    Hope I was clear and look forward to see if someone has an idea.

    sandman
    Last edited by sandman; 10-29-2016, 03:44 AM.

    #2
    Hello sandman,

    You can run the script with Calculate on bar close set to false, this will trigger OnBarUpdate for each real-time tick, and use FirstTickOfBar to know when a bar has closed.



    As a heads up, Calculate on bar close is always true when historical data.

    You could also add a 1 tick series to your script, and processing for BarsInProgress 0 for any actions that need to take place when a bar closes and BarsInProgress 1 for any actions that need to take place tick for tick.

    This would work historically.

    Below is a link to an official reference sample that demonstrates how to add intra-bar granularity.
    You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


    Also, here is a link to the differences on real-time vs backtest (historical).


    As well as a link to the help guide on the Add() method.


    A link to the help guide on BarsInProgress.


    And a link to the help guide on Multi-Time Frame & Instruments. Please see the section 'How Bar Data is Referenced', and 'Accessing the Price Data in a Multi-Bars NinjaScript'.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for all the references, Chelsea. I had not known FirstTickOfBar could be included in the midst of the code to temporarily override the OnBarCloseFalse condition so to speak. I have put it into another indicator I am writing for a friend which draws arrows. It compiled alright. I still would appreciate if you could have a brief look at it and give me an indication whether that looks proper or if it's off the wall. If it's the latter I will study some more.

      sandman
      Attached Files

      Comment


        #4
        Hello sandman,

        The code with FirstTickOfBar appears redundant. The same drawing object is being drawn outside of that condition on every tick. Remove the condition and action with FirstTickOfBar and the behavior would be the same.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea,

          Thanks. You are right, my mistake. To achieve what I want I needed to also give the tags different names. That together with FirstTickOfBar solved it fully.

          Another question: How is it possible to only show the currently in progress bar in a chart but not the past ones? Do you have a reference for me for that?

          sandman

          Comment


            #6
            Hello sandman,

            To prevent actions from occurring historically, require the Historical bool be false in a condition for the action to be triggered.

            To prevent the entire script from processing historically, add if (Historical) return; to the top of OnBarUpdate.

            Historical - http://ninjatrader.com/support/helpG...historical.htm

            You could also re-use the tag name. Then only one drawing object will ever exist and move instead of new drawing objects being made.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Works just fine with the "Historical" inserted into the code. Thank you.

              While we are at it, please allow another question. Need to decribe what I am trying to solve and I enclose an image to illustrate what I mean.

              I use a 1Min candle stick chart with an added 5Minute Data Series. Using a box instead of candle sticks for the 5Minute gives me colored 5Min boxes. The 1Min candle sticks fit perfectly into the 5Min box. Sofar so good. But now when I use a BackColorSeries on the 5Min DataSeries, it places the background color stripe not with the 5Min box but with the 5Min candlesticks. How do I solve this so the background stripe aligns with the box?

              sandman
              Attached Files

              Comment


                #8
                Hello sandman, and thank you for your question. To aid the support community, who may have questions similar to those you are asking, I would like to ask if you could start new topics in their own threads. This will just make it easier for everyone to find answers to their queries.

                That said, I would like to recommend the DrawRegion method. A way to make background colors only respond to opening and closing prices while ignoring high and low prices (or vice versa at your preference) would be this,

                Code:
                [FONT=Courier New]
                // http://ninjatrader.com/support/helpGuides/nt7/drawregion.htm[/FONT][FONT=Courier New][FONT=Courier New]
                IDataSeries upper = Close[0] > Open[0] ? Close : Open;[/FONT][/FONT][FONT=Courier New][FONT=Courier New][FONT=Courier New]
                IDataSeries lower = Close[0] <= Open[0] ? Close : Open;
                [/FONT][/FONT]DrawRegion("Upper" + CurrentBar, 1, 0, upper, Int32.MaxValue, Color.Black, [B]Color.Red[/B], 10);[/FONT][FONT=Courier New][FONT=Courier New][FONT=Courier New]
                [/FONT][/FONT]DrawRegion("Lower" + CurrentBar, 1, 0, lower, Int32.MinValue, Color.Black, [B]Color.Red[/B], 10);[/FONT]
                I have bolded the fill color to make it easier to substitute in a variable you can change between red, green, and white. Please let us know if there are any other ways we can help.
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Jessica,
                  Got it. Guess I got carried away. To fix it somewhat I edited the original post by adding keywords into the subject line. Thank you for the DrawRegion sample.

                  sandman

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by maybeimnotrader, Yesterday, 05:46 PM
                  2 responses
                  22 views
                  0 likes
                  Last Post maybeimnotrader  
                  Started by adeelshahzad, Today, 03:54 AM
                  5 responses
                  32 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by stafe, 04-15-2024, 08:34 PM
                  7 responses
                  32 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by merzo, 06-25-2023, 02:19 AM
                  10 responses
                  823 views
                  1 like
                  Last Post NinjaTrader_ChristopherJ  
                  Started by frankthearm, Today, 09:08 AM
                  5 responses
                  22 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Working...
                  X