I am having trouble with an API call response where when I try to reference a variable from the JSON data, it returns 0, even though the data received is not 0. I’m using Newtonsoft.Json to deserialize it.
This is the model that I use as a reference for handling the JSON:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
namespace OptionsChainResponse
{
[System.Serializable]
public class Greeks
{
public float delta;
public float gamma;
public float theta;
public float vega;
public float rho;
public float phi;
public float bid_iv;
public float mid_iv;
public float ask_iv;
public float smv_vol;
public string updated_at;
}
[System.Serializable]
public class Quote
{
public string symbol;
public string description;
public string exch;
public string type;
public float last;
public float change;
public int volume;
public float open;
public float high;
public float low;
public float close;
public float bid;
public float ask;
public string underlying;
public float strike;
public Greeks greeks;
public float change_percentage;
public int average_volume;
public int last_volume;
public long trade_date;
public float prevclose;
public float week_52_high;
public float week_52_low;
public int bidsize;
public string bidexch;
public long bid_date;
public int asksize;
public string askexch;
public long ask_date;
public int open_interest;
public int contract_size;
public string expiration_date;
public string expiration_type;
public string option_type;
public string root_symbol;
}
[System.Serializable]
public class Quotes
{
public Quote quote;
}
[System.Serializable]
public class Root
{
public Quotes quotes;
}
}
As I said, when I reference any of the above variables in another script, they return a value of 0, and I cannot figure out why. The portion of the script that I am running into problems with is as below:
IEnumerator GetOptionChainData()
{
ExpirationDate = (Expiration_Year.ToString() + Expiration_Month.ToString() + Expiration_Day.ToString());
AnalyzeButton1.text = "Loading...";
AnalyzeButton2.text = "Loading...";
string uri = "https://api.tradier.com/v1/markets/quotes?symbols=" + Ticker + ExpirationDate + PutOrCallString + StrikePriceString + "&greeks=true";
using (UnityWebRequest request = UnityWebRequest.Get(uri))
{
request.SetRequestHeader("Authorization", "Bearer " + TradierAPIKey);
request.SetRequestHeader("Accept", "application/json");
yield return request.SendWebRequest();
if ((request.isNetworkError) || (request.isHttpError))
{
AnalyzeButton1.text = "Error!";
AnalyzeButton2.text = "Error!";
Debug.Log("Quote Error:" + request.error);
}
else
{
if (request.isDone)
{
var opt1 = JsonConvert.DeserializeObject<OptionsChainResponse.Greeks>(request.downloadHandler.text);
if (request.downloadHandler.text != null && request.downloadHandler.text != "")
{
ImpliedVolatility = opt1.mid_iv; //Implied volatility of options
Theta = opt1.theta; //Theta of the contract
Gamma = opt1.gamma; //Gamma of the contract
}
var opt2 = JsonConvert.DeserializeObject<OptionsChainResponse.Quote>(request.downloadHandler.text);
if (request.downloadHandler.text != null && request.downloadHandler.text != "")
{
OptionsChainResponse = request.downloadHandler.text;
//MaxPain; //Maximum Pain Price
//MaxPainString;
OpenInterest = opt2.open_interest; //Open interest of options
Bid = opt2.bid; //Bid price of the contract
Mid = ((opt2.bid + opt2.ask) / 2); //Mid price of the contract
Ask = opt2.ask; //Ask price of the contract
if (Price > StrikePrice)
{
ITMText.SetActive(true);
OTMText.SetActive(false);
}
if (Price <= StrikePrice)
{
OTMText.SetActive(true);
ITMText.SetActive(false);
}
Debug.Log(OptionsChainResponse);
}
}
}
}
}
I have no idea what I’m doing wrong at this point, as when I Debug.Log the response that I get, all of the data is there properly, so it has to be something with deserializing the JSON data. I’m not sure if it would help here, but the response from the API would look as follows:
{
"quotes": {
"quote": {
"symbol": "AAPL220121P00167500",
"description": "AAPL Jan 21 2022 $167.50 Put",
"exch": "Z",
"type": "option",
"last": 2.45,
"change": 0.00,
"volume": 0,
"open": null,
"high": null,
"low": null,
"close": null,
"bid": 2.3,
"ask": 2.47,
"underlying": "AAPL",
"strike": 167.5,
"greeks": {
"delta": -0.484713,
"gamma": 0.0999226,
"theta": -0.392862,
"vega": 0.0498053,
"rho": 0.00441615,
"phi": -0.0044981,
"bid_iv": 0.335705,
"mid_iv": 0.337713,
"ask_iv": 0.339721,
"smv_vol": 0.332,
"updated_at": "2022-01-19 20:33:07"
},
"change_percentage": 0.00,
"average_volume": 0,
"last_volume": 50,
"trade_date": 1642625993207,
"prevclose": 2.45,
"week_52_high": 0.0,
"week_52_low": 0.0,
"bidsize": 0,
"bidexch": "W",
"bid_date": 1642665600000,
"asksize": 0,
"askexch": "W",
"ask_date": 1642665600000,
"open_interest": 18476,
"contract_size": 100,
"expiration_date": "2022-01-21",
"expiration_type": "standard",
"option_type": "put",
"root_symbol": "AAPL"
}
}
}
Any help here would be immensely appreciated as I’ve tried searching for an answer but I cannot find anything about this specific problem. Thank you for your time.