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:	44
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.

        https://ninjatrader.com/support/help...ub=usercontrol

        https://ninjatrader.com/support/foru...t=1#post499327
        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 Net_Trader, 03-15-2023, 03:17 PM
            16 responses
            178 views
            0 likes
            Last Post Net_Trader  
            Started by Vern10, Today, 10:51 AM
            4 responses
            30 views
            0 likes
            Last Post Vern10
            by Vern10
             
            Started by HalTech, Today, 02:11 PM
            2 responses
            36 views
            0 likes
            Last Post HalTech
            by HalTech
             
            Started by Gianpiero, Today, 01:41 PM
            1 response
            15 views
            0 likes
            Last Post NinjaTrader_Ray  
            Started by josh18955, Today, 11:16 AM
            1 response
            19 views
            0 likes
            Last Post Leeroy_Jenkins  
            Working...
            X