Sorry in advance for posting such a long piece of code, I shortened it as far as possible, but I think that the problem can be connected to the incorrect positioning of lines in a long loop so I see it necessary to illustrate it until the place where the error arises.
In the 10th line from the end (marked by "# Error arises here (!)") at the line of the "else" operator, I get an error "SyntaxError: invalid syntax". My initial intuition was that an error can be connected to improper positioning of the operator, but the text of the error doesn't seem to say so (i would expect "IndentationError: unexpected indent" error then) and it seems to me that "else" operator is positioned correctly.
Thank you.
if (Test_set.loc[n, '4_signals'] == 1):
if (Portfolio_1_4_Date_sell < pd.Timestamp(Test_set.loc[n, 'feedTimestamp'])):
try:
SP1500selldate_2 = n_short
SP1500sellprice_2 = SP1500DailyReturns.loc[SP1500selldate_2, 'S&P 1500 SUPER COMPOSITE']
except KeyError:
try:
SP1500buydate_2 = SP1500buydate_2 - pd.Timedelta("1 day")
except KeyError:
SP1500buydate_2 = SP1500buydate_2 - pd.Timedelta("2 days")
Test_set.loc[n, 'Portfolio_2_4'] = Portfolio_2_4
try:
SP1500selldate_3 = n_short
SP1500sellprice_3 = SP1500DailyReturns.loc[SP1500selldate_3, 'S&P 1500 SUPER COMPOSITE - PRICE INDEX']
except KeyError:
try:
SP1500buydate_3 = SP1500buydate_3 - pd.Timedelta("1 day")
except KeyError:
SP1500buydate_3 = SP1500buydate_3 - pd.Timedelta("2 days")
Portfolio_3_4 = Portfolio_3_4 * SP1500sellprice_3 / SP1500buyprice_3
SP1500buydate_3_SP1500 = n_short
Test_set.loc[n, 'Portfolio_3_4'] = Portfolio_3_4
else:
Test_set.loc[n, 'Portfolio_1_4_allocation'] = 'Portfolio 1 is already allocated!'
Test_set.loc[n, 'Portfolio_1_4'] = Portfolio_1_4
try:
SP1500selldate_3 = n_short
SP1500sellprice_3 = SP1500DailyReturns.loc[SP1500selldate_3, 'S&P 1500 SUPER COMPOSITE - PRICE INDEX']
except KeyError:
try:
SP1500buydate_3 = SP1500buydate_3 - pd.Timedelta("1 day")
SP1500buydate_3_str = SP1500buydate_3.strftime("%Y-%m-%d")
except KeyError:
SP1500buydate_3 = SP1500buydate_3 - pd.Timedelta("2 days")
SP1500buydate_3_str = SP1500buydate_3.strftime("%Y-%m-%d")
Portfolio_3_4 = Portfolio_3_4 * SP1500sellprice_3 / SP1500buyprice_3
SP1500buydate_3_SP1500 = n_short
Test_set.loc[n, 'Portfolio_3_4'] = Portfolio_3_4
else: # Error arises here (!)
Test_set.loc[n, 'Portfolio_2_4_allocation'] = 'Portfolio 1 and 2 are already allocated!'
Test_set.loc[n, 'Portfolio_2_4'] = Portfolio_2_4
if (Portfolio_3_4_Date_sell < pd.Timestamp(Test_set.loc[n, 'feedTimestamp'])):
Portfolio_3_4 = Portfolio_3_4 * Test_set.loc[n, 'Cumulative stock Returns over the 30 days after transaction (22 working days)']
Test_set.loc[n, 'Portfolio_3_4'] = Portfolio_3_4
Portfolio_3_4_Date_sell = Date_Buy pd.Timedelta("30 days")
else:
Test_set.loc[n, 'Portfolio_3_4_allocation'] = 'Portfolio 1, 2 and 3 are already allocated!'
Test_set.loc[n, 'Portfolio_3_4'] = Portfolio_3_3
CodePudding user response:
If you are trying
try:
#something
except KeyError:
#something
else:
#something
then there cannot be anything in between them at the indentation level of the try
.
Your code is something like
try:
#something
except KeyError:
#something
#something
else:
#something
which is why interpreter is reporting a syntax error.