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 to Manually Pause a Strategy?

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

    How to Manually Pause a Strategy?

    Hi,

    I have a simple strategy to which I would like to add a button on the right like shown in the screen shot. The button is to pause/unpause a strategy. This will allow me to let the strategy run but only enabled when I want it to execute trades. Are there code samples you can share that I can start with?

    Please assist.

    Thanks.


    Click image for larger version

Name:	image.png
Views:	139
Size:	14.1 KB
ID:	1233369​
    ​

    #2
    Hello givemefood,

    I am not aware of anything that I could link to which shows that but really you are just asking about using a bool variable here. A button can toggle a bool variable which your strategy can use in its conditions to know if it can trade or not.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Can you point me to any reference material I can use to add the button? I can add a bool variable within the code based on the action taken on the button. Thanks.

      Comment


        #4
        Hello givemefood,

        You can find an example of adding buttons to charts in the following links.



        Hello All, Moving forward this will be maintained in the help guide reference samples and no longer maintained on the forum. Creating Chart WPF (UI) Modifications from an Indicator - https://ninjatrader.com/support/help...ui)-modifi.htm (https://ninjatrader.com/support/helpGuides/nt8/creating-chart-wpf-(ui)-modifi.htm) I've
        JesseNinjaTrader Customer Service

        Comment


          #5
          With someone's help I got the following code built. But I get many errors. Can you please check what might be wrong with it?


          // Declare a bool variable to control the strategy
          bool strategyPaused = false;

          protected override void OnBarUpdate()
          {
          // Check if the strategy is paused, if it is, exit the method and do not execute any trades
          if (strategyPaused)
          return;

          // Strategy logic goes here
          }

          protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
          {
          // Define the button width and height
          int buttonWidth = 100;
          int buttonHeight = 30;

          // Define the button position on the chart
          int buttonX = chartControl.Bounds.Width - buttonWidth - 10;
          int buttonY = 10;

          // Draw the button on the chart
          if (GUI.Button(new Rect(buttonX, buttonY, buttonWidth, buttonHeight), strategyPaused ? "Resume" : "Pause"))
          {
          // Toggle the strategyPaused variable when the button is clicked
          strategyPaused = !strategyPaused;
          }
          }
          ​

          Comment


            #6
            Hello givemefood,

            What errors are you seeing?

            From the code you provided that is not valid so that would not compile. OnRender is also not where you would add buttons.

            Did you try the help guide sample that I previously linked which adds buttons?

            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by sidlercom80, 10-28-2023, 08:49 AM
            170 responses
            2,273 views
            0 likes
            Last Post sidlercom80  
            Started by Irukandji, Yesterday, 02:53 AM
            2 responses
            17 views
            0 likes
            Last Post Irukandji  
            Started by adeelshahzad, Today, 03:54 AM
            0 responses
            3 views
            0 likes
            Last Post adeelshahzad  
            Started by CortexZenUSA, Today, 12:53 AM
            0 responses
            3 views
            0 likes
            Last Post CortexZenUSA  
            Started by CortexZenUSA, Today, 12:46 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Working...
            X