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

Reload Script within Program

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

    Reload Script within Program

    I was looking for a way to have my script reload at the beginning of the day and found this post by koganam under Indicator Development.



    I thought this would be exactly what I was looking for, but it does not seem to be working for me when testing in Market Replay. I have a Strategy using daily bars with COBC = true. When I try adding the code in the link, it still does not reload. I have tried with COBC = false, and that does not work either. I am using a multiple bars script, so I don't know if that matters. A couple questions:

    1) Does this only work during RT data and not during Replay?

    2) If it sends an F5 key command to reload, do you still then have to select YES in the window for it to actually reload? When I manually do it, a window pops up asking to verify you want to stop running and reload the script. I don't know if the SendKeys command automatically chooses yes for this so that it would be truly automated.


    My code looks like this:

    #region Variables
    private bool boolAlreadyRefreshed = false;


    protected override void OnBarUpdate ()
    {

    if(BarsInProgress != 0) return;

    if (!this.boolAlreadyRefreshed && !Historical && FirstTickOfBar)
    {
    this.boolAlreadyRefreshed = true;
    System.Windows.Forms.SendKeys.Send("{F5}");
    }

    Code XXX....
    }


    I know that NT does not support this (maybe NT 8?). But considering a multitude of other issues I am having, this seems like the simplest solution to achieve the goal of consistent results. Any insight or recommendations is appreciated, as well as suggestions on other options.

    Thanks,

    Lee

    #2
    I can help you with closing the panel.

    1. Download AutoHotKey (free)
    2. Create a new file with an *.ahk extension - let's call it 'closeninjanag.ahk.
    3. Paste the following into that file and save it:

    Code:
    #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
    ; #Warn  ; Enable warnings to assist with detecting common errors.
    SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
    SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
    
    #Persistent
    SetTimer, ClosePopup, 250
    return
    
    ClosePopup:
    WinClose, Reload NinjaScript
    return
    'Reload NinjaScript' is the name of the popup to be closed. So if yours has a different title then you must change that.

    You can even compile it with AutoHotKey and use that binary on other systems (without installing AutoHotKey).

    Be aware that this script will close ALL Ninja popups, so be careful. If you hear the voice alert you should always check the log to verify what you missed.

    Hope this sorts you out
    Last edited by molecool; 09-26-2015, 01:33 PM.

    Comment


      #3
      Hi molecool,

      Thank you for the response. Just to clarify, this will only close the popup window, correct? Will it close other ninjatrader windows like the output window or Market Analyzer window?

      Thanks,

      Lee

      Comment


        #4
        Hi molecool,

        I downloaded AutoHotKey and created the file (script) incorporating the code you provided with a .ahk extension. I ran the script, and I can see the icon in the lower right taskbar. But when I hit the F5 key to bring up the Reload popup in NT, nothing happens.

        Is this code suppose to close it, or actually select the "Yes" option in the popup to close it? For the Reload to work, the "Yes" button needs to be selected. This is my first experience with AHK, so any advice is welcome.

        Thanks,

        Lee

        Comment


          #5
          First question: It will only close that particular type of popup - remember it's title specific.

          Second question: Yes, I realized that this could be a problem. The key lies in writing the proper script to select either Yes or No. The current script expects only an okay response. Shouldn't be too hard - just dig around the AHK documentation. 20 seconds of googling revealed this:

          Autoclick "Yes" in confirmation message box - posted in Ask for Help: I have an application that pops up a Do you want to close this window? message box when I exit the application. Of course I want to close it. I want to set up AHK to auto-click the Yes button in this message box, but only this message box. I found a thread (http://www.autohotke...7322.html opens) that discusses something very similar. I used the information from this thread to create the following script: #Pe...


          Just replace the button name with Yes and you're good to go
          Last edited by molecool; 09-27-2015, 05:08 AM.

          Comment


            #6
            Hi Guys,

            So I'm still looking for some help on Reloading the script with code. I started with this from the following post:




            #region Variables
            private bool boolAlreadyRefreshed = false;

            protected override void OnBarUpdate ()
            {

            if(BarsInProgress != 0) return;

            if (!this.boolAlreadyRefreshed && !Historical && FirstTickOfBar)
            {
            this.boolAlreadyRefreshed = true;
            System.Windows.Forms.SendKeys.Send("{F5}");
            }

            But this did not reload the script during replay. If I remove the !Historical, then the script will reload, but goes into a loop where it keeps asking to reload until you specify No. So I changed it to the following in attempt to fix this loop.

            protected override void OnBarUpdate ()
            {

            if(BarsInProgress != 0) return;

            if (Time[0].DayOfWeek != Time[1].DayOfWeek && FirstTickOfBar)
            {
            this.boolAlreadyRefreshed = false;
            }

            if (!this.boolAlreadyRefreshed && FirstTickOfBar)
            {
            this.boolAlreadyRefreshed = true;
            System.Windows.Forms.SendKeys.Send("{F5}");
            }

            But once again, the script will reload at the first tick of bar, but goes into a loop continually asking to reload.

            So what am I doing wrong? Why does it not reload at all when !Historical is included? According to the post I previously listed, this is the way to reload the script using code. But it seems like it's ignoring the bool that should stop it from reloading.

            Any help or suggestions are greatly appreciated.

            Thanks again,

            Lee

            Comment


              #7
              Hello Lee,

              Since you are using FirstTickOfBar could you please confirm that you are also using CalculateOnBarClose = false in your Initialize method per: http://ninjatrader.com/support/helpG...firsttickofbar

              Thank you in advance.
              Michael M.NinjaTrader Quality Assurance

              Comment


                #8
                Hi Mike,

                Thanks for the response. I'm using COBC = True with FirstTickOfBar. So that could be the problem. If I remove FirstTickOfBar, should it then work with COBC = True using daily bars? I believe I tried that and it still did not work...

                I will give it a try with COBC = False and FirstTickOfBar and see what happens.

                Thanks again,

                Lee

                Comment


                  #9
                  Hello Lee,

                  Could you please confirm that this issue is specific to the market replay connection? I would also like to confirm whether your script is a multi instrument script. If so, could you please make sure that you have market replay for all the instruments being loaded?

                  Thank you in advance.
                  Michael M.NinjaTrader Quality Assurance

                  Comment


                    #10
                    Try using SendWait() instead of Send().

                    Comment


                      #11
                      Hi Mike,

                      Yes, my script is a multi instrument script. So I made sure I had all the instruments loaded (I loaded them on a separate chart, as well as the same chart.) and had COBC = false. After testing in replay, I got it to work a few times, but it was inconsistent. I tried many variations, including different time periods, closing all other windows, refresh before starting replay, etc... I could not find any rhyme or reason behind why it worked sometimes and not others.

                      I planned to test it this morning during real time, but not surprising, I had a different issue. So I will have to give the real time test another try tomorrow. I can't say it is specific to replay at this time, but for me, it was inconsistent at best.

                      koganam,

                      Thank you for the suggestion, I will give that a try tonight as well

                      Thanks again,

                      Lee
                      Last edited by lee612801; 09-30-2015, 10:00 AM.

                      Comment


                        #12
                        Hello Lee,

                        Please let me know if the issue persists after changing to SendWait as using Send has been known to cause issues in the past.

                        Thank you in advance.
                        Michael M.NinjaTrader Quality Assurance

                        Comment


                          #13
                          Hi Guys,

                          So the SendWait worked great! I ran it in both Replay and tested it this morning during RT, and it worked as advertised.

                          So here is a summary of what I have for anyone interested:

                          COBC = false

                          #region Variables
                          private bool boolAlreadyRefreshed = false;

                          protected override void OnBarUpdate ()
                          {

                          if (Time[0].DayOfWeek != Time[1].DayOfWeek && FirstTickOfBar)
                          {
                          this.boolAlreadyRefreshed = false;
                          }

                          if (!this.boolAlreadyRefreshed && FirstTickOfBar)
                          {
                          this.boolAlreadyRefreshed = true;
                          System.Windows.Forms.SendKeys.SendWait("{F5}");
                          }


                          And the AutoHotKey code to close the window:

                          WinWait, Reload NinjaScript
                          ControlClick, &Yes, Reload NinjaScript

                          This will close the popup one time then exit the script.

                          If you want the AHK script to run continuously, add the Loop command.

                          Loop
                          {
                          WinWait, Reload NinjaScript
                          ControlClick, &Yes, Reload NinjaScript
                          }

                          Thanks again guys!

                          Lee
                          Last edited by lee612801; 10-01-2015, 03:15 PM.

                          Comment


                            #14
                            Nice work

                            Glad you figured it all out - quite a hack! :-)

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by yertle, Yesterday, 08:38 AM
                            7 responses
                            28 views
                            0 likes
                            Last Post yertle
                            by yertle
                             
                            Started by bmartz, 03-12-2024, 06:12 AM
                            2 responses
                            21 views
                            0 likes
                            Last Post bmartz
                            by bmartz
                             
                            Started by funk10101, Today, 12:02 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post funk10101  
                            Started by gravdigaz6, Yesterday, 11:40 PM
                            1 response
                            9 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by MarianApalaghiei, Yesterday, 10:49 PM
                            3 responses
                            11 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Working...
                            X