Merge pull request #453 from GamestonkTerminal/gov-contracts

This commit is contained in:
jmaslek
2021-05-21 20:32:26 -04:00
committed by GitHub
11 changed files with 402 additions and 49 deletions

View File

@@ -154,6 +154,7 @@
* [x] Add robinhood (@jmaslek) - [PR #229](https://github.com/DidierRLopes/GamestonkTerminal/pull/229)
* [x] Add Ally Invest (@jmaslek) - [PR #267](https://github.com/DidierRLopes/GamestonkTerminal/pull/267)
* [x] Degiro support (@deel) - [PR #381](https://github.com/DidierRLopes/GamestonkTerminal/pull/381)
* [x] Degiro improvement (@Chavithra) - [PR #445](https://github.com/DidierRLopes/GamestonkTerminal/pull/381)
**NEXT**
* [ ] Merge data from different brokers (@jmaslek)
@@ -212,6 +213,7 @@ ___
**NEXT**
* [ ] Add [Coinpaprika](https://coinpaprika.com/api/)
* [ ] Add top altcoin lists
---
## Screener
@@ -221,6 +223,7 @@ ___
* [x] Plot screener historical using Yahoo Finance data (@didier) - [PR #319](https://github.com/DidierRLopes/GamestonkTerminal/pull/319)
* [x] Add command to take tickers to portfolio optimization (@didier) - [PR #349](https://github.com/DidierRLopes/GamestonkTerminal/pull/349)
* [x] Add flags to save screeners and run papermill on returned tickers (@alokan) - [PR #414](https://github.com/DidierRLopes/GamestonkTerminal/pull/414)
* [x] Add several new screener presets (@Traceabl3) - [PR #447](https://github.com/DidierRLopes/GamestonkTerminal/pull/447)
**NEXT**
@@ -253,6 +256,8 @@ ___
## Government
* [x] Add congress data (@didier) [PR #438](https://github.com/DidierRLopes/GamestonkTerminal/pull/438)
* [x] Add senate and house data (@didier) [PR #446](https://github.com/DidierRLopes/GamestonkTerminal/pull/446)
* [x] Add government contracts (@didier) [PR #453](https://github.com/DidierRLopes/GamestonkTerminal/pull/453)
**NEXT**

View File

@@ -451,21 +451,25 @@ Command|Website
## Government [»](government/README.md)
Command|Website
----|----
`last_congress` |last congress trading
`buy_congress` |top buy congress tickers
`sell_congress` |top sell congress tickers
`last_senate` |last senate trading
`buy_senate` |top buy senate tickers
`sell_senate` |top sell senate tickers
`last_house` |last house trading
`buy_house` |top buy house tickers
`sell_house` |top sell house tickers
with ticker provided |
`last_congress` | last congress trading
`buy_congress` | plot top buy congress tickers
`sell_congress` | plot top sell congress tickers
`last_senate` | last senate trading
`buy_senate` | plot top buy senate tickers
`sell_senate` | plot top sell senate tickers
`last_house` | last house trading
`buy_house` | plot top buy house tickers
`sell_house` | plot top sell house tickers
`last_contracts` | last government contracts
`sum_contracts` | plot sum of last government contracts
with ticker provided |
`raw_congress` | raw congress trades on the ticker
`congress` | congress trades on the ticker
`congress` | plot congress trades on the ticker
`raw_senate` | raw senate trades on the ticker
`senate` | senate trades on the ticker
`senate` | plot senate trades on the ticker
`raw_house` | raw house trades on the ticker
`house` | house trades on the ticker
`house` | plot house trades on the ticker
`raw_contracts` | raw contracts on the ticker
`contracts` | plot sum of contracts on the ticker
 

View File

@@ -66,7 +66,7 @@ class CryptoController:
print(" candle show candles")
print(" balance show coin balance")
print("")
print(" >ta technical analysis menu for")
print("> ta technical analysis menu for")
print("")
def switch(self, an_input: str):

View File

@@ -6,37 +6,44 @@ This data has been provided by [quiverquant](https://www.quiverquant.com).
* [last_congress](#last_congress)
* last congress trading
* [buy_congress](#buy_congress)
* top buy congress tickers
* plot top buy congress tickers
* [sell_congress](#sell_congress)
* top sell congress tickers
* plot top sell congress tickers
* [last_senate](#last_senate)
* last senate trading
* [buy_senate](#buy_senate)
* top buy senate tickers
* plot top buy senate tickers
* [sell_senate](#sell_senate)
* top sell senate tickers
* plot top sell senate tickers
* [last_house](#last_house)
* last house trading
* [buy_house](#buy_house)
* top buy house tickers
* plot top buy house tickers
* [sell_house](#sell_house)
* top sell house tickers
* plot top sell house tickers
* [last_contracts](#last_contracts)
* last government contracts
* [sum_contracts](#sum_contracts)
* plot sum of last government contracts
#### WITH TICKER PROVIDED
* [raw_congress](#raw_congress)
* raw congress trades on the ticker
* [congress](#congress)
* congress trades on the ticker
* plot congress trades on the ticker
* [raw_senate](#raw_senate)
* raw senate trades on the ticke
* [senate](#senate)
* senate trades on the ticker
* plot senate trades on the ticker
* [raw_house](#raw_house)
* raw house trades on the ticker
* [house](#house)
* house trades on the ticker
* plot house trades on the ticker
* [raw_contracts](#raw_contracts)
* raw contracts on the ticker
* [contracts](#contracts)
* plot sum of contracts on the ticker
## last_congress <a name="last_congress"></a>
```text
@@ -146,6 +153,29 @@ Top sell house trading. [Source: www.quiverquant.com]
![sell_house](https://user-images.githubusercontent.com/25267873/118394645-c5a33580-b63d-11eb-8dbe-a9b9d948df24.png)
## last_contracts <a name="last_contracts"></a>
```text
usage: last_contracts [-p PAST_TRANSACTIONS_DAYS] [-l LIMIT_CONTRACTS]
```
Last contracts. [Source: www.quiverquant.com]
* -p : Past transaction months. Default: 2.
* -t : Number of top tickers. Default: 20.
<img width="1081" alt="last_contracts" src="https://user-images.githubusercontent.com/25267873/119065144-d3a5dd00-b9d4-11eb-85c9-ab16255a6996.png">
## sum_contracts <a name="sum_contracts"></a>
```text
usage: sum_contracts
```
Sum latest contracts. [Source: www.quiverquant.com]
![sum_contracts](https://user-images.githubusercontent.com/25267873/119065220-f33d0580-b9d4-11eb-8870-37ca352b187c.png)
#### WITH TICKER PROVIDED
## raw_congress <a name="raw_congress"></a>
```text
usage: raw_congress [-p PAST_TRANSACTIONS_DAYS]
@@ -210,3 +240,25 @@ House trading. [Source: www.quiverquant.com]
* -p : Past transaction months. Default: 6.
![house](https://user-images.githubusercontent.com/25267873/118394690-19158380-b63e-11eb-85ba-87fc2fd7df15.png)
## raw_contracts <a name="raw_contracts"></a>
```text
usage: raw_contracts [-p PAST_TRANSACTIONS_DAYS]
```
Raw contracts. [Source: www.quiverquant.com]
* -p : Past transaction months. Default: 10.
<img width="1109" alt="raw_contracts" src="https://user-images.githubusercontent.com/25267873/119065403-59298d00-b9d5-11eb-832c-3dd85d66bee1.png">
## contracts <a name="contracts"></a>
```text
usage: contracts [-p PAST_TRANSACTIONS_DAYS]
```
Contracts associated with ticker. [Source: www.quiverquant.com]
* -p : Past transaction months. Default: 10.
![contracts](https://user-images.githubusercontent.com/25267873/119065405-59c22380-b9d5-11eb-9a34-9cad288b22de.png)

View File

@@ -34,6 +34,10 @@ class GovController:
"sell_house",
"house",
"raw_house",
"last_contracts",
"sum_contracts",
"raw_contracts",
"contracts",
]
def __init__(
@@ -58,24 +62,28 @@ class GovController:
print("")
print("Explore:")
print(" last_congress last congress trading")
print(" buy_congress top buy congress tickers")
print(" sell_congress top sell congress tickers")
print(" buy_congress plot top buy congress tickers")
print(" sell_congress plot top sell congress tickers")
print(" last_senate last senate trading")
print(" buy_senate top buy senate tickers")
print(" sell_senate top sell senate tickers")
print(" buy_senate plot top buy senate tickers")
print(" sell_senate plot top sell senate tickers")
print(" last_house last house trading")
print(" buy_house top buy house tickers")
print(" sell_house top sell house tickers")
print(" buy_house plot top buy house tickers")
print(" sell_house plot top sell house tickers")
print(" last_contracts last government contracts")
print(" sum_contracts plot sum of last government contracts")
print("")
if self.ticker:
print(f"Ticker: {self.ticker}")
print(" raw_congress raw congress trades on the ticker")
print(" congress congress trades on the ticker")
print(" congress plot congress trades on the ticker")
print(" raw_senate raw senate trades on the ticker")
print(" senate senate trades on the ticker")
print(" senate plot senate trades on the ticker")
print(" raw_house raw house trades on the ticker")
print(" house house trades on the ticker")
print("")
print(" house plot house trades on the ticker")
print(" raw_contracts raw contracts on the ticker")
print(" contracts plot sum of contracts on the ticker")
print("")
return
def switch(self, an_input: str):
@@ -166,6 +174,22 @@ class GovController:
"""Process house command"""
quiverquant_view.government_trading(other_args, self.ticker, "house")
def call_last_contracts(self, other_args: List[str]):
"""Process last_contracts command"""
quiverquant_view.last_contracts(other_args)
def call_sum_contracts(self, other_args: List[str]):
"""Process sum_contracts command"""
quiverquant_view.sum_contracts(other_args)
def call_raw_contracts(self, other_args: List[str]):
"""Process raw_contracts command"""
quiverquant_view.raw_contracts(other_args, self.ticker)
def call_contracts(self, other_args: List[str]):
"""Process contracts command"""
quiverquant_view.contracts(other_args, self.ticker)
def menu(ticker: str):
"""Government Menu"""

View File

@@ -5,13 +5,13 @@ import pandas as pd
API_QUIVERQUANT_KEY = "5cd2a65e96d0486efbe926a7cdbc1e8d8ab6c7b3"
def get_government_trading(type: str, ticker: str = "") -> pd.DataFrame:
def get_government_trading(gov_type: str, ticker: str = "") -> pd.DataFrame:
"""Returns the most recent transactions by members of government
Parameters
----------
type: str
Type of government data between: Congress, Senate and House
gov_type: str
Type of government data between: Congress, Senate, House and Contracts
ticker : str
Ticker to get congress trading data from
@@ -21,7 +21,7 @@ def get_government_trading(type: str, ticker: str = "") -> pd.DataFrame:
Most recent transactions by members of U.S. Congress
"""
if type == "congress":
if gov_type == "congress":
if ticker:
url = (
f"https://api.quiverquant.com/beta/historical/congresstrading/{ticker}"
@@ -29,18 +29,26 @@ def get_government_trading(type: str, ticker: str = "") -> pd.DataFrame:
else:
url = "https://api.quiverquant.com/beta/live/congresstrading"
elif type == "senate":
elif gov_type == "senate":
if ticker:
url = f"https://api.quiverquant.com/beta/historical/senatetrading/{ticker}"
else:
url = "https://api.quiverquant.com/beta/live/senatetrading"
elif type == "house":
elif gov_type == "house":
if ticker:
url = f"https://api.quiverquant.com/beta/historical/housetrading/{ticker}"
else:
url = "https://api.quiverquant.com/beta/live/housetrading"
elif gov_type == "contracts":
if ticker:
url = (
f"https://api.quiverquant.com/beta/historical/govcontractsall/{ticker}"
)
else:
url = "https://api.quiverquant.com/beta/live/govcontractsall"
else:
return pd.DataFrame()
@@ -51,8 +59,11 @@ def get_government_trading(type: str, ticker: str = "") -> pd.DataFrame:
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return pd.DataFrame(response.json()).rename(
columns={"Date": "TransactionDate", "Senator": "Representative"}
)
if gov_type in ["congress", "senate", "house"]:
return pd.DataFrame(response.json()).rename(
columns={"Date": "TransactionDate", "Senator": "Representative"}
)
else:
return pd.DataFrame(response.json())
return pd.DataFrame()

View File

@@ -513,3 +513,251 @@ def raw_government(other_args: List[str], ticker: str, gov_type: str):
except Exception as e:
print(e, "\n")
def last_contracts(other_args: List[str]):
"""Last contracts
Parameters
----------
other_args : List[str]
Command line arguments to be processed with argparse
"""
parser = argparse.ArgumentParser(
add_help=False,
prog="last_contracts",
description="Last contracts. [Source: www.quiverquant.com]",
)
parser.add_argument(
"-p",
"--past_transactions_days",
action="store",
dest="past_transactions_days",
type=check_positive,
default=2,
help="Past transaction days",
)
parser.add_argument(
"-l",
"--limit",
action="store",
dest="limit_contracts",
type=check_positive,
default=20,
help="Limit of contracts to display",
)
try:
if other_args:
if "-" not in other_args[0]:
other_args.insert(0, "-p")
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
df_contracts = quiverquant_model.get_government_trading("contracts")
if df_contracts.empty:
print("No government contracts found\n")
return
df_contracts.sort_values("Date", ascending=False)
df_contracts["Date"] = pd.to_datetime(df_contracts["Date"])
df_contracts.drop_duplicates(inplace=True)
df_contracts = df_contracts[
df_contracts["Date"].isin(
df_contracts["Date"].unique()[: ns_parser.past_transactions_days]
)
]
df_contracts = df_contracts[
["Date", "Ticker", "Amount", "Description", "Agency"]
][: ns_parser.limit_contracts]
print(df_contracts.to_string(index=False))
print("")
except Exception as e:
print(e, "\n")
def sum_contracts(other_args: List[str]):
"""Sum contracts
Parameters
----------
other_args : List[str]
Command line arguments to be processed with argparse
"""
parser = argparse.ArgumentParser(
add_help=False,
prog="sum_contracts",
description="Sum latest contracts. [Source: www.quiverquant.com]",
)
try:
if other_args:
if "-" not in other_args[0]:
other_args.insert(0, "-p")
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
df_contracts = quiverquant_model.get_government_trading("contracts")
if df_contracts.empty:
print("No government contracts found\n")
return
df_contracts["Date"] = pd.to_datetime(df_contracts["Date"]).dt.date
df_contracts.drop_duplicates(inplace=True)
plt.figure(figsize=plot_autoscale(), dpi=PLOT_DPI)
df_contracts.groupby("Date").sum().div(1000).plot(
kind="bar", rot=0, ax=plt.gca()
)
plt.ylabel("Amount [1k $]")
plt.title("Sum of latest government contracts")
if gtff.USE_ION:
plt.ion()
plt.show()
print("")
except Exception as e:
print(e, "\n")
def raw_contracts(other_args: List[str], ticker: str):
"""Raw contracts
Parameters
----------
other_args : List[str]
Command line arguments to be processed with argparse
ticker: str
Ticker to get congress trading data from
"""
parser = argparse.ArgumentParser(
add_help=False,
prog="raw_contracts",
description="Raw contracts. [Source: www.quiverquant.com]",
)
parser.add_argument(
"-p",
"--past_transactions_days",
action="store",
dest="past_transactions_days",
type=check_positive,
default=10,
help="Past transaction days",
)
try:
if other_args:
if "-" not in other_args[0]:
other_args.insert(0, "-p")
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
df_contracts = quiverquant_model.get_government_trading("contracts", ticker)
if df_contracts.empty:
print("No government contracts found\n")
return
df_contracts["Date"] = pd.to_datetime(df_contracts["Date"]).dt.date
df_contracts.drop_duplicates(inplace=True)
df_contracts = df_contracts[
df_contracts["Date"].isin(
df_contracts["Date"].unique()[: ns_parser.past_transactions_days]
)
]
df_contracts.drop_duplicates(inplace=True)
print(df_contracts.to_string(index=False))
print("")
except Exception as e:
print(e, "\n")
def contracts(other_args: List[str], ticker: str):
"""Contracts
Parameters
----------
other_args : List[str]
Command line arguments to be processed with argparse
ticker: str
Ticker to get congress trading data from
"""
parser = argparse.ArgumentParser(
add_help=False,
prog="contracts",
description="Contracts associated with ticker. [Source: www.quiverquant.com]",
)
parser.add_argument(
"-p",
"--past_transactions_days",
action="store",
dest="past_transactions_days",
type=check_positive,
default=10,
help="Past transaction days",
)
try:
if other_args:
if "-" not in other_args[0]:
other_args.insert(0, "-p")
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
df_contracts = quiverquant_model.get_government_trading("contracts", ticker)
if df_contracts.empty:
print("No government contracts found\n")
return
df_contracts["Date"] = pd.to_datetime(df_contracts["Date"]).dt.date
df_contracts = df_contracts[
df_contracts["Date"].isin(
df_contracts["Date"].unique()[: ns_parser.past_transactions_days]
)
]
df_contracts.drop_duplicates(inplace=True)
plt.figure(figsize=plot_autoscale(), dpi=PLOT_DPI)
df_contracts.groupby("Date").sum().div(1000).plot(
kind="bar", rot=0, ax=plt.gca()
)
plt.ylabel("Amount [1k $]")
plt.title(f"Sum of latest government contracts to {ticker}")
if gtff.USE_ION:
plt.ion()
plt.show()
print("")
except Exception as e:
print(e, "\n")

View File

@@ -172,7 +172,9 @@ def arima(other_args: List[str], s_ticker: str, df_stock: pd.DataFrame):
seasonal=False,
information_criteria=ns_parser.s_ic,
)
l_predictions = model.predict(n_periods=ns_parser.n_days)
l_predictions = [
i if i > 0 else 0 for i in model.predict(n_periods=ns_parser.n_days)
]
# Prediction data
l_pred_days = get_next_stock_market_days(

View File

@@ -175,7 +175,7 @@ def exponential_smoothing(other_args: List[str], s_ticker: str, df_stock: pd.Dat
)
if model.mle_retvals.success:
forecast = model.forecast(ns_parser.n_days)
forecast = [i if i > 0 else 0 for i in model.forecast(ns_parser.n_days)]
l_pred_days = get_next_stock_market_days(
last_stock_day=df_stock["5. adjusted close"].index[-1],

View File

@@ -151,7 +151,9 @@ def k_nearest_neighbors(other_args: List[str], s_ticker: str, df_stock: pd.DataF
preds = knn.predict(X_valid.reshape(X_valid.shape[0], X_valid.shape[1]))
forecast_data = knn.predict(forecast_data_input.reshape(1, -1))
forecast_data_df = pd.DataFrame(forecast_data.T, index=future_dates)
forecast_data_df = pd.DataFrame(
[i if i > 0 else 0 for i in forecast_data.T], index=future_dates
)
print_pretty_prediction(
forecast_data_df[0], df_stock["5. adjusted close"].values[-1]
)

View File

@@ -172,9 +172,14 @@ def regression(
)
model.fit(stock_x, stock_y)
l_predictions = model.predict(
df_stock["5. adjusted close"].values[-ns_parser.n_inputs :].reshape(1, -1)
)[0]
l_predictions = [
i if i > 0 else 0
for i in model.predict(
df_stock["5. adjusted close"]
.values[-ns_parser.n_inputs :]
.reshape(1, -1)
)[0]
]
# Prediction data
l_pred_days = get_next_stock_market_days(