Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Looking SetTrailStop sample codes

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

    Looking SetTrailStop sample codes

    I am writing codes to test SetTrailStop()

    protected override void OnBarUpdate()
    {
    SetProfitTarget("MyLongTrade", CalculationMode.Ticks, 6, false);
    SetTrailStop("MyLongTrade", CalculationMode.Ticks, 10, false);​

    EnterLong("MyLongTrade");

    }

    Enterlog is trigged,,, but I don't see NJ shows PT and TrailStop on chart. Have any sample codes that show to perform SetTrailStop().

    #2
    Hello sysmatrix,

    Thank you for your post.

    Please confirm the strategy is showing with the Enabled checkbox checked on the Strategies tab of the Control Center?

    Are you seeing historical orders on the chart?

    Please note only real-time stops and targets will appear in the Chart Trader. You can prevent historical orders from processing by entering the following code in OnBarUpdate():

    Code:
    if (State == State.Historical)
    
    return;

    Please let me know if you have any other questions.

    Comment


      #3

      attached. This is the first order be placed. Not showing the TP and TSL at all.

      https://forum.ninjatrader.com/forum/...jAx#post802269​ I also refer to previous post. The sample codes also not working as it states. Do I miss anything how to get Trai stop to work?
      Attached Files

      Comment


        #4
        Please take a look at this video I created for you testing out your code and the ProfitChaseStopTrailSetMethodsExample:



        World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.

        Comment


          #5
          Thank you Gaby for the video. I watched and tried it again. My strategy still show just the order itself not TP/ SL. Where possible I need to check the configuration? or could you send me your codes so I can try locally. Thank you !!

          Also, If I use the UI's ATM strategy and use simulate data I am able to see the TSL is tracing the price or adjust the price itself. This is also I don't see behave that way in all sample codes. Why? If you need a video to show you what I see, I can send it to you.

          Comment


            #6
            Do you have Chart Trader open? Is the account selected the same account that is selected in the Strategies window in your strategy settings?
            Make sure the account selected in Chart Trader and the account selected in your strategy settings match.

            Do you see the execution marker for the entry order submitted by the strategy on the chart?​

            Comment


              #7
              HI, Gaby

              Do you have Chart Trader open? "Yes" Is the account selected the same account that is selected in the Strategies window in your strategy settings? "Yes"
              Make sure the account selected in Chart Trader and the account selected in your strategy settings match. "Match"

              I still don't see the same screen like you showed in video.

              I make a simple strategy like you did. here is what I got..

              .
              I enabled TraceOrders, here is the order outputs.
              Enabling NinjaScript strategy 'SetTrailStopTest/306672361' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=True CancelExitsOnStrategyDisable=True Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
              9/20/2023 3:45:01 PM Strategy 'SetTrailStopTest/306672361': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='MyLongTrade' Mode=Ticks Value=6 IsSimulatedStop=False IsMarketIfTouched=False
              9/20/2023 3:45:01 PM Strategy 'SetTrailStopTest/306672361': Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='MylongTrade' Mode=Ticks Value=10 IsSimulatedStop=False IsMarketIfTouched=False
              9/20/2023 3:45:01 PM Strategy 'SetTrailStopTest/306672361': Entered internal SubmitOrderManaged() method at 9/20/2023 3:45:01 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='MylongTrade' FromEntrySignal=''



              here is the portion of codes... which most codes you already have.

              public class SetTrailStopTest : Strategy
              {
              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Enter the description for your new custom Strategy here.";
              Name = "SetTrailStopTest";
              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 = true;
              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)
              {
              }
              }

              protected override void OnBarUpdate()
              {
              //Add your custom strategy logic here.

              if (State == State.Historical) return;

              SetProfitTarget("MyLongTrade", CalculationMode.Ticks, 6, false);
              SetTrailStop("MylongTrade", CalculationMode.Ticks, 10, false);

              EnterLong("MylongTrade");
              }
              }​




              Comment


                #8
                Hello sysmatrix,

                I'm not seeing anything wrong in the code. And as Gaby tested on her end, everything appears to be fine.

                This could be an issue in the environment.

                May I have you test on a new chart with no template and no indicators applied?

                May I confirm you are updated to the latest release 8.1.1.7? (Help -> About)

                We haven't confirmed an item from Gaby's post, do you see the execution marker for the entry order submitted by the strategy on the chart?​
                (This would be a teal right-facing triangle for the buy entry order on the submission bar at the price of the order)
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you ChelseaB... I have an older version of NJ (8.0.27.1 64-bit). What is the best way to upgrade NJ? I have many strategies in the system and want to keep them safe.

                  I open a new chart. still the same.

                  Comment


                    #10
                    Hello sysmatrix,

                    You can create a backup before updating if this concerns you. There are some code breaking changes but primarily effect non-documented code.

                    Below I have provided a link to the Backup & Restore section of our help guide.
                    http://ninjatrader.com/support/helpG...p__restore.htm

                    Currently NinjaTrader has released 8.1.1.7.

                    Updates to NinjaTrader will contain the latest security updates and bug fixes, as well as occasional new features and instruments.

                    To update NinjaTrader
                    • Shutdown all programs including NinjaTrader *important
                    • Download NinjaTrader from the link below
                      Click here to download NinjaTrader
                    • Double click the downloaded file to execute it
                    • Follow the on screen instructions
                    • Restart NinjaTrader
                    You can read about changes to this release in the release notes linked below.
                    https://ninjatrader.com/support/help...ease_notes.htm
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Finally I got my NJ upgraded to the latest version.

                      The above codes, I still not able to get the order and TP, TSL three marks showing on the chart. Where I need to check? Do you need a video clip?


                      Here is the outputs.. Look like very is fine like previously.

                      /21/2023 11:06:00 AM CancelAllOrders: BarsInProgress=0
                      9/21/2023 11:07:01 AM Strategy 'SetTrailStopTest/306672364': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='MyLongTrade' Mode=Ticks Value=6 IsSimulatedStop=False IsMarketIfTouched=False
                      9/21/2023 11:07:01 AM Strategy 'SetTrailStopTest/306672364': Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='MylongTrade' Mode=Ticks Value=10 IsSimulatedStop=False IsMarketIfTouched=False
                      9/21/2023 11:07:01 AM Strategy 'SetTrailStopTest/306672364': Entered internal SubmitOrderManaged() method at 9/21/2023 11:07:01 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='MylongTrade' FromEntrySignal=''
                      9/21/2023 11:07:01 AM Strategy 'SetTrailStopTest/306672364': Ignored SubmitOrderManaged() method at 9/21/2023 11:07:01 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='MylongTrade' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
                      Disabling NinjaScript strategy 'SetTrailStopTest/306672364'
                      9/21/2023 11:05:00 AM CancelAllOrders: BarsInProgress=0
                      Enabling NinjaScript strategy 'SetTrailStopTest/306672363' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=True CancelExitsOnStrategyDisable=True Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
                      9/21/2023 11:08:01 AM Strategy 'SetTrailStopTest/306672363': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='MyLongTrade' Mode=Ticks Value=6 IsSimulatedStop=False IsMarketIfTouched=False
                      9/21/2023 11:08:01 AM Strategy 'SetTrailStopTest/306672363': Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='MylongTrade' Mode=Ticks Value=10 IsSimulatedStop=False IsMarketIfTouched=False
                      9/21/2023 11:08:01 AM Strategy 'SetTrailStopTest/306672363': Entered internal SubmitOrderManaged() method at 9/21/2023 11:08:01 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='MylongTrade' FromEntrySignal=''

                      Comment


                        #12
                        If you would please email us at PlatformSupport [AT] NinjaTrader [DOT] com so we can schedule a support call. In your message please include a link to this forum thread and a message 'Case 04059448'.

                        Thank you.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Today, 05:17 AM
                        0 responses
                        43 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        124 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        65 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        42 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        46 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X