Skip to content

Instantly share code, notes, and snippets.

import alpaca_trade_api as tradeapi
api = tradeapi.REST(
'your_api_key_here',
'https://paper-api.alpaca.markets', api_version='v2'
# Submit a market order to buy 1 share of Apple at market price
api.submit_order(
symbol='AAPL',
qty=1,
name share change filingDate transactionDate transactionCode transactionPrice dollarAmount insiderPortfolioChange buyOrSale
Baglino Andrew D 19114 -1500 2021-03-31 2021-03-29 S 615.75 -923625.0 -0.07276608130396818 Sale
Baglino Andrew D 20614 1000 2021-03-31 2021-03-29 M 41.57 41570.0 0.05098399102681758 Buy
Kirkhorn Zachary 57234 -1250 2021-03-19 2021-03-17 S 655.81 -819762.49 -0.021373367074755487 Sale
Guillen Jerome M 50598 -100 2021-03-11 2021-03-10 S 715.49 -71549.0 -0.0019724643970176338 Sale
Guillen Jerome M 50698 -100 2021-03-11 2021-03-10 S 712.16 -71216.0 -0.0019685814402141816 Sale
Guillen Jerome M 50798 -400 2021-03-11 2021-03-10 S 710.033 -284013.2 -0.007812805187702644 Sale
Guillen Jerome M 51198 -100 2021-03-11 2021-03-10 S 707.83 -70783.0 -0.0019493937385473119 Sale
Guillen Jerome M 51298 -200 2021-03-11 2021-03-10 S 706.605 -141321.0 -0.0038836459668336633 Sale
Guillen Jerome M 51498 -100 2021-03-11 2021-03-10 S 703.61 -70361.0 -0.0019380596147137487 Sale
# Convert the data into a dataframe.
df = pd.DataFrame(data=test['data'])
# Derived attributes from the data.
df['dollarAmount'] = df['change']*df['transactionPrice']
df['insiderPortfolioChange'] = df['change']/(df['share'] - df['change'])
# print(type(df['transactionPrice'][0]))
conditions = [
(df['change'] >= 0) & (df['transactionPrice'] > 0),
# Specify the stock that you want to analyze.
stock = 'TSLA'
# Request for data from Finhub.io (30 calls per second limit: https://finnhub.io/docs/api/rate-limit).
r = requests.get('https://finnhub.io/api/v1/stock/insider-transactions?symbol='+stock+'&token=YOUR API KEY')
# Load the JSON file as a string.
test = json.loads(r.text)
# Necessary Libraries.
import requests
import json
import pandas as pd
import numpy as np
mom_data = add_all_ta_features(hist_data, open="Open", high="High", low="Low", close="Close", volume="Volume") # Substantiate data with momentum indicators
mom_data.columns
tick = yf.Ticker(str('MSFT'))
hist_data = tick.history(period="max") # Tells yfinance what kind of data we want about this stock (In this example, all of the historical data)
hist_data.head() # Observe the historical stock data
import yfinance as yf, pandas as pd
from ta import add_all_ta_features
from ta.utils import dropna
@cameronShadmehry
cameronShadmehry / pullData.py
Created August 26, 2020 03:40
Pull stock data
i=0
while (i < len(tickers)) and (Amount_of_API_Calls < 1800):
try:
stock = tickers[i] # Gets the current stock ticker
temp = yf.Ticker(str(stock)) # Instantiate the ticker as a stock with Yahoo Finance
Hist_data = temp.history(period="max") # Tells yfinance what kind of data we want about this stock (In this example, all of the historical data)
Hist_data.to_csv("C:\\Users\\Your Desired Path to File") # Saves the historical data in csv format for further processing later
time.sleep(2) # Pauses the loop for two seconds so we don't cause issues with Yahoo Finance's backend operations
Amount_of_API_Calls += 1
Stock_Failure = 0
@cameronShadmehry
cameronShadmehry / ATLR_Model.py
Created August 9, 2020 23:18
Run the logistic regression model
Hold_Results = []
list_files2 = (glob.glob("<Your Path>\\Bayesian_Logistic_Regression\\Stocks_Sub\\*.csv")) # Creates a list of all csv filenames in the stocks folder
for interval2 in list_files2:
Stock_Name = ((os.path.basename(interval2)).split(".csv")[0])
data = pd.read_csv(interval2,index_col=0)
data = data.replace([np.inf, -np.inf], np.nan)
data = data.fillna(0)
dependents = [data["Five_Day_Observation_Outcome"].to_list(), data["Thirty_Day_Observation_Outcome"].to_list(), data["Sixty_Day_Observation_Outcome"].to_list()]
data = data.drop(['Five_Day_Observation_Outcome', 'Thirty_Day_Observation_Outcome', 'Sixty_Day_Observation_Outcome', 'Date', 'Open', 'High', 'Low', 'Close'], axis = 1)
scaler = StandardScaler()