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

Parabolic SAR Implementation

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

    Parabolic SAR Implementation

    Hi NT,

    I am having trouble implementing a simple parabolic SAR strategy. I am looking to passively follow the signal (long when dots are below price action, short when dots are above price action). I have gotten this to work for the most part, but when back-testing I find a few "switch" signals where the script does nothing. Can anyone tell what is wrong with this snippet? I have walked through the logic step by step, but I cant tell where this is breaking down. I am calculating on each tick.

    Thanks in advance!

    Click image for larger version

Name:	Capture.PNG
Views:	628
Size:	39.6 KB
ID:	1122374

    Code:
    protected override void OnBarUpdate()
    {
    
    //Determine intial position
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    if (ParabolicSAR1[0] < Low[0])
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    }
    
    else if (ParabolicSAR1[0] > High[0])
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    }
    }
    
    //Stop and reverse signals
    if ((Position.MarketPosition == MarketPosition.Long)
    && (ParabolicSAR1[1] < Low[1])
    && (ParabolicSAR1[0] > High[0]))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"Stop", @"Long");
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    }
    
    if ((Position.MarketPosition == MarketPosition.Short)
    && (ParabolicSAR1[1] > High[1])
    && (ParabolicSAR1[0] < Low[0]))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), @"Stop", @"Short");
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    }
    Attached Files

    #2
    Hello collic653,

    Thank you for your post.

    First, I note you're calling both an exit method in one direction and an entry method in the other on the same run through of OnBarUpdate. When calling an entry method in the opposite direction of your current position this will cause your position to be reversed. NinjaTrader will automatically submit an order to close your existing position and then enter an order to enter you into the opposite position.

    If you call an exit method and call an entry method in the same run of OnBarUpdate, which occurs once per bar, the OnPositionUpdate() method will not have yet run and NinjaTrader will not have known that your position is closed.

    This will cause both actions to complete and end up sending 3 orders. The first order is the exit position from your exit method, the second order is to close the position from NinjaTrader automatically reversing your position, the third order is to enter you into the opposite position.

    The result is that either the script will double the quantity when it reverses or it will cause an overfill and stop the script.

    From the help guide:

    "Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position."

    https://ninjatrader.com/support/help...d_approach.htm

    So you don't need those exit orders, the EnterLong() or EnterShort() would automatically do that step of exiting the existing order for you already.

    Next, I'd add some prints to your script so you can determine if all conditions would have been true and a trade should have been taken on that particular bar.

    This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going in the correct direction. You can even add these using the Strategy Builder.

    https://ninjatrader.com/support/foru...ns-not-working

    If you run into issues like we saw here, the above information will allow you to print out all values used in the condition in question that may be evaluating differently than you'd expect. With the printout information you can assess what is different between the two.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate,

      Thanks for this. I think i was taking the stop AND reverse too literally .

      I am still running into trouble trying to get the position to match the indicator. In addition to debugging with prints, is there potentially a snippet I could take from the indicator itself? I took a look at it but it is very difficult to follow

      Comment


        #4
        Hello collic653,

        Thank you for your reply.

        The Parabolic SAR indicator is pretty complex in terms of the code for it, and I'm not sure if there's a particular snippet from the indicator itself that would be helpful here.

        I'm attaching an example of prints where you can go through and verify whether this should be taking trades on a particular bar. I'd recommend using this with the Data Box so you can pinpoint which bar the prints are from and verify whether the trade should have been taken according to that logic.

        Thus far I'm seeing that trades are simply not being taken due to conditions being false on reviewing this. You may want to look at adjusting your conditions based on the prints you see from this.

        Please let us know if we may be of further assistance to you.
        Attached Files
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hi Kate,

          Thank you very much for the script, this is helpful.

          I was able to pinpoint one of the bars where the strategy should have changed positions but did not. Running 10 min bars on ES 12-20 for 10/9, the strategy should have switched from short to long in bar 54, (which was 2:10 AM). Based on the prints, all conditions were met, and the strategy submitted the appropriate orders. I can see these orders showing up in strategy analyzer, but for some reason they didn't fill until bar 112, as opposed to bar 55 as expected. I marked these bars with a blue diamonds in the screenshot below.

          It seems like there was an issue with the order itself, not the conditions. I'm not sure what I am missing here...

          Print output:

          Bar 54
          Current Position: Short
          ParabolicSAR1[1]: 3450.77110732527
          Low[1]: 3445.5
          ParabolicSAR1[0]: 3445.25
          High[0]: 3452.25
          ParabolicSAR1[1] < Low[1]: False
          ParabolicSAR1[0] > High[0]): False
          Conditions for SHORT not met
          ParabolicSAR1[1]: 3450.77110732527
          Low[0]: 3445.5
          ParabolicSAR1[0]: 3445.25
          High[1]: 3452.25
          (ParabolicSAR1[1] > High[1]): True
          (ParabolicSAR1[0] < Low[0]): True
          Conditions for LONG MET
          10/9/2020 2:10:00 AM Strategy 'TestParabolicSAR/-1': Entered internal SubmitOrderManaged() method at 10/9/2020 2:10:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Long' FromEntrySignal=''



          Orders:

          Click image for larger version

