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

How to implement SL/TP in NT8

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

    How to implement SL/TP in NT8

    I am using NT8, 8.0.21.1 64-bit. This is my first time to implement strategy on NT platform. I would like to create something with SL/TP such as SuperDOM -> ATM Strategy -> Custom. Shown in the picture.


    I have searched for some resources from web and this forum.
    - Order types, https://ninjatrader.com/blog/3-basic...pes-explained/
    - Internal Order Handling Rules that Reduce Unwanted Positions, https://ninjatrader.com/support/helpGuides/nt8/

    Here is my approach:
    HTML Code:
    procted override void OpBarUpdate()
    {
        if (conditionOkay)
        {
            EnterLong(1);
            ExitLongLimit(1, GetCurrentBid() + TickSize * 100); // TP location
            ExitLongStopMarket(1, GetCurrentBid() - TickSize * 30); // SL location
        }
    }
    I am doing backtest with tick data replay, on AUDUSD. From the Strategy Analyzer -> Display (Orders), I can see only 2 action is done, the first is the buy action, the second is the sell action due to the test is ended but not because of the ExitLongStopmarket() or ExitLongLimit().
    I checked the log file also, and find no error message.

    I would like to ask how to implement the SL/TP order in NT8?

    Thank you very much.

    #2
    Here is my second try:
    HTML Code:
    procted override void OnPositionUpdate(...)
    {
        if (conditionOkay)
        {
            ExitLongStopMarket(1, GetCurrentBid() - TickSize * 30); // SL location
        }
    }
    Then in the Order report I see the sell order is cancelled. Shown in pictures

    Comment


      #3
      Hello Sulfred,

      Thanks for your posts and welcome to the forums!

      You can use the SetProfitTarget and SetStopLoss to accomplish your goals. These "set" methods are OCO tied.

      There are a couple of ways these can be used.

      If you want to have the same number of ticks profit/stop on every trade, like your ATM, then you need only code them once into State.Configure as the example show on these help guide pages:


      These methods automatically set correct based on the direction of the entry as well as the price of the entry.

      If you want to change the stop and profit target on every trade then you can use them in OnBarUpdate, however you absolutely must set them before placing the entry order. The reason for this is once you set the values the methods retain the value and are automatically deployed on each entry so it would be critical to set their values first. (this is discussed on each of the links in the "Tips" section.

      Please note from the help guide on both methods, "Should you have multiple Bars objects of the same instrument while using SetProfitTarget() in your strategy, you should only submit orders for this instrument to the first Bars context of that instrument. This is to ensure your order logic is processed correctly and any necessary order amendments are done properly."
      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Thank you so much for your quick response. Here is a follow up question.

        Q: Is that what I should do if I have different TP/SL per every trade orders?
        Example of multiple trade order with different TP/SL:

        HTML Code:
        procted override void OpBarUpdate()
        {
               if (conditionOkay)
              {
                SetProfitTarget(CalculationMode.Price, GetCurrentBid() + TickSize * 100); // TP location
                SetStopLoss(CalculationMode.Price, GetCurrentBid() - TickSize * 30); // SL location
                EnterLong(1);
        
                 SetProfitTarget(CalculationMode.Price, GetCurrentBid() + TickSize * 200); // TP location
                SetStopLoss(CalculationMode.Price, GetCurrentBid() - TickSize * 30); // SL location
                EnterLong(1);
            }
        }

        Q: Is it possible to change the TP/SL for the second position once the order is activated? For example: after the first trade order is TP.
        Q: I will connect my NT8 to IB for live trading. If those trade orders are placed and then the connection of NT8 and IB is closed or even the connection between IB gateway and IB server is closed, will those TP/SL still be activated and do what they should do?

        Thank you very much.

        Comment


          #5
          Hello Sulfred,

          Thanks for your reply.

          In your example, I would suggest tieing specific entries to specific stops and targets through the use of unique signal names, for example:

          Code:
          if (your entry conditions are true)
          {
          SetProfitTarget([B]"First"+CurrentBar.ToString()[/B], CalculationMode.Ticks, 100);
          SetStopLoss([B]"First"+CurrentBar.ToString()[/B], CalculationMode.Ticks, 25, false);
          EnterLong(1, ([B]"First"+CurrentBar.ToString()[/B]);
          
          SetProfitTarget([B]"Second"+CurrentBar.ToString()[/B], CalculationMode.Ticks, 120);
          SetStopLoss([B]"Second"+CurrentBar.ToString()[/B], CalculationMode.Ticks, 15, false);
          EnterLong(1, ([B]"Second"+CurrentBar.ToString()[/B]);
          .
          .
          }
          "Q: Is it possible to change the TP/SL for the second position once the order is activated? For example: after the first trade order is TP." Yes, however in that case you may want to store the unique signal name in a string variable to use after the initial entry when you want to adjust the SL/PT as you will need to use that same signal name from the entry. In my example i am using the current bar number to create a unique signal name but if you adjust after that bar then CurrentBar would have a different value.

          "Q: I will connect my NT8 to IB for live trading. If those trade orders are placed and then the connection of NT8 and IB is closed or even the connection between IB gateway and IB server is closed, will those TP/SL still be activated and do what they should do?". Anytime you lose connection it is always a good practice to call your brokers trade desk to verify position/order status and have them take any appropriate action. Please see this link which shows where you orders reside: https://ninjatrader.com/support/help...rs_reside_.htm
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Thank you very much.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by TradeForge, 04-19-2024, 02:09 AM
            2 responses
            28 views
            0 likes
            Last Post TradeForge  
            Started by aprilfool, 12-03-2022, 03:01 PM
            3 responses
            327 views
            0 likes
            Last Post NinjaTrader_Adrian  
            Started by giulyko00, Today, 12:03 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by f.saeidi, Today, 12:14 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by AnnBarnes, Today, 12:17 PM
            1 response
            2 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Working...
            X