Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Exception during registration:
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.CommandLine.Invocation.Process.StartProcess(String command, String args, String workingDir, Action`1 stdOut, Action`1 stdErr, ValueTuple`2[] environmentVariables)
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_1>d.MoveNext()
38 changes: 38 additions & 0 deletions black-scholes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import requests
from scipy.stats import norm
from math import log, sqrt, exp

# Function to fetch real-time stock price from Alpha Vantage API
def get_stock_price(symbol):
url = f"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return float(data['Global Quote']['05. price'])
else:
print(f"Failed to fetch stock price for {symbol}.")
return None

# Function to calculate the Black-Scholes call option price
def black_scholes_call(S, K, r, sigma, T):
d1 = (log(S / K) + (r + 0.5 * sigma**2) * T) / (sigma * sqrt(T))
d2 = d1 - sigma * sqrt(T)
return S * norm.cdf(d1) - K * exp(-r * T) * norm.cdf(d2)

# Function to calculate the Black-Scholes put option price
def black_scholes_put(S, K, r, sigma, T):
d1 = (log(S / K) + (r + 0.5 * sigma**2) * T) / (sigma * sqrt(T))
d2 = d1 - sigma * sqrt(T)
return K * exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)

# Example usage
if __name__ == "__main__":
symbol = "AAPL" # Stock symbol
K = 200 # Strike price
r = 0.05 # Risk-free interest rate
sigma = 0.2 # Volatility
T = 1 # Time to expiration (in years)


S = get_stock_price(symbol)

88 changes: 88 additions & 0 deletions qualAnalysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import requests
import logging
import json
from functools import lru_cache

# Initialize logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

@lru_cache(maxsize=128)
def get_sentiment_score(article):
"""
Function to get sentiment score of a news article using NLTK Vader sentiment analysis.
"""
try:
sid = SentimentIntensityAnalyzer()
sentiment_score = sid.polarity_scores(article)['compound']
return sentiment_score
except Exception as e:
logger.error("Error getting sentiment score: %s", str(e))
return 0

@lru_cache(maxsize=128)
def get_news_articles(symbol, news_sources):
"""
Function to get news articles related to a given symbol from News API.
"""
try:
url = f'https://newsapi.org/v2/everything?q={symbol}&sources={news_sources}'
response = requests.get(url)
data = response.json()
if 'articles' in data:
articles = [article['description'] for article in data['articles']]
return articles
else:
return []
except Exception as e:
logger.error("Error getting news articles: %s", str(e))
return []

@lru_cache(maxsize=128)
def get_analyst_recommendations(symbol):
"""
Function to get analyst recommendations for a given symbol from Alpha Vantage.
"""
try:
url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol={symbol}'
response = requests.get(url)
data = response.json()
if 'AnalystRating' in data:
return int(data['AnalystRating']) # Convert to int
else:
return 0
except Exception as e:
logger.error("Error getting analyst recommendations: %s", str(e))
return 0

def get_financial_sentiment(symbol, news_sources):
"""
Function to compute the overall strength value based on sentiment analysis of news articles and analyst recommendations.
"""
try:
articles = get_news_articles(symbol, news_sources)
if not articles:
logger.warning("No news articles found for symbol %s", symbol)
return 0

total_sentiment_score = sum(get_sentiment_score(article) for article in articles) / len(articles)
analyst_recommendations = get_analyst_recommendations(symbol)
overall_strength = total_sentiment_score + analyst_recommendations
return overall_strength
except Exception as e:
logger.error("Error computing overall strength: %s", str(e))
return 0

if __name__ == "__main__":
try:
# Configuration
company = 'AAPL' # Specify the company symbol
news_sources = 'bbc-news, bloomberg, cnn, reuters' # Specify news sources

# Get overall strength
strength = get_financial_sentiment(company, news_sources)
print("Overall Strength:", strength)
except Exception as e:
logger.error("Error in main: %s", str(e))
99 changes: 99 additions & 0 deletions testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import unittest
from your_module import black_scholes_call, black_scholes_put, get_sentiment_score, get_stock_price

class TestBlackScholes(unittest.TestCase):
def test_black_scholes_call(self):
# Test case for call option pricing
S = 100 # Stock price
K = 100 # Strike price
r = 0.05 # Risk-free interest rate
sigma = 0.2 # Volatility
T = 1 # Time to expiration
call_price = black_scholes_call(S, K, r, sigma, T)
self.assertIsNotNone(call_price)

def test_black_scholes_put(self):
# Test case for put option pricing
S = 100 # Stock price
K = 100 # Strike price
r = 0.05 # Risk-free interest rate
sigma = 0.2 # Volatility
T = 1 # Time to expiration
put_price = black_scholes_put(S, K, r, sigma, T)
self.assertIsNotNone(put_price)

class TestSentimentAnalysis(unittest.TestCase):
def test_get_sentiment_score(self):
# Test case for sentiment analysis
article = "This is a positive article about the company."
sentiment_score = get_sentiment_score(article)
print("Sentiment Score:", sentiment_score)
self.assertIsNotNone(sentiment_score)

class TestLiveness(unittest.TestCase):
def test_get_stock_price(self):
# Test case for liveness of get_stock_price function
symbol = "AAPL" # Stock symbol

stock_price = get_stock_price(symbol)
self.assertIsNotNone(stock_price)

def test_get_financial_sentiment(self):
# Test case for liveness of get_financial_sentiment function
company = 'AAPL' # Specify the company symbol
news_sources = 'bbc-news, bloomberg, cnn, reuters' # Specify news sources
overall_strength = get_financial_sentiment(company, news_sources)
self.assertIsNotNone(overall_strength)

if __name__ == '__main__':
unittest.main()
import unittest
from your_module import black_scholes_call, black_scholes_put, get_sentiment_score, get_stock_price

class TestBlackScholes(unittest.TestCase):
def test_black_scholes_call(self):
# Test case for call option pricing
S = 100 # Stock price
K = 100 # Strike price
r = 0.05 # Risk-free interest rate
sigma = 0.2 # Volatility
T = 1 # Time to expiration
call_price = black_scholes_call(S, K, r, sigma, T)
self.assertIsNotNone(call_price)

def test_black_scholes_put(self):
# Test case for put option pricing
S = 100 # Stock price
K = 100 # Strike price
r = 0.05 # Risk-free interest rate
sigma = 0.2 # Volatility
T = 1 # Time to expiration
put_price = black_scholes_put(S, K, r, sigma, T)
self.assertIsNotNone(put_price)

class TestSentimentAnalysis(unittest.TestCase):
def test_get_sentiment_score(self):
# Test case for sentiment analysis
article = "This is a positive article about the company."
sentiment_score = get_sentiment_score(article)
print("Sentiment Score:", sentiment_score)
self.assertIsNotNone(sentiment_score)

class TestLiveness(unittest.TestCase):
def test_get_stock_price(self):
# Test case for liveness of get_stock_price function
symbol = "AAPL" # Stock symbol

stock_price = get_stock_price(symbol)
self.assertIsNotNone(stock_price)

def test_get_financial_sentiment(self):
# Test case for liveness of get_financial_sentiment function
company = 'AAPL' # Specify the company symbol
news_sources = 'bbc-news, bloomberg, cnn, reuters' # Specify news sources

overall_strength = get_financial_sentiment(company, news_sources)
self.assertIsNotNone(overall_strength)

if __name__ == '__main__':
unittest.main()