My automated strategy is a memory hog. Running the strategy in real time or in Market Replay and looking at memory allocation in the Windows Task Manager, I see the memory allocation balloon until NT crashes.
NT takes around 80-200 MB of memory on my Windows XP box. Starting up the strategy comes in around 50 MB more. Upon running the strategy, memory consumption monotonically grows to around 1.4 GB when NT crashes.
I have deconstructed my strategy and narrowed down the cause. The cause is invoking SetStopLoss, or at least my method of using this NT capability. The cause is *not* ill-written indicators, as I tested this strategy using only the SMA as an indicator and the same thing happened. The issue is invocation of SetStopLoss. The code fragment below is how I am setting the StopLoss:
protected void ManageStopLoss (MarketDataEventArgs MarketData)
{
double StopValue = 0.0;
if (Position.MarketPosition != MarketPosition.Flat)
{
StopValue = GetMyStopLossValue(MarketData);
if (Position.MarketPosition == MarketPosition.Long)
{
if (PrevStoploss != StopValue)
{
SetStopLoss("Long",CalculationMode.Price,StopValue,false);
}
PrevStoploss = StopValue;
}
else if (Position.MarketPosition == MarketPosition.Short)
{
if (PrevStoploss != StopValue)
{
SetStopLoss("Short",CalculationMode.Price,StopValue,false);
}
PrevStoploss = StopValue;
}
}
else
{
PrevStoploss = 0.0;
}
}
The ManageStopLoss function given above is invoked every OnBarUpdate cycle.
If I *do not* call ManageStopLoss in OnBarUpdate, my strategy does not balloon, and it appears that I could run it indefinitely. Calling ManageStopLoss in OnBarUpdate, I get less than 10 hours in real time with 5 minute bars, or an equivalent amount of time in Market Replay.
My theory is that somehow the SetStopLoss is generating completely new orders but not canceling/updating existing StopLoss orders. The PrevStoploss != StopValue logic in ManageStopLosswas implemented to prevent redundant orders.
Help! I am not going to run an automated strategy without stops, but it seems I cannot get a strategy to run with stops.
Suggestions?

Comment