+ Reply to Thread
Results 1 to 12 of 12

Out of stack space error

Hybrid View

luv2glyd Out of stack space error 07-16-2008, 07:08 PM
shg The stack is where recursive... 07-16-2008, 07:55 PM
piyushdabomb Use the collection object and... 07-17-2008, 12:55 AM
luv2glyd OK, thank you. 07-17-2008, 07:41 AM
luv2glyd Out of stack space error 07-18-2008, 12:43 PM
royUK Post your code, remember to... 07-18-2008, 01:00 PM
luv2glyd Here it is: Master... 07-18-2008, 01:08 PM
luv2glyd Any luck with this? 07-20-2008, 09:17 AM
Norie Sorry but that code appears... 07-20-2008, 10:01 AM
shg Threads merged. luv2glyd,... 07-20-2008, 12:49 PM
shg Routine A calls B and C... 07-20-2008, 01:12 PM
protonLeah 1. You have not declared and... 07-20-2008, 03:47 PM
  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 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.

  2. #2
    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

  3. #3
    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

  4. #4
    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?

  5. #5
    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.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Threads merged. luv2glyd, please don't do that.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Routine A calls B and C
    Routine B calls A
    Routine C calls A
    So the stack gets deeper until it overflows.

    I don't think anyone could help without having a clue as to what you're trying to do, and seeing the six missing routines.

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,927
    1. You have not declared and typed your variables, so VB is assigning them the default type of Variant (16 bytes vs 2 for Integer and 4 for Long)
    2. The variable intrade is used in each of the three procedures and it appears that you intended to have the value accessed and changed by each of the three variables; but, since it is undeclared, each procedure initializes a local variable of 16 byte Variant-type with no link to any previous value each time the procedure is called. You should declare it as Public at the top of the module (outside any procedure) and type it as Integer.
    3. You are using "application.run..." to run code in each module, instead, use CALL procedure name.
    4. In your sub procedures, you are trying to exit the procedure and return to the main procedure by using the App...run.. language; however, this will only call the main as a subroutine of the calling subroutine and start it from the beginning of the code. Since main is calling each of the subs, you have an untended infinite loop that crashes Excel. If you had CALLed the sub from the main, the End Sub would exit the sub and return to the main at the next line after the CALL line:

    Public intrade as Integer
    sub main()
        ....
        do some stuff
        ....
        call function_one
        return here from function one and do more stuff
        call function_two
        return here from function two, etc
    end sub
    
    sub function_one()
        do something
    end sub
    sub function_two()
    ...
    end sub

    Declare & Type all variables
    Use Call vs. app/run
    Ben Van Johnson

+ 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