반응형
오늘은 무위험 수익률, 옵션 만기까지 남은 기간을 구하는 코드를 만들어봅시다.
일단 FinanceDataReader 모듈로 금리 데이터를 받는건 쉬우니까
ChatGPT를 사용하지 않겠습니다.
import finance-dataReader as fdr
df2 = fdr.DataReader('FRED:FEDFUNDS', data_source='fred')
print(df2.tail(1))
df2.plot()
이렇게 나오네요.
코드가 아주 간단하죠?
가장 최근 금리정보를 얻으려면
print(df2[-1])을 입력하면 됩니다.
일단 Deribit에서 옵션 ticker가 어떤 형식으로 나오는지 알아봅시다.
'BTC/USD:BTC-230127-21500-C' 이렇게 나옵니다.
이제 ChatGPT를 써봅시다.
"'BTC/USD:BTC-230127-21500-c'를 -를 기준으로 datetime.datetime이
인식할 수 있는 형식으로 바꾸는 파이썬 코드를 짜줘"
"'BTC/USD:BTC-230127-21500-C' Write a Python code that splits based on - and converts it to a date format that datetime.timedelta can recognize."
이렇게 나옵니다.
from datetime import datetime, timedelta
# Define the input date string
date_string = "BTC/USD:BTC-230127-21500-C"
# Split the string by '-'
date_string_split = date_string.split("-")
# Extract the date string from the list
date_string = date_string_split[1]
# Convert the date string to a datetime object
date_object = datetime.strptime(date_string, "%y%m%d")
# Create a timedelta object with the date object
date_delta = timedelta(days=date_object.day, months=date_object.month, years=date_object.year)
#Print the date delta
print(date_delta)
실행해보니 timedelta에서 오류가 발생합니다.
현재와의 차이를 구하는 코드와
오류가 발생하는 부분을 조금 수정해봅시다.
from datetime import datetime, timedelta
# Define the input date string
date_string = "BTC/USD:BTC-230127-21500-C"
# Split the string by '-'
date_string_split = date_string.split("-")
# Extract the date string from the list
date_string = date_string_split[1]
# Convert the date string to a datetime object
date_object = datetime.strptime(date_string, "%y%m%d")
print("만기일 :",date_object)
now = datetime.now()
print("현재 :", now)
date_diff = date_object - now
print("차이 :", date_diff)
# Create a timedelta object with the date object
date_delta = timedelta(days=date_object.day, months=date_object.month, years=date_object.year)
#Print the date delta
print(date_delta)
더보기
만기일 : 2023-01-27 00:00:00
현재 : 2023-01-24 20:32:49.393160
차이 : 2 days, 3:27:10.606840
이렇게 나옵니다.
잘 작동하죠?
다음번에는 1편에서 만든 가격계산함수에
그동안 만든 함수를 합쳐보겠습니다.
반응형
'코딩 공부 > python' 카테고리의 다른 글
온체인 데이터를 이용한 지표 웹사이트 쉽게 만들기(1) (0) | 2023.02.04 |
---|---|
ChatGPT와 함께 커버드 콜 전략 자동실행 알고리즘 짜기 (4) (0) | 2023.01.25 |
ChatGPT와 함께 커버드 콜 전략 자동실행 알고리즘 짜기 (2) (0) | 2023.01.24 |
ChatGPT와 함께 커버드 콜 전략 자동실행 알고리즘 짜기 (0) | 2023.01.22 |
볼린저 밴드와 현금흐름 지표를 이용해 매수, 매도 타이밍 구하기 (1) | 2022.03.02 |
댓글