Fix getpoly to return only US stocks (#801)

* Fix getpoly to return only US stocks

* Fix addresses #757
* Getpoly will only return stocks listed on US stock exchanges.
* Index/mutual funds will also not be returned by getpoly.

* Remove unneeded else condition

* Add --us_only flag for getpoly to get US stocks only

* Fix black check error
This commit is contained in:
Sugandh
2021-10-25 21:58:15 +02:00
committed by GitHub
parent becdbb5bf1
commit 55768aefe2
2 changed files with 21 additions and 2 deletions

View File

@@ -240,12 +240,22 @@ Finviz:
prog="getpoly",
description="""Get similar companies from polygon to compare with.""",
)
parser.add_argument(
"-u",
"--us_only",
action="store_true",
default=False,
dest="us_only",
help="Show only stocks from the US stock exchanges",
)
try:
ns_parser = parse_known_args_and_warn(parser, other_args)
if not ns_parser:
return
self.similar, self.user = polygon_model.get_similar_companies(self.ticker)
self.similar, self.user = polygon_model.get_similar_companies(
self.ticker, ns_parser.us_only
)
if self.ticker.upper() in self.similar:
self.similar.remove(self.ticker.upper())

View File

@@ -7,7 +7,7 @@ import requests
from gamestonk_terminal import config_terminal as cfg
def get_similar_companies(ticker: str) -> Tuple[List[str], str]:
def get_similar_companies(ticker: str, us_only: bool) -> Tuple[List[str], str]:
"""Get similar companies from Polygon
Parameters
@@ -29,6 +29,15 @@ def get_similar_companies(ticker: str) -> Tuple[List[str], str]:
if result.status_code == 200:
similar = result.json()["similar"]
source = "Polygon"
if us_only:
us_similar = []
mkw_link = "https://www.marketwatch.com/investing/stock/"
for sym in similar:
prep_link = mkw_link + sym
sent_req = requests.get(prep_link)
if prep_link == sent_req.request.url:
us_similar.append(sym)
similar = us_similar
else:
print(result.json()["error"])
similar = [""]