Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, infoobj, start, end=yesterdaydash(), totmoney=100000):
self.aim = infoobj
self.totmoney = totmoney
self.price = infoobj.price[
(infoobj.price["date"] >= start) & (infoobj.price["date"] <= end)
]
if len(self.price) == 0:
self.start = convert_date(start)
self.end = convert_date(end)
self.status = pd.DataFrame(data={"date": [], self.aim.code: []})
else:
self.start = self.price.iloc[0].date
self.end = self.price.iloc[-1].date
datel = []
actionl = []
times = pd.date_range(self.start, self.end)
for date in times:
action = self.status_gen(date)
if action > 0:
datel.append(date)
actionl.append(action)
elif action < 0:
datel.append(date)
actionl.append(action * 0.005)
def __init__(self, infoobj, start, end=yesterdaydash(), totmoney=100000):
self.aim = infoobj
self.totmoney = totmoney
self.price = infoobj.price[
(infoobj.price["date"] >= start) & (infoobj.price["date"] <= end)
]
if len(self.price) == 0:
self.start = convert_date(start)
self.end = convert_date(end)
self.status = pd.DataFrame(data={"date": [], self.aim.code: []})
else:
self.start = self.price.iloc[0].date
self.end = self.price.iloc[-1].date
datel = []
actionl = []
times = pd.date_range(self.start, self.end)
for date in times:
action = self.status_gen(date)
if action > 0:
datel.append(date)
actionl.append(action)
elif action < 0:
datel.append(date)
actionl.append(action * 0.005)
df = pd.DataFrame(data={"date": datel, self.aim.code: actionl})
.price[["date", "netvalue"]]
.rename(columns={"netvalue": fundobjs[0].code})
)
for fundobj in fundobjs[1:]:
self.totprice = self.totprice.merge(
fundobj.price[["date", "netvalue"]].rename(
columns={"netvalue": fundobj.code}
),
on="date",
)
startdate = self.totprice.iloc[0].date
if start is None:
self.start = startdate
else:
start = convert_date(start)
if start < startdate:
raise Exception("Too early start date")
else:
self.start = start
self.totprice = self.totprice[self.totprice["date"] >= self.start]
self.totprice = self.totprice.reset_index(drop=True)
for col in self.totprice.columns:
if col != "date":
self.totprice[col] = self.totprice[col] / self.totprice[col].iloc[0]
def sell(remc, share, date):
"""
:returns: tuple, (sold rem, new rem)
sold rem is the positions being sold while new rem is the positions being held
"""
rem = copy(remc)
share = myround(share)
date = convert_date(date)
totposition = sum([pos[1] for pos in rem]) # the remaining shares
if totposition == 0:
return ([], [])
if (date - rem[-1][0]).days < 0:
raise Exception(_errmsg)
if share > totposition:
share = totposition # not raise error when you sell more than you buy
soldrem = []
newrem = []
for i, pos in enumerate(rem):
if share > myround(sum([rem[j][1] for j in range(i + 1)])):
soldrem.append(rem[i])
elif share == myround(sum([rem[j][1] for j in range(i + 1)])):
soldrem.append(rem[i])
elif share < myround(sum([rem[j][1] for j in range(i + 1)])):
if share > sum([rem[j][1] for j in range(i)]):
def buy(remc, share, date):
"""
:param remc: array of two-elements arrays, eg [[pd.Timestamp(), 50],[pd.Timestamp(), 30]
the first element in tuple is pandas.Timestamp object for date while the second
element is positive float for remaining shares, tuples in rem MUST be time ordered.
:param share: positive float, only 2 decimal is meaningful.
:param date: string in the date form or datetime object
:returns: new rem after the buying
"""
rem = copy(remc)
share = myround(share)
date = convert_date(date)
if len(rem) == 0:
return [[date, share]]
elif (date - rem[-1][0]).days > 0:
rem.append([date, share])
return rem
elif (date - rem[-1][0]).days == 0:
rem[-1][1] = rem[-1][1] + share
return rem
else:
raise Exception(_errmsg)
def trans(remc, coef, date):
"""
在基金份额折算时,将之前持有的仓位按现值折算,相当于前复权
:param coef: the factor shown in comment column of fundinfo().price, but with positive value
:param date: string in date form or datetime obj
:returns: new rem after converting
"""
rem = copy(remc)
date = convert_date(date)
if len(rem) == 0:
return []
if (date - rem[-1][0]).days <= 0:
raise Exception(_errmsg)
newrem = [[item[0], myround(item[1] * coef)] for item in rem]
return newrem