Instantly share code, notes, and snippets.
Last active
April 4, 2018 19:22
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save a-stankevich/7d87bf0267bb50cb7df187019057902c to your computer and use it in GitHub Desktop.
Strategy with custom toolbar button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source: https://ninjatrader.com/support/forum/showthread.php?t=23279 | |
// Here's code that will add buttons you can use to place orders - so, it has to be in a strategy. You can add similar code to an indicator, but you won't be able to place orders from that code. I found the start of this on another site - I'll have to find the link to give credit where credit is due. | |
// Please note - I found that in the button click events, the standard NT Open[0], etc. were not available. I had to cache values by updating private variables in OnBarUpdate() that I wanted to reference in the button click event code. | |
// I stripped out my own logic from the code in notepad. You may find a line of code or two that needs tweaking to get it to compile. | |
#region Using declarations | |
using System; | |
using System.Collections; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Drawing.Drawing2D; | |
using System.IO; | |
using System.Windows.Forms; | |
using System.Xml.Serialization; | |
using NinjaTrader.Cbi; | |
using NinjaTrader.Data; | |
using NinjaTrader.Gui.Chart; | |
using NinjaTrader.Indicator; | |
using NinjaTrader.Strategy; | |
#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) | |
#endregion | |
private System.Windows.Forms.ToolStrip strip = null; | |
private System.Windows.Forms.ToolStripButton btnLong = null; | |
bool isInitialized = false; | |
private string defaultATM = "My favorite ATM"; | |
private int defaultQuantity = 1; | |
private string atmStrategyId = null; | |
private double open_0; | |
private double open_1; | |
private double open_2; | |
private double high_0; | |
private double high_1; | |
private double high_2; | |
private double low_0; | |
private double low_1; | |
private double low_2; | |
private double close_0; | |
private double close_1; | |
private double close_2; | |
private DateTime date_time_0; | |
private DateTime date_time_1; | |
private DateTime date_time_2; | |
private string symbol = null; | |
private string chartInstrument = null; | |
private string intervalType = null; | |
private Period period = null; | |
private PeriodType periodType = 0; | |
private int intervalValue = 0; | |
private int numTicksPerPoint = 0; | |
private Font boldFont = null; | |
private Font regularFont = null; | |
/// <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; | |
} | |
public override void Dispose () { | |
if (isInitialized) { | |
isInitialized = false; | |
strip.Items.Remove (btnLong); | |
} | |
} | |
private void btnLong_Click (object sender, EventArgs e) { | |
// -- if going long, the current or immediately prior | |
// bar low must be l... | |
// -- note, have to make sure is range chart, know range, etc. | |
switch (Position.MarketPosition) { | |
case MarketPosition.Flat: | |
log ("Currently flat"); | |
break; | |
case MarketPosition.Long: | |
log ("Currently long"); | |
break; | |
case MarketPosition.Short: | |
log ("Currently short"); | |
break; | |
} | |
logBarInfo ("long"); | |
if (Position.MarketPosition != MarketPosition.Flat) { | |
log ("Position currently open - not placing new order."); | |
return; | |
} | |
btnLong.Font = regularFont; | |
btnLong.Enabled = false; | |
bool placeBuyOrder = true; | |
if (!placeBuyOrder) { | |
string message = "Order not placed for reason(s): "; | |
log (message); | |
btnLong.Font = boldFont; | |
btnLong.Enabled = true; | |
return; | |
} | |
// -- first number, limit, second number, stop | |
atmStrategyId = GetAtmStrategyUniqueId (); | |
bool atmResult = false; | |
atmResult = AtmStrategyCreate (Action.Buy, OrderType.Limit, limitPrice, 0, | |
TimeInForce.Day, entryOrderId, defaultATM, | |
atmStrategyId); | |
// -- log info | |
log ("Preparing buy limit at " + limitPrice + " using atm " + defaultATM); | |
if (!atmResult) log ("Error placing order"); | |
btnLong.Font = boldFont; | |
btnLong.Enabled = true; | |
} | |
/// <summary> | |
/// Called on each bar update event (incoming tick) | |
/// </summary> | |
protected override void OnBarUpdate () { | |
open_0 = Open[0]; | |
open_1 = Open[1]; | |
open_2 = Open[2]; | |
high_0 = High[0]; | |
high_1 = High[1]; | |
high_2 = High[2]; | |
low_0 = Low[0]; | |
low_1 = Low[1]; | |
low_2 = Low[2]; | |
close_0 = Close[0]; | |
close_1 = Close[1]; | |
close_2 = Close[2]; | |
date_time_0 = Time[0]; | |
date_time_1 = Time[1]; | |
date_time_2 = Time[2]; | |
// -- paint buttons | |
if (!isInitialized) { | |
isInitialized = true; | |
symbol = Instrument.MasterInstrument.Name; | |
chartInstrument = Instrument.FullName; | |
period = Bars.Period; | |
periodType = period.Id; | |
intervalValue = period.Value; | |
numTicksPerPoint = Convert.ToInt32 (Convert.ToDouble (1) / TickSize); | |
switch (periodType) { | |
case PeriodType.Minute: | |
intervalType = "M"; | |
break; | |
case PeriodType.Tick: | |
intervalType = "T"; | |
break; | |
case PeriodType.Volume: | |
intervalType = "V"; | |
break; | |
case PeriodType.Day: | |
intervalType = "D"; | |
break; | |
case PeriodType.Range: | |
intervalType = "R"; | |
break; | |
default: | |
intervalType = "UK"; | |
break; | |
} | |
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 (btnTemp.Font, FontStyle.Bold); | |
regularFont = new Font (btnTemp.Font, FontStyle.Regular); | |
btnTemp = null; | |
btnLong = new System.Windows.Forms.ToolStripButton ("btnLong"); | |
btnLong.Font = boldFont; | |
btnLong.ForeColor = Color.Green; | |
btnLong.Text = "Long"; | |
strip = (System.Windows.Forms.ToolStrip) controls[0]; | |
strip.Items.Add (btnLong); | |
btnLong.Click += btnLong_Click; | |
} | |
log ("Initialized ToolBarButtons."); | |
} // -- if (!isInitialized) | |
} | |
private void log (string message) { | |
DirectoryInfo Di = | |
new DirectoryInfo (System.Environment.GetFolderPath (Env ironment.SpecialFolder.MyDocuments)); | |
string FileName = Di.FullName + "\\NinjaTrader 6.5\\" + | |
symbol + "-" + Convert.ToString (intervalValue) + intervalType + "-orderinfo.txt"; | |
// -- Subfolder would be "NinjaTrader 6.5" | |
StreamWriter Writer = new StreamWriter (FileName, true); | |
Writer.WriteLine (Convert.ToString (DateTime.Now) + " " + message); | |
Writer.WriteLine (""); | |
Writer.Close (); | |
} | |
#region Properties | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment