I'm working to finish up my final project for a financial modeling class. Below is the code for a section. I haven't made many changes lately, but all of a sudden, every time I go to run it, I'm getting an error saying that the next to last "Next" does not have a "For". I've gone over it so many times, I can't begin to understand what I'm missing. I'm thinking I just need a different set of eyes to look at it, as I've been staring at it a lot this last week. Any help or suggestions are much appreciated.
Code:
For j = 1 To 4
cash(j) = 50000
row30 = RSIrow(30, RSIcol(j) + 1)
row70 = RSIrow(70, RSIcol(j) + 1)
'if RSI > 70 comes before RSI < 30, start will a long position
If row70 < row30 Then
purchase(j) = Range(drange).Offset(1, prcCol(j)).Value * 0.95
shares(j) = cash(j) / purchase(j)
cash(j) = 0
Else
shares(j) = 0
purchase(j) = 0
End If
Next j
For i = 1 To numRows
balance = 0
'This is where our 1.005 for each stock we hold should go.
For x = 1 To 4
If cash(x) > 0 Then cash(x) = cash(x) * (1 + Range(rfr).Offset(i, 0).Value / 100)
If margin(x) < 0 Then margin(x) = margin(x) * 1.005 'interest expense
Next x
For j = 1 To 4
' sell if RSI > 70
If Range(drange).Offset(i, RSIcol(j)).Value > 70 Then
If shares(j) > 0 Then
cash(j) = shares(j) * Range(drange).Offset(i, prcCol(j)).Value
shares(j) = 0
purchase(j) = 0
ElseIf cash(j) > 0 And Not prudent Then ' short stock
shares(j) = (-2 * cash(j) * 0.96 / Range(drange).Offset(i, prcCol(j)).Value)
margin(j) = 3 * cash(j) - 2 * 0.04 * cash(j)
cash(j) = 0
purchase(j) = Range(drange).Offset(i, prcCol(j)).Value
End If
End If
' buy if RSI < 30
If Range(drange).Offset(i, RSIcol(j)).Value < 30 Then
If cash(j) > 0 Then
If prudent Then
shares(j) = cash(j) / Range(drange).Offset(i, prcCol(j)).Value
cash(j) = 0
purchase(j) = Range(drange).Offset(i, prcCol(j)).Value
ElseIf shares(j) < 0 Then ' close short position if it exists
margin(j) = margin(j) - shares(j) * Range(drange).Offset(i, prcCol(j)).Value
cash(j) = margin(j)
margin(j) = 0
shares(j) = 0
purchase(j) = 0
End If
End If
'Code for StopLoss and StopBuy
If prcCol(j) < (1 + Range(stopBuy).Value) * purchase(j) And shares(j) > 0 Then 'stopbuy
cash(j) = shares(j) * Range(drange).Offset(i, prcCol(j)).Value
shares(j) = 0
purchase(j) = 0
If prcCol(j) > (1 - Range(stopBuy).Value) * purchase(j) And shares(j) < 0 Then 'stoploss
cash(j) = shares(j) * Range(drange).Offset(i, prcCol(j)).Value
shares(j) = 0
purchase(j) = 0
End If
End If
balance = balance + cash(j) + shares(j) * Range(drange).Offset(i, prcCol(j)).Value + margin(j)
Next j
Range(outrange).Offset(i, 0).Value = balance
Next i