I am running the following script:
# STEP 1: import packages, declare tindexes
import pandas as pd
import yfinance as yf
import datetime as dt
bnpl_tindex = ["APT.AX","Z1P.AX","LFS.AX","SZL.AX","HUM.AX","SPT.AX","OPY.AX","IOU.AX","LBY.AX","DOU.AX"]
target_dates = pd.date_range(start=dt.date.today() - dt.timedelta(days=365), end=dt.date.today(), freq="M").strftime('%Y-%m-%d')
target_dates = target_dates.append(pd.Index([dt.date.today().strftime('%Y-%m-%d')]))
target_dates.sort_values()
#STEP 2: source functions
from collect_index_data import collect_index_data
#DELETE LATER... TESTING!
collect_index_data(bnpl_tindex, target_dates)
#
collect_index_data is as follows:
def collect_index_data(ticker_list,date_list):
if (bool(ticker_list) and all(isinstance(elem, str) for elem in ticker_list) )==False:
sys.exit('Input should be a list or a single string.')
else:
print("Components of Index: ")
#initialise dictionary
d = {}
#loop through ticker list
for x in ticker_list:
d["DF.{0}".format(x)] = yf.Ticker(x)
#testing
print(x)
#testing
print(d)
and I get the following error message
Components of Index:
Traceback (most recent call last):
File "C:\Users\thoma\Desktop\Files\Programming\Python\run_tindex_data.py", line 27, in <module>
collect_index_data(bnpl_tindex, target_dates)
File "C:\Users\thoma\Desktop\Files\Programming\Python\collect_index_data.py", line 12, in collect_index_data
d["DF.{0}".format(x)] = yf.Ticker("MSFT")
NameError: name 'yf' is not defined
My question is why is yfinance package not being recognised in my function?
I could import it inside the function, but I plan to run the function multiple times in a script - so this would be computationally wasteful.
thanks!
Related
import pybullet as p
import pybullet_data
import time
obstacles_coor=[[-5,10,2.5],[2,10,2.5],[10,10,2.5],[0,5,2.5],[-10,-5,2.5]]
physicsClient = p.connect(p.GUI)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
p.loadURDF('plane.urdf', [0,0,0])
p.setGravity(0,0,-10)
for obs in obstacles_coor:
p.loadURDF("cube.urdf",obs,globalScaling=0.5)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
obstacles_coor.pop()
# obs = p.loadURDF("cube.urdf", globalScaling=0.5)
cube=p.loadURDF("cube.urdf", [0,0,1])
def contactPoints():
#obs is a list>
contacts = p.getContactPoints(bodyA=cube, bodyB=obs)
for contact in contacts:
link_index = contact[2]
if link_index >= 2:
# p.removeBody(obstacles_coor[0])
del(obstacles_coor[0])
for i in range(10000):
p.stepSimulation()
contactPoints()
time.sleep(1./240.)
the error jumps at the line:
contacts = p.getContactPoints(bodyA=cube, bodyB=obs)
With this error log:
Exception has occurred: TypeError
an integer is required (got type list)
File "C:\Users\AisyahJeff\niche_evo_single_species\old_model\test.py", line 58, in contactPoints
contacts = p.getContactPoints(bodyA=cube, bodyB=obs)
File "C:\Users\AisyahJeff\niche_evo_single_species\old_model\test.py", line 69, in <module>
contactPoints()
I am thinking it is python type of error instead of pybullet.
For anyone else that have this problem, my friend gave me a solution:
import pybullet as p
import pybullet_data
import time
obstacles_coor=[[-5,10,2.5],[2,10,2.5],[10,10,2.5],[0,5,2.5],[-10,-5,2.5]]
physicsClient = p.connect(p.GUI)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
p.loadURDF('plane.urdf', [0,0,0])
cube=p.loadURDF("cube.urdf", [0,0,1])
obstacles_list=[]
for obs in obstacles_coor:
obstacles_list.append(p.loadURDF("cube.urdf",obs,globalScaling=0.5))
p.setGravity(0,0,-10)
def contactPoints():
#obs is a list>
for obs_idx in obstacles_list:
contacts = p.getContactPoints(bodyA=cube, bodyB=obs_idx)
for contact in contacts:
link_index = contact[2]
if link_index >= 2:
#del(obstacles_coor.pop(0))
p.removeBody(obstacles_coor.pop(0))
for i in range(10000):
p.stepSimulation()
contactPoints()
time.sleep(1./240.)
obstacles_coor or obs in this context is a list, not an int. Therefore it is not suitable for the p.getContactPoints argument. So, recognize it as index 'idx' in the argument so it can be an int.
I hope someone can help me. I've been stuck with this error for a while. I have two .py files that I am importing in a jupyter notebook. When running the last cell (see code below) I get a Traceback error I can'f fix.
I think there is some error in my ch_data_prep.py file related to variable df_ch not correctly passed between files. Is this possible? Any suggestion on how to solve this problem?
Thanks!
ch_data_prep.py
def seg_data(self):
seg_startdate = input('Enter start date (yyyy-mm-dd): ')
seg_finishdate = input('Enter end date (yyyy-mm-dd): ')
df_ch_seg = df_ch[(df_ch['event_datetime'] > seg_startdate)
& (df_ch['event_datetime'] < seg_finishdate)
]
return df_ch_seg
df_ch_seg = seg_data(df_ch)
ch_db.py
def get_data():
# Some omitted code here to connect to database and get data...
df_ch = pd.DataFrame(result)
return df_ch
df_ch = get_data()
Jupyter Notebook data_analysis.ipynb
In[1]: import ch_db
df_ch = ch_db.get_data()
In[2]: import ch_data_prep
When I run cell 2, I get this error
--------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-fbe2d4fceba6> in <module>
----> 1 import ch_data_prep
~/clickstream/ch_data_prep.py in <module>
34 return df_ch_seg
35
---> 36 df_ch_seg = seg_data()
TypeError: seg_data() missing 1 required positional argument: 'df_ch'
I am trying to learn how to use the backtrader module on Python. I copied the code directly from the website but receiving an error message.
Here is the website: https://www.backtrader.com/docu/quickstart/quickstart/
I downloaded S&P500 stock data from Yahoo Finance and saved it into an excel file named 'SPY'. Here is the code so far:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import datetime # For datetime objects
import os.path # To manage paths
import sys # To find out the script name (in argv[0])
# Import the backtrader platform
import backtrader as bt
if __name__ == '__main__':
# Create a cerebro entity
cerebro = bt.Cerebro()
# Datas are in a subfolder of the samples. Need to find where the script is
# because it could have been called from anywhere
modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
datapath = os.path.join(modpath, 'C:\\Users\\xboss\\Desktop\\SPY.csv')
# Create a Data Feed
data = bt.feeds.YahooFinanceCSVData(
dataname=datapath,
# Do not pass values before this date
fromdate=datetime.datetime(2000, 1, 1),
# Do not pass values after this date
todate=datetime.datetime(2000, 12, 31),
reverse=False)
# Add the Data Feed to Cerebro
cerebro.adddata(data)
# Set our desired cash start
cerebro.broker.setcash(100000.0)
# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
# Run over everything
cerebro.run()
# Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
Here is the error that I am receiving:
C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\Scripts\python.exe C:/Users/xboss/PycharmProjects/BackTraderDemo/backtrader_quickstart.py
Traceback (most recent call last):
File "C:/Users/xboss/PycharmProjects/BackTraderDemo/backtrader_quickstart.py", line 39, in <module>
cerebro.run()
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies
data.preload()
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 688, in preload
while self.load():
Starting Portfolio Value: 100000.00
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 479, in load
_loadret = self._load()
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 710, in _load
return self._loadline(linetokens)
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feeds\yahoo.py", line 129, in _loadline
dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10]))
ValueError: invalid literal for int() with base 10: '1/29'
Process finished with exit code 1
Does anyone have any suggestions? Any help would be greatly appreciated. Thank you so much for your time!
You get the error because of using custom csv with YahooFinanceCSVData method.
You should import them using GenericCSVData method.
data = btfeed.GenericCSVData(
dataname='SPY.csv',
fromdate=datetime.datetime(2000, 1, 1),
todate=datetime.datetime(2000, 12, 31),
nullvalue=0.0,
dtformat=('%Y-%m-%d'),
datetime=0,
high=1,
low=2,
open=3,
close=4,
volume=5,
openinterest=-1
)
For more information you can see the instruction here
import urllib # cmd- pip install requests
import bs4 # 실행전cmd- pip install BeautifulSoup4 설치
import matplotlib.pyplot # 만들어진 데이터 포인트를 graph로 출력, 사용하기전 cmd-pip install matplotlib 실행
from datetime import datetime
company=input('찾으시고 싶은 회사명을 Ticker는 stock symbol로 적어주세요(ex)apple=appl) :' )
def get_company_data(company):
findothercompany=company # https://www.nasdaq.com/symbol/aapl/dividend-history 창에서 appl부분을 인풋값으로 변환
data=[] # 공백 리스트 형식으로 append 함수를 이용해 추가할 예정
dividendplot=[] # 공백 리스트에 데이터값을 받아와서 plot함수를 이용해 graph를 그린다.
url='https://www.nasdaq.com/symbol/'+findothercompany+'/dividend-history' # nasdaq사이트에서 dividend history를 찾는것 없을수도 있다.
print('본 데이터는 NASDAQ의 '+url+'를 참고한 것입니다.')
tables=bs4.BeautifulSoup(urllib.request.urlopen(url).read(),features='lxml').find_all('table')# lxml은 코드들을 html 형식으로 가져오는것 (1조 발표 html형식)
#print(len(tables))
dividend_data=tables[2].find_all('tr')
#print(dividend_data[0])
#print(dividend_data[1])
for tr in dividend_data[1:]:
tds=tr.find_all('td')
exdate=tds[0].text
Type=tds[1].text
CashAmount=tds[2].text
Declarationdate=tds[3].text
Recorddate=tds[4].text
Paymentdate=tds[5].text
data.append([exdate,Type,CashAmount,Declarationdate,Recorddate,Paymentdate])
dividendplot.append([Paymentdate,float(CashAmount)])
return data,dividendplot
dividenddata,dividendplot=get_company_data(company)
print(dividenddata)
then I got this message:
Traceback (most recent call last):
File "C:/Users/월토커/Desktop/금융소프트웨어 project.py", line 32, in <module>
dividenddata,dividendplot=get_company_data(company)
File "C:/Users/월토커/Desktop/금융소프트웨어 project.py", line 22, in get_company_data
Declarationdate=tds[3].text
IndexError: list index out of range
your code is fine but appl has no data, check https://www.nasdaq.com/symbol/appl/dividend-history try with another like ppl
check your td length if it length lower than 6 skip the loop
tds=tr.find_all('td')
if len(tds) < 6:
continue # or break
I am trying to batch run workspaces from a standalone python script and it fails. I am particularly wondering if I am passing the parameters correctly. I was using an exception catch with a message but it actually provided less information:
'code' Traceback (most recent call last):
File "C:\Users\YouDoWellItDoesGood\Downloads \SixtyFourTest.py", line 89, in <module>
wrkRunner.runWithParameters(qcWorkspace,parameters)
FMEException: FMEException: 1: Failure running workspace 'C:\Users\YouDoWellItDoesGood\Downloads\Anadarko_QC_Tool.fmwt' 'code'
The code:
# Note:
# The path to fmeobjects must by in your python
# path so you may need something like this:
import sys
sys.path.append(r"C:\Program Files\FME\fmeobjects\python27")
import fmeobjects
import fnmatch
import xlrd
import arcpy
import datetime
import os
##dictionary to hold the day of week value generated by the statusRunDateNum
##variable. the first
##number in the dictionary entery is the day of the week as a number. The other
##two numbers are the column numbers where we will find our project status values
datColumnDict={1:[2,3],2:[5,6],3:[8,9],4:[11,12],5:[14,15]}
qcWorkspace=r"C:\Users\YouDoWellItDoesGood\Downloads\Anadarko_QC_Tool.fmwt"
#will return a number depending what day it is.
statusRunDateNum=datetime.datetime.today().isoweekday()
# location of the status report/ will be turned into a variable.
PSL=r"C:\Users\YouDoWellItDoesGood\Downloads\Project%20Status%20Worksheet%203_20%20to%203_24.xlsx"
pjStatus=xlrd.open_workbook(PSL)
pjSheet=pjStatus.sheet_by_index(0)
pjList=[]
for row_index in range(1,pjSheet.nrows):
statusColumn=str(pjSheet.cell(row_index,datColumnDict[statusRunDateNum][1]))
statusColumnNoColon=statusColumn.split(":")[1]
projColumn=str(pjSheet.cell(row_index,datColumnDict[statusRunDateNum][0]))
projColumnNoDec=projColumn.split(".")[0]
projColumnNoColon=projColumnNoDec.split(":")[1]
if "QC" in str(statusColumn):
pjList.append(projColumnNoColon)
print statusColumnNoColon
print pjList
pjDir=r"F:\Projects\ANADARKO-DELAWARE BASIN"
pjDirList=os.listdir(pjDir)
wrkspc=[]
for x in range(len(pjList)):
#create string from TGS proj number
match=str(pjList[x])+"*"
#declare feature list
for f in pjDirList:
if fnmatch.fnmatch(f,match):
## print match[:-1]
gisPath=os.path.join(pjDir,f,"GIS")
for gf in os.listdir(gisPath):
if gf.startswith("DB_") or gf.startswith("ID") and gf.endswith(".gdb"):
wrkspc.append(os.path.join(gisPath,gf))
print wrkspc
for w in wrkspc:
parameters={}
parameters['_gdbpath']=w
parameters['_user']="test"
parameters['_with_corrections']='False'
#parameters['FEATURE_TYPES']="WE_PT WE_PATH_LN WE_PAD_POLY WD_PT WD_POLY WD_LN VG_PT VG_POLY VG_LN VG_BUFFER_POLY TR_TRANS_OTHER_PT TR_TRANS_OTHER_POLY TR_TRANS_OTHER_LN TR_RUNWAY_POLY TR_ROAD_LOW_WATER_PT TR_ROAD_CENTER_LN TR_RAILROAD_PT TR_RAILROAD_POLY TR_RAILROAD_LN TR_LANDING_ZONE_PT TR_LANDING_ZONE_POLY TR_EDGE_OF_PAVEMENT_LN ST_OTHER_PT ST_OTHER_POLY ST_OTHER_LN SI_FACILITY_PERIMETER_POLY SI_FACILITY_OTHER_POLY RC_WETLANDS_POLY RC_SOILBED_PREPARATION_POLY RC_SOIL_SAMPLE_PT RC_SOIL_AMENDMENT_POLY RC_SEEDBED_PREPARATION_POLY RC_RECLAMATION_OTHER_PT RC_RECLAMATION_OTHER_POLY RC_RECLAMATION_OTHER_LN RC_MULCH_POLY RC_HYDROMULCH_POLY RC_BORROW_PIT_POLY PL_VENT_PIPE_PT PL_TRENCH_BREAKER_LN PL_TEST_LEAD_PT PL_TEE_PT PL_TAP_PT PL_SLEEVE_LN PL_ROUTING_NOTE_PT PL_ROCK_SHIELD_LN PL_REDUCER_PT PL_PUMP_STATION_PT PL_PIPELINE_LN PL_PIPE_BEND_LN PL_PIG_SIGNAL_PT PL_PI_EXCAVATION_PT PL_NAT_GROUND_PT PL_METER_STATION_PT PL_JOIN_PT PL_INJECTOR_PT PL_GIRTH_WELD_PT PL_FLANGE_PT PL_ELBOW_PT PL_DRIP_PT PL_DEPTH_OF_COVER_PT PL_COMPRESSOR_STATION_PT"
wrkRunner=fmeobjects.FMEWorkspaceRunner()
wrkRunner.runWithParameters(qcWorkspace,parameters)
print w
print parameters