Open amitach opened 3 years ago
Hello @amitach,
Thanks for using Pandas TA and providing a detailed analysis for this Issue! I will add it to my list.
Expected behavior* The EMA_20 column or any strategy for that matter should bed added for the subsequent execution of the strategy.
Unfortunately I currently am tied up with other _PR_s and Issues and do not have an estimated time to resolve this. If you would like to help solve this Issue and submit a PR to speed up the progress of this bug, that would be greatly appreciated! 😎
Kind Regards, KJ
A workaround for subsequent executions is to re-create the strategy before it is used each time. This example pulls a list of ticker data data from Yahoo and outputs it in CSV files. ARKX is missing SMA_50 and SMA_200.
# Update_Stock_Metrics
# Necessary Libraries
import yfinance as yf, pandas as pd, shutil, os, time
import pandas_ta as ta
#
if __name__ == '__main__': # Bug Fix for multithreading
#
# Create Stocks folder. Obtain Stock History from Yahoo Finance in CSV files by ticker Symbol.
#
tickers = ["AAPL","ABT","AMD","ARKF","ARKG","ARKK","ARKQ","ARKX",
"CHPT","CRM","F","ICHR","KMX","LHX","MSFT","PBD","PBW","PEP",
"PH","PHO","PKB","PKB","PLTR","PPA","PPL","PSJ","PWB","PXE","PXI",
"QCOM","ROKU","SPHD","TSM","VLO","YUM"]
# Check that the amount of tickers isn't more than 2000
print("The amount of stocks chosen to observe: " + str(len(tickers)))
# These two lines remove the Stocks folder and then recreate it in order to remove old stocks.
# Make sure you have created a Stocks Folder the first time you run this.
shutil.rmtree("stocks")
os.mkdir("stocks")
# Use this folder for all data files
os.chdir("stocks")
# for use with Review_Stock_Metrics
os.mkdir("graphs")
#
# Do not make more than 2,000 calls per hour or 48,000 calls per day or Yahoo Finance may block your IP.
# The clause "(Amount_of_API_Calls < 1800)" below will stop the loop from making too many calls to the yfinance API.
Stock_Failure = 0
Stocks_Not_Imported = 0
Amount_of_API_Calls=0
#
# Iterate through list of tickers
i=0
while (i < len(tickers)) and (Amount_of_API_Calls < 1800):
try:
print("Iteration = " + str(i))
#
stock = tickers[i] # Gets the stock from ticker list
# Load data
temp = yf.Ticker(str(stock))
df = temp.history(period="2y") # Tells yfinance what kind of data we want about this stock (Use "Max" for all historical data)
#
# (1) Create the Strategy
MyStrategy = ta.Strategy(
name="Momo and Volatility",
description="SMA 50,200, BBANDS, RSI, MACD and Volume SMA 20",
ta=[
{"kind": "sma", "close": "close", "length": 50, "prefix": "Close"},
{"kind": "sma", "close": "close", "length": 200, "prefix": "Close"},
{"kind": "bbands", "length": 20},
{"kind": "rsi"},
{"kind": "macd", "fast": 8, "slow": 21},
{"kind": "sma", "close": "volume", "length": 20, "prefix": "VOLUME"},
]
)
#
# (2) Run the Strategy
df.ta.strategy(MyStrategy)
#df.ta.strategy("all") # This is the kitchen sink strategy
#
# Output data to CSV file
df.to_csv(stock+".csv") # Saves the historical data in csv format for further processing later
time.sleep(1) # Pauses the loop for 1 second so we don't cause issues with Yahoo Finance's backend operations
Amount_of_API_Calls += 1
Stock_Failure = 0
i += 1 # Iteration to the next ticker
except ValueError:
print("Yahoo Finance Backend Error, Attempting to Fix") # An error occured on Yahoo Finance's backend. We will attempt to retreive the data again
if Stock_Failure > 5: # Move on to the next ticker if the current ticker fails more than 5 times
i+=1
Stocks_Not_Imported += 1
Amount_of_API_Calls += 1
Stock_Failure += 1
# Handle SSL error
except requests.exceptions.SSLError as e:
print("Yahoo Finance Backend Error, Attempting to Fix SSL") # An error occured on Yahoo Finance's backend. We will attempt to retreive the data again
if Stock_Failure > 5: # Move on to the next ticker if the current ticker fails more than 5 times
i+=1
Stocks_Not_Imported += 1
Amount_of_API_Calls += 1
Stock_Failure += 1
print("The amount of stocks we successfully imported: " + str(i - Stocks_Not_Imported))
Which version are you running? The lastest version is on Github. Pip is for major releases.
The bug Let's say we have a sample strategy defined for calculating the 20 EMA. This strategy will be run on 3 tickers identified by
data
,data1
anddata2
data-frames respectivelyTo Reproduce
250 rows × 6 columns
250 rows × 6 columns
*Expected behavior** The EMA_20 column or any strategy for that matter should bed added for the subsequent execution of the strategy.
Additional context
pandas_ta version: 0.2.62b0
python version: 3.9.4
Thanks for making Pandas TA!