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

Chart button causing issues with data box & sendmail

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

    Chart button causing issues with data box & sendmail

    I have a strategy (code excerpts below) which loads a button onto the chart in NT7. When I click the button, the code gets all my trade data (entry price, stop loss, position size, etc) and places a trade. That all works just fine except I've noticed using the strategy creates bugs elsewhere; specifically two:

    1. The data box (Ctrl-D shortcut) stops working. If I try open the data box either by using the right click menu, the chart toolbar icon or keyboard shortcut, nothing happens. If I have tried to open the data box I can see the option selected in the right click menu, but there is no sign of the window anywhere, not even if I alt-tab through windows.

    2. I have another two strategies that I load in the control center (not on any chart) that work like a signalling service. When the criteria for either of those strategies are met, they write to file (using streamwriter), flag an alert in the alerts window and send an email using the sendmail function. These two strategies will continue to work just fine, except that the sendmail function will stop working (alerts and streamwriter continue to work just fine).

    I had a look in the log file but there was nothing there...the trace file on the other hand had some clues (see attached). Any help in identifying the cause of the problem would be much appreciated.

    Code:
     #region Variables
    //chart button variables
    private System.Windows.Forms.ToolStrip strip = null;
    private System.Windows.Forms.Control[] controls = null;
    private System.Windows.Forms.ToolStripButton btnManual = null;
    private Font boldFont = null;
    private Font regularFont = null;
    private bool buttonsloaded = false;
    #endregion
    
    
    protected override void OnStartUp()
    {
    System.Windows.Forms.Control[] controls = ChartControl.Controls.Find("tsrTool", false);
    
    if (controls.Length > 0)
    {
    ToolStripButton btnTemp = new System.Windows.Forms.ToolStripButton("temp");
    boldFont = new Font("Arial", 8,FontStyle.Bold);
    regularFont = new Font("Arial", 8);
    btnTemp = null;
    
    btnManual = new System.Windows.Forms.ToolStripButton("btnManual");
    //btnManual.Font = boldFont;
    btnManual.Font =regularFont ;
    btnManual.ForeColor = Color.White;
    btnManual.BackColor=Color.Green;
    btnManual.Text = "TRADE_"+strategy;
    
    strip = (System.Windows.Forms.ToolStrip)controls[0];
    strip.Items.Add(btnManual);
    btnManual.Click += btnManual_Click;
    buttonsloaded=true;
    
    } //end if
    
    } //end OnStartUp
    
    public override void Dispose()
    {
    
    if (buttonsloaded==true)
    {
    strip.Items.Remove(btnManual);
    }
    
    
    } //end Dispose
    
    private void btnManual_Click(object sender, EventArgs e)
    {
    
    
    if (btnManual.Text == "TRADE_"+strategy)
    {
    
    //here I do all the calculations to work out my position size and place a trade and it works just fine (i.e. orders have executed as intended in live trading)
    
    } //end if text == trade
    btnManual.Enabled = true;
    
    } //end btnManual_Click

    #2
    24 jan extract.txt

    Comment


      #3
      sorry attachment in second post, had issues trying to upload to original post

      Comment


        #4
        To start with ...
        You should remove btnManual from OnTermination, not OnDispose.

        Rule of thumb:
        Anything done in OnStartUp that must be undone should be undone
        in OnTermination -- these two function calls are paired for exactly
        this purpose. (That is, OnTermination is only called if OnStartUp
        was actually called.)

        Comment


          #5
          Also, anything to do with btnTemp should be removed or
          commented out, since btnTemp is never used in the code.

          Many examples exist which could help you cleanup your
          toolbar code, your code snippets are quite disorganized,
          which could be contributing to your issue. (I'm just saying,
          easy to read and well organized code helps to prevent
          bugs -- it pays to focus on keeping your code neat and
          organized.)

          I personally think the problem might be in your callback
          btnManual_Click -- inside the code you're not showing.

          Do you call TriggerCustomEvent in your callback?

          Just my 2˘.


          Comment


            #6
            Hello newuser,

            For the question about the databox I would suggest to test if that still happens when you have all workspaces closed and only a new empty untitled workspace being used. Please also ensure you have not selected any kind of chart preset. That would help to confirm if some modification you have or something imported is relating to that.

            For the SendMail question, that would generally be expected to work if that is part of your normal OnBarUpdate logic. If you don't see it working in that use case I would suggest to test a new empty script that sends an email once in realtime to check if that works or not. You can also verify if the email service is working in the options menu when setting up the email account, there is a test button.

            If the SendMail is part of a custom event like a button press you will need to use TriggerCustomEvent, that would be needed for any script based variable/method to be in sync when called.



            JesseNinjaTrader Customer Service

            Comment


              #7
              Also, make sure you understand that calls to SendMail will be ignored for historical data.

              That means, for example, backtesting in Strategy Analyzer will never send email, since
              Strategy Analyzer only uses historical data.

              Comment


                #8
                Thanks for all the replies, responding to the various points:

                - I removed Dispose() and use OnTermination() as advised
                - I commented out btnTemp...I didn't actually realize it wasn't being used, which leads to the next point...
                - Thanks for the link to examples. I actually found the toolbar code that I used/posted here elsewhere on the forum. I should've noticed that btnTemp wasn't being used
                - I just copied the code that I posted straight from NT7 and it lost all the formatting. I should've cleaned up the paste so it was easier to read
                - I don't call TriggerCustomEvent anywhere. I did in an earlier version when I was developing but went another way
                - I don't have much in the callback portion of the code, but I was using (and disposing of) streamreader there...
                - Further to advice received I moved the code to dispose of streamreader inside OnTermination
                - I am aware that SendMail will be ignored for historical data; I'm only using it for live trading.

                So after implementing the changes (OnTermination/disposing of resources) that seems to have solved the problem. I have been able to use my button click event and SendMail is still working in my live strategy (as is the data box in charts).

                Thanks again.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bortz, 11-06-2023, 08:04 AM
                47 responses
                1,603 views
                0 likes
                Last Post aligator  
                Started by jaybedreamin, Today, 05:56 PM
                0 responses
                8 views
                0 likes
                Last Post jaybedreamin  
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                18 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                4 views
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                12 views
                0 likes
                Last Post Javierw.ok  
                Working...
                X