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

Custom Stop Loss Hook.

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

    Custom Stop Loss Hook.

    I'm looking to write a custom stop loss hook. The logic is simple, however, backtesting will not work properly unless I use the market replay feature, which is not suitable.

    SetStopLoss() / SetProfitTarget() can be called in State.Configure and will trigger the stop loss in backtesting properly. Is there a way to build a custom stop loss / profit target trigger that will function properly in back tests just as the two built in functions?

    Thanks for your help.

    #2
    Hello chasebank,

    Thanks for your post.

    Set methods can be called in OnBarUpdate to trigger them on your own, or you can consider using Exit methods to control your targets and stops.

    Set methods are tied to an Entry order by the Signal Name given to the Entry order and the FromEntrySignal given to the Set method. If you had previously used a Set method with an Entry Signal name, then NinjaTrader will submit the target/stop to the levels that were last given to the Set method when the next Entry order fills. (Set methods prep NinjaTrader to submit targets and stops once the associated Entry order is seen as filled.) If you use Set methods dynamically in OnBarUpdate, we would suggest resetting the Set method to an initial level when the strategy is flat and before that next entry is submitted.

    The example below describes how the Set methods can be used dynamically in OnBarUpdate.

    SamplePriceModification - https://ninjatrader.com/support/help...of_stop_lo.htm

    You could also consider submitting your own stops/targets with Exit methods. An example can be found below.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    We should note that with historical processing, logic is always processed OnBarClose unless Tick Replay is used, and order fill simulations will use the OHLC values of the data series that the order is submitted to to fill the order. (Historical fill processing does not have intrabar granularity.) To have order fills simulated with intrabar granularity, the orders can be submitted to a single tick dataseries.

    Backtesting with intrabar granularity - https://ninjatrader.com/support/help...ipt_strate.htm

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      RE: "We should note that with historical processing, logic is always processed OnBarClose unless Tick Replay is used, and order fill simulations will use the OHLC values of the data series that the order is submitted to to fill the order. (Historical fill processing does not have intrabar granularity.) To have order fills simulated with intrabar granularity, the orders can be submitted to a single tick dataseries."

      The SetStopLoss() will execute the stoploss trade in the middle of a bar at the stoploss value correctly in backtesting (see attached screenshot) but if I catch a custom stop loss in OnBarUpdate and ExitShort(), for instance, the short will not be called at the custom stop loss price, but at on bar close price. I'm trying to figure out how to exit the position at the price in which the custom stop loss was initially triggered. Is that possible? It works seamlessly with the built in function.

      Comment


        #4
        Hello chasebank,

        More information on historical fill processing can be found here - https://ninjatrader.com/support/help...ical_fill_.htm

        Since historical processing forces Calculate.OnBarClose processing unless Tick Replay is used, calling ExitShort() in OnBarUpdate will occur when the bar closes. the Exit is a market order so it will fill at the current market price on that bar's closure.

        You could consider adding a single tick data series and when the single tick data series is processing in OnBarUpdate, you can call ExitShort to exit your position when Close[0] is the price you want to exit.

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

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hmmm. It seems to work but the backtests take drastically longer and doesn't seem realistic. Is the only way to force an order at a particular price through the unmanaged order methods? I'd like to utilize the managed approach. I'm just trying to get more accurate backtests.

          Any chance you have the actual function of SetStopLoss() or SetProfitTarget()? I can just rebuild them as necessary. I can't seem to find the actual code of the function anywhere.

          Comment


            #6
            Hello chasebank,

            SetStopLoss ans SetProfitTarget are internal and the source code is not available.

            Let's approach this thinking about what each of these methods does.

            SetStopLoss will prep NinjaTrader so when an entry order fills, a stop market order is placed to protect the position. If SetProfitTarget is used, a limit order will be submitted for a profit target order as well. These orders are with an OCO ID.

            SampleOnOrderUpdate uses ExitLongStopMarket and ExitLongLimit in OnExecutionUpdate to submit the stop market order and limit order which are uses as stop loss and profit target orders The only real difference between doing this and using the Set methods is that the Set methods provide an OCO ID, when the Exit orders will cancel following Managed Approach internal order handling. (If a strategy using Exit methods for target/stop goes offline then the strategy will not be able to cancel the other order if one fills. Using Set methods, the broker's order routing server will cancel the order if one fills since they are tied with an OCO ID.

            If you want to include OCO ID's and provide the same functionality as the Set methods, you can take the same approach as SampleOnOrderUpdate but use the Unmanaged Approach since you will have full control over OCO ID's. I have attached an example Unmanaged strategy that follows this approach.

            Adding a single tick data series and submitting an Exit order from that data series lets you submit an order in between the primary data series bars. Remember, with backtesting we just work with the OHLCV values of a data series and there is no intrabar granularity. If we want orders to be submitted intrabar, we'll need to add a smaller data series, or use Tick Replay. Tick Replay is significantly more resource intensive than adding a single tick data series. If we want orders to be filled intrabar, we will need to submit them to a smaller data series.

            There is not any difference between the Managed Approach and the Unmanaged Approach as far as how the orders fill with historical processing. If we submit a market order (ExitLong) on a bar close, it will fill at the market price at the time of the submission. See Understanding Historical Fill Processing from post #4 for more information on how orders fill.

            If you want to simply submit a stop market order to exit your position when price action allows the order to be filled, you can use ExitLongStopMarket. If you want that order to fill closer to market dynamics following intrabar movement, you will need to submit that order to a single tick data series.

            Let me know if there is anything else that needs clarification.
            Attached Files
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by PaulMohn, Today, 03:49 AM
            0 responses
            4 views
            0 likes
            Last Post PaulMohn  
            Started by inanazsocial, Today, 01:15 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by rocketman7, Today, 02:12 AM
            0 responses
            10 views
            0 likes
            Last Post rocketman7  
            Started by dustydbayer, Today, 01:59 AM
            0 responses
            2 views
            0 likes
            Last Post dustydbayer  
            Started by trilliantrader, 04-18-2024, 08:16 AM
            5 responses
            23 views
            0 likes
            Last Post trilliantrader  
            Working...
            X