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 use trailing stop at strategy builder

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

    #46
    Hi Cormick,
    I'm afraid, you have to take a deeper dive into some basics before coding Limit strategies.
    What Is a Limit Order?
    A limit order is a type of order to purchase or sell an instrument at a specified price or better.
    BUY LIMIT orders will only be executed at the limit price or lower. [in your example: 0 or lower, now 2 or lower, challenging, which instrument do you want to buy at this price point?]
    SELL LIMIT orders, the order will be executed only at the limit price or a higher one. [in your example: 0 or higher, easy, never a show stopper for short].
    I hope this clarifies.
    NT-Roland

    Comment


      #47
      Hi Roland,

      Thanks a lot! Yes that helped.
      I assumed 0 meant at the Bid or lower (for Limit Long) or Ask or higher (for Limit short).
      And 2 meant at Bid + 2 ticks or lower.
      Now I see it means absolute price ($0.00 and $2.00)!
      Is that correct?

      I reviewed the EnterLongLimit documentation page:
      Code:
      EnterLongLimit(int quantity, double limitPrice, string signalName)


      Example:
      Code:
      EnterLongLimit(GetCurrentBid(), "SMA Cross Entry");


      And tested substituting the GetCurrentBid() method to the 0/2.

      Now it's working!
      The working code:

      Code:
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class MultiStepBreakeven : Strategy
      {
      private int StopLossModeLong;
      private int StopLossModeShort;
      
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "MultiStepBreakeven";
      Calculate = Calculate.OnEachTick;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      StopLossModeLong = 0;
      StopLossModeShort = 0;
      }
      else if (State == State.Configure)
      {
      }
      }
      
      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;
      
      // Set 1
      if ((Position.MarketPosition == MarketPosition.Flat)
      && (Close[0] > Open[0]))
      {
      EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), "");
      StopLossModeLong = 0;
      }
      
      if (CurrentBars[0] < 1)
      return;
      
      
      // Set 2
      if ((Position.MarketPosition == MarketPosition.Long)
      && (StopLossModeLong == 0))
      {
      ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), (Position.AveragePrice + (-10 * TickSize)) , "", "");
      }
      
      // Set 3
      if ((Position.MarketPosition == MarketPosition.Long)
      && (Close[0] >= (Position.AveragePrice + (10 * TickSize)) )
      && (StopLossModeLong == 0))
      {
      StopLossModeLong = 1;
      }
      
      // Set 4
      if ((Position.MarketPosition == MarketPosition.Long)
      && (StopLossModeLong == 1))
      {
      ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), Position.AveragePrice, "", "");
      }
      
      // Set 5
      if ((Position.MarketPosition == MarketPosition.Long)
      && (Close[0] >= (Position.AveragePrice + (20 * TickSize)) )
      && (StopLossModeLong == 1))
      {
      StopLossModeLong = 2;
      }
      
      // Set 6
      if ((Position.MarketPosition == MarketPosition.Long)
      && (StopLossModeLong == 2))
      {
      ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), (Position.AveragePrice + (10 * TickSize)) , "", "");
      }
      
      Print(@"Time/Date " + Convert.ToString(Times[0][0]) + @" Long Orders = " + Convert.ToString(StopLossModeLong) + @" Price ");
      }
      }
      }


      Thanks for the great help and keen eyes/sense and hand in resolving the matter. You rock!

      Still not sure why it worked with zero for the short code though.
      But then again it worked only partially as there was one order that did not execute the trailing stop.

      Attached the working code .zip.
      Attached Files

      Comment


        #48
        Is there a trailing stop loss feature in the Stops and targets section? I tried to use it setting a user input but it seems to be ignoring that input.


        Click image for larger version

Name:	Trailing stop image.png
Views:	281
Size:	15.9 KB
ID:	1159794

        did I get that wrong?

        Thanks!
        Attached Files

        Comment


          #49
          Hello Holligoly,

          Thanks for your post.

          Your screenshot shows a trailing stop that would be applied to an Entry order with the signal name of 1 using an initial stop value of Profitzz.

          Does your entry order have the signal name of 1? If not then remove the signal name of 1 from the trailing stop in order for it to function with each entry.

          Signal names are useful when you want to apply a specific profit target or stop to a specific entry when you have multiple entries.

          Paul H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by love2code2trade, 04-17-2024, 01:45 PM
          4 responses
          36 views
          0 likes
          Last Post love2code2trade  
          Started by alifarahani, Today, 09:40 AM
          2 responses
          13 views
          0 likes
          Last Post alifarahani  
          Started by junkone, Today, 11:37 AM
          3 responses
          15 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by pickmyonlineclass, Today, 12:23 PM
          0 responses
          1 view
          0 likes
          Last Post pickmyonlineclass  
          Started by frankthearm, Yesterday, 09:08 AM
          12 responses
          44 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Working...
          X