|
The following code allows you to add Chart buttons to NinjaTrader Version 7.0
These buttons can be used to toggle values in a strategy or execute orders when clicked. This is very useful in adding an additional level of control to your strategy. When trading using a strategy you can use the buttons to expose some of the strategy features and toggle them on and off based on market conditions.
#region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Indicator; using NinjaTrader.Gui.Chart; using NinjaTrader.Strategy; using System.IO; using System.Collections; using System.Windows.Forms; #endregion // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { /// <summary> /// /// </summary> [Description("")] public class ToolBarButtons : Strategy { #region Variables // Wizard generated variables // User defined variables (add any user defined variables below)
private System.Windows.Forms.ToolStrip strip = null; private System.Windows.Forms.Control[] controls = null;
private System.Windows.Forms.ToolStripButton btnManual = null; private System.Windows.Forms.ToolStripButton btnEntry = null;
private Font boldFont = null; private Font regularFont = null; private bool buttonsloaded = false; #endregion
/// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { CalculateOnBarClose = false; }
protected override void Initialize() { 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 = "Auto";
strip = (System.Windows.Forms.ToolStrip)controls[0];
strip.Items.Add(btnManual); btnManual.Click += btnManual_Click;
btnEntry = new System.Windows.Forms.ToolStripButton("btnManual"); //btnEntry.Font = boldFont; btnEntry.Font = regularFont; btnEntry.ForeColor = Color.Black; btnEntry.BackColor=Color.Green; btnEntry.Text = "Enter ON";
strip.Items. Add(btnEntry); btnEntry.Click += btnEntry_Click; buttonsloaded=true; }
} public override void Dispose() { if (buttonsloaded==true) { strip.Items.Remove(btnManual); strip.Items.Remove(btnEntry); }
}
private void btnManual_Click(object sender, EventArgs e) { if (btnManual.Text == "Manual") { btnManual.Text = "Auto"; btnManual.Font =regularFont ; btnManual.ForeColor = Color.White; btnManual.BackColor=Color.Green; } else { btnManual.Text = "Manual"; btnManual.Font =boldFont ; btnManual.ForeColor = Color.White; btnManual.BackColor=Color.Red; }
Alert("MjCHD_IB", NinjaTrader.Cbi.Priority.Low, "HD Entry Detected", "Alert1.wav", 4, Color.Black, Color.Yellow); btnManual.Enabled = true; } private void btnEntry_Click(object sender, EventArgs e) { if (btnEntry.Text == "Enter ON") { btnEntry.Text = "Enter OFF"; btnEntry.Font = boldFont ; btnEntry.ForeColor = Color.Black; btnEntry.BackColor=Color.Red; } else { btnEntry.Text = "Enter ON"; btnEntry.Font = regularFont ; btnEntry.ForeColor = Color.Black; btnEntry.BackColor=Color.Green; } btnEntry.Enabled = true;
} protected override void OnBarUpdate() { if (btnEntry.Text == "Enter ON") { btnEntry.Text = "Enter OFF"; btnEntry.Font = boldFont ; btnEntry.ForeColor = Color.Black; btnEntry.BackColor=Color.Red;
/// /// Now Make Your Make Your Entry Here // if (btnManual.Text != "Manual") { // Do Something Here Alert("Fun", NinjaTrader.Cbi.Priority.Low, "Entry Type 1 Detected", "Alert4.wav", 4, Color.Black, Color.Yellow); } else { Alert("Fun", NinjaTrader.Cbi.Priority.Low, "Entry Type 2 Detected", "Alert3.wav", 4, Color.Black, Color.Yellow); } } } #region Properties #endregion } } |