Ad

How To Fix The Error ? IndentationError: Unexpected Indent?

I was trying to run the code from : https://medium.datadriveninvestor.com/the-supertrend-implementing-screening-backtesting-in-python-70e8f88f383d

but when it came to this block I saw that there was an error that i've not been able to fix.

It shows IndentationError: unexpected indent. How to fix this?

def find_optimal_parameter(df):
    # predefine several parameter sets
    atr_period = [7, 8, 9, 10]
    atr_multiplier = [1.0, 1.5, 2.0, 2.5, 3.0]
roi_list = []

# for each period and multiplier, perform backtest
    for period, multiplier in [(x,y) for x in atr_period for y in atr_multiplier]:
        new_df = df
        supertrend = Supertrend(df, period, multiplier)
        new_df = df.join(supertrend)
        new_df = new_df[period:]
        entry, exit, roi = backtest_supertrend(new_df, 100000)
        roi_list.append((period, multiplier, roi))
    
    print(pd.DataFrame(roi_list, columns=['ATR_period','Multiplier','ROI']))
    
    # return the best parameter set
    return max(roi_list, key=lambda x:x[2])
df = yf.download('TSLA', start='2010-01-01')

optimal_param = find_optimal_parameter(df)

print(f'Best parameter set: ATR Period={optimal_param[0]}, Multiplier={optimal_param[1]}, ROI={optimal_param[2]}')
Ad

Answer

Place a tab or 4 spaces in front of roi_list = []

Edit: As chris pointed out, 4 spaces are recommended by python style guide.

Ad
source: stackoverflow.com
Ad