python量化平台怎么搭建_python之在线平台与量化投资

0. 第一个量化策略#初始化函数,设定基准等等

definitialize(context):

set_benchmark('000300.XSHG')

g.security= get_index_stocks('000300.XSHG') #股票池

set_option('use_real_price', True)

set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

log.set_level('order','warning')defhandle_data(context, data):#一般情况下先卖后买

tobuy=[]for stock ing.security:

p=get_current_data()[stock].day_open

amount=context.portfolio.positions[stock].total_amount

cost=context.portfolio.positions[stock].avg_costif amount > 0 and p >= cost * 1.25:

order_target(stock, 0)#止盈

if amount > 0 and p <= cost * 0.9:

order_target(stock, 0)#止损

if p <= 10.0 and amount ==0:

tobuy.append(stock)if len(tobuy)>0:

cash_per_stock= context.portfolio.available_cash /len(tobuy)for stock intobuy:

order_value(stock, cash_per_stock)1. 双均线策略definitialize(context):

set_benchmark('600519.XSHG')

set_option('use_real_price', True)

set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

g.security= ['600519.XSHG']

g.p1= 5g.p2= 30

defhandle_data(context, data):for stock ing.security:#金叉:如果5日均线大于10日均线并且不持仓

#死叉:如果5日均线小于10日均线并且持仓

df =attribute_history(stock, g.p2)

ma10= df['close'].mean()

ma5= df['close'][-5:].mean()if ma10 > ma5 and stock incontext.portfolio.positions:#死叉

order_target(stock, 0)if ma10 < ma5 and stock not incontext.portfolio.positions:#金叉

order_value(stock, context.portfolio.available_cash * 0.8)#record(ma5=ma5, ma10=ma10)

2. 因子选股definitialize(context):

set_benchmark('000002.XSHG')

set_option('use_real_price', True)

set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

g.security= get_index_stocks('000002.XSHG')

g.q=query(valuation).filter(valuation.code.in_(g.security))

g.N= 20run_monthly(handle,1)defhandle(context):

df= get_fundamentals(g.q)[['code', 'market_cap']]

df= df.sort('market_cap').iloc[:g.N,:]

to_hold= df['code'].valuesfor stock incontext.portfolio.positions:if stock not into_hold:

order_target(stock, 0)

to_buy= [stock for stock in to_hold if stock not incontext.portfolio.positions]if len(to_buy) >0:

cash_per_stock= context.portfolio.available_cash /len(to_buy)for stock into_buy:

order_value(stock, cash_per_stock)3. 多因子选股definitialize(context):

set_benchmark('000002.XSHG')

set_option('use_real_price', True)

set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

g.security= get_index_stocks('000002.XSHG')

g.q=query(valuation, indicator).filter(valuation.code.in_(g.security))

g.N= 20run_monthly(handle,1)defhandle(context):

df= get_fundamentals(g.q)[['code', 'market_cap', 'roe']]

df['market_cap'] = (df['market_cap'] - df['market_cap'].min()) / (df['market_cap'].max()-df['market_cap'].min())

df['roe'] = (df['roe'] - df['roe'].min()) / (df['roe'].max()-df['roe'].min())

df['score'] = df['roe']-df['market_cap']

df= df.sort('score').iloc[-g.N:,:]

to_hold= df['code'].valuesfor stock incontext.portfolio.positions:if stock not into_hold:

order_target(stock, 0)

to_buy= [stock for stock in to_hold if stock not incontext.portfolio.positions]if len(to_buy) >0:

cash_per_stock= context.portfolio.available_cash /len(to_buy)for stock into_buy:

order_value(stock, cash_per_stock)4. 均值回归importjqdataimportmathimportnumpy as npimportpandas as pddefinitialize(context):

set_option('use_real_price', True)

set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

set_benchmark('000002.XSHG')

g.security= get_index_stocks('000002.XSHG')

g.ma_days= 30g.stock_num= 10run_monthly(handle,1)defhandle(context):

sr= pd.Series(index=g.security)for stock insr.index:

ma= attribute_history(stock, g.ma_days)['close'].mean()

p=get_current_data()[stock].day_open

ratio= (ma-p)/ma

sr[stock]=ratio

tohold=sr.nlargest(g.stock_num).index.values#print(tohold)

#to_hold = #

for stock incontext.portfolio.positions:if stock not intohold:

order_target_value(stock, 0)

tobuy= [stock for stock in tohold if stock not incontext.portfolio.positions]if len(tobuy)>0:

cash=context.portfolio.available_cash

cash_every_stock= cash /len(tobuy)for stock intobuy:

order_value(stock, cash_every_stock)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值