+ Reply to Thread
Results 1 to 12 of 12

Out of stack space error

Hybrid View

  1. #1
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Out of stack space error

    I'm running a while loop and after about 3000 iterations get this error:

    "Out of stack space"

    Any idea why this happens/ how to fix it?

    Thanks

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    The stack is where recursive routines store their active variables when recursed, and each recursion pushes another set of data (stack gets deeper) until things unwind and get taken off.

    You have finite space on the stack; if rountines nest too deeply, you run out.

    It's what happens if you stack work in an inbox as received and work only on the top item. If you get too far behind, the stack falls over.

    The way to fix it is to redesign your code.

  3. #3
    Registered User
    Join Date
    08-10-2007
    Posts
    51
    Use the collection object and push things to the collection object or to a hash table.

  4. #4
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679
    OK, thank you.

  5. #5
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Out of stack space error

    I have a master macro that runs a loop of 8,000 iterations or so.

    Within the loop, if condition 1 is met, function 1 runs (function 1 is written in another module, outside of master), then returns back to master macro; if condition 2 is met, function 2 runs (function 2 is written in another module, outside of master), then returns back to master macro;

    Every time I run this, I get "Out of stack space error" after about 2000 iterations.

    If I put the code for function 1 and 2 within the code of master, and run the master, no error pops up. My prefference is to have master and functions 1 and 2 separate.

    I'm a bit new to programming - what is the language I need to use to elliminate this error?

    Thanks.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Post your code, remember to use Code tags
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679
    Here it is:


    Master Macro:

    
    'TESTING THE 55-DAY SYSTEM:
    
    Sub test_55_day_system()
    
    'establishing the last row of historical price data
    last_row = Worksheets("Sheet1").Range("D29").Value
    
    'cycle throught every row of data:
    For j = 1 To last_row
    
    'updating the spinner cell value:
    Worksheets("Sheet1").Range("E29").Value = Worksheets("Sheet1").Range("E29").Value + 1
        
        'checking to see if a buy or sell signal is given:
        signal = Worksheets("Sheet1").Range("K41").Value
    
        If signal = 1 Then
        'entering long trade:
        Application.Run "test_55_day_long_system"
        End If
    
        If signal = -1 Then
        'entering short trade:
        Application.Run "test_55_day_short_system"
        End If
        
    'close the for loop
    Next
    
    End Sub

    Functions 1 and 2:


    
    'TESTING THE 55-DAY LONG SYSTEM:
    Sub test_55_day_long_system()
    
    'setting 'intrade' value to 0 to indicate that trade has not taken place
    intrade = 0
        
        'entering trade:
        Application.Run "Long_55_day_enter"
        'setting 'intrade' value to 1 to avoid looking at entry signals:
        intrade = 1
    
    'checking to see if stops are hit.  If hit the loop exits:
    Do While intrade = 1
    
        'checking to see if a 2% stop loss was hit (placed ahead of profit stop to insure
        'that if a 20-day low is hit at the same time as the 2% stop, 2% will be recorded
        'over the profit stop
        profit_exit_signal = Worksheets("Sheet1").Range("I54").Value
            If profit_exit_signal = 1 Then
            Application.Run "Long_55_day_profit_exit"
            intrade = 0
            End If
    
        'checking to see if a 2% stop loss was hit
             stop_loss_signal = Worksheets("Sheet1").Range("I48").Value
             If stop_loss_signal = 1 Then
             Application.Run "Long_55_day_stop_exit"
             intrade = 0
             End If
            
       ' recording next row of data to read:
        If intrade = 1 Then
        Worksheets("Sheet1").Range("E29").Value = Worksheets("Sheet1").Range("E29").Value + 1
        End If
        
    'ends the stop loop as soon as intrade = 0 (stop is hit) or there is no more data
    Loop
    
    'recording new position of spinner once trade has been exited:
    Worksheets("Sheet1").Range("E29").Value = Worksheets("Sheet1").Range("E29").Value + 1
    
    'sending back to the main test_55_day_system macro
    Application.Run "test_55_day_system"
    
    End Sub
    
    
    
    
    
    
    'TESTING THE 55-DAY SHORT SYSTEM:
    Sub test_55_day_short_system()
    
    'setting 'intrade' value to 0 to indicate that trade has not taken place
    intrade = 0
    
        'entering the trade:
        Application.Run "Short_55_day_enter"
        'setting 'intrade' value to 1 to avoid looking at entry signals:
        intrade = 1
    
    'checking to see if stops are hit.  If hit the loop exits:
    Do While intrade = 1
        
        'checking to see if a 2% stop loss was hit (placed ahead of profit stop to insure
        'that if a 20-day low is hit at the same time as the 2% stop, 2% will be recorded
        'over the profit stop
        profit_exit_signal = Worksheets("Sheet1").Range("J54").Value
            If profit_exit_signal = 1 Then
            Application.Run "Short_55_day_profit_exit"
            intrade = 0
            End If
        
        'checking to see if a 2% stop loss was hit
             stop_loss_signal = Worksheets("Sheet1").Range("J48").Value
             If stop_loss_signal = 1 Then
             Application.Run "Short_55_day_stop_exit"
             intrade = 0
             End If
                
       ' recording next row of data to read:
        If intrade = 1 Then
        Worksheets("Sheet1").Range("E29").Value = Worksheets("Sheet1").Range("E29").Value + 1
        End If
        
    'ends the stop loop as soon as intrade = 0 (stop is hit) or there is no more data
    Loop
    
    'recording new position of spinner once trade has been exited:
    Worksheets("Sheet1").Range("E29").Value = Worksheets("Sheet1").Range("E29").Value + 1
    
    'sending back to the main test_55_day_system macro
    Application.Run "test_55_day_system"
    
    End Sub

  8. #8
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679
    Any luck with this?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Sorry but that code appears to be all over the place.

    You seem to be running subs from other subs then calling back to those subs again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1