Skip to content

Commit d084f1b

Browse files
committed
Create OptionsData class
This provides functionality to retrieve options-specific data from Tradier though the OptionsData Tradier-derived class. It includes methods to fetch expiries, strike prices, contract-sizes, OCC symbols, etc.
1 parent 18b00fa commit d084f1b

4 files changed

Lines changed: 178 additions & 18 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(COMMON_SRC
3030
src/EquityOrder.cpp
3131
src/OptionsOrder.cpp
3232
src/EquitiesData.cpp
33+
src/OptionsData.cpp
3334
)
3435

3536

include/OptionsData.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// FILE: `include/OptionsData.h`
2+
#ifndef OPTIONS_DATA_H
3+
#define OPTIONS_DATA_H
4+
5+
#include <string>
6+
#include "Tradier.h"
7+
8+
class OptionsData : public Tradier {
9+
public:
10+
OptionsData(const std::string& accountNumber, const std::string& authToken, bool liveTrade);
11+
virtual ~OptionsData();
12+
nlohmann::json getChain(const std::string& symbol, const std::string& expiry, const bool greekAndVol=false) const;
13+
nlohmann::json getStrikesForExpiry(const std::string& symbol, const std::string& expiry, const bool includeAllRoots=false) const;
14+
nlohmann::json getOptionsContracts(const std::string& symbol, const bool strikePrice=false, const bool contractSize=false, const bool expiryFreq=false) const;
15+
nlohmann::json getOccsFromUnderlying(const std::string& symbol) const;
16+
17+
private:
18+
std::string OPTIONS_CHAINS_ENDPOINT;
19+
std::string OPTIONS_STRIKES_ENDPOINT;
20+
std::string OPTIONS_EXPIRIES_ENDPOINT;
21+
std::string OPTIONS_LOOKUP_ENDPOINT;
22+
};
23+
24+
#endif

src/OptionsData.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// FILE: `src/OptionsData.cpp`
2+
#include <iostream>
3+
#include "OptionsData.h"
4+
5+
OptionsData::OptionsData (const std::string& accountNumber, const std::string& authToken, bool liveTrade)
6+
: Tradier(accountNumber, authToken, liveTrade),
7+
OPTIONS_CHAINS_ENDPOINT("/v1/markets/options/chains"), // OptionsData::getChain
8+
OPTIONS_STRIKES_ENDPOINT("/v1/markets/options/strikes"), //
9+
OPTIONS_EXPIRIES_ENDPOINT("/v1/markets/options/expirations"),
10+
OPTIONS_LOOKUP_ENDPOINT("/v1/markets/options/lookup")
11+
{
12+
// whadduhya got whadduhya got
13+
}
14+
15+
OptionsData::~OptionsData () {}
16+
17+
18+
//
19+
// Retrieve Option Chain for a fixed (symbol, expiry) pair
20+
// https://documentation.tradier.com/brokerage-api/markets/get-options-chains
21+
//
22+
23+
nlohmann::json OptionsData::getChain(const std::string& symbol, const std::string& expiry, const bool greekAndVol) const {
24+
std::cout << "hello, getChain!" << std::endl;
25+
26+
std::string endpoint = OPTIONS_CHAINS_ENDPOINT + "?symbol=" + symbol
27+
+ "&expiration=" + expiry
28+
+ "&greeks=" + std::to_string(greekAndVol);
29+
try {
30+
auto response = sendGetRequest(endpoint);
31+
return response["options"]["option"];
32+
} catch (std::exception& e) {
33+
std::cerr << "Option Expiration getChain GET no good: " << e.what() << std::endl;
34+
return nlohmann::json();
35+
}
36+
37+
return nlohmann::json();
38+
}
39+
40+
41+
//
42+
// Get an options strike prices for a specified underlying ticker symbol (and contract expiration date?).
43+
// https://documentation.tradier.com/brokerage-api/markets/get-options-strikes
44+
//
45+
46+
nlohmann::json OptionsData::getStrikesForExpiry(const std::string& symbol, const std::string& expiry, const bool includeAllRoots) const {
47+
std::cout << "hello, getStrikesForExpiry!" << std::endl;
48+
49+
std::string endpoint = OPTIONS_STRIKES_ENDPOINT + "?symbol=" + symbol
50+
+ "&expiration=" + expiry
51+
+ "&includeAllRoots=" + std::to_string(includeAllRoots);
52+
53+
try {
54+
auto response = sendGetRequest(endpoint);
55+
return response["strikes"]["strike"];
56+
} catch (std::exception& e) {
57+
std::cerr << "Option strike GET request no good: " << e.what() << std::endl;
58+
return nlohmann::json();
59+
}
60+
61+
return nlohmann::json();
62+
}
63+
64+
65+
//
66+
// Get Spect About Options Contracts
67+
// Ex: {"contract_size":100,"date":"2024-06-28","expiration_type":"quarterlys"}
68+
//
69+
70+
nlohmann::json OptionsData::getOptionsContracts(const std::string& symbol, bool strikePrice, bool contractSize, bool expiryFreq) const {
71+
std::cout << "hello, getOptionsContracts!" << std::endl;
72+
73+
std::string endpoint = OPTIONS_EXPIRIES_ENDPOINT + "?symbol=" + symbol
74+
+ "&strikePrice=" + std::to_string(strikePrice)
75+
+ "&contractSize=" + std::to_string(contractSize)
76+
+ "&expirationType=" + std::to_string(expiryFreq);
77+
try {
78+
auto response = sendGetRequest(endpoint);
79+
return response["expirations"]["expiration"];
80+
} catch (std::exception& e) {
81+
std::cerr << "Option expiration GET no good: " << e.what() << std::endl;
82+
return nlohmann::json();
83+
}
84+
return nlohmann::json();
85+
}
86+
87+
88+
//
89+
// Retrieve All Listed Options Clearning Corp (OCC) Symbols for Stock
90+
// https://documentation.tradier.com/brokerage-api/markets/get-lookup-options-symbols
91+
//
92+
93+
nlohmann::json OptionsData::getOccsFromUnderlying(const std::string& symbol) const {
94+
std::cout << "hello, getOccsFromUnderlying!" << std::endl;
95+
std::string endpoint = OPTIONS_LOOKUP_ENDPOINT + "?underlying=" + symbol;
96+
try {
97+
auto response = sendGetRequest(endpoint);
98+
return response["symbols"];
99+
} catch (std::exception& e) {
100+
std::cerr << "Option symbol lookup no good: " << e.what() << std::endl;
101+
return nlohmann::json();
102+
}
103+
return nlohmann::json();
104+
}

