44
loading...
This website collects cookies to deliver better user experience
import requests
page = requests.get("https://marketwatch.com/investing/technology")
import pandas as pd
from bs4 import BeautifulSoup
soup = BeautifulSoup(page.content, "html.parser")
article_contents = soup.find_all("div", class_="article__content")
for article in article_contents:
headline = article.find("a", class_="link").text.strip()
ticker = article.find("span", class_="ticker__symbol")
headline_ticker = [headline, ticker]
headlines.append(headline_ticker)
columns = ["headline", "US_ticker"]
headlines_df = pd.DataFrame(headlines, columns=columns)
from nltk.sentiment.vader import SentimentIntensityAnalyzer
vader = SentimentIntensityAnalyzer()
scores = []
for headline in headlines_df.loc[:,"headline"]:
score = vader.polarity_scores(headline).get("compound")
scores.append(score)
headlines_df.loc[:,"score"] = scores
headlines_df = headlines_df.groupby("ticker").mean()
headlines_df.reset_index(level=0, inplace=True)
buy = []
sell = []
for index, row in headlines_df.iterrows():
if row['score'] > 0.5 and row['isin'] != 'No ISIN found':
buy.append(row['isin'])
if row['score'] < -0.5 and row['isin'] != 'No ISIN found':
sell.append(row['isin'])
orders = []
# place buy orders
for isin in buy:
side = 'buy'
order = requests.post(
f"https://paper-trading.lemon.markets/v1/orders/",
data={"isin": isin,
"expires_at": "p0d",
"side": side,
"quantity": 1,
"venue": "XMUN",
"space_id": YOUR-SPACE-ID},
headers={"Authorization": f"Bearer {<YOUR-API-KEY>}"}).json()
orders.append(order)
# place sell orders
for isin in sell:
side = 'sell'
order = requests.post(
f"https://paper-trading.lemon.markets/v1/orders/",
data={"isin": isin,
"expires_at": "p0d",
"side": side,
"quantity": 1,
"venue": "XMUN",
"space_id": YOUR-SPACE-ID},
headers={"Authorization": f"Bearer {<YOUR-API-KEY>}"}).json()
orders.append(order)
# activate orders
for order in orders:
order_id = order['results'].get('id')
requests.post(
f"https://paper-trading.lemon.markets/v1/orders/{order_id}/activate/",
headers={"Authorization": f"Bearer {<YOUR-API-KEY>}"})
print(f'Activated {order["results"].get("isin")}')