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

Check Trailing Stop status

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

    Check Trailing Stop status

    I have coded a strategy that adds a Trailing Stop to an open position once the position hits a certain profit target (note: the Trailing Stop is added after the position has opened, not before).

    The problem is that my strategy keeps trying to add new Trailing Stops to the position after it adds the first one (which ends up just constantly adjusting the Trailing Stop price of the first Trailing Stop).

    How can I add an additional rule to my strategy that tells it not to add a Trailing Stop to a position if the position already has a Trailing Stop?

    Would really appreciate a code example of how to add this rule. Thanks!
    Last edited by Matheyas5; 01-22-2016, 10:52 PM.

    #2
    FYI:
    I can post the code I have written so far if anyone thinks it will help them answer my question, however I think it would confuse matters more (since the code has other issues I'm still fixing that don't relate to my question)

    Thanks!

    Comment


      #3
      Hello Matheyas5,

      Thank you for your post.

      Try setting a condition that would only place the Trail Stop once. For example:
      Code:
      if (Position.MarketPosition == MarketPosition.Flat)
      reset = true;
      if (Position.MarketPosition == MarketPosition.Long && reset)
      {
      SetTrailStop(...); // your desired trail stop
      reset = false;
      }
      Where is reset is a boolean you define.

      Comment


        #4
        Thanks so much Patrick! I think thats code is just what I was looking for.

        Comment


          #5
          Hi Patrick,

          I ran a market replay with the code below and no "trailing stop" orders were placed (as far as I can tell) or filled.

          Any idea what the issue could be?

          Code:
          protected override void OnMarketData(MarketDataEventArgs e)
          {
                  if (Position.MarketPosition == MarketPosition.Flat)   
                  {
                  allowttp = true;
                  }
                  if (allowttp && Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 100)
                  { 
                    SetTrailStop(CalculationMode.Ticks, GetCurrentAsk() + 1 * TickSize);
                    allowttp = false;
                  }
            }
          Please note:

          1. allowttp is a Bolean
          2. The attached TrailingExample.cs file is a full strategy that contains the code above. This .cs file is what I was using to test the code above. I figure this .cs file could help explain what is going wrong.
          3. I wrote this code in the protected override void OnMarketData(MarketDataEventArgs e) section instead of the protected override void OnBarUpdate() because I want it to be calculated every time new Market Data comes in (instead of once per bar).

          Look forward to your thoughts.
          Thanks!
          Attached Files

          Comment


            #6
            Hello Matheyas5,

            Thank you for your patience.

            Open the Output window under Tools > Output. Then add the following code:
            Code:
            			if (allowttp && Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >= 100)
            			{
            				Print("");
            				Print(Time[0]);
            				Print("Trail Stop Set");
            				SetTrailStop(CalculationMode.Ticks, GetCurrentAsk() + 1 * TickSize);
            		        allowttp = false;
            			}
            Run the strategy, do you see the prints in the Output window?

            Comment


              #7
              Hi Patrick,

              I added that code and ran a new market replay. Here is what happened

              1. The output window (you mentioned) showed the Trailing Stop was set 4 times (as shown in Screenshot 1.jpg).
              2. No exits happened due to the Trailing Stop. They happened due to other exit strategies coded in my strategy. I can tell none of the exits were due to the trailing stop because none of the exits were profitable (and the logic of how I'm using the Trailing Stop in this strategy means any exit it causes should be profitable). See screenshot 2.jpg of "Trades" results for more details.
              3. Attached TrailingExample.zip is the full strategy code I ran, in case you need to take a look at.

              Look forward to your thoughts. Thanks!
              Attached Files

              Comment


                #8
                Thanks for your response.

                The SetTrailStop() is set to a price value instead of a value/number of ticks. So instead of SetTrailStop(CalculationMode.Ticks, GetCurrentAsk() + 1 * TickSize) we would instead use SetTrailStop(CalculationMode.Ticks, 1) as you wish to trail 1 tick behind.

                Now if you want to trail behind the ask you would need to use SetStopLoss() and re-submit it on each tick or bar close with CalculationMode as Price.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by elderan, Yesterday, 08:03 PM
                1 response
                12 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Started by samish18, Yesterday, 08:57 AM
                8 responses
                25 views
                0 likes
                Last Post samish18  
                Started by DJ888, 04-16-2024, 06:09 PM
                3 responses
                10 views
                0 likes
                Last Post NinjaTrader_Erick  
                Started by RookieTrader, Today, 07:41 AM
                0 responses
                3 views
                0 likes
                Last Post RookieTrader  
                Started by maybeimnotrader, Yesterday, 05:46 PM
                1 response
                19 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X