Home > Mobile >  How to fix a syntax error in a def function?
How to fix a syntax error in a def function?

Time:03-16

I have problems to get this code to work. Python says that there is a syntax error. It seems as though there might be something wrong with the indentation, however I cannot find the problem (see screenshot attached)

Code Screenshot

Thank you very much for your help in advance.

Best regards.

CodePudding user response:

The return statement should be indented at the same level as the function's code, not the def statement.

CodePudding user response:

The code looks like this now. The error message is the same.

def monte_carlo(stock,start):
    t_intervals=60 ##forecasting for upcoming 700 days
    itterations=1 #10 series / scenarios for upcoming 700 days forecast 
    
    data = pd.DataFrame()
    data[stock] = wb.DataReader(stock   '.AX',data_source = 'yahoo', start= start) ['Close']

    log_returns = np.log(1   data.pct_change())
    u = log_returns.mean()
    var = log_returns.var()
    drift = u - (0.5 * var)
    stdev = log_returns.std()
    
    daily_returns = np.exp(drift.values   stdev.values * norm.ppf(np.random.rand(t_intervals,itterations)))
    last_stock_price = data.iloc[-1]
    price_list = np.zeros_like(daily_returns)
    price_list[0] = last_stock_price
    
    for i in range(1, t_intervals):
        price_list[i] = price_list[i - 1] * daily_returns[i]
      
    target = np.round(price_list[59],2)
    average_price = price_list.mean()
    std_price = price_list.std()
    std_price2 = 2*std_price
    min_price = price_list.min()
    max_price = price_list.max()
    low_vol = average_price - (1 * std_price)
    high_vol = average_price   (1 * std_price)
    rev_stats = list(np.around(np.array([min_price, max_price, average_price,std_price2]),2))
    rev_mov = list(np.around(np.array([high_vol,low_vol]),2))
    symbol_title = ("Predicting {} Price Using Brownian Motion for Next {} Days".format(stocks,t_intervals))
    
    fig = plt.figure(dpi=200, figsize=(14, 6))
    plt.title(symbol_title, fontsize=14)
    plt.style.use('dark_background')

    plt.grid()
    plt.grid(color='gray',linestyple=':',linewidth=0.5)
    plt.ylabel("Levels",fontsize=10)
    plt.xlabel("Days",fontsize=10)
    plt.axhspan(low_vol,high_vol,facecolor='0.7',alpha=0.4)
    plt.axhline(y=average_price,linewidth=1.2,color='red',linestyle='--',label="Average Level Line",lw=2)
    plt.legend(loc='lower left',fontsize=10)
    rev_mov=list(np.around(nparray([high_vol,low_vol]),2))
    x_pos=-1
    y_pos=average_price (1*std_price)
    plt.text(x_pos,y_pos,
                            '- Target Price : {} \n'
                            '- Expected Movement (95% Chances / 2 Standard Deviation)\n is between {} and {} \n- Minimum'
                            '- Maximum Level : {} \n- Average Level : {} \n'
                            '- Volatility : {}'.format(target,rev_mov[0],rev_mov[1],rev_stats[0],rev_stats[1])
    (plt.plot(price_list,linewidth=0.5))
    (plt.savefig("{}_next_30_days.png".format(stock)))
    
    return print (" {} Day Target Price : {} ".format(t_intervals,np.round(price_list[59],2)))

  • Related