Custom toolbar button in LFPinBarDetector

CtrlAltDaveLargeI have always wanted to fire rays out of my fingertips.

Len Wein

Those adept at financial modelling may be able to implement Black-Scholes in under a single cup of coffee. Personally, I would just Google for it, while drinking tea. But those same people may struggle at creating a truly useful system since the majority of the effort in a technical indicator may not actually be finance related. For example, the majority of the code within the Pin Bar indicator handles the internal data structure to store details about all previously known Pin Bars and cycle through displaying them yet the trickiest maths is comparing the lengths of nose, body and tails of bars. In this case, the hard part is not the finance, but the creation of an easy to use yet powerful system as a whole. Naturally, there are plenty of cases where the reverse is true but the Exotic and Structured teams got axed before I could ask them.

Sometimes, it is really useful to add a button to the toolbar which is quite possible to do in NinjaTrader using the standard Windows Forms. First add the required using directive,

using System.Windows.Forms;

 

Then we need a few member variables,

private ToolStrip toolStrip = null;

private ToolStripSeparator toolStripSeparator = null;

private ToolStripButton toolStripButton = null;

 

And to configure the button, you will need to call this function at some point. As an added nice feature, it will only add the button once, even if you run multiple instances of the same indicator.

 

protected void setupButton()

{

System.Windows.Forms.Control[] parentControl = ChartControl.Controls.Find( “tsrTool”, false );

if( parentControl.Length > 0 )

{

toolStrip = (ToolStrip)parentControl[0];

bool alreadyExists = false;

for( int index = 0; index < toolStrip.Items.Count; index++ )

{

if( toolStrip.Items[index].Text == “Print Money” )

{

alreadyExists = true;

break;

}

}

if( !alreadyExists )

{

toolStripSeparator = new ToolStripSeparator();

toolStrip.Items.Add( toolStripSeparator );

toolStripButton = new ToolStripButton( “printMoney” );

toolStripButton.Text = “Print Money”;

toolStripButton.Click += onButtonPrintMoney;

toolStrip.Items.Add( toolStripButton );

}

}

}

 

private void onButtonPrintMoney( object sender, EventArgs e )

{

MessageBox.Show( “The printer is low on ink, please replace ink cartridge” );

}

The button will call the named function for your own custom logic. In this example, it will popup a simple dialog box reminding the user that printers are useless, except for Governments.

 

It is wise practice to clean up any resources which are no longer used though generally sadly forgotten because it does not affect the all important chart. Well, until your computer dies in a fireball under the extra strain. 

public void Dispose()

{

if( toolStrip != null )

{

toolStrip.Items.Remove( toolStripSeparator );

toolStrip.Items.Remove( toolStripButton );

}

toolStrip = null;

toolStripSeparator = null;

}

 

As for a real world example, the Pin Bar Indicator has several buttons to cycle through the Pin Bars, as well as highlight risk/reward levels.

PinBar CADave