-2

我对 td ameritrade 的“价格历史记录”api 返回的 json 字符串有疑问。在我的 python 代码中,我能够将蜡烛数据放入 pd 数据框中。我使用 CBOE/NYSE 等各种网站来创建我的代码列表,但是我注意到一些代码,例如 AACQ,不返回任何数据。如果在 json 字符串的“empty = true”部分创建一个 If 循环,则可以处理此问题。这是通用 json 响应摘要

//CandleList:
{
  "candles": [
    {
      "close": 0,
      "datetime": 0,
      "high": 0,
      "low": 0,
      "open": 0,
      "volume": 0
    }
  ],
  "empty": false,
  "symbol": "string"
}

我通过以下方式获取 OHLC 数据

    # define the payload
    payload = {'apikey':client_id,
               'periodType':'day',
               'period':'10',
               'frequencyType':'minute',
               'frequency':'1',
               'needExtendedHoursData':'false'}

    # make a request content 
    time.sleep(0.501)
    content = requests.get(url = endpoint, params = payload)

    # convert it dictionary object

    data = content.json()
    #print(data)    
    hist = pd.DataFrame(data['candles'])

在我看来,json 字符串可以制成 3 个对象,1 个“蜡烛”、2 个“空”和 3 个“符号”。我的这种想法是否正确,我该怎么做。我尝试过使用“空”来处理我的代码并创建一个数据框,但这不起作用。

4

1 回答 1

0

我能够使用 if 和 continue 语句在我的 while 循环中完成我想要的

if df1.empty == 1:
        continue

但我仍然很好奇我是否如何处理后两个对象,甚至对选项链 json 响应更加好奇,即

//OptionChain:
{
  "symbol": "string",
  "status": "string",
  "underlying": {
    "ask": 0,
    "askSize": 0,
    "bid": 0,
    "bidSize": 0,
    "change": 0,
    "close": 0,
    "delayed": false,
    "description": "string",
    "exchangeName": "string",
    "fiftyTwoWeekHigh": 0,
    "fiftyTwoWeekLow": 0,
    "highPrice": 0,
    "last": 0,
    "lowPrice": 0,
    "mark": 0,
    "markChange": 0,
    "markPercentChange": 0,
    "openPrice": 0,
    "percentChange": 0,
    "quoteTime": 0,
    "symbol": "string",
    "totalVolume": 0,
    "tradeTime": 0
  },
  "strategy": "'SINGLE' or 'ANALYTICAL' or 'COVERED' or 'VERTICAL' or 'CALENDAR' or 'STRANGLE' or 'STRADDLE' or 'BUTTERFLY' or 'CONDOR' or 'DIAGONAL' or 'COLLAR' or 'ROLL'",
  "interval": 0,
  "isDelayed": false,
  "isIndex": false,
  "daysToExpiration": 0,
  "interestRate": 0,
  "underlyingPrice": 0,
  "volatility": 0,
  "callExpDateMap": "object",
  "putExpDateMap": "object"
}
 //StrikePriceMap:
{}
 //Option:
{
  "putCall": "'PUT' or 'CALL'",
  "symbol": "string",
  "description": "string",
  "exchangeName": "string",
  "bidPrice": 0,
  "askPrice": 0,
  "lastPrice": 0,
  "markPrice": 0,
  "bidSize": 0,
  "askSize": 0,
  "lastSize": 0,
  "highPrice": 0,
  "lowPrice": 0,
  "openPrice": 0,
  "closePrice": 0,
  "totalVolume": 0,
  "quoteTimeInLong": 0,
  "tradeTimeInLong": 0,
  "netChange": 0,
  "volatility": 0,
  "delta": 0,
  "gamma": 0,
  "theta": 0,
  "vega": 0,
  "rho": 0,
  "timeValue": 0,
  "openInterest": 0,
  "isInTheMoney": false,
  "theoreticalOptionValue": 0,
  "theoreticalVolatility": 0,
  "isMini": false,
  "isNonStandard": false,
  "optionDeliverablesList": [
    {
      "symbol": "string",
      "assetType": "string",
      "deliverableUnits": "string",
      "currencyType": "string"
    }
  ],
  "strikePrice": 0,
  "expirationDate": "string",
  "expirationType": "string",
  "multiplier": 0,
  "settlementType": "string",
  "deliverableNote": "string",
  "isIndexOption": false,
  "percentChange": 0,
  "markChange": 0,
  "markPercentChange": 0
}
 //Underlying:
{
  "ask": 0,
  "askSize": 0,
  "bid": 0,
  "bidSize": 0,
  "change": 0,
  "close": 0,
  "delayed": false,
  "description": "string",
  "exchangeName": "'IND' or 'ASE' or 'NYS' or 'NAS' or 'NAP' or 'PAC' or 'OPR' or 'BATS'",
  "fiftyTwoWeekHigh": 0,
  "fiftyTwoWeekLow": 0,
  "highPrice": 0,
  "last": 0,
  "lowPrice": 0,
  "mark": 0,
  "markChange": 0,
  "markPercentChange": 0,
  "openPrice": 0,
  "percentChange": 0,
  "quoteTime": 0,
  "symbol": "string",
  "totalVolume": 0,
  "tradeTime": 0
}
 //ExpirationDate:
{
  "date": "string"
}
 //OptionDeliverables:
{
  "symbol": "string",
  "assetType": "string",
  "deliverableUnits": "string",
  "currencyType": "string"
}

我一直在试图弄清楚如何将这些数据带入我的数据库。

于 2021-10-07T19:51:39.710 回答