커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
숙제
undefined주차
북마크
장*림
댓글
1
추천
0
조회수
8
조회수
8
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


코드를 따로 실행하니 어떤 위치에 함수가 들어가 활용되는지 헷갈려

코드를 합쳐 작성하였을 경우 함수의 위치가 어디에 들어가야 하는지 궁금합니다



def get_return_sl(code,short,long) :

df = fdr.DataReader(code,'2018')


df = df[['Close']].copy()


df['ma1']=df['Close'].rolling(short).mean().shift(1)

df['ma2']=df['Close'].rolling(long).mean().shift(1)


df['action'] = np.where(df['ma1'] > df['ma2'],'buy','sell')


df.iloc[-1,-1] = 'sell'


cond_1=(df['action']=='buy') & (df['action'].shift(1) =='sell')

cond_2=(df['action']=='sell') & (df['action'].shift(1) =='buy')


df_buy = df[cond_1].reset_index()

df_buy.colums=['date','close','ma1','ma2','action']


df_sell = df[cond_2].reset_index()

df_sell.colums=['date','close','ma1','ma2','action']


df_result = pd.concat([df_buy,df_sell],axis=1)


df_result['수익률'] = df_sell['Close'] / df_buy['Close']


df_final = (df_result[['수익률']].cumprod().tail(1)-1)*100

df_final['단기'] = short

df_final['장기'] = long


return df_final




dfs=[]

for short in range(3,11):

for long in range(30,61):

df = get_return_sl('005930',short,long)

dfs.append(df)


df_result = pd.concat(dfs)

df_result.sort_values(by = '수익률',ascending = False)







취소
 공유
취소
댓글 0
댓글 알림
나의얼굴