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

trailing stop with frequency in strategy builder

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

    trailing stop with frequency in strategy builder

    i'm building a strategy that i made and tested on chart trader. on the chart trader its an atm strategy with a trailing stop. i'm not seeing a frequency option like in the atm strategy builder on chart trader. i'd like the stop loss to step to a certain amount after a certain profit in ticks is met and stay there. on chart trader its a frequency of 999. where is that in the strategy builder? and, is "from entry" the amount of ticks from entry that triggers the stepped stop to occur?
    Attached Files

    #2
    i looked through the forum and found some topics on making a trailing stop within a strategy. i built it the best i could from looking at trailbuilder example provided in one topic. this is what i have. its not executing the stops. i'm not sure why. its not letting me attach the strategy so i shared a download link to it on my google drive.

    Last edited by SteveReiza; 01-28-2024, 01:54 AM.

    Comment


      #3
      Hello SteveReiza,

      Thanks for your notes.

      The Strategy Builder SetTrailStop() method for trailing stops does not have a frequency option available to set.

      I do not have access to the link you shared so I cannot view your code.

      You could use Exit order methods and add coding logic in the Strategy Builder to accomplish this goal.

      Please note that you would not be able to use the Exit methods and the Set methods in the Stop and Targets window as that creates a violation of the Managed Approach Internal Order Handling Rules, linked here: https://ninjatrader.com/support/help...d_approach.htm

      See the educational examples in the forum thread below that my colleague Chelsea has created regarding strategy builder breakeven and trailing stop in the Strategy Builder.
      https://ninjatrader.com/support/foru...der#post806596

      If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true or the order is being ignored for other reasons.

      To understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

      In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

      Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

      Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

      Below is a link to a forum post that demonstrates how to use prints to understand behavior.
      https://ninjatrader.com/support/foru...121#post791121​​​
      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        i sent the strategy to zip so i could upload it.

        it would be helpful for a simple example of a trail stop where the trailing stop doesn't trail but has a frequency of 999 as when in use in the chart trader. Such as profit trigger then trailing stop with one frequency a distance from the profit trigger. i think a lot of people would find it helpful.

        On mine, i'm attempting to have a profit trigger (ticks) of 45, 70, and 85 and a one time stepped trail stop to a trailing distance (ticks) of 40, 45, and 33 attached to each of those respectively.
        Attached Files

        Comment


          #5
          Hello SteveReiza,

          Thanks for your notes.

          The TrailBuilderExample on the forum thread linked in post # 3 demonstrates including a frequency which is TrailFrequency in the sample script.

          TrailStopDistance is how far away the stop is placed from the Close price in ticks since the stop loss uses CurrentStopPrice and we assign (Close[0] + (TrailStopDistance * TickSize)) to the Exit method.

          For example, if TrailStopDistance is set to -4, this means that CurrentStopPrice will be assigned a value of the Close price minus 4 ticks. So the initial stop order would be placed 4 ticks below the entry and trail from there.

          TrailFrequency is used to offset the CurrentTriggerPrice by a set number of ticks. CurrentTriggerPrice is assigned (Close[0] + (TrailFrequency * TickSize)).

          If we set the TrailFrequency to 4, this means that CurrentTriggerPrice is assigned a value of the Close price plus 4 ticks. Then, anytime the price moves 4 ticks in your favor, the stop would move to the value assigned to CurrentStopPrice.​

          You could study the TrailBuilderExample script to see how TrailFrequency is used in the script.
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            thanks for the reply.

            For example, if TrailStopDistance is set to -4, this means that CurrentStopPrice will be assigned a value of the Close price minus 4 ticks. So the initial stop order would be placed 4 ticks below the entry and trail from there.

            okay. so i have confirmation from what you're saying is that the trail stop distance needs to be a negative number? because i have that in mine. so i feel pretty good about that. there is the min value? on the trail builder it is set to -9999. what is that? i put that on mine to.
            Attached Files
            Last edited by SteveReiza; 01-29-2024, 08:30 AM.

            Comment


              #7
              Hello SteveReiza,

              Thanks for your notes.

              The minimum value would be the minimum value that the user-defined input could be set to.

              If the minumum value is set to -9999, this means that is the minimum value you would be able to use for that property in the UI when adding the script to a chart and enabling it.
              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                okay sweet. thanks for the explanation.

                do the trigger price and stop price need to be a double set to 0. or, do i put the tick quantity there in profit - as that would be the quantity for the trigger to happen?
                Attached Files

                Comment


                  #9
                  Hello SteveReiza,

                  Thanks for your notes.

                  The TriggerPrice and StopPrice could be set to 0 in the Inputs and Variables screen of the Strategy Builder.

                  A value is assigned to these variables when we check if we are in a flat market position as seen in Set 2 of the TrailBuilderExample reference sample linked in post # 3.
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    okay, awesome. thanks for the clarification. i have them set to 0 now. i get how they are defined as we go forward.

                    i have more than one trigger price and stop price. as you can see on the attachment i have this set identical to the trailbuilder example. i think i'm keyed in okay. but, i'm adding another trigger price and stop price. do i put that in the same set and select "any" or do i put them in another set?
                    Attached Files

                    Comment


                      #11
                      Hello SteveReiza,

                      Thanks for your notes.

                      You will likely need to assign a value to the other trigger price and stop price variables in a separate Set.

                      This Set is assigning a new trigger price to InitialTriggerPrice and a new stop price to InitialStopPrice when you are in a long market position and the Close price is greater than InitialTriggerPrice.

                      In another Set, you could create a condition checking if you are in a long position and check if the Close price is greater than say TriggerPrice1, and assign a new value to the TriggerPrice1 variable. A separate Set would likely need to be created for each trigger price and stop price variable.
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        okay sweet. that's what i did. i think i have it dialed in. but, for some reason when i do the backtest the stops aren't registering. can you take a look?
                        Attached Files

                        Comment


                          #13
                          Hello SteveReiza,

                          Thanks for your notes.

                          I see you have the strategy set to Calculate.OnEachTick.

                          The Strategy Analyzer does not have intrabar information available to use for backtests so the Calculate mode used in backtests will be Calculate.OnBarClose.

                          Please review the help guide document on the differences on real-time vs backtest (historical).


                          When in historical data (backtesting), only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

                          Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

                          Below is a link to the help guide on Calculate.
                          https://ninjatrader.com/support/help.../calculate.htm

                          To improve the accuracy of a backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar.

                          Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single tick data series and submit orders to that single tick data series for a strategy that uses Tick Replay.

                          Note that you must unlock the code from the Strategy Builder by clicking the 'Unlock code' button and manually program your script to implement intrabar granularity. This is not possible to do in the Strategy Builder.

                          High Order Fill Resolution allows for intra-bar order fills with historical processing, but is not compatible with Tick Replay.

                          Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

                          SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

                          TickReplay — https://ninjatrader.com/support/help...ick_replay.htm

                          Developing for Tick Replay -
                          https://ninjatrader.com/support/helpGuides/nt8/developing_for__tick_replay.htm?zoom_highlightsub= developing+for+tick+replay

                          Additional information may be found in this NinjaTrader Forum post:
                          https://ninjatrader.com/support/forum/forum/ninjatrader-8/strategy-development/100192-comparing-real-time-historical-and-replay-performance?t=102504

                          Or, you could consider testing your strategy using the Playback connection with Market Replay data.

                          Playback: https://ninjatrader.com/support/help...​​
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            many thanks for the reply.

                            what takes precedent when a stoploss is present. the stoploss being actuated or the bar being closed. meaning if during a bar the price drops below the stop loss - is the stop loss recorded then? or, not until the bar is closed and is computed in hindsight? as we're backtesting. but same if we're in live trading. if i'm on bar close and i have a stop at 20. the active bar has a price drop below 20. does the stop loss happen then. or does it have to wait until the bar closes and for that to register?

                            Comment


                              #15
                              Hello SteveReiza,

                              Thanks for your notes.

                              The Strategy Analyzer uses Historical Fill Algorithm to calculate the trades a strategy would have made on historical data. Each order is filled using the final fill price calculated for that particular order based on Historical Order Backfill Logic ​

                              When running a strategy on real-time data, market dynamics determine the fill of orders.

                              See this help guide documentation below for more information about Historical Fill Processing and Historical Order Backfill Logic.

                              Historical Fill Processing: https://ninjatrader.com/support/help...ical_fill_.htm
                              Historical Order Backfill Logic: https://ninjatrader.com/support/help...fill_logic.htm

                              Ultimately, if you want the backtest results to be more accurate, within 1 Tick compared to running the strategy real-time, you must enable Tick Replay, add a 1-tick secondary series to the script, and submit orders to that added 1-tick series as noted in post # 13.

                              Without adding intrabar granularity to the strategy, there will be discrepancies when comparing backtest results to realtime results.

                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by pibrew, Today, 06:37 AM
                              0 responses
                              0 views
                              0 likes
                              Last Post pibrew
                              by pibrew
                               
                              Started by rbeckmann05, Yesterday, 06:48 PM
                              1 response
                              12 views
                              0 likes
                              Last Post bltdavid  
                              Started by llanqui, Today, 03:53 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post llanqui
                              by llanqui
                               
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              15 views
                              0 likes
                              Last Post AaronKoRn  
                              Working...
                              X