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

Trail stop only updating at candle close.

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

    Trail stop only updating at candle close.

    Hello,

    I am trying to have a trail stop that follow the price of the candle and not updating at candle close

    as you can see in the picture entry @ 22104 original stop @ 22074 - the current price of the is 22109 so 5 tick higher and even higher since the high of the candle is higher than the current price - the trail stop should move the same. Click image for larger version

Name:	trail stop not working.JPG
Views:	403
Size:	154.8 KB
ID:	1091864

    I watch the candle move and also was looking at the order flow as well and the stop is getting updated only at close not during

    here the sample code I am using

    Code:
     
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
     public class MyCustomStrategy1 : Strategy
     {
      protected override void OnStateChange()
      {
       if (State == State.SetDefaults)
       {
        Description         = @"Enter the description for your new custom Strategy here.";
        Name          = "MyCustomStrategy1";
        Calculate         = Calculate.OnBarClose;
        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;
       }
       else if (State == State.Configure)
       {
        SetTrailStop("", CalculationMode.Ticks, 30, false);
       }
      }
    
      protected override void OnBarUpdate()
      {
       if (BarsInProgress != 0)
        return;
    
       if (CurrentBars[0] < 1)
        return;
    
        // Set 1
       if (Close[0] > Open[0])
       {
        EnterLong(Convert.ToInt32(DefaultQuantity), "");
       }
    
      }
     }
    }






    #2
    Hello madams212121,

    Thanks for your post.

    The screenshot of your code shows that thee code is being run with Calculate.OnBarClose. In that mode, the trail stop can only move when the bar closes which is what you are reporting and is what would be expected.

    In the help guide link for the SetTrailStop() method you will find this applicable note: "Trail stop orders are modified based on the strategies 'Calculate' settings. In the case of 'Calculate' on bar close, when the bar closes the trail stop order modification will occur using the closing price of the bar as the reference price to apply the trail offset. Subsequently if the open price of the next bar is significantly higher or lower then the current close price then there is a possibility that the calculated trail stop price is now an invalid stop price. This is a risk with modifying any stop order closer to the current market price since any modification above/below the current price would be rejected." Link: https://ninjatrader.com/support/help...ttrailstop.htm

    For the trail stop to execute on a tick by tick basis (IE: for each tick of profit, the trail stop will move the same), the strategy itself would need to run Calculate.OnPriceChange or Calculate.OnEachTick. Reference: https://ninjatrader.com/support/help...?calculate.htm

    Please note that if your strategy runs Calculate.OnPriceChange or Calculate.OnEachTick. it is possible that you will get multiple entries because the entry conditions will remain the same over a number of ticks, each which will cause your code to run and execute the entries. This process happens way faster than the actual transmission of orders and fill of orders that would update the strategy position (meaning entries per direction will not protect against this). You may need to add further logic to your entry conditions to prevent multiple entries in the same bar. An example of this would be to create an int variable and add the entry condition to check that the Current bar is not equal to the variable. In the action section (where the entry order is), you would add the action to store the Current bar number in the int variable, this will limit you to one entry per bar.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      this plateform has been one problem after another. my entire signal is based on the specific candle to close. Can I trail a entry based on entry triggered by another strategy ? is that possible ?

      Comment


        #4
        Hello madams212121,

        Thanks for your reply.

        Sorry to hear of your concern.

        I thought my reply accurately and adequately addressed your question and provided solutions to work.

        Regarding, "Can I trail a entry based on entry triggered by another strategy ? is that possible ?" No, a strategy would not be aware of another strategies entry.

        If you are struggling to create a strategy that meets your needs, an option you have is that we can provide you with a link to 3rd party programmers that are well versed with Ninjascript strategies.
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by SantoshXX, Today, 03:09 AM
        0 responses
        11 views
        0 likes
        Last Post SantoshXX  
        Started by DanielTynera, Today, 01:14 AM
        0 responses
        2 views
        0 likes
        Last Post DanielTynera  
        Started by yertle, 04-18-2024, 08:38 AM
        9 responses
        41 views
        0 likes
        Last Post yertle
        by yertle
         
        Started by techgetgame, Yesterday, 11:42 PM
        0 responses
        12 views
        0 likes
        Last Post techgetgame  
        Started by sephichapdson, Yesterday, 11:36 PM
        0 responses
        2 views
        0 likes
        Last Post sephichapdson  
        Working...
        X