Name:	orders.jpg
Views:	492
Size:	40.2 KB
ID:	1122569

          Chart:

          Click image for larger version

Name:	chart.jpg
Views:	478
Size:	90.8 KB
ID:	1122571
          Attached Files

          Comment


            #6
            Hello collic653,

            Thank you for your reply.

            You mentioned you're testing this in the Strategy Analyzer - could you supply a screenshot of your settings so I can try to reproduce? Running this on a 10 day chart I'm not seeing this occur on that date and time.

            Thanks in advance; I look forward to assisting you further.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Sure thing, here you go. Thanks for testing!

              Comment


                #8
                Hello collic653,

                Thank you for your reply.

                That image seems to be broken, can you try uploading it as an attachment instead?

                Thanks in advance; I look forward to assisting further.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Sorry about that, let me know if this works
                  Attached Files

                  Comment


                    #10
                    Hello collic653,

                    Thank you for your reply.

                    I've replicated this and interestingly I'm seeing different results there than I'm seeing in your results for the same date range. So I can make absolutely sure that we're testing on the same data, who is your data provider?

                    Thanks in advance; I look forward to resolving this for you.
                    Attached Files
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Kate,

                      I am using Kinetick. Would it be worth it to try to re-download historical data?

                      Comment


                        #12
                        Hello collic653,

                        Thank you for your reply.

                        I would recommend deleting and redownloading the historical data and also clearing your cache, as there could be an issue with the cached data.

                        Please delete your historical data using the instructions provided at the following page of the NinjaTrader Help Guide:

                        Removing Historical Data
                        • Once the data has been deleted close NinjaTrader. Open the Documents\NinjaTrader 8\db\cache folder. Select all files then right mouse click and select “delete.”
                        • Open NinjaTrader and connect to your data provider. Open a new chart with at least 6 days of data on it so new data is loaded. Enable tick replay on this chart so the tick data is also reloaded that you'll need for the High order fill resolution as well.
                        • Lastly, run the test again.
                        Thanks in advance; I look forward to assisting you further.
                        Kate W.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi Kate, that actually cleared it up! Sorry for all the back and forth, I wouldn't have expected it to be data related as the chart and indicator were both printing to the chart. Thanks for your help

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by arvidvanstaey, Today, 02:19 PM
                          4 responses
                          11 views
                          0 likes
                          Last Post arvidvanstaey  
                          Started by samish18, 04-17-2024, 08:57 AM
                          16 responses
                          61 views
                          0 likes
                          Last Post samish18  
                          Started by jordanq2, Today, 03:10 PM
                          2 responses
                          9 views
                          0 likes
                          Last Post jordanq2  
                          Started by traderqz, Today, 12:06 AM
                          10 responses
                          18 views
                          0 likes
                          Last Post traderqz  
                          Started by algospoke, 04-17-2024, 06:40 PM
                          5 responses
                          48 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X