If the user toggles the line off and then closes the workspace, the line wont reappear and they have to unload and reload the indicator. Whilst the workspace is open the toggle button works fine.
I have checked that my bool which initialises the line is true and it is ... the line just doesn't re-appear! I am missing some code logic here that I just can't fathom.
Any help much appreciated.
basic code below but the full indicator is in the zip file
namespace NinjaTrader.NinjaScript.Indicators.Utilities
{
public class LongLine : Indicator
{
#region OnStateChange
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "";
Name = "LongLine";
Calculate = Calculate.OnPriceChange;
IsOverlay = true;
DisplayInDataBox = false;
DrawOnPricePanel = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
IsSuspendedWhileInactive = true;
SetAlarm = true;// remove this and line doesn't start up!
NChar = 0;
TextBrush = System.Windows.Media.Brushes.DodgerBlue;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
//SetUp_Line_bool = true;
if (ChartControl != null)
{
ChartControl.Dispatcher.InvokeAsync((Action)(() =>
{
CreateWPFControls();
}));
}
}
else if (State == State.Terminated)
{
if (ChartControl != null)
{
ChartControl.Dispatcher.InvokeAsync((Action)(() =>
{
DisposeWPFControls();
}));
}
}
}
#endregion
#region OnBarUpdate
protected override void OnBarUpdate()
{
if (CurrentBar < ChartBars.ToIndex)
return;
if (SetUp_Line_bool)
{
if(CurrentBar == ChartBars.ToIndex)
{
longline = Draw.HorizontalLine(this,"longline",Close[0] + TickSize * 10,Brushes.Blue,DashStyleHelper.DashDotDot,2);
//longline = Draw.HorizontalLine(this,"longline",Close[0]+TickSize *10,Brushes.Blue);
longline.IsLocked = false;
SetUp_Line_bool = false;
}
}
last = Close[0];//required
}///onbarupdate
#endregion
#region OnRender
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
foreach (DrawingTool draw in DrawObjects.ToList())
{
if (draw is DrawingTools.HorizontalLine)
{
DrawingTools.HorizontalLine temp = draw as DrawingTools.HorizontalLine;
if(DrawObjects["longline"] != null &&
DrawObjects["longline"] is DrawingTools.HorizontalLine)
{
//Print( draw.Tag);
if(draw.Tag == "longline")
{
myPrice1 = Instrument.MasterInstrument.RoundToTickSize(temp.StartAnchor.Price);
pricestr = myPrice1.ToString().Remove(0,NChar);
SharpDX.Direct2D1.Brush textBrushDx;
textBrushDx = textBrush.ToDxBrush(RenderTarget);
NinjaTrader.Gui.Tools.SimpleFont simpleFont = chartControl.Properties.LabelFont ?? new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12);
SharpDX.DirectWrite.TextFormat textFormat1 = simpleFont.ToDirectWriteTextFormat();
SharpDX.DirectWrite.TextFormat textFormat2 =
new SharpDX.DirectWrite.TextFormat(NinjaTrader.Core.Globals.DirectWriteFactory, "Verdana", SharpDX.DirectWrite.FontWeight.Bold,
SharpDX.DirectWrite.FontStyle.Italic, fontSize);
SharpDX.DirectWrite.TextLayout textLayout2 =
new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
pricestr, textFormat2, 400, textFormat1.FontSize);
SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(5,
ChartPanel.Y + ((float)temp.StartAnchor.GetPoint(chartControl, ChartPanel, chartScale).Y)-(float)textLayout2.Metrics.Height);
RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
}
}
} ///ishorizontalline
}///foreach
if ( last > myPrice1)
{
//PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav");
PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\longlinesound.wav");
}/// if last> price
}///onrender
#endregion
#region Add Button as Menu
#region Create Buttons ---- WPF Controls
protected void CreateWPFControls()
{
// the main chart window
chartWindow = System.Windows.Window.GetWindow(ChartControl.Parent) as Chart;
if (chartWindow == null)
return;
ntBarMenu = new System.Windows.Controls.Menu
{
// important to set the alignment, otherwise you will never see the menu populated
VerticalAlignment = VerticalAlignment.Top,
VerticalContentAlignment = VerticalAlignment.Top,
// make sure to style as a System Menu
Style = System.Windows.Application.Current.TryFindResource("SystemMenuStyle") as Style
};
// this is the menu item which will appear on the chart's Main Menu
myButton = new NTMenuItem()
{
Header = "L",
//Icon = topMenuItem1Icon,
Margin = new Thickness(0),
Padding = new Thickness(1),
VerticalAlignment = VerticalAlignment.Center,
//Style = mainMenuItemStyle
Foreground = Brushes.RoyalBlue,
};
ntBarMenu.Items.Add(myButton);
myButton.Click += OnMouseClick;
// add the menu which contains all menu items to the chart
chartWindow.MainMenu.Add(ntBarMenu);
if (TabSelected())
ShowWPFControls();
chartWindow.MainTabControl.SelectionChanged += TabChangedHandler;
}
#endregion
#region remove handlers / dispose objects
private void DisposeWPFControls()
{
if (chartWindow != null)
chartWindow.MainTabControl.SelectionChanged -= TabChangedHandler;
HideWPFControls();
myButton.Click -= OnMouseClick;
if (ntBarMenu != null)
{
chartWindow.MainMenu.Remove(ntBarMenu);
ntBarActive = false;
}
}
#endregion
#region On Mouse Click better? - Disable Line/Alarm
private void OnMouseClick(object s, EventArgs e)
{
Print ("Bool in Long line "+ SetUp_Line_bool);
if (DrawObjects["longline"] != null && DrawObjects["longline"] is DrawingTools.HorizontalLine)
{
if(DrawObjects["longline"].IsVisible)
{
DrawObjects["longline"].IsVisible = false;
IsVisible = false;//removes the price text
myButton.Foreground = Brushes.RoyalBlue;
}
if(DrawObjects["longline"].IsVisible == false);
{
DrawObjects["longline"].IsVisible = true;
IsVisible = true;
myButton.Foreground = Brushes.Gray;
}
}
ForceRefresh();
}
#endregion
#region hide controls.
private void HideWPFControls()
{
if (ntBarActive)
{
myButton.Visibility = Visibility.Collapsed;
ntBarActive = false;
}
}
#endregion
#region insert controls
private void ShowWPFControls()
{
if (!ntBarActive)
{
myButton.Visibility = Visibility.Visible;
ntBarActive = true;
}
}
#endregion
#region Tab selection and handler
private bool TabSelected()
{
bool tabSelected = false;
// loop through each tab and see if the tab this indicator is added to is the selected item
// full qualified namespaces used here to show where these tools are
foreach (TabItem tab in chartWindow.MainTabControl.Items)
if ((tab.Content as ChartTab).ChartControl == ChartControl && tab == chartWindow.MainTabControl.SelectedItem)
tabSelected = true;
return tabSelected;
}
// Runs ShowWPFControls if this is the selected chart tab, other wise runs HideWPFControls()
private void TabChangedHandler(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count <= 0)
return;
tabItem = e.AddedItems[0] as TabItem;
if (tabItem == null)
return;
chartTab = tabItem.Content as ChartTab;
if (chartTab == null)
return;
}
#endregion
#endregion
#region Properties
[Description("Set the size of the display font")]
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Font Size", GroupName = "Parameters", Order = 0)]
public float FontSize
{
get { return fontSize; }
set { fontSize = value; }
}
[Display(Name="Price characters to remove", Description="", Order=8, GroupName="Parameters")]
[RefreshProperties(RefreshProperties.All)]
public int NChar
{ get; set; }
[XmlIgnore]
[Display(ResourceType = typeof(Custom.Resource), Name = "TextColor", GroupName = "Parameters")]
public System.Windows.Media.Brush TextBrush
{
get { return textBrush; }
set { textBrush = value; }
}
[Browsable(false)]
public string TextBrushSerialize
{
get { return Serialize.BrushToString(TextBrush); }
set { TextBrush = Serialize.StringToBrush(value); }
}
#endregion
}
}

Comment