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

Transition from SetStopLoss to SetTrailStop after certain conditions are met??

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

    Transition from SetStopLoss to SetTrailStop after certain conditions are met??

    Hi,

    I would appreciate advice on how to accomplish the following in a strategy for backtesting....

    1) EnterLongLimit @ entryPrice (lets say 100$)
    2) SetStopLoss @ stopLossPrice (lets say -10% which is 90$)
    3) define a trailStopPrice with trigger : trigger=20% (120$) and new stopLossPrice=15% (115$)
    when the price hit (in real time - not in the end of the bar !!!) 120$ then the stopLossPrice will immediately update to 115$.

    Thanks !
    Last edited by orenytz; 12-25-2017, 08:31 AM.

    #2
    Hello,

    Thank you for the post. You will only need to use SetTrailStop to manage the stop. SetStopLoss cannot be used in conjunction with SetTrailStop.

    You would need to set up the trailing stop of 10% in Initialize:

    Code:
     protected override void Initialize()
    {
    
    SetTrailStop(CalculationMode.Percent, 0.1);
    
    }
    Then update the trailing stop in OnBarUpdate() once your profit trigger is hit:

    OnBarUpdate()
    {

    if(Position.GetProfitLoss(Close[0], PerformanceUnit.Percent)*10 >= .20)
    {

    SetTrailStop(CalculationMode.Percent, 0.15);

    }

    }

    GetProfitLoss(Close[0], PerformanceUnit.Percent) will calculate the strategies unrealized PnL in the form of a percent. Pass the close for the market price to calculate off of.

    https://ninjatrader.com/support/help...profitloss.htm -GetProfitLoss

    https://ninjatrader.com/support/help...ttrailstop.htm - SetTrailStop

    Please let us know if we may be of any further assistance.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      intra bar check or close bar check...?

      Thanks Chris.
      just to make sure that the trailing stop operation occurs at intra bar and not in the end of the bar,
      i attached a picture which illustrate my scenario.

      requested behaviour is: the upper tail of the the second bar is hiting the trigger (120$), immidiatly the stop loss updated to 115$ and when it's going down it will exit on stop loss (115$).

      faulty behaviour is: the bar hiting check will occur at the close of the bar (107$) and trailing stop will not occur..

      so, what is the behaviour of the code ?

      thanks again
      Oren
      Attached Files

      Comment


        #4
        Hello Oren,

        Thank you for the reply.

        You would need to set the Strategies CalculateOnBarClose to false in order to calculate on each tick. This will give your strategy more opportunities to check the condition while the bar is developing.

        You can do this from code in the Initialize() method (CalculateOnBarClose = false)

        Or you can set this through the Strategy configuration menu.

        I look forward to hearing of your results.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hello,

          This is along the same line, but how would I go about entering a position with multiple contracts (lets say 3) with a trailing stop. Then when the target to sell one contract is hit, the trail stop updates the amount of contracts form 3 to 2, and moves to a break even position. Then, once target 2 is hit and another contract is sold, the trail stop is again updated and moved to the previous target of the first contract sold?

          Sorry for the confusion. Hopefully I made it as clear as possible.

          Thanks,
          Chris

          Comment


            #6
            Hello,

            Thank you for the post.

            Please review this reference sample on modifying stops and targets:



            If you are having trouble with something particular after reviewing this sample and the documentation I linked in my reply to orenytz, please let me know and I will advise further.
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Barry Milan, Yesterday, 10:35 PM
            5 responses
            16 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by DanielSanMartin, Yesterday, 02:37 PM
            2 responses
            13 views
            0 likes
            Last Post DanielSanMartin  
            Started by DJ888, 04-16-2024, 06:09 PM
            4 responses
            12 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by terofs, Today, 04:18 PM
            0 responses
            11 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by nandhumca, Today, 03:41 PM
            0 responses
            8 views
            0 likes
            Last Post nandhumca  
            Working...
            X