Announcement

Collapse
No announcement yet.

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:
      https://ninjatrader.com/support/help...ofittarget.htm
      https://ninjatrader.com/support/help...etstoploss.htm
      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("First"+CurrentBar.ToString(), CalculationMode.Ticks, 100);
          SetStopLoss("First"+CurrentBar.ToString(), CalculationMode.Ticks, 25, false);
          EnterLong(1, ("First"+CurrentBar.ToString());
          
          SetProfitTarget("Second"+CurrentBar.ToString(), CalculationMode.Ticks, 120);
          SetStopLoss("Second"+CurrentBar.ToString(), CalculationMode.Ticks, 15, false);
          EnterLong(1, ("Second"+CurrentBar.ToString());
          .
          .
          }
          "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 iseseora, Yesterday, 11:55 PM
            0 responses
            1 view
            0 likes
            Last Post iseseora  
            Started by dorotwart, Yesterday, 11:43 PM
            0 responses
            3 views
            0 likes
            Last Post dorotwart  
            Started by MatthewLesko, Yesterday, 11:13 PM
            0 responses
            3 views
            0 likes
            Last Post MatthewLesko  
            Started by Lance El Camino, Yesterday, 04:14 PM
            1 response
            13 views
            0 likes
            Last Post s.kinra
            by s.kinra
             
            Started by jtrading82, Yesterday, 04:56 PM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_ShawnB  
            Working...
            X