Technical analysis with Python | Technical indicators Python

This post discusses how to do technical analysis with Python. We will cover three Python libraries for getting stock indicators.Skiptocontent02Feb2021byAndrewTreadwayInthispost,wewillintroducehowtodotechnicalanalysiswithPython.Pythonhasseverallibrariesforperformingtechnicalanalysisofinvestments.We’regoingtocomparethreelibraries–ta,pandas_ta,andbta-lib.ThetalibraryfortechnicalanalysisOneofthenicestfeaturesofthetapackageisthatitallowsyoutoadddozenoftechnicalindicatorsallatonce.Togetstarted,installthetalibraryusingpip:pipinstalltaNext,let’simportthepackagesweneed.We’llbeusingyahoo_fintopullinstockpricedata.Now,datacontainsthehistoricalpricesforAAPL.#loadpackagesimportyahoo_fin.stock_infoassiimportpandasaspdfromtaimportadd_all_ta_features#pulldatafromYahooFinancedata=si.get_data("aapl")Next,let’susetatoaddinacollectionoftechnicalfeatures.Below,wejustneedtospecifywhatfieldscorrespondtotheopen,high,low,close,andvolume.Thissinglecallautomaticallyaddsinover80technicalindicators,includingRSI,stochastics,movingaverages,MACD,ADX,andmore.#addtechnicalanalysisfeaturesdata=add_all_ta_features(data,open="open",high="high",low="low",close="adjclose",volume="volume")Forexample,here’stheRSIvalues(usingthestandard14-daycalculation):taalsohasseveralmodulesthatcancalculateindividualindicatorsratherthanpullingthemallinatonce.Thesemodulesallowyoutogetmorenuancedvariationsoftheindicators.Forexample,ifyouwanttocalculatethe21-dayRSI,ratherthanthedefault14-daycalculation,youcanusethemomentummodule.fromta.momentumimportRSIIndicatorrsi_21=RSIIndicator(close=data.adjclose,window=21)data["rsi_21"]=rsi_21.rsi()Similarly,wecouldusethetrendmoduletocalculateMACD.fromta.trendimportmacddata["macd"]=macd(data.adjclose,window_slow=26,window_fast=12)Tolearnmoreabouttacheckoutitsdocumentationhere.Thepandas_talibraryAnalternativetotaisthepandas_talibrary.Let’sgetstartedwithpandas_tabyinstallingitwithpip:pipinstallpandas_taWhenyouimportpandas_ta,itletsyouaddnewindicatorsinaniceobject-orientedfashion.pandas_t


常見投資理財問答


延伸文章資訊