<repo_name>Dashora7/Python-Stock-Trader<file_sep>/deploy_ml.py
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import VotingRegressor
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np

df_weekly = pd.read_csv('data//tech_df')

df_weekly.replace([np.inf, -np.inf], np.nan)

df_weekly.dropna(inplace=True)
df_weekly.drop(columns='Unnamed: 0', inplace=True)

print(df_weekly.columns)

features = df_weekly[['open', 'high', 'low', 'close', 'adx', 'aroon', 'macd', 'rsi', 'stoch', 'obv', 'ma_50', 'current_close_pct_change']].values
labels = df_weekly['future_close_pct_change'].values
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)
reg1 = GradientBoostingRegressor(random_state=1, n_estimators=10)
reg2 = RandomForestRegressor(random_state=1, n_estimators=10)
reg3 = LinearRegression()
ereg = VotingRegressor(estimators=[('gb', reg1), ('rf', reg2), ('lr', reg3)])
ereg = ereg.fit(X_train, y_train)
print(ereg.score(X_test, y_test))<file_sep>/sentdex_scraper.py
# -*- coding: utf-8 -*-
"""
Created on Wed May  6 10:26:32 2020

@author: nrdas
"""
import requests
from bs4 import BeautifulSoup
import pandas as pd
import pickle

def get_sentdex_main_data(ticker):
    response = requests.get('http://sentdex.com/financial-analysis/')
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        soup.prettify()
    tags = soup.find_all('span')[3:]
    hislist = ['increasing' if tag['class'][1] == 'glyphicon-chevron-up' else 'decreasing' for tag in tags]
    response = requests.get('http://sentdex.com/financial-analysis/')
    df = pd.read_html(response.text)[0]
    hdf = pd.DataFrame(hislist)
    hdf.columns = ['history']
    df = pd.concat([df, hdf], axis=1)
    ser = df.loc[df['Symbol'] == ticker]
    return {'volume': ser['all Volume of Mentions'].tolist()[0], 'sentiment': ser['all Overall Sentiment'].tolist()[0],
            'history': ser['history'].tolist()[0]}

