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

ISO Automated Strategy Script code for dynamic StopLoss

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

    ISO Automated Strategy Script code for dynamic StopLoss

    Looking to see if anyone has some code willing to share that uses dynamic stoploss features. Such as Supertrend or Fibonacci pivots. Right now using e.order code to protect profit and loss. But that creates an instant stoploss and profit orders. What I am thinking is code to then adjust the initial stops to follow ie Supertrend pivots. So now I have ExitLongStopMarket(stoploss) and ExitLongLimit(Profit) on initial buy or sell entry. What would be the proper language to use to create an IF statement to then adjust that stop limit?

    #2
    Hello set2win,

    Thank you for your post.

    While I don't have an example of setting stops to Supertrend or Fibonacci pivots, I do have an example of a strategy that modifies its stops based on a multiple of the ATR, which may be of some help as a jumping off point. Please see this older forum post, which has an unlocked example:

    Good afternoon, I would like through the strategy builder to be able to use the trailing stop, I don't know how to use it so that my stop decreases when I have advanced a certain number of ticks? Thanks


    What you'd use for your if statement to control when the stop/target prices update would be dependent on when you want them to update - so for example, you could check that the current price is 5 ticks above the average entry price and then modify the stop/target.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Yes, I am not necessarily looking for the condition code as in Supertrend or Fibonacci I am making sure I use the right code to remove the existing stoploss that was placed when order entry created and now use a new stop. I always want to cover myself in case computer breakdown or internet interruption so it needs to place the initial stop. So the code you referred to in the linked post I don't think it will do that. It will exit the trade and what I want is to put a new stoploss order.
      Here is what I got now.

      if( e.Order.OrderAction == OrderAction.Buy )
      {
      // Set stop loss
      double stopOrderPrice = e.Order.AverageFillPrice - StopLoss * TickSize;
      ExitLongStopMarket( 0, true, 3, stopOrderPrice, "Stop loss", e.Order.FromEntrySignal );

      // Set profit target 1
      double profitTarget = e.Order.AverageFillPrice + ProfitTarget * TickSize;;
      ExitLongLimit( 0, true, 1, profitTarget, "Profit target", e.Order.FromEntrySignal );
      }

      That places the initial stoploss.

      But as the trade progresses I want to change the existing stoploss to a new value based on conditions.
      Hope this makes it clearer.

      Comment


        #4
        Hello set2win,

        Thank you for your reply.

        You should be able to just call whichever ExitLongLimit() or ExitLongStopMarket() again with the same Signal name and the revised price and that will update the order. You could do that within OnBarUpdate() as necessary to make changes to the price the stop or target is placed at.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Thanks, Kate. I guess that answered my question whether the program could override an existing stoploss order. I'll give it a try.

          Comment


            #6
            Originally posted by NinjaTrader_Kate View Post
            Hello set2win,

            Thank you for your reply.

            You should be able to just call whichever ExitLongLimit() or ExitLongStopMarket() again with the same Signal name and the revised price and that will update the order. You could do that within OnBarUpdate() as necessary to make changes to the price the stop or target is placed at.

            Please let us know if we may be of further assistance to you.
            in the onbarupdate()

            I tried this method with no update to existing stopLoss
            // Dynamic StopLoss
            if (Close[0] > Close[1])
            {

            double stopOrderPriceNew = Low[0] - StopLoss * TickSize;
            Print("New Stop Price: "+ stopOrderPriceNew );
            ExitLongStopMarket(0,true,1,stopOrderPriceNew, "Stop loss", "Stop loss");

            }

            Output shows stoprice updating but it does not replace the existing stoploss order

            Comment


              #7
              Hello set2win,

              Thank you for your note.

              Could you provide the original line where you're setting the StopLoss? Right now, you have this one with the same "From Entry Signal" name as the signal name for this order - these values need to exactly match the original order when it was submitted for the order to be updated.

              Thanks in advance; I look forward to assisting you further.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Hi,
                the code I posted in Reply #3 of this posting

                Comment


                  #9
                  Hello set2win,

                  Thank you for your reply.

                  I don't see that you're not saving the entry signal name to a variable so you can reuse it, and from your sample code I can't tell what the signal name for the entry might be since you're just using e.Order.FromEntrySignal. Here's an example from our help guide that goes over both initially submitting the stop loss and moving the stop to a different value:



                  Please let us know if we may be of further assistance to you.
                  Kate W.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks, Kate. I believe this example has the information to handle the orders.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by kujista, Today, 06:23 AM
                    4 responses
                    14 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by traderqz, Yesterday, 09:06 AM
                    2 responses
                    15 views
                    0 likes
                    Last Post traderqz  
                    Started by traderqz, Today, 12:06 AM
                    3 responses
                    6 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by RideMe, 04-07-2024, 04:54 PM
                    5 responses
                    28 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by f.saeidi, Today, 08:13 AM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Working...
                    X