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

Doji for a strategy question

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

    Doji for a strategy question

    Hello,Support and All!

    I have a question regarding the Doji usage in a strategy.I want to keep a strategy triggering signal per current bar,but wouldn`t want it to get jerky - back and forth,when and if the price level would stick at the doji level.How do i prevent the strategy,or in the indicator,from multiple alternations?Let`s say,i want it to alternate only one time - from long to short,or from short to long and then quit for the current bar.

    for,e.g,here is the snippet:

    if(Low[0] < Open[0] && Close[0] > Open[0] )

    {enter a position}

    else if(High[0] > Open[0] && Close[0] < Open[0] )

    {enter a position}

    Thank you!

    #2
    Hello outsource, and thank you for your query.

    There are two ways to prevent what is known as "bouncing". The first is to ensure that you are in a certain market position before making trades, e.g.

    Code:
    [FONT=Courier New]if([/FONT][FONT=Courier New][B]Position.MarketPosition != MarketPosition.Long[/B] && Low[0] < Open[0] && Close[0] > Open[0] )
    {
        EnterLong();
    }
    else if([/FONT][FONT=Courier New][B]Position.MarketPosition != MarketPosition.Short[/B] && High[0] > Open[0] && Close[0] < Open[0] )
    {
        EnterShort();
    }[/FONT]
    This will prevent you from making e.g. multiple long entries or multiple short entries, so that you can only switch from long to short, or from short to long.

    The second thing we can do is enforce a time limit between orders. For the one you mentioned, only switching once per bar, we will use the FirstTickOfBar method.

    Code:
    [FONT=Courier New][B]if(FirstTickOfBar)
    {[/B][/FONT][FONT=Courier New]
        if([/FONT][FONT=Courier New]Position.MarketPosition != MarketPosition.Long && Low[0] < Open[0] && Close[0] > Open[0] )
    [/FONT][FONT=Courier New]     {
            [/FONT][FONT=Courier New]EnterLong();
        [/FONT][FONT=Courier New]}
        [/FONT][FONT=Courier New]else if([/FONT][FONT=Courier New]Position.MarketPosition != MarketPosition.Short && High[0] > Open[0] && Close[0] < Open[0] )
        [/FONT][FONT=Courier New]{
            [/FONT][FONT=Courier New]EnterShort();
        [/FONT][FONT=Courier New]}
    [B]}[/B]
    [/FONT]
    Of course, that does what it says on the tin. If you'd prefer a little more flexibility - switching once per bar, on any tick in the bar - you will want to use code like this,

    Code:
    [FONT=Courier New][B]private bool tradedYet = false;
    protected override void OnBarUpdate()
    {
    
        // ...
    
        if(FirstTickOfBar)
        {
            tradedyet = false;
        }
    
        if(! tradedYet)
        {
    [/B]        [/FONT][FONT=Courier New] if([/FONT][FONT=Courier New]Position.MarketPosition != MarketPosition.Long && Low[0] < Open[0] && Close[0] > Open[0] )
    [/FONT][FONT=Courier New]         {
                [/FONT][FONT=Courier New]EnterLong();[/FONT][FONT=Courier New]
                [B]tradedYet = true;[/B][/FONT][FONT=Courier New]
            [/FONT][FONT=Courier New]}
            [/FONT][FONT=Courier New]else if([/FONT][FONT=Courier New]Position.MarketPosition != MarketPosition.Short && High[0] > Open[0] && Close[0] < Open[0] )
            [/FONT][FONT=Courier New]{
                [/FONT][FONT=Courier New]EnterShort();[/FONT][FONT=Courier New][FONT=Courier New]
                [B]tradedYet = true;
    [/B][/FONT]        [/FONT][FONT=Courier New]}
        [B]}
    
        // ...
    
    }
    [/B] [/FONT]
    I am providing links to the Help Guide pages for FirstTickOfBar and MarketPosition.




    Please let us know if there is any other way we can help.
    Last edited by NinjaTrader_JessicaP; 04-14-2016, 08:42 AM.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi,Jessica!

      Thank you for the reply and examples.

      Can you clarify how the last one works with any tick in the bar?I don`t see any properties for the leadtime or smth.like that.Maybe it`s not required,i`m not sure.All in all,what do you mean by any tick in the bar?

      Thank you!

      Comment


        #4
        Hello outsource,

        When setting CalculateOnBarClose = false, OnBarUpdate will run once a tick, not once a bar. I am providing the following table demonstrating how the third method would work across 2 6-tick bars.

        Code:
        [FONT=Courier New][FONT=Courier New]+------+-----+--------------------+-------------+--------+[/FONT][/FONT]
        [FONT=Courier New]| Tick | Bar | First Tick of Bar? | Traded Yet? | Action |
        [/FONT][FONT=Courier New][FONT=Courier New][FONT=Courier New][FONT=Courier New]+------+-----+--------------------+-------------+--------+
        [/FONT][/FONT][/FONT][/FONT][FONT=Courier New]| 1    | 1   | yes                | no          |        |[/FONT]
        [FONT=Courier New]| 2    | 1   | no                 | no          |        |
        [/FONT][FONT=Courier New]| 3    | 1   | no                 | no          |        |
        [/FONT][FONT=Courier New]| 4    | 1   | no                 | no          | trade  |
        [/FONT][FONT=Courier New]| 5    | 1   | no                 | yes         |        |
        [/FONT][FONT=Courier New]| 6    | 1   | no                 | yes         |        |
        [/FONT][FONT=Courier New]| 7    | 2   | yes                | no          |        |
        [/FONT][FONT=Courier New]| 8    | 3   | no                 | no          | trade  |
        [/FONT][FONT=Courier New]| 9    | 2   | no                 | yes         |        |
        [/FONT][FONT=Courier New][FONT=Courier New][FONT=Courier New][FONT=Courier New]+------+-----+--------------------+-------------+--------+[/FONT][/FONT][/FONT][/FONT]
        I hope this helps to clear up that code sample. Please let us know if there is any other way we can help.
        Last edited by NinjaTrader_JessicaP; 04-15-2016, 10:32 AM.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_JessicaP View Post
          Hello outsource,

          When setting CalculateOnBarClose = false, OnBarUpdate will run once a tick, not once a bar. I am providing the following table demonstrating how the third method would work across 2 6-tick bars.

          Code:
          [FONT=Courier New][FONT=Courier New]+------+-----+--------------------+-------------+--------+[/FONT][/FONT]
          [FONT=Courier New]| Tick | Bar | First Tick of Bar? | Traded Yet? | Action |
          [/FONT][FONT=Courier New][FONT=Courier New][FONT=Courier New][FONT=Courier New]+------+-----+--------------------+-------------+--------+
          [/FONT][/FONT][/FONT][/FONT][FONT=Courier New]| 1    | 1   | yes                | no          |        |[/FONT]
          [FONT=Courier New]| 2    | 1   | no                 | no          |        |
          [/FONT][FONT=Courier New]| 3    | 1   | no                 | no          |        |
          [/FONT][FONT=Courier New]| 4    | 1   | no                 | no          | trade  |
          [/FONT][FONT=Courier New]| 5    | 1   | no                 | yes         |        |
          [/FONT][FONT=Courier New]| 6    | 1   | no                 | yes         |        |
          [/FONT][FONT=Courier New]| 7    | 2   | yes                | no          |        |
          [/FONT][FONT=Courier New]| 8    | 3   | no                 | no          | trade  |
          [/FONT][FONT=Courier New]| 9    | 2   | no                 | yes         |        |
          [/FONT][FONT=Courier New][FONT=Courier New][FONT=Courier New][FONT=Courier New]+------+-----+--------------------+-------------+--------+[/FONT][/FONT][/FONT][/FONT]
          I hope this helps to clear up that code sample. Please let us know if there is any other way we can help.

          Hi,Jessica!

          Actually not clear.Could you please dumb it down a bit?In the first respond you said,''on any tick in the bar''.What exactly that meant?

          Thank you and have a tremendous weekend!

          Comment


            #6
            I did, thank you.

            I think an easy way to explain what I mean by "any tick in a bar" would be an example. Let's say that I had a one minute bar. This means that I build a new bar once a minute no matter how much volume there is. Let's also say that the price changed 20 times during the first minute, and 30 times in the second. Ticks are nothing more than price changes, so in this case, there would be 20 ticks in the first bar, and 30 in the second.
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Hello,Support!

              Is there a way to achieve the entry intrabar from the different type of bars,while they are forming intrabar?For e.g.,first - the doji appears and then some ticks later it mutates into the outside bar during the same bar.

              Thank you!

              Comment


                #8
                Hello Outsource,

                CalculateOnBarClose = false does work with bar types other than minute. Please let us know if there are any other ways we can help.
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_JessicaP View Post
                  Hello Outsource,

                  CalculateOnBarClose = false does work with bar types other than minute. Please let us know if there are any other ways we can help.
                  Hi Jessica,

                  it`s not what i asked.By bar types i meant the bar pattern that are forming on the chart,that is,Outside bar,Inside bar,Doji,etc,etc,....

                  What i asked for ,was i need the way to get signals from the two bars on bar[0] - cobc = false.For e.g. - Doji appears and then it mutatse into outside bar - all withing one bar[0].How do i accomplish that?so far it`s not working.

                  Comment


                    #10
                    Hello outsource,

                    So that I may better assist, would it be possible to provide me with a code snippet I could add to OnBarUpdate on my end? You can leave any entry logic out. I believe this will allow me to better understand what it is I can help with.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_JessicaP View Post
                      Hello outsource,

                      So that I may better assist, would it be possible to provide me with a code snippet I could add to OnBarUpdate on my end? You can leave any entry logic out. I believe this will allow me to better understand what it is I can help with.
                      Hi,Jessica,

                      ofcourse.Here is,for e.g., the snippet:

                      if (High[0] < High[1] && Low[0] > Low[1])
                      && (High[0] > High[1] && Low[0] < Low[1])
                      {Enter}

                      I need the first one to show up first ,and enter on the second one.All with in the single developing bar - intrabar,bar[0].

                      Thank you

                      Comment


                        #12
                        It sounds like you are interested in the GetCurrentAsk() and GetCurrentBid() methods. High[0] and Low[0] both refer to the most recent bar. You can track these values yourself intrabar to get a high and low for the current bar, like so :

                        Code:
                        [FONT=Courier New]          private double CurrentMin;
                                  private double CurrentMax;
                                  protected override void OnBarUpdate()
                                  {
                                      if (FirstTickOfBar)
                                      {
                                          CurrentMin = CurrentMax;
                                          CurrentMax = 0;
                                      }
                                      CurrentMin = GetCurrentAsk() < CurrentMin ? GetCurrentAsk() : CurrentMin;
                                      CurrentMax = GetCurrentBid() > CurrentMax ? GetCurrentBid() : CurrentMax;
                        
                                      // ... more code here
                        
                                  }[/FONT]
                        Please note this only works for live data, historically, GetCurrentAsk() and GetCurrentBid() default to the nearest value you have historical data for. I am including a link to the help guide page for GetCurrentAsk().

                        Last edited by NinjaTrader_JessicaP; 05-24-2016, 08:01 AM.
                        Jessica P.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_JessicaP View Post
                          It sounds like you are interested in the GetCurrentAsk() and GetCurrentBid() methods. High[0] and Low[0] both refer to the most recent bar. You can track these values yourself intrabar to get a high and low for the current bar, like so :

                          Code:
                          [FONT=Courier New]          private double CurrentMin;
                                    private double CurrentMax;
                                    protected override void OnBarUpdate()
                                    {
                                        if (FirstTickOfBar)
                                        {
                                            CurrentMin = CurrentMax;
                                            CurrentMax = 0;
                                        }
                                        CurrentMin = GetCurrentAsk() < CurrentMin ? GetCurrentAsk() : CurrentMin;
                                        CurrentMax = GetCurrentBid() > CurrentMax ? GetCurrentBid() : CurrentMax;
                          
                                        // ... more code here
                          
                                    }[/FONT]
                          Please note this only works for live data, historically, GetCurrentAsk() and GetCurrentBid() default to the nearest value you have historical data for. I am including a link to the help guide page for GetCurrentAsk().

                          http://ninjatrader.com/support/helpG...currentask.htm

                          Hi,Jessica,have no idea what this snippet says.It seems that i`d need to remodel the whole strategy.I just need two different bars to be recognized intrabar by a strategy.Is there more simplier way to accomplish that?

                          Comment


                            #14
                            The GetCurrentAsk() and GetCurrentBid() methods are the simplest ways to get at intrabar data. Is there anything I can help clarify in the snippet I provided?
                            Jessica P.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_JessicaP View Post
                              The GetCurrentAsk() and GetCurrentBid() methods are the simplest ways to get at intrabar data. Is there anything I can help clarify in the snippet I provided?
                              Everything,man

                              Seeing that one i have no idea what to ask you...It`s glyphs for me,totally

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by FrazMann, Today, 11:21 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post FrazMann  
                              Started by geddyisodin, Yesterday, 05:20 AM
                              8 responses
                              52 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              10 responses
                              36 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by DayTradingDEMON, Today, 09:28 AM
                              4 responses
                              24 views
                              0 likes
                              Last Post DayTradingDEMON  
                              Started by George21, Today, 10:07 AM
                              1 response
                              19 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Working...
                              X