Skip to content

Instantly share code, notes, and snippets.

@financial-python
financial-python / main.py
Last active February 22, 2024 15:26
Coding and Visualizing a Stock Price crossing a Simple Moving Average in Python
import yfinance as yf
import pandas as pd
import plotly.graph_objs as go
from plotly.subplots import make_subplots
#Get Historical Stock Data from AAPL
stock_data = yf.download(tickers='AAPL', interval='1d', period='3y')
#Calculate an SMA
stock_data['SMA50'] = stock_data['Close'].rolling(window=50).mean()
"""
2 Pre-Requisites - install the package, and get an OpenAI API key
pip install openai
"""
#import the package and add your key
import openai
openai.api_key = "YOUR_API_KEY_HERE"
@financial-python
financial-python / ema_crossover.py
Last active May 21, 2024 15:54
How to code an EMA crossover in Python
#import the necessary packages
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#download the historical stock data
aapl_df = yf.download("AAPL", start="2018-03-24", end="2023-03-24")
@financial-python
financial-python / get_stocks_from_reddit.py
Last active October 1, 2024 19:00
How to get stock tickers from reddit using Praw
"""
First, we need to create a Reddit account and get an API key to access the Reddit API. Once you have created an account, you can obtain an API key by following these steps:
Go to https://ssl.reddit.com/prefs/apps/
Scroll down to "Developed Applications" and click "Create App"
Choose "Web app" and give your app a name and description.
Set "About url" and "Redirect uri" to "http://localhost:8000"
Click "Create app" and copy the "client_id" and "client_secret" values.
Once you have the API key, you can start coding.
"""
Will need to install boto3: pip install boto3
"""
import os
import boto3
# variables come from AWS Management Console
os.environ['AWS_ACCESS_KEY_ID'] = 'YOUR_ACCESS_KEY'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'YOUR_SECRET_KEY'
@financial-python
financial-python / get_stocks_from_twitter.py
Last active February 22, 2024 15:27
Get Stock Tickers from Twitter using Python
import tweepy as tw #An easy-to-use Python library for accessing the Twitter API
import string
from collections import Counter
TWITTER_CONSUMER_KEY = '[FROM_TWITTER_DEVELOPER_CONSOLE]'
TWITTER_CONSUMER_SECRET = '[FROM_TWITTER_DEVELOPER_CONSOLE]'
TWITTER_ACCESS_TOKEN = '[FROM_TWITTER_DEVELOPER_CONSOLE]'
TWITTER_ACCESS_TOKEN_SECRET = '[FROM_TWITTER_DEVELOPER_CONSOLE]'
TWITTER_LIST_ID = '[LIST_ID]' #get from the url of the twitter list: https://twitter.com/i/lists/[LIST_ID]