def save_sp500_tickers():
    resp = requests.get('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
    soup = BeautifulSoup(resp.text, 'lxml')
    table = soup.find('table', {'class': 'wikitable sortable'})
    tickers = []
    for row in table.findAll('tr')[1:]:
        ticker = row.findAll('td')[0].text
        ticker = ticker.strip()
        if '.' in ticker:
            a, b = ticker.split('.')
            ticker = a+'-'+b
        tickers.append(ticker)

    with open("data//sp500tickers.pickle", "wb") as f:
        pickle.dump(tickers, f)

    return tickers

save_sp500_tickers()<file_sep>/save_tech.py
import pickle
import technical_acquisition_methods as tam
import pandas as pd

df_list = []
with open('data//sp500tickers.pickle', 'rb') as f:
    tickers = pickle.load(f)

for ticker in tickers:
    df_list.append(tam.get_tech_factors(ticker))
    print('added' + ticker)

df_weekly = pd.concat(df_list)
df_weekly.to_csv('data//tech_df')<file_sep>/technical_acquisition_methods.py
import pandas as pd
import numpy as np
import quandl
import FundamentalAnalysis as fa
import datetime as dt
import pandas as pd
import pandas_datareader.data as web
import ta
'''
quandl.ApiConfig.api_key = '<KEY>***'
fm = '07a2e7ec***'
'''

'''

#CONSUMER SENTIMENT INDEX
df_monthly_sentiment = quandl.get('UMICH/SOC1')
#df_monthly_sentiment.tail()

#NATIONAL GDP DATA
df_gdp = quandl.get('FRED/GDP')
df_gdp.tail()

'''

def get_price_data(ticker):
    start = dt.datetime(2010, 4, 5)
    end = dt.datetime.now()
    df = web.DataReader(ticker, 'yahoo', start, end)
    df.reset_index(inplace=True)
    df.set_index("Date", inplace=True)
    return df

def get_weekly_returns(price_data):
    df_ohlc = price_data['Adj Close'].resample('7D').ohlc()
    df_ohlc = df_ohlc.reset_index()
    df_ohlc['future_close_pct_change'] = df_ohlc['close'].pct_change()
    df_ohlc['current_close_pct_change'] = df_ohlc['close'].pct_change()
    df_ohlc['future_close_pct_change'] = df_ohlc['future_close_pct_change'].shift(periods=-1)
    num_rows = df_ohlc.shape[0]
    df_ohlc = df_ohlc.drop(num_rows-1)
    df_ohlc = df_ohlc.drop(0)
    return df_ohlc
    
def get_50_day_ma(price_data):
    series = price_data['Adj Close'].rolling(window=50,min_periods=0).mean()
    return series

'''
# Show the available companies
companies = fa.available_companies()

# Collect general company information
profile = fa.profile(ticker)

# Collect recent company quotes
quotes = fa.quote(ticker)

# Collect market cap and enterprise value
enterprise_value = fa.enterprise(ticker)

# Show recommendations of Analysts
ratings = fa.rating(ticker)

# Obtain DCFs over time
dcf_quarterly = fa.discounted_cash_flow(ticker, period="quarter")

# Collect the Balance Sheet statements
balance_sheet_quarterly = fa.balance_sheet_statement(ticker, period="quarter")

# Collect the Income Statements
income_statement_quarterly = fa.income_statement(ticker, period="quarter")

# Collect the Cash Flow Statements
cash_flow_statement_quarterly = fa.cash_flow_statement(ticker, period="quarter")

# Show Key Metrics
key_metrics_quarterly = fa.key_metrics(ticker, period="quarter")

# Show a large set of in-depth ratios
financial_ratios = fa.financial_ratios(ticker)

# Show the growth of the company
growth_quarterly = fa.financial_statement_growth(ticker, period="quarter")

'''

def get_tech_factors(ticker):
    df_tick = get_price_data(ticker)
    weekly_ma50 = get_50_day_ma(df_tick).resample('7D').mean().to_frame()
    df_weekly = get_weekly_returns(df_tick)
    adx = ta.trend.ADXIndicator(high = df_tick['High'], low = df_tick['Low'], close = df_tick['Close']).adx()
    aroon = ta.trend.AroonIndicator(close = df_tick['Close']).aroon_indicator()
    macd = ta.trend.MACD(close = df_tick['Close']).macd()

    # THESE ARE MOMENTUM INDICATORS

    rsi = ta.momentum.RSIIndicator(close = df_tick['Close']).rsi()
    stoch = ta.momentum.StochasticOscillator(high = df_tick['High'], low = df_tick['Low'],
                                             close = df_tick['Close']).stoch()

    # THESE ARE VOLUME INDICATORS

    adl = ta.volume.AccDistIndexIndicator(high = df_tick['High'], low = df_tick['Low'],
                                            close = df_tick['Close'], volume=df_tick['Volume']).acc_dist_index()
    obv = ta.volume.OnBalanceVolumeIndicator(close = df_tick['Close'], volume = df_tick['Volume']).on_balance_volume()

    # THESE ARE VOLATILITY INDICATORS

    mbol = ta.volatility.BollingerBands(close = df_tick['Close']).bollinger_mavg()

    adx_weekly = adx.rolling(window=5,min_periods=0).mean().resample('7D').mean().to_frame()
    aroon_weekly = aroon.rolling(window=5,min_periods=0).mean().resample('7D').mean().to_frame()
    macd_weekly = macd.rolling(window=5,min_periods=0).mean().resample('7D').mean().to_frame()
    rsi_weekly = rsi.rolling(window=5,min_periods=0).mean().resample('7D').mean().to_frame()
    stoch_weekly = stoch.rolling(window=5,min_periods=0).mean().resample('7D').mean().to_frame()
    obv_weekly = obv.rolling(window=5,min_periods=0).mean().resample('7D').mean().to_frame()
    adl_weekly = adl.rolling(window=5,min_periods=0).mean().resample('7D').mean().to_frame()
    mbol_weekly = mbol.rolling(window=5,min_periods=0).mean().resample('7D').mean().to_frame()

    df_weekly['adx'] = adx_weekly[1:-1].set_index(df_weekly.index)
    df_weekly['aroon'] = aroon_weekly[1:-1].set_index(df_weekly.index)
    df_weekly['macd'] = macd_weekly[1:-1].set_index(df_weekly.index)
    df_weekly['rsi'] = rsi_weekly[1:-1].set_index(df_weekly.index)
    df_weekly['stoch'] = stoch_weekly[1:-1].set_index(df_weekly.index)
    df_weekly['obv'] = obv_weekly[1:-1].set_index(df_weekly.index)
    df_weekly['adl'] = adl_weekly[1:-1].set_index(df_weekly.index)
    df_weekly['mbol'] = mbol_weekly[1:-1].set_index(df_weekly.index)
    df_weekly['ma_50'] = weekly_ma50[1:-1].set_index(df_weekly.index)

    df_weekly = df_weekly.drop(columns=['Date'])[5:]

    return df_weekly


'''

#WE NEED TO GENERATE TEHSE DATAFRAMES FOR ALL STOCKS, APPEND THEM ALL, THEN RUN THIS CODE

features = df_weekly[['open', 'high', 'low', 'close', 'adx', 'aroon', 'macd', 'rsi', 'stoch', 'obv', 'ma_50', 'current_close_pct_change']].values
labels = df_weekly['future_close_pct_change'].values
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)
reg1 = GradientBoostingRegressor(random_state=1, n_estimators=10)
reg2 = RandomForestRegressor(random_state=1, n_estimators=10)
reg3 = LinearRegression()
ereg = VotingRegressor(estimators=[('gb', reg1), ('rf', reg2), ('lr', reg3)])
ereg = ereg.fit(X_train, y_train)
print(ereg.score(X_test, y_test))


'''
<file_sep>/sentiment_tools.py
# -*- coding: utf-8 -*-
"""
Created on Mon May  4 15:46:33 2020

@author: nrdas
"""
from textblob import TextBlob
from statistics import mean
try:
    import json
except ImportError:
    import simplejson as json

# Import the tweepy library
import tweepy

# Variables that contains the user credentials to access Twitter API 
ACCESS_TOKEN = '1215****'
ACCESS_SECRET = 'xhj8****'
CONSUMER_KEY = '1QCej****'
CONSUMER_SECRET = 'iwGMq****'

# Setup tweepy to authenticate with Twitter credentials:

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

# Create the api to connect to twitter with your creadentials
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)

class StreamListener(tweepy.StreamListener):
    tweet_list = []
    
    def on_status(self, status):
        self.tweet_list.append(json.dumps(status._json)['text'])
    
    def on_error(self, status_code):
        if status_code == 420:
            return False

def check_sentiment(tweet_list):
    sent = []
    sbjt = []
    for tweets in tweet_list:
        b = TextBlob(tweets)
        sent.append(b.sentiment.polarity)
        sbjt.append(b.sentiment.subjectivity)
    return (mean(sent), mean(sbjt))  
    
def load_tweets(name):
    tweet_list = []
    for tweet in tweepy.Cursor(api.search, q=name, lang='en').items(300):
        tweet_list.append(json.loads(json.dumps(tweet._json))['text'])
    return tweet_list

<file_sep>/README.md
# Python-Stock-Trader
A Python-based stock trading analyzation and decision-making tool
