App error when stopping strategy that listens to TradeManager events

Questions about MultiCharts .NET and user contributed studies.
jarym
Posts: 58
Joined: 16 Feb 2015
Has thanked: 14 times
Been thanked: 6 times

App error when stopping strategy that listens to TradeManager events

Postby jarym » 22 Feb 2023

Hi, I'm using the following code to watch TradeManager Orders but every now and then when I click to stop the strategy in the Studies window (and almost always when I remove the study) I get a memory error:
mem_error.png
(342.09 KiB) Not downloaded yet
Below is a VERY minimal example that reproduces this - I suppose I might not be un-registering the event handlers correctly but as you can see I tried adding them everywhere and still I get this error.

Can someone from MC.NET help with this?

Code: Select all

using System; using System.Collections.Generic; using System.Linq; using PowerLanguage.TradeManager; namespace PowerLanguage.Strategy { [IOGMode(IOGMode.Enabled)] public class TradeTest : SignalObject { public TradeTest(object _ctx):base(_ctx){} private DateTime syncStartTime; private DateTime lastProcessedTime; protected override void Create() { } protected override void StartCalc() { // assign inputs syncStartTime = DateTime.Now; lastProcessedTime = syncStartTime; // Start listening to TradeManager events TradeManager.TradingData.Orders.FinishChanging -= OrdersFinishChanging; TradeManager.TradingData.Orders.FinishChanging += OrdersFinishChanging; TradeManager.TradingData.Orders.Added -= Added; TradeManager.TradingData.Orders.Added += Added; //TradeManager.TradingData.Orders.IntervalFltr.From = syncStartTime; } protected override void Dispose(bool A_0) { TradeManager.TradingData.Orders.FinishChanging -= OrdersFinishChanging; TradeManager.TradingData.Orders.Added -= Added; base.Dispose(A_0); } protected override void StopCalc() { TradeManager.TradingData.Orders.FinishChanging -= OrdersFinishChanging; TradeManager.TradingData.Orders.Added -= Added; base.StopCalc(); } protected override void CalcBar(){ if (Bars.LastBarOnChart) { ExecControl.RecalcLastBarAfter(TimeSpan.FromSeconds(0.5)); } } protected override void OnRecalcLastBarAfterEvent() { TradeManager.ProcessEvents(); ExecControl.RecalcLastBarAfter(TimeSpan.FromSeconds(0.5)); } private void Added(IEnumerable<TradeManager.Order> _items) { if (!_items.Any()) return; lastProcessedTime = DateTime.Now; //TradeManager.TradingData.Orders.IntervalFltr.From = lastProcessedTime; foreach (TradeManager.Order order in _items) { Output.WriteLine("Added - ID: {0}, State: {1}, OCO GroupId: {2}, LimitPriceStr: {3}, StopPriceStr: {4} --- {5} {6} -- {7}", order.ID, order.StateStr, order.OCOGroupID, order.LimitPriceStr, order.StopPriceStr, order.MCBasedID, order.BrokerBasedID, order.CategoryStr); } } private void OrdersFinishChanging(object sender, EventArgs e) { IOrders orders = sender as IOrders; IEnumerable<TradeManager.Order> _items = orders.Items.Where(order => (order.FinalDT.HasValue ? order.FinalDT : order.GeneratedDT) >= lastProcessedTime); if (!_items.Any()) return; lastProcessedTime = DateTime.Now; //TradeManager.TradingData.Orders.IntervalFltr.From = lastProcessedTime; foreach (TradeManager.Order order in _items) { Output.WriteLine("Changed - ID: {0}, State: {1}, OCO GroupId: {2}, LimitPriceStr: {3}, StopPriceStr: {4} --- {5} {6} -- {7}", order.ID, order.StateStr, order.OCOGroupID, order.LimitPriceStr, order.StopPriceStr, order.MCBasedID, order.BrokerBasedID, order.CategoryStr); } } } }

HellGhostEvocatorX
Posts: 80
Joined: 10 Feb 2022
Has thanked: 52 times
Been thanked: 11 times

Re: App error when stopping strategy that listens to TradeManager events

Postby HellGhostEvocatorX » 28 Feb 2023

Hello, maybe the two screenshots will help you. When removing the indicator I get a nullreference exception. I'm not deep enough into your code to fix this error, but you might be able to do it yourself. A second error is also displayed. However, the code can still be compiled (see 2nd screenshot.

P.S.: use Visual Studio, this gives you extended debug options...
Attachments
TradeTest2.png
(631.53 KiB) Not downloaded yet
TradeTest.png
(676.78 KiB) Not downloaded yet

jarym
Posts: 58
Joined: 16 Feb 2015
Has thanked: 14 times
Been thanked: 6 times

Re: App error when stopping strategy that listens to TradeManager events

Postby jarym » 17 Dec 2023

Thanks, actually MC support helped me with this - it turned out my Dispose method was unnecessary and causing the crash. Once I removed that it all worked great.


Return to “MultiCharts .NET”