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

ATR Trailing short stop not works

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

    ATR Trailing short stop not works

    Hello,

    I am using an ATR trailing stop for exits in a strategy. I used the ATRTrailing.cs for your website in order to program it. My problem is that the stop does not work when I have a short entry order, whereas it works normally when I have a long entry order. The code I am using is:

    ATRTrailingStopLower = ATRTrailing(ATRTimes, ATRPeriod, ATRRatched).Lower;
    ATRTrailingStopUpper = ATRTrailing(ATRTimes, ATRPeriod, ATRRatched).Upper;

    _longATRStop = ExitLongStopMarket(_ATRTrailingStopUpper[0], LongTrailingStop, LongEntry);
    _shortATRStop = ExitShortStopMarket(_ATRTrailingStopLower[0], ShortTrailingStop, ShortEntry);

    could you please tell me why it does not work for both sides even though the code is the same?

    Thank you.


    #2
    Hello yannistsoupakis,

    In general, it would be difficult for anyone to tell you specifically what is wrong without debugging the script, like any developer.

    Are you getting an error? (If so, what is the error?)

    Is the value used for the stop price a valid price? (buy stop prices must be above the ask, sell stop prices must be below the bid, for both manual orders or orders submitted by code)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello again and thanks for your response.

      I debugged my code and I attach a screenshot which some strange things happen.

      I get the Upper(blue) value and the Lower(red) value of the ATR trailing stop for each bar. and the values are listed in the output results. I am using the following code to print to the output window the close value and the red or blue ATR value, which it is OK I guess:
      var _ATRTrailingStopLower = ATRTrailing(ATRTimes, ATRPeriod, ATRRatched).Lower;
      var _ATRTrailingStopUpper = ATRTrailing(ATRTimes, ATRPeriod, ATRRatched).Upper;
      Print("ATR Low:" + _ATRTrailingStopLower[0] + " " + "Close: " + Close[0] + " " + Time[0]);
      Print("ATR Up:" + _ATRTrailingStopUpper[0] + " " + "Close: " + Close[0] + " " + Time[0]);

      I list below my questions, which are also pointed on the chart for clarity.

      1) By looking at the chart and the results on the output window, it seems it skips some days which exist on the chart and does not give results. By searching the results, there are many parts with this problem. Why is this?? Is this a problem from my code or something else?
      2) I write in my code to give me values of ATRTrailingStopLower[0], ATRTrailingStopUpper[0] and Close[0]. In my screenshot, I move my cursor on a bar and you can see that the data box date is 28/2/2020 and on the output window, these results (for close, ATRTrailingStopLower, ATRTrailingStopUpper) are shown on day 29/2/2020 which is the next day. Is this correct? Shouldn't it give the result on the same day with the chart (28/2/2020)?
      3) The strategy for the long side looks to work fine with ATR Trailing Exit whereas it does not work for short side, even though I am doing the same thing. It looks like the short exits are deactivated. Do you understand the reason it works only from one side, even though I see from my results that it should give and short exit order?
      My exit code is:
      _longATRStop = ExitLongStopMarket(_ATRTrailingStopUpper[0], LongTrailingStop, LongEntry); ---> Long orders (this works)
      _shortATRStop = ExitShortStopMarket(_ATRTrailingStopLower[0], ShortTrailingStop, ShortEntry); ---> Short orders (this doesn't work)

      Thank you!

      Yannis
      Attached Files

      Comment


        #4
        Hello Yannis,

        This is Jim, responding on behalf of Chelsea who is out of the office at this time.

        I suggest setting up a test indicator that simply prints Time[0] so you can add it to the chart and confirm you are getting OnBarUpdate iterations that match the timestamps on your chart. It is not clear where your print is made. If the print is within a code branch, that code branch might not be executed for logical reasons. Testing a simple indicator with Print(Time[0]); in OnBarUpdate will be a quick way to see that those bars are being processed in the script.

        If you see the test script prints on those bars but your strategy does not, you will want to examine any logic controlling those prints as they would to allow the prints to be reached.

        As for the issue with the stop loss not submitting, I can only note that you are not using a LiveUntilCancelled overload, so the order will be cancelled on the next bar if the method is not called again. If you want to call the order method once and keep it alive, you will want to use the following overload:

        ExitShortStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)

        To better understand what is happening with this order, I suggest the following in a separate test:
        1. Identify a range of bars where the order would be expected to be submitted.
        2. Open the Data Box on your chart and enable Bar Indexes.
        3. Take note of the bar indexes when you expect the order submission to be placed
        4. Use the bar indexes to filter your prints. CurrentBar represents the bar index of the processing bar.
        5. Create filtered prints that will only print for the bars you want to examine(You will want to place prints beside the order submission method to confirm it is being reached when you expect it to.
        6. Also enable TraceOrders. If the order submission method is hit but ignored, we will see TraceOrders feedback in the Output window telling us why.
        Example Print:

        Code:
        if (CurrentBar > 10 && CurrentBar <  20) Print(String.Format("ExitShotStopMarket Reached. _ATRTrailingStopLower[0]: {0} ShortTrailingStop: {1} ShortEntry: {2} CurrentBar: {3}", _ATRTrailingStopLower[0], ShortTrailingStop, ShortEntry, CurrentBar));
        _shortATRStop = ExitShortStopMarket(_ATRTrailingStopLower[0], ShortTrailingStop, ShortEntry);
        Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm
        TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm
        Debugging Demo - https://drive.google.com/file/d/1rOz...w?usp=drivesdk

        Internal rules of the Managed Approach can be referenced here - https://ninjatrader.com/support/help...antedPositions

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim

          Thank you for your detailed answer.

          Regarding the time mismatches between the chart and the output results I did a test and used a Print(Close[0] + " " + Time[0]); in the OnBarUpdated() block of the default SampleMACrossOver strategy. Again the time is not correct and the output close is in different date than the ones on the chart. Please check the screenshot for detail. I wrote on the screenshot the close values and dates under each bar for 14 days in order to realize the mismatch with the output. You can also see that the date 28/12/2020 is missing from the results even though it is on the chart. I cant really understand why this is not working??



          Regarding the short order execution, I am not really understanding how to use the barsInProgressIndex part of it. I am using the following code:

          _shortATRStop = ExitShortStopMarket(0,true,_ATRTrailingStopLower[0], ShortTrailingStop, ShortEntry);

          and it gives an error for 0. I am trying to find an example online but I couldn't.

          Thank you!



          Attached Files

          Comment


            #6
            Hello Yannis,

            Please test the script I have attached and please also see the video for testing so we can ensure we are making the same test.

            Demo - https://drive.google.com/file/d/1e29...w?usp=drivesdk

            If you see missing prints following this same test with my script, could you test again in a clean environment? (I have included instruction at the end of my post.) If issues are seen here, could you let me know your data provider and your timezone?

            Getting past this, I recommend following debugging procedures like that mentioned in post #4.

            As for using the IsLiveUntilCancelled overload, the order method requires 6 parameters and you are providing only 5. An example that shows using IsLiveUntilCancelled overloads can be found below.



            BarsInProgress is used to specify a data series to submit the order to if the script is multi time frame/multi instrument. You can use 0 for single series scripts. More information on BarsInProgress and Multi time Frame NinjaScripts can be found below.

            Multi Time Frame and Instruments - https://ninjatrader.com/support/help...nstruments.htm

            Clean Environment Test:

            Creating a clean environment can be done by following the steps below:
            1. Close NinjaTrader 8, and rename the "NinjaTrader 8" folder in My Documents to something like: "NinjaTrader 8 OLD" Do not delete this folder.
            2. Uninstall NinjaTrader from the Windows Control Panel
            3. Delete the C:\Program Files (x86)\NinjaTrader 8 folder
            4. Reinstall using the installer from http://ninjatrader.com/PlatformDirect
            5. Import the test indicator and perform the test demonstrated above.
            If you ever need to switch back to your original platform, you may do so by closing NinjaTrader and swapping the platform folder names.

            For example, Close NinjaTrader and rename the new "NinjaTrader 8" folder to "NinjaTrader 8 NEW" and the "NinjaTrader 8 OLD" folder to "NinjaTrader 8." Then restart the platform. Simply put: NinjaTrader 8 will always load the "NinjaTrader 8" folder in My Documents.

            We look forward to assisting.
            Attached Files
            Last edited by NinjaTrader_Jim; 01-18-2021, 09:51 AM.
            JimNinjaTrader Customer Service

            Comment


              #7
              Hello Jim,

              I have uninstalled Ninjatrader, reinstalled it according to the clean environment test mentioned in post #6 and again I have the same issues. The data provider I currently use is Kinetick end of day and my time zone is UTC/GMT +2:00 hours.

              Furthermore, I don't know why, but even after using the Isliveuntilcanceled, the short side order is not submitted. I dont know if the problem with the dates causes this.

              Please give any assistance you can.

              thank you.

              Comment


                #8
                Hello yannistsoupakis,

                Regarding the behavior where you see daily bars on your chart, but do not see prints from my test script, could you reach out to me at scriptingsupport [at] ninjatrader [dot] com with the text "Attn Jim 2913634" and a link to this thread? I would like to arrange a remote support session so I can get connected to your PC and test my script. I will also need a phone number and a good time to reach you. (Please include a time zone.) Our regular support hours are 8:30am to 6pm US Eastern, Monday through Friday. I can personally schedule a call as late as 4:30PM EST.

                If an order is not submitting when you expect, we suggest using debugging prints to confirm that your logic is allowing the order submission method to be reached.

                If the order submission method is reached, but no order is submitted, please retest with TraceOrders enabled to check for feedback if the order was ignored due to an internal rule.

                We look forward to assisting.
                JimNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Aviram Y, Today, 05:29 AM
                0 responses
                1 view
                0 likes
                Last Post Aviram Y  
                Started by quantismo, 04-17-2024, 05:13 PM
                3 responses
                25 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by ScottWalsh, 04-16-2024, 04:29 PM
                7 responses
                34 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by cls71, Today, 04:45 AM
                0 responses
                6 views
                0 likes
                Last Post cls71
                by cls71
                 
                Started by mjairg, 07-20-2023, 11:57 PM
                3 responses
                216 views
                1 like
                Last Post PaulMohn  
                Working...
                X