|
Sydney, NS
Experience: Intermediate
Platform: Ninja
Broker: Zen-Fire
Trading: TF,S,GC
Posts: 605 since Nov 2009
Thanks Given: 248
Thanks Received: 394
|
If anyone runs into serializing issues, this thread has some good info with clear coding examples.
NinjaTrader Support Forum - View Single Post - Complete list of NinjaTrader.Gui.Design.Serializable options?
I paste it in here too (the main contribution):
Warning: I've been asked to remind all that "Unsupported" means this may break in a future release. Or might not work with other aspects of the program eg Analyzer etc. Use this insight with caution, it is not documented.
Thanks everyone.
I used .NET reflection to rip out all the Methods, Properties etc.
I can state with confidence that there are only 3 extra serialisation methods provided by Ninja above that which comes standard with .NET.
They are :-- .Gui.Design.SerializableColor
- NinjaTrader.Gui.Design.SerializableFont
- NinjaTrader.Gui.Design.SerializablePen
Pen seems to work differently so get confident with the others first.
On Serialising the File Object.
Clearly not difficult to parse the properties into a Parameter & use XMLSerialise & StreamWriter to create my own serialization class. But unlike the Color & Font it doesn't appear that the Ninja properties dialog is designed to handle the File Objects properties. So hooking it up the FileOpen dialog looked like it would make the indicator too hard to install on other peoples machines. As getting an "easy to change property value" experience wa what I was going for. Its back to the drawing board.
I hope you find the code below useful.
Partial code walking you thru the step to use build-in Ninja Serialization.
Step 1 - Declare local Variables
#region Variables
// Wizard generated variables
private float yOffset = 15; // Gap up from the bottom of the Panel
private Color textColor = Color.Black;
// User defined variables (add any user defined variables below)
//----------< Text for Y Axis Labels >--------------------
StringFormat stringFormat = new StringFormat();
private SolidBrush textBrush = new SolidBrush(Color.Black);
private Font textFont = new Font("Arial", 8);
#endregion
Step 2 - Set Brush color to property
protected override void Initialize()
{
textBrush.Color = textColor;
Step 3 - Set Properties & Serialise Correctly
[Description("Y Offset of text from Bottom of Panel")]
[Category("Parameters - Font")]
[Gui.Design.DisplayName("Text Position")]
public float YOffset
{
get { return yOffset; }
set { yOffset = Math.Min(Math.Max(0,value),1000); }
}
[Browsable(false)]
public string MyFontSerialize
{
get { return NinjaTrader.Gui.Design.SerializableFont.ToString(textFont); }
set { textFont = NinjaTrader.Gui.Design.SerializableFont.FromString (value); }
}
[XmlIgnore()]
[Description("Font of Bar Numbers")]
[Gui.Design.DisplayName("Text Font")]
[Category("Parameters - Font")]
public System.Drawing.Font TextFont
{
get { return textFont; }
set { textFont = value; }
}
[Browsable(false)]
public string TextColorSerialize
{
get { return NinjaTrader.Gui.Design.SerializableColor.ToString(textColor); }
set { textColor = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
}
[XmlIgnore()]
[Description("Color of Bar Numbers")]
[Category("Parameters - Font")]
[Gui.Design.DisplayName("Text Color")]
public Color TextColor
{
get { return textColor; }
set { textColor = value; }
}
#endregion
Step 4 - Use it Somewhere
public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
{
// ---< ensure you call the base Plot Method prior to exiting this method >---
base.Plot(graphics, bounds, min, max);
if (Bars == null || /*Plots.Count < 2 ||*/ ChartControl == null)
return;
// if (CurrentBar <= 1)
// return;
// ------------< Initialise Offsets & Graphic environment >---------------------
int barWidth = ChartControl.ChartStyle.GetBarPaintWidth(ChartCont rol.BarWidth);
int barSpace = ChartControl.BarSpace;
SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
// ===< Determine where Text will be displayed.Bottom of the Price Chart >===
float y = bounds.Top + bounds.Height - yOffset - textFont.Height;
graphics.DrawString( "CB-cntBar", textFont, textBrush, bounds.Left, y, stringFormat);
float y2 = y - 15;
graphics.DrawString( "cntBar", textFont, textBrush, bounds.Left, y2, stringFormat);
// ---< For each bar that is displayed on the screen >---
for(int cntBar = ChartControl.FirstBarPainted; cntBar <= ChartControl.LastBarPainted; cntBar++)
{
// if (!ChartControl.ShowBarsRequired && cntBar < BarsRequired)
// continue;
float x = ChartControl.GetXByBarIdx(cntBar);
graphics.DrawString( (CurrentBar - cntBar).ToString(), textFont, textBrush, x, y, stringFormat);
graphics.DrawString( cntBar.ToString(), textFont, textBrush, x, y2, stringFormat);
}
//=====< Return SmoothingMode to its original value >========
graphics.SmoothingMode = oldSmoothingMode;
}
|