-
Notifications
You must be signed in to change notification settings - Fork 13
/
queries.py
121 lines (91 loc) · 20.7 KB
/
queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import json
from urllib.parse import urlencode
class Queries(object):
@staticmethod
def read_user_id():
return json.dumps({"operationName":"ReadUserId","variables":{},"query":"query ReadUserId {\n readUser {\n ... on UserErrorResponse {\n errorMessages\n __typename\n }\n ... on User {\n id\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def read_user_portfolios():
return json.dumps({"operationName":"MyActiveGames","variables":{},"query":"query MyActiveGames {\n readUserPortfolios(input: {filter: ACTIVE}) {\n ... on UserDoesNotExistError {\n errorMessages\n __typename\n }\n ... on UserPortfoliosError {\n errorMessages\n __typename\n }\n ... on PagedPortfolioList {\n list {\n id\n game {\n id\n gameDetails {\n ... on GameDetails {\n active\n description\n endDate\n gameType\n id\n name\n numberOfPlayers\n ownerId\n rules {\n allowLateEntry\n allowMargin\n allowOptionTrading\n allowPortfolioResetting\n allowPortfolioViewing\n allowShortSelling\n cashInterestPercent\n commissionDollars\n commissionPerContractDollars\n dailyVolumePercent\n diversificationOptionsPercent\n diversificationPercent\n marginInterestPercent\n marketDelayMinutes\n minStockForMarginDollars\n minimumPriceDollars\n minimumPriceToShortDollars\n quickSellDurationMinutes\n startingCashDollars\n __typename\n }\n startDate\n owner {\n ... on UserResponse {\n id\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def portfolio_summary_query(portfolio_id):
return json.dumps({"operationName":"PortfolioSummary","variables":{"portfolioId":portfolio_id},"query":"query PortfolioSummary($portfolioId: String!) {\n readPortfolio(portfolioId: $portfolioId) {\n ... on Portfolio {\n summary {\n accountValue\n annualReturn\n buyingPower\n cash\n dayGainDollar\n dayGainPercent\n __typename\n }\n __typename\n }\n ... on PortfolioErrorResponse {\n errorMessages\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def open_stock_trades(portfolio_id):
return json.dumps({"operationName":"PendingStockTrades","variables":{"portfolioId":portfolio_id,"holdingType":"STOCKS"},"query":"query PendingStockTrades($portfolioId: String!, $holdingType: HoldingType!) {\n readPortfolio(portfolioId: $portfolioId) {\n ... on PortfolioErrorResponse {\n errorMessages\n __typename\n }\n ... on Portfolio {\n holdings(type: $holdingType) {\n ... on CategorizedStockHoldings {\n pendingTrades {\n stock {\n ... on Stock {\n description\n technical {\n lastPrice\n __typename\n }\n __typename\n }\n __typename\n }\n symbol\n transactionTypeDescription\n orderPriceDescription\n tradeId\n action\n cancelDate\n quantity\n quantityType\n transactionType\n limit {\n limit\n stop\n trailingStop {\n percentage\n price\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n ... on HoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedHoldingsErrorResponse {\n errorMessages\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def open_option_trades(portfolio_id):
return json.dumps({"operationName":"PendingOptionTrades","variables":{"portfolioId":portfolio_id,"holdingType":"OPTIONS"},"query":"query PendingOptionTrades($portfolioId: String!) {\n readPortfolio(portfolioId: $portfolioId) {\n ... on PortfolioErrorResponse {\n errorMessages\n __typename\n }\n ... on Portfolio {\n holdings(type: OPTIONS) {\n ... on HoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedHoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedOptionHoldings {\n pendingTrades {\n option {\n ... on Option {\n isPut\n expirationDate\n lastPrice\n strikePrice\n stock {\n ... on Stock {\n symbol\n technical {\n lastPrice\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n symbol\n transactionTypeDescription\n orderPriceDescription\n tradeId\n action\n cancelDate\n quantity\n quantityType\n transactionType\n limit {\n limit\n stop\n trailingStop {\n percentage\n price\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def open_short_trades(portfolio_id):
return json.dumps({"operationName":"PendingStockTrades","variables":{"portfolioId":portfolio_id,"holdingType":"SHORTS"},"query":"query PendingStockTrades($portfolioId: String!, $holdingType: HoldingType!) {\n readPortfolio(portfolioId: $portfolioId) {\n ... on PortfolioErrorResponse {\n errorMessages\n __typename\n }\n ... on Portfolio {\n holdings(type: $holdingType) {\n ... on CategorizedStockHoldings {\n pendingTrades {\n stock {\n ... on Stock {\n description\n technical {\n lastPrice\n __typename\n }\n __typename\n }\n __typename\n }\n symbol\n transactionTypeDescription\n orderPriceDescription\n tradeId\n action\n cancelDate\n quantity\n quantityType\n transactionType\n limit {\n limit\n stop\n trailingStop {\n percentage\n price\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n ... on HoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedHoldingsErrorResponse {\n errorMessages\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def cancel_order(order_id):
return json.dumps({"operationName":"CancelTrade","variables":{"input":order_id},"query":"mutation CancelTrade($input: String!) {\n submitCancelTrade(tradeId: $input) {\n ... on CancelTradeErrorResponse {\n errorMessages\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def refresh_token(refresh_token):
return urlencode({'grant_type': 'refresh_token', 'refresh_token': refresh_token, 'client_id': 'finance-simulator'})
@staticmethod
def stock_holdings(portfolio_id):
return json.dumps({"operationName":"StockHoldings","variables":{"portfolioId":portfolio_id,"holdingType":"STOCKS"},"query":"query StockHoldings($portfolioId: String!, $holdingType: HoldingType!) {\n readPortfolio(portfolioId: $portfolioId) {\n ... on PortfolioErrorResponse {\n errorMessages\n __typename\n }\n ... on Portfolio {\n holdings(type: $holdingType) {\n ... on HoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedHoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedStockHoldings {\n holdingsSummary {\n marketValue\n dayGainDollar\n dayGainPercent\n totalGainDollar\n totalGainPercent\n __typename\n }\n executedTrades {\n stock {\n ... on Stock {\n symbol\n description\n technical {\n lastPrice\n __typename\n }\n __typename\n }\n __typename\n }\n symbol\n quantity\n purchasePrice\n marketValue\n dayGainDollar\n dayGainPercent\n totalGainDollar\n totalGainPercent\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def option_holdings(portfolio_id):
return json.dumps({"operationName":"OptionHoldings","variables":{"portfolioId":portfolio_id,"holdingType":"OPTIONS"},"query":"query OptionHoldings($portfolioId: String!) {\n readPortfolio(portfolioId: $portfolioId) {\n ... on PortfolioErrorResponse {\n errorMessages\n __typename\n }\n ... on Portfolio {\n holdings(type: OPTIONS) {\n ... on HoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedHoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedOptionHoldings {\n holdingsSummary {\n marketValue\n dayGainDollar\n dayGainPercent\n totalGainDollar\n totalGainPercent\n __typename\n }\n executedTrades {\n option {\n ... on SymbolNotFoundResponse {\n errorMessages\n __typename\n }\n ... on InvalidSymbolResponse {\n errorMessages\n __typename\n }\n ... on Option {\n symbol\n isPut\n lastPrice\n expirationDate\n strikePrice\n stock {\n ... on Stock {\n symbol\n description\n technical {\n lastPrice\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n quantity\n purchasePrice\n marketValue\n dayGainDollar\n dayGainPercent\n totalGainDollar\n totalGainPercent\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def short_holdings(portfolio_id):
return json.dumps({"operationName":"StockHoldings","variables":{"portfolioId":portfolio_id,"holdingType":"SHORTS"},"query":"query StockHoldings($portfolioId: String!, $holdingType: HoldingType!) {\n readPortfolio(portfolioId: $portfolioId) {\n ... on PortfolioErrorResponse {\n errorMessages\n __typename\n }\n ... on Portfolio {\n holdings(type: $holdingType) {\n ... on HoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedHoldingsErrorResponse {\n errorMessages\n __typename\n }\n ... on CategorizedStockHoldings {\n holdingsSummary {\n marketValue\n dayGainDollar\n dayGainPercent\n totalGainDollar\n totalGainPercent\n __typename\n }\n executedTrades {\n stock {\n ... on Stock {\n symbol\n description\n technical {\n lastPrice\n __typename\n }\n __typename\n }\n __typename\n }\n symbol\n quantity\n purchasePrice\n marketValue\n dayGainDollar\n dayGainPercent\n totalGainDollar\n totalGainPercent\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def stock_search(symbol):
return json.dumps({"operationName":"StockSearch","variables":{"input":{"term":symbol}},"query":"query StockSearch($input: StockSearchInput!) {\n searchStockSymbols(input: $input) {\n list {\n symbol\n description\n __typename\n }\n totalSize\n __typename\n }\n}\n"})
@staticmethod
def stock_exchange(symbol):
return json.dumps({"operationName":"stockExchange","variables":{"symbol":symbol},"query":"query stockExchange($symbol: String!) {\n readStock(symbol: $symbol) {\n ... on Stock {\n exchange\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def stock_quote(symbol):
return json.dumps({"operationName":"CompanyProfile","variables":{"symbol":symbol},"query":"query CompanyProfile($symbol: String!) {\n readStock(symbol: $symbol) {\n ... on Stock {\n technical {\n volume\n dayHighPrice\n dayLowPrice\n askPrice\n bidPrice\n __typename\n }\n fundamental {\n lowestPriceLast52Weeks\n highestPriceLast52Weeks\n __typename\n }\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def validate_stock_trade(trade):
expiry = trade.expiration
limit = trade.order_limit
portfolio_id = trade.portfolio_id
quantity = trade.quantity
symbol = trade.symbol
transaction_type = trade.transaction_type
return json.dumps({"operationName":"PreviewStockTrade","variables":{"input":{"expiry":expiry,"limit":limit,"portfolioId":portfolio_id,"quantity":quantity,"symbol":symbol,"transactionType":transaction_type}},"query":"query PreviewStockTrade($input: TradeEntityInput!) {\n previewStockTrade(stockTradeEntityInput: $input) {\n ... on TradeDetails {\n bill {\n commission\n price\n quantity\n total\n __typename\n }\n __typename\n }\n ... on TradeInvalidEntity {\n errorMessages\n __typename\n }\n ... on TradeInvalidTransaction {\n errorMessages\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def execute_stock_trade(trade):
expiry = trade.expiration
limit = trade.order_limit
portfolio_id = trade.portfolio_id
quantity = trade.quantity
symbol = trade.symbol
transaction_type = trade.transaction_type
return json.dumps({"operationName":"StockTrade","variables":{"input":{"expiry":expiry,"limit":limit,"portfolioId":portfolio_id,"quantity":quantity,"symbol":symbol,"transactionType":transaction_type}},"query":"mutation StockTrade($input: TradeEntityInput!) {\n submitStockTrade(stockTradeEntityInput: $input) {\n ... on TradeInvalidEntity {\n errorMessages\n __typename\n }\n ... on TradeInvalidTransaction {\n errorMessages\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def option_expiration_dates(symbol):
return json.dumps({"operationName":"OptionExpiries","variables":{"symbol":symbol},"query":"query OptionExpiries($symbol: String!) {\n readOptionsExpirationDates(symbol: $symbol) {\n ... on OptionsExpirationDates {\n expirationDates\n __typename\n }\n ... on SymbolNotFoundResponse {\n errorMessages\n __typename\n }\n ... on InvalidSymbolResponse {\n errorMessages\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def options_by_expiration(symbol,expiration,option_scope):
options_limit = 1000
if option_scope == 'NEAR_THE_MONEY':
options_limit = 6
return json.dumps({"operationName":"OptionsByExpiration","variables":{"symbol":symbol,"expiration":expiration,"optionsLimit":options_limit,"optionFilter":option_scope},"query":"query OptionsByExpiration($symbol: String!, $expiration: Long!, $optionsLimit: Int!, $optionFilter: OptionStrikePriceRange) {\n readStock(symbol: $symbol) {\n ... on Stock {\n technical {\n lastPrice\n __typename\n }\n options(\n optionSearchInput: {limit: $optionsLimit, optionFilter: {expirationDate: $expiration, strikePriceRange: $optionFilter}}\n ) {\n ... on OptionsListsResponse {\n callOptions {\n list {\n symbol\n strikePrice\n lastPrice\n dayChangePrice\n dayChangePercent\n dayLowPrice\n dayHighPrice\n bidPrice\n askPrice\n volume\n openInterest\n isInTheMoney\n __typename\n }\n __typename\n }\n putOptions {\n list {\n symbol\n strikePrice\n lastPrice\n dayChangePrice\n dayChangePercent\n dayLowPrice\n dayHighPrice\n bidPrice\n askPrice\n volume\n openInterest\n isInTheMoney\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n __typename\n }\n ... on InvalidSymbolResponse {\n errorMessages\n __typename\n }\n ... on SymbolNotFoundResponse {\n errorMessages\n __typename\n }\n __typename\n }\n}\n"})
@staticmethod
def validate_option_trade(trade):
expiry = trade.expiration
limit = trade.order_limit
portfolio_id = trade.portfolio_id
quantity = trade.quantity
symbol = trade.symbol
transaction_type = trade.transaction_type
return json.dumps({"operationName":"PreviewOptionTrade","variables":{"input":{"expiry":expiry,"limit":limit,"portfolioId":portfolio_id,"quantity":quantity,"transactionType":transaction_type,"symbol":symbol}},"query":"query PreviewOptionTrade($input: TradeEntityInput!) {\n previewOptionTrade(optionTradeEntityInput: $input) {\n ... on TradeDetails {\n bill {\n commission\n price\n quantity\n total\n __typename\n }\n __typename\n }\n ... on TradeInvalidEntity {\n errorMessages\n __typename\n }\n ... on TradeInvalidTransaction {\n errorMessages\n __typename\n }\n __typename\n }\n}\n"}
)
@staticmethod
def execute_option_trade(trade):
expiry = trade.expiration
limit = trade.order_limit
portfolio_id = trade.portfolio_id
quantity = trade.quantity
symbol = trade.symbol
transaction_type = trade.transaction_type
return json.dumps({"operationName":"OptionTrade","variables":{"input":{"expiry":expiry,"limit":limit,"portfolioId":portfolio_id,"quantity":quantity,"transactionType":transaction_type,"symbol":symbol}},"query":"mutation OptionTrade($input: TradeEntityInput!) {\n submitOptionTrade(optionTradeEntityInput: $input) {\n ... on TradeInvalidEntity {\n errorMessages\n __typename\n }\n ... on TradeInvalidTransaction {\n errorMessages\n __typename\n }\n __typename\n }\n}\n"})