Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss = Memory Hog?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    SetStopLoss = Memory Hog?

    Hi all,

    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:

    Code:
    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 function GetMyStopLossValue is based on an indicator. I have tried dozens of methods for this function (including an indicator-less method using the minimum or maximum or the high/low for an N number of bars back for long/short positions), and this is not the cause.

    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?

    #2
    Serac, you can run your strategy with TraceOrders = true in the Initialize() section and you will see all of the output for orders. You can then use this to see if your stops are being called too frequently. The code you posted for managing your stops looks fine, but depending on how often you call it, and also the code that makes up the GetMyStopLossValue(), this could definitely be where the issue is.
    AustinNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    646 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    367 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    107 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    569 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X