Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def status_gen(self, date):
# 过滤交易日这一需求,交给各个类自由裁量,这里网格类就需要过掉非交易日干扰,
# 而定投类中则不过掉,遇到非交易日顺延定投更合理些
if date.strftime("%Y-%m-%d") not in opendate:
return 0
if date == self.start:
if self.buypercent[0] == 0:
self.pos += 1
return myround(self.totmoney / self.division)
else:
return 0
value = self.price[self.price["date"] <= date].iloc[-1].loc["netvalue"]
valueb = self.price[self.price["date"] <= date].iloc[-2].loc["netvalue"]
action = 0
for i, buypt in enumerate(self.buypts):
if (value - buypt) <= 0 and (valueb - buypt) > 0 and self.pos <= i:
self.pos += 1
action += myround(self.totmoney / self.division)
for j, sellpt in enumerate(self.sellpts):
if (value - sellpt) >= 0 and (valueb - sellpt) < 0 and self.pos > j:
action += -1 / self.pos
self.pos += -1
return action
for i, term in enumerate(self.buy):
if (
judge * (value - term[0]) <= 0 < judge * (valueb - term[0])
and self.pos + sum([it[1] for it in self.buy[i:]]) <= 1
):
self.pos += term[1]
action += myround(self.totmoney * term[1])
self.selllevel = 0
if self.sell is not None:
for i, term in enumerate(self.sell):
if (
judge * (value - term[0]) >= 0 > judge * (valueb - term[0])
and self.pos > 0
and self.selllevel <= i
):
deltaaction = myround(
term[1] / sum([it[1] for it in self.sell[i:]])
)
action -= (1 + action) * deltaaction # 需考虑一日卖出多仓的情形
self.pos = (1 - deltaaction) * self.pos
self.selllevel = i + 1
return action
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
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 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)])):
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)]):
soldrem.append([rem[i][0], share - sum([rem[j][1] for j in range(i)])])
newrem.append(
[rem[i][0], sum([rem[j][1] for j in range(i + 1)]) - share]
)
elif share <= sum([rem[j][1] for j in range(i)]):
newrem.append(rem[i])
return (soldrem, newrem)
if len(rows) == 1:
return 0
value = rows.iloc[-1].loc[self.col]
valueb = rows.iloc[-2].loc[self.col]
action = 0
if self.buylow is True:
judge = 1
else:
judge = -1
for i, term in enumerate(self.buy):
if (
judge * (value - term[0]) <= 0 < judge * (valueb - term[0])
and self.pos + sum([it[1] for it in self.buy[i:]]) <= 1
):
self.pos += term[1]
action += myround(self.totmoney * term[1])
self.selllevel = 0
if self.sell is not None:
for i, term in enumerate(self.sell):
if (
judge * (value - term[0]) >= 0 > judge * (valueb - term[0])
and self.pos > 0
and self.selllevel <= i
):
deltaaction = myround(
term[1] / sum([it[1] for it in self.sell[i:]])
)
action -= (1 + action) * deltaaction # 需考虑一日卖出多仓的情形
self.pos = (1 - deltaaction) * self.pos
self.selllevel = i + 1
return action