src/main.cpp

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "EquitiesData.h"
55
#include "EquityOrder.h"
66
#include "OptionsOrder.h"
7+
#include "OptionsData.h"
78
#include "Tradier.h"
89
#include <iostream>
910
#include <iomanip>
@@ -66,13 +67,14 @@ int main() {
6667
bool liveTrade = false;
6768

6869
//
69-
// Instantiate Account, EquitiesData, EquityOrder, OptionsOrder classes
70+
// Instantiate Account, EquitiesData, EquityOrder, OptionsOrder, OptionsData classes
7071
//
7172

7273
Account myAccount(accountNumber, authToken, liveTrade);
7374
EquityOrder equityOrder(accountNumber, authToken, liveTrade);
7475
OptionsOrder optionsOrder(accountNumber, authToken, liveTrade);
7576
EquitiesData equitiesData(accountNumber, authToken, liveTrade);
77+
OptionsData optionsData(accountNumber, authToken, liveTrade);
7678

7779
//
7880
// Retrieve general account information
@@ -121,15 +123,16 @@ int main() {
121123

122124
std::cout << "!!!!! QUOTES - DOW 30 !!!!!" << std::endl;
123125

124-
std::string symbolList = "AMZN,AXP,AMGN,AAPL,BA,CAT,CSCO,CVX,GS,HD,HON,IBM,INTC,JNJ,KO,JPM,MCD,MMM,MRK,MSFT,NKE,PG,TRV,UNH,CRM,VZ,V,WMT,DIS,DOW";
125-
auto dowQuotes = equitiesData.getQuotes(symbolList);
126-
std::cout << dowQuotes << std::endl;
126+
// std::string symbolList = "AMZN,AXP,AMGN,AAPL,BA,CAT,CSCO,CVX,GS,HD,HON,IBM,INTC,JNJ,KO,JPM,MCD,MMM,MRK,MSFT,NKE,PG,TRV,UNH,CRM,VZ,V,WMT,DIS,DOW";
127+
// auto symbolListNoDividends = equitiesData.getQuotes(symbolList);
128+
// std::string symbolListNoDividends = "AMZN, BKNG, CMG, CRM, SHOP, UBER, SQ, SNAP, GΟΟG";
129+
// std::cout << symbolListNoDividends << std::endl;
127130

128131
//
129132
// Print Quotes in Sequential Blocks
130133
//
131134

132-
// for (const auto& quote : dowQuotes) {
135+
// for (const auto& quote : symbolListNoDividends) {
133136
// std::cout << "Symbol: " << quote["symbol"] << std::endl;
134137
// std::cout << "Ask: " << quote["ask"] << std::endl;
135138
// std::cout << "Bid: " << quote["bid"] << std::endl;
@@ -151,17 +154,17 @@ int main() {
151154
<< std::endl;
152155
std::cout << std::setfill('-') << std::setw(55) << "" << std::setfill(' ') << std::endl;
153156

154-
for (const auto& quote : dowQuotes) {
155-
std::cout << std::left
156-
<< std::setw(10) << quote["symbol"].get<std::string>() << std::fixed << std::setprecision(2)
157-
<< std::setw(10) << quote["ask"].get<double>()
158-
<< std::setw(10) << quote["bid"].get<double>()
159-
<< std::setw(10) << quote["change"].get<double>()
160-
<< std::setw(10) << quote["change_percentage"].get<double>()
161-
<< std::endl;
162-
}
163-
std::cout << std::setfill('-') << std::setw(55) << "" << std::setfill(' ') << std::endl;
164-
std::cout << std::endl;
157+
// for (const quote : symbolListNoDividends) {
158+
// std::cout << std::left
159+
// << std::setw(10) << quote["symbol"].get<std::string>() << std::fixed << std::setprecision(2)
160+
// << std::setw(10) << quote["ask"].get<double>()
161+
// << std::setw(10) << quote["bid"].get<double>()
162+
// << std::setw(10) << quote["change"].get<double>()
163+
// << std::setw(10) << quote["change_percentage"].get<double>()
164+
// << std::endl;
165+
// }
166+
// std::cout << std::setfill('-') << std::setw(55) << "" << std::setfill(' ') << std::endl;
167+
// std::cout << std::endl;
165168

166169

167170
//
@@ -206,7 +209,6 @@ int main() {
206209
std::string duration = "day";
207210

208211
auto response = optionsOrder.order(occSymbol, orderType, side, quantity, underlying, limitPrice, stopPrice, duration);
209-
// std::cout << "Broker Response: " << response.dump(4) << std::endl;
210212
std::cout << std::setfill('-') << std::setw(55) << "" << std::setfill(' ') << std::endl;
211213
std::cout << std::endl;
212214

@@ -227,6 +229,33 @@ int main() {
227229
std::cerr << "Exception occurred: " << e.what() << std::endl;
228230
}
229231

232+
233+
//
234+
// OPTIONS DATA
235+
//
236+
237+
std::cout << "!!!!! EQUITY DATA !!!!!" << std::endl;
238+
239+
nlohmann::json dupontChain = optionsData.getChain("DD", "2024-06-28");
240+
std::cout << "Dupont Chain: " << dupontChain.dump(4) << std::endl;
241+
std::cout << std::endl;
242+
243+
nlohmann::json dupontStrikes = optionsData.getStrikesForExpiry("DD", "2024-06-28");
244+
std::cout << "Dupont Strikes Jun28: " << dupontStrikes.dump(4) << std::endl;
245+
std::cout << std::endl;
246+
247+
nlohmann::json dupontContracts = optionsData.getOptionsContracts("DD", true, true, true);
248+
std::cout << "Dupont Contracts: " << dupontContracts << std::endl;
249+
std::cout << std::endl;
250+
251+
nlohmann::json dupontOccs = optionsData.getOccsFromUnderlying("DD");
252+
std::cout << "Dupont OCC Symbols" << std::endl;
253+
std::cout << dupontOccs << std::endl;
254+
std::cout << std::endl;
255+
256+
std::cout << std::setfill('-') << std::setw(55) << "" << std::setfill(' ') << std::endl;
257+
std::cout << std::endl;
258+
230259
return 0;
231260
}
232261

@@ -275,4 +304,6 @@ int main() {
275304

276305

277306
//
278-
//
307+
// ERROR
308+
//
309+

0 commit comments

Comments
 (0)