Unverified Commit efbb432e authored by Dr.李's avatar Dr.李 Committed by GitHub

Merge pull request #3 from lion-sing/master

update outright data fetch function
parents 19bb12be ebd7a27f
...@@ -1920,8 +1920,8 @@ class Gogoal(Base): ...@@ -1920,8 +1920,8 @@ class Gogoal(Base):
tcap = Column(Float(53)) tcap = Column(Float(53))
class Outright(Base): class OutrightTmp(Base):
__tablename__ = 'outright' __tablename__ = 'outright_tmp'
__table_args__ = ( __table_args__ = (
Index('outright_trade_date_code_portfolio_name_uindex', 'trade_date', 'code', 'portfolio_name', unique=True), Index('outright_trade_date_code_portfolio_name_uindex', 'trade_date', 'code', 'portfolio_name', unique=True),
) )
...@@ -1932,8 +1932,8 @@ class Outright(Base): ...@@ -1932,8 +1932,8 @@ class Outright(Base):
volume = Column(Integer, nullable=False) volume = Column(Integer, nullable=False)
class OutrightTmp(Base): class Outright(Base):
__tablename__ = 'outright_tmp' __tablename__ = 'outright'
__table_args__ = ( __table_args__ = (
Index('outright_trade_id_trade_date_code_portfolio_name_uindex', 'trade_id', 'trade_date', 'code', Index('outright_trade_id_trade_date_code_portfolio_name_uindex', 'trade_id', 'trade_date', 'code',
'portfolio_name', unique=True), 'portfolio_name', unique=True),
......
...@@ -33,7 +33,7 @@ from alphamind.data.dbmodel.models import Formulas ...@@ -33,7 +33,7 @@ from alphamind.data.dbmodel.models import Formulas
from alphamind.data.dbmodel.models import DailyPortfoliosSchedule from alphamind.data.dbmodel.models import DailyPortfoliosSchedule
from alphamind.data.dbmodel.models import Performance from alphamind.data.dbmodel.models import Performance
from alphamind.data.dbmodel.models import Positions from alphamind.data.dbmodel.models import Positions
from alphamind.data.dbmodel.models import OutrightTmp from alphamind.data.dbmodel.models import Outright
from alphamind.data.transformer import Transformer from alphamind.data.transformer import Transformer
from alphamind.model.loader import load_model from alphamind.model.loader import load_model
from alphamind.formula.utilities import encode_formula from alphamind.formula.utilities import encode_formula
...@@ -795,15 +795,25 @@ class SqlEngine(object): ...@@ -795,15 +795,25 @@ class SqlEngine(object):
index=False, index=False,
dtype={'weight': sa.types.JSON}) dtype={'weight': sa.types.JSON})
def fetch_outright_status(self, ref_date: str): def fetch_outright_status(self, ref_date: str, is_open=True):
table = OutrightTmp table = Outright
if is_open:
id_filter = 'notin_'
else:
id_filter = 'in_'
t = select([table.trade_id]).\ t = select([table.trade_id]).\
where(and_(table.trade_date <= ref_date, where(and_(table.trade_date <= ref_date,
table.operation == 'withdraw')).alias('t') table.operation == 'withdraw')).alias('t')
query = select([table]).\ query = select([table]).\
where(and_(table.trade_id.notin_(t), where(and_(getattr(table.trade_id, id_filter)(t),
table.trade_date <= ref_date)) table.trade_date <= ref_date,
df = pd.read_sql(query, engine.engine).set_index('trade_id') table.operation == 'lend'))
df = pd.read_sql(query, self.engine).set_index('trade_id')
if df.empty:
return
# calc total volume # calc total volume
df['total_volume'] = df.groupby('trade_id')['volume'].transform(sum) df['total_volume'] = df.groupby('trade_id')['volume'].transform(sum)
...@@ -815,8 +825,11 @@ class SqlEngine(object): ...@@ -815,8 +825,11 @@ class SqlEngine(object):
if rule[0] in ['closePrice', 'openPrice']: if rule[0] in ['closePrice', 'openPrice']:
query = select([getattr(Market, rule[0])]).\ query = select([getattr(Market, rule[0])]).\
where(and_(Market.code == code, Market.trade_date == rule[1])) where(and_(Market.code == code, Market.trade_date == rule[1]))
data = pd.read_sql(query, engine.engine) data = pd.read_sql(query, self.engine)
price = data.values[0][0] if not data.empty:
price = data.values[0][0]
else:
price = None
elif rule[0] == 'fixedPrice': elif rule[0] == 'fixedPrice':
price = float(rule[1]) price = float(rule[1])
else: else:
...@@ -824,7 +837,7 @@ class SqlEngine(object): ...@@ -824,7 +837,7 @@ class SqlEngine(object):
return price return price
df['price'] = df.apply(lambda x: parse_price_rule(x), axis=1) df['price'] = df.apply(lambda x: parse_price_rule(x), axis=1)
df.drop(['remark', 'price_rule'], axis=1, inplace=True) df.drop(['remark', 'price_rule', 'operation'], axis=1, inplace=True)
# pivot portfolio volume # pivot portfolio volume
total_cols = df.columns total_cols = df.columns
pivot_cols = ['portfolio_name', 'volume'] pivot_cols = ['portfolio_name', 'volume']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment