You could always test for existence of the form if it has been already created and then hook it...
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!
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
NinjaTrader
Using windows Form instead of file io
Collapse
X
-
form test for existence
You could always test for existence of the form if it has been already created and then hook it...
-
object spy not needed use win32 api
you can get the window hanlde by its text..
Comment
-
buttons on chart
It is a rather old topic, which I just bumped into.
If you guys only need buttons on the chart, here is my workspace changer indicator. I made four nonfancy but clickable buttons on the chart. Maybe you can benefit from it....Attached Files
Comment
-
Nice buttons
Here is another example on how to create a button in ToolStrip of a chart window. Don't pay attention to those resources stuff as I just wanted to read resx file from Visual Studio.
Main idea is just get toolstrip control and add button as it is simply done in Visual Studio.
Pay special attention to call Dispose and remove button when it is disposing.
This is just a button and click handler to switch bool flag ( CanTrade or similar ). You may want to add other controls to toolstrip, just be careful and test as NT guys told us: It is dangerous.
PHP Code:#region Using declarations
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Resources;
using System.Collections;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
using System.Windows.Forms;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// TestStrat
/// </summary>
[Description("StrategyEx")]
public abstract class StrategyEx : Strategy
{
protected bool Initialized = false;
private ToolStripButton tsbtnStop = null; // Play/Stop Button to prevent Strategy from submitting orders
private ToolStripSeparator tsSeparator = null;
private bool bCanTrade = false;
protected string strStrategyResXPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\NinjaTrader 7\\bin\\Custom\\Strategy\\Strategy.resx";
/// <summary>
/// Protect from being instantiated.
/// </summary>
protected StrategyEx()
{}
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
}
/// <summary>
/// overridden Dispose for disposing controls
/// </summary>
public override void Dispose()
{
if(Initialized)
{
if( ChartControl != null )
{
ToolStrip toolStrip = (ToolStrip) ChartControl.Controls["tsrTool"];
if( toolStrip != null )
{
//
// Dispose separator
//
if( tsSeparator != null )
{
toolStrip.Items.Remove(tsSeparator);
}
//
// Dispose Stop button
//
if( tsbtnStop != null )
{
tsbtnStop.Click -= new EventHandler(tsbtnStop_Click);
toolStrip.Items.Remove(tsbtnStop);
}
}
}
}
}
/// <summary>
/// This method is used to configure the strategy on first OnBarUpdate
/// </summary>
protected virtual void OnInitializeBeforeStart()
{
CreateToolStripButtons();
}
///
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if( !Initialized )
{
OnInitializeBeforeStart();
Initialized = true;
}
}
protected void CreateToolStripButtons()
{
if( ChartControl != null )
{
ToolStrip toolStrip = (ToolStrip) ChartControl.Controls["tsrTool"];
if( toolStrip != null )
{
//
// Create Separator
//
ToolStripSeparator tsSeparator = new ToolStripSeparator();
tsSeparator.Name = "tsSeparator";
toolStrip.Items.Add(tsSeparator);
//
// Create Stop Button
//
tsbtnStop = new ToolStripButton();
tsbtnStop.ImageTransparentColor = Color.Magenta;
tsbtnStop.Name = "tsbtnStop";
tsbtnStop.ImageTransparentColor = System.Drawing.Color.Magenta;
Image imgBtn = GetImageFromResX( strStrategyResXPath, bCanTrade ? "tsbtnStop.Image" : "tsbtnPlay.Image");
if( imgBtn != null )
{
tsbtnStop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
tsbtnStop.Image = imgBtn;
}
else
{
tsbtnStop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
}
tsbtnStop.Text = bCanTrade ? "Disable trading" : "Enable trading";
tsbtnStop.Click += new EventHandler(tsbtnStop_Click);
toolStrip.Items.Add(tsbtnStop);
//
// Create another Button
//
}
}
}
private void tsbtnStop_Click(object sender, EventArgs e)
{
bCanTrade = !bCanTrade;
Image imgBtn = GetImageFromResX( strStrategyResXPath, bCanTrade ? "tsbtnStop.Image" : "tsbtnPlay.Image");
if( imgBtn != null )
{
tsbtnStop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
tsbtnStop.Image = imgBtn;
}
else
{
tsbtnStop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
}
tsbtnStop.Text = bCanTrade ? "Disable trading" : "Enable trading";
}
static public Image GetImageFromResX(string resXPath, string imageResName)
{
// Create a ResXResourceReader for the resx file
ResXResourceReader rsxr = new ResXResourceReader(resXPath);
// Create an IDictionaryEnumerator to iterate through the resources.
IDictionaryEnumerator id = rsxr.GetEnumerator();
// Iterate through the resources and display the contents to the console.
foreach (DictionaryEntry d in rsxr)
{
if (d.Key.ToString() == imageResName)
{
//Close the reader.
rsxr.Close();
return (Image)d.Value;
}
}
//Close the reader.
rsxr.Close();
return null;
}
}
}
Last edited by dimkdimk; 01-23-2010, 12:57 AM.
Comment
-
dimkdimk
This is exactly what I am looking for. Could you do me a favor and tell me how to incorporate this in my strategy. What syntax would I need and where to call your code and get the extra buttons displayed. Probably a dumb question but I would welcome your help to get me started in the right direction.
-thanks
Comment
-
you have shared something i had been dreaming about for a year or so. this opens a whole world of possibilities. thaks a lot!!!!
is it possible to add another row on the toolbar. you have demonstrated that it is possible to add components to the existing toolbar.
i was thinking of the following
1. add another row to the toolbar so that i can add more components specific to my strategy.
2. is it possible to add a textbox at the bottom of the form so that i can use it as a logging window for any messages that i get from my strategy.
Originally posted by dimkdimk View PostHere is another example on how to create a button in ToolStrip of a chart window. Don't pay attention to those resources stuff as I just wanted to read resx file from Visual Studio.
Main idea is just get toolstrip control and add button as it is simply done in Visual Studio.
Pay special attention to call Dispose and remove button when it is disposing.
This is just a button and click handler to switch bool flag ( CanTrade or similar ). You may want to add other controls to toolstrip, just be careful and test as NT guys told us: It is dangerous.
PHP Code:#region Using declarations
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Resources;
using System.Collections;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Strategy;
using System.Windows.Forms;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// TestStrat
/// </summary>
[Description("StrategyEx")]
public abstract class StrategyEx : Strategy
{
protected bool Initialized = false;
private ToolStripButton tsbtnStop = null; // Play/Stop Button to prevent Strategy from submitting orders
private ToolStripSeparator tsSeparator = null;
private bool bCanTrade = false;
protected string strStrategyResXPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\NinjaTrader 7\\bin\\Custom\\Strategy\\Strategy.resx";
/// <summary>
/// Protect from being instantiated.
/// </summary>
protected StrategyEx()
{}
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
}
/// <summary>
/// overridden Dispose for disposing controls
/// </summary>
public override void Dispose()
{
if(Initialized)
{
if( ChartControl != null )
{
ToolStrip toolStrip = (ToolStrip) ChartControl.Controls["tsrTool"];
if( toolStrip != null )
{
//
// Dispose separator
//
if( tsSeparator != null )
{
toolStrip.Items.Remove(tsSeparator);
}
//
// Dispose Stop button
//
if( tsbtnStop != null )
{
tsbtnStop.Click -= new EventHandler(tsbtnStop_Click);
toolStrip.Items.Remove(tsbtnStop);
}
}
}
}
}
/// <summary>
/// This method is used to configure the strategy on first OnBarUpdate
/// </summary>
protected virtual void OnInitializeBeforeStart()
{
CreateToolStripButtons();
}
///
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if( !Initialized )
{
OnInitializeBeforeStart();
Initialized = true;
}
}
protected void CreateToolStripButtons()
{
if( ChartControl != null )
{
ToolStrip toolStrip = (ToolStrip) ChartControl.Controls["tsrTool"];
if( toolStrip != null )
{
//
// Create Separator
//
ToolStripSeparator tsSeparator = new ToolStripSeparator();
tsSeparator.Name = "tsSeparator";
toolStrip.Items.Add(tsSeparator);
//
// Create Stop Button
//
tsbtnStop = new ToolStripButton();
tsbtnStop.ImageTransparentColor = Color.Magenta;
tsbtnStop.Name = "tsbtnStop";
tsbtnStop.ImageTransparentColor = System.Drawing.Color.Magenta;
Image imgBtn = GetImageFromResX( strStrategyResXPath, bCanTrade ? "tsbtnStop.Image" : "tsbtnPlay.Image");
if( imgBtn != null )
{
tsbtnStop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
tsbtnStop.Image = imgBtn;
}
else
{
tsbtnStop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
}
tsbtnStop.Text = bCanTrade ? "Disable trading" : "Enable trading";
tsbtnStop.Click += new EventHandler(tsbtnStop_Click);
toolStrip.Items.Add(tsbtnStop);
//
// Create another Button
//
}
}
}
private void tsbtnStop_Click(object sender, EventArgs e)
{
bCanTrade = !bCanTrade;
Image imgBtn = GetImageFromResX( strStrategyResXPath, bCanTrade ? "tsbtnStop.Image" : "tsbtnPlay.Image");
if( imgBtn != null )
{
tsbtnStop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
tsbtnStop.Image = imgBtn;
}
else
{
tsbtnStop.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
}
tsbtnStop.Text = bCanTrade ? "Disable trading" : "Enable trading";
}
static public Image GetImageFromResX(string resXPath, string imageResName)
{
// Create a ResXResourceReader for the resx file
ResXResourceReader rsxr = new ResXResourceReader(resXPath);
// Create an IDictionaryEnumerator to iterate through the resources.
IDictionaryEnumerator id = rsxr.GetEnumerator();
// Iterate through the resources and display the contents to the console.
foreach (DictionaryEntry d in rsxr)
{
if (d.Key.ToString() == imageResName)
{
//Close the reader.
rsxr.Close();
return (Image)d.Value;
}
}
//Close the reader.
rsxr.Close();
return null;
}
}
}
Comment
-
Originally posted by junkone View Post
is it possible to add another row on the toolbar. you have demonstrated that it is possible to add components to the existing toolbar.
i was thinking of the following
1. add another row to the toolbar so that i can add more components specific to my strategy.
2. is it possible to add a textbox at the bottom of the form so that i can use it as a logging window for any messages that i get from my strategy.
p.2 - I am trying to achieve the same but I worry about coordinates of the chart window. If we start cutting it with our extra controls then we can screw up NinjaTrader charting window. I suggest, instead, to create a separate form ( similar to Data Box ) and dump all your stuff there. I will try to implement this soon.
For p.2 : there is alternative approach: you can use margin on the right of the chart. There were a few examples on this forum that utilise this area. I guess, you can tell then to Chart to not to draw in this area. There is a property of a chart about this.
With all these good things that you can do please remember that there might be some code in Ninja that rely on current state of forms and can be broken if we stuff it with new extra "frameworks" :-)Last edited by dimkdimk; 01-27-2010, 05:51 PM.
Comment
-
I have been unable to get this to work....I copied the code you posted into a strategy file. Although it does compile, it does not show up in my strategies list on the chart??
I added the line
[Gui.Design.DisplayName("StrategyEx")]
but that did not help.
I as sure that it is something simple that I am missing...but C# seems to continue to baffle me.
It would be really great to have the option to put buttons on the tool bar to be able to control the strat without having to reload it every time.
Thanks for the help
Comment
-
Originally posted by clfield View PostI have been unable to get this to work....I copied the code you posted into a strategy file. Although it does compile, it does not show up in my strategies list on the chart??
The reason it is not showing up :
This is an abstract class. When I create my new strategies I inherit them from this class, but not from the deafault one:
MyNewStrategy : public StrategyEx
So this class is not intendent to be shown in the list. It is just for programming purposes. You can add similar code to UserDefined.cs-like code. It is very well described in NT help ( for v.6.5 ). By adding methods of the code above to strategy's partial class in UserDefault.cs you will enable ALL your strategy to behave like this. If you don't want them all to do this then make something like I did above.
Or, just create a new strategy and add those things into its body. In this case only one strategy will be able to do this stuff.
P.S. This is advanced stuff so please think what you are doing. If you don't know how then it is better not to do this as it might break something in NT.Last edited by dimkdimk; 02-04-2010, 01:13 AM.
Comment
-
Button Indicator
I was able to fashion the code into an indicator which does put the buttons in the tool bar...but it does not seem to update the state of the bool operators in a timely fashion. I am new to this area of C#. I tried to place print statements in the indicator to verify the state of the bool variables attached to the buttons and print statements in the event method. The second seems to cause NT trouble.
Any ideas?
Comment
-
ZapZap
Learning from your workspace button code...
Why is restore empty? Doesn't seem to be required.
Code:if (String.Compare(ws[cnt],ws[0]) == 0) restore(ws[cnt]); else switchTo(ws[cnt]);
Code:if (String.Compare(ws[cnt],ws[0]) != 0) switchTo(ws[cnt]);
Comment
-
I have not been able to bring up a button on the chart. I tried putting the code in UserDefined.cs and instantiate from another indicator or strategy but I am denied the possibility. It is very important for me to accomplish this because the source of a large project. Someone could give me a basic but functional example?
Comment
-
Question:
Question: how could you do to call the FORM with a button on the CHART?
Originally posted by Folls View PostI am currently using a file to pass information between an application I am developing with Visual Studio C# Express and NT. My life and my application would be much better if I could run this all from NT instead.
I'm trying to define a form class and use it but so far I am unsuccessful. Questions:
1) Is this this possible?
2) If not, is there a better way than file io available to communicate with NinjaTrader?
Here is the code I used. It gets an error "The type of namespace 'TestForm' could not be found."
public class FormsTest : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
//define class for test Form
public class testForm : Form
{
//constructor
public void TestForm()
{
//commented out for now
//this.Text = "this is a test Form";
}
}
#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 = true;
TestForm display = new TestForm();
}
Thanks!
Folls
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Net Change
by Segwin
Started by Segwin, 05-07-2018, 02:15 PM
|
14 responses
1,789 views
0 likes
|
Last Post
by aligator
Today, 11:47 AM
|
||
Started by Jimmyk, 01-26-2018, 05:19 AM
|
6 responses
837 views
0 likes
|
Last Post
by emuns
Today, 11:27 AM
|
||
Started by jxs_xrj, 01-12-2020, 09:49 AM
|
6 responses
3,293 views
1 like
|
Last Post
by jgualdronc
Today, 10:56 AM
|
||
Started by Touch-Ups, Today, 10:36 AM
|
0 responses
13 views
0 likes
|
Last Post
by Touch-Ups
Today, 10:36 AM
|
||
Started by geddyisodin, 04-25-2024, 05:20 AM
|
11 responses
63 views
0 likes
|
Last Post Today, 10:08 AM |
Comment