Ahh!
I solved it now... I discovered that code inside "Bars.LastBarOnChart" forced me to use "ExecControl.Recalculate();", to activate the code. It seems that there is something strange with "Bars.LastBarOnChart". But I don't know what else I could do.
Full Code:
Code: Select all
private bool m_Buy, m_Sell;
private bool m_Toolbar_Inited = false;
private void TSB_Buy( object Sender, EventArgs E )
{
m_Buy = true;
m_Sell = false;
// Used this with "Bars.LastBarOnChart".
//ExecControl.Recalculate();
}
private void TSB_Sell( object Sender, EventArgs E )
{
m_Sell = true;
m_Buy = false;
// Used this with "Bars.LastBarOnChart".
//ExecControl.Recalculate();
}
private void AddItemToolStrip( ToolStrip TB, ToolStripItem Item )
{
Item.Tag = this;
TB.Items.Add(Item);
}
protected override void StartCalc()
{
if (!m_Toolbar_Inited)
{
ChartToolBar.AccessToolBar( TB =>
{
{
var TSB = new ToolStripButton
{
Text = "Buy"
};
TSB.Click += TSB_Buy;
AddItemToolStrip( TB, TSB );
}
AddItemToolStrip( TB, new ToolStripSeparator() );
{
var TSB = new ToolStripButton
{
Text = "Sell"
};
TSB.Click += TSB_Sell;
AddItemToolStrip( TB, TSB );
}
});
m_Toolbar_Inited = true;
}
}
protected override void CalcBar()
{
if (Bars.LastBarOnChart)
{
// Code to be triggered ( inside "Bars.LastBarOnChart" ), didn't trigger ( with buttons ). :(
if (m_Buy)
{
// Code.
}
else if (m_Sell)
{
// Code.
}
}
}
protected override void Destroy()
{
if (m_Toolbar_Inited)
{
ChartToolBar.AccessToolBar( TB =>
{
var For_Erase = new List<ToolStripItem>();
foreach ( ToolStripItem Item in TB.Items )
if (ReferenceEquals( this, Item.Tag ))
For_Erase.Add(Item);
foreach ( var Item in For_Erase )
TB.Items.Remove(Item);
});
}
}
I will post some updates if I discover something else.