Skip to content

Instantly share code, notes, and snippets.

@scubamut
Last active June 6, 2024 17:09
Show Gist options
  • Save scubamut/a9ceaff5899be8cbee4940cd87f16cec to your computer and use it in GitHub Desktop.
Save scubamut/a9ceaff5899be8cbee4940cd87f16cec to your computer and use it in GitHub Desktop.
import talib
# routine to get args required for talib function
def find_talib_args(func):
s = eval('talib.' + func + '.__doc__')
try:
s = s.splitlines()[0].split('(')[1].split(')')[0].replace('[','').replace(']','').replace('?','')
return s.split(',')
except:
return None
func='WILLR'
print(eval('talib.' + func + '.__doc__'))
print(find_talib_args(func))
import pandas as pd
from datetime import datetime, timezone
import pytz
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from pandas_highcharts.core import serialize
from pandas_highcharts.display import display_charts
from fintools import *
tickers = ['^GSPC','^DJI','^NDX','^OEX','SPY','XLF']
symbols = ['GSPC','DJI','NDX','OEX','SPY','XLF']
start = datetime(2018, 1, 1, 0, 0, 0, 0, pytz.utc)
end = datetime(2018, 1, 5, 0, 0, 0, 0, pytz.utc)
data_path = '/home/scubamut/MEGAsync/10_DATA/'
da = get_DataArray(tickers, start, end)
dp = da.to_pandas().transpose(2,1,0)
from datetime import datetime, timezone, timedelta
import pandas as pd
import pytz
import xarray as xr
from fintools import get_DataArray, endpoints
import talib
start = datetime(2016, 1, 5, 0, 0, 0, 0, pytz.utc)
end = datetime(2017, 1, 1, 0, 0, 0, 0, pytz.utc)
tickers = ['XLP','XLY']
da = get_DataArray(tickers, start, end)
prices = da.loc['close'].transpose().to_pandas()
# Apply function to column of DAILY ADJ_CLOSE prices, COLUMN_BY_COLUMN
df = prices.copy()
function = talib.MA
lookback = 21
# make sure that TALIB fuction only requires one price!
try:
transformed = pd.DataFrame({etf:function(df[etf], lookback) for etf in df.columns}).dropna()
except:
print('ValueError: Shape of passed values is incorrect!')
# print(transformed)
MA_daily = transformed
print(MA_daily[:5])
end_points = endpoints(period='M', trading_days=df.index)
df = prices.loc[end_points]
function = talib.MA
lookback = 3
# make sure that TALIB fuction only requires one price!
try:
transformed = pd.DataFrame({etf:function(df[etf], lookback) for etf in df.columns}).dropna()
except:
print('ValueError: Shape of passed values is incorrect!')
MA_monthly = transformed
MA_monthly[:5]
from datetime import datetime, timezone, timedelta
import pytz
import xarray as xr
from fintools import get_DataArray
import talib
start = datetime(2016, 1, 5, 0, 0, 0, 0, pytz.utc)
end = datetime(2016, 3, 1, 0, 0, 0, 0, pytz.utc)
tickers = ['XLP','XLY']
da = get_DataArray(tickers, start, end)
xlp = da.loc['close','XLP'].to_pandas()
print(talib.STDDEV(xlp.values,10))
print_stats(xlp)
import talib
import pandas as pd
%matplotlib inline
SPY = pd.read_csv('/home/scubamut/MEGAsync/10_DATA/daily/etfs/SPY.csv')
def ADXR(data, args_ls):
try:
return talib.ADXR(data.high.values, data.low.values, data.close.values, args_ls[0])
except :
print ('ADXR: USING DEFAULT PARAMETERS')
return talib.ADXR(data.high.values, data.low.values, data.close.values)
print(ADXR (SPY, [252]))
data = SPY
func = "ADXR"
args = "data.high.values, data.low.values, data.close.values, 252"
mycode = "data[func] = talib." + func + "(" + args + ")"
exec (mycode)
data[func].plot(grid=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment