
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
작성한 코드 및 에러 메세지
def get_return(code,n):
df=fdr.DataReader(code,'2018')
df=df[['Close']].copy()
df['ma']=df.rolling(n).mean().shift(1)
df['action']=np.where(df['Close']>df['ma'],'buy','sell')
df.iloc[-1,-1]='sell'
cond1= (df['action'] == 'buy') & (df['action'].shift(1)=='sell')
cond2= (df['action'] == 'sell') & (df['action'].shift(1)=='buy')
df_buy = df[cond1].reset_index()
df_buy.columns = ['날짜', '종가(buy)','이평값','액션']
df_sell = df[cond2].reset_index()
df_sell.columns = ['날짜', '종가(sell)','이평값','액션']
df.result = pd.concat([df_buy,df_sell],axis=1)
df_result['수익률'] = df_result['종가(sell)']/df_result['종가(buy)']
return df_result[['수익률']].cumprod().iloc[-1,-1] - 1
get_return('005930',3)
<ipython-input-22-c24c16386715>:15: UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
df.result = pd.concat([df_buy,df_sell],axis=1)
0.20833942432949182
get_return('066570',3)
<ipython-input-22-c24c16386715>:15: UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access df.result = pd.concat([df_buy,df_sell],axis=1) 0.20833942432949182
