Hi,
I'm experimenting with TradeManager and trying to get a snapshot of the account status when some external event happens. So I'm seeing a threading issue but I'm not sure how to work around it. When the event is triggered, it calls a delegate to look up TradeManager. Always the first time, TradeManager properties return empty values. I've noticed if I pass TradeManager to some serializer then next time I look it up, the values are populated. Another issue is that the next time the delegate is called, TradeManager values are old (not updated).
Do you know what is exactly causing the issue and what's the correct way to do this?
Thanks a bunch!
TradeManager and Multi-Threading
- Angelina MultiСharts
- Posts: 260
- Joined: 28 Dec 2016
- Has thanked: 28 times
- Been thanked: 66 times
Re: TradeManager and Multi-Threading
Hello orad,
TradeManager receives the information only after TradeManager.ProcessEvent() is called.
If it is called in CalcBar then the events you have subscribed to will be received only upon the next calculation (tick by tick with IOG-On, on bar close with IOG+Off). In order to call this event more often you can use RecalcLastBarAfter.
TradeManager receives the information only after TradeManager.ProcessEvent() is called.
If it is called in CalcBar then the events you have subscribed to will be received only upon the next calculation (tick by tick with IOG-On, on bar close with IOG+Off). In order to call this event more often you can use RecalcLastBarAfter.
Code: Select all
protected override void CalcBar()
{
if (Bars.LastBarOnChart)
ExecControl.RecalcLastBarAfter(TimeSpan.FromSeconds(1));
TradeManager.ProcessEvents();
}
Re: TradeManager and Multi-Threading
Thanks, that seems to partially fix the problem. i.e. TradeManager values are populated now. However, the second issue still exists. For subsequent times that I read TradeManager properties their value stays unchanged. For example if there is an open position, the OpenPL does not change even though I can see it's changing in MultiCharts. Any ideas?
Re: TradeManager and Multi-Threading
Actually that didn't fix the first problem (initial read of TradeManager returns empty property values). But I added call to TradeManager.ProcessEvents(); at the beginning of the event ballback and that fixed the second issue (subsequent reads of TradeManager give updated values).
I still don't know why the first time it gives empty values. The workaround I found is like this, which I don't know why works!
I still don't know why the first time it gives empty values. The workaround I found is like this, which I don't know why works!
Code: Select all
protected override void CalcBar()
{
if (Bars.LastBarOnChart && Bars.Status == EBarState.Close)
{
// This is a workaround to populate TradeManager
Output.WriteLine("TradeManager initialized");
Serializer.SerializeObject(TradeManager);
}
}