Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.2 kB
7
Indexable
Never
import os
import sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

import json
import requests

import thetadata


from datetime import date
from thetadata import (
    ThetaClient,
    DateRange,
    StockReqType,
    SecType
)


START_DATE = "20230901"
END_DATE = "20230901"
STRIKE_MULTIPLIER = 1_000
STRIKE = str(250 * STRIKE_MULTIPLIER)
EXP = "20230908"
SIDE = "C"
TICKER = "TSLA"



import http.client

conn = http.client.HTTPConnection("127.0.0.1:25510")

headers = { 'Accept': "application/json" }

# request_str_base = "/hist/option/trade"
# request_str_base = '/hist/option/trade_greeks_second_order'
request_str_base = '/hist/option/trade_quote'
request_str = f"{request_str_base}?root={TICKER}&start_date={START_DATE}&end_date={END_DATE}&strike={STRIKE}&exp={EXP}&right={SIDE}"
print("Request string: {}".format(request_str))

conn.request("GET", 
             request_str, headers=headers)

res = conn.getresponse()
data = res.read()


res = json.loads(data.decode('utf-8'))
print(type(res['response']))
print(res['header'])

response_df = pd.DataFrame(columns=res['header']['format'], data=np.array(res['response']))
response_df