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
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
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.
Use the collection object and push things to the collection object or to a hash table.
OK, thank you.
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.
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
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
Any luck with this?
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks