聚宽源码59
原文策略源碼如下:
#次新+小市值+KAMA擇時 輪動
導入TA-Lib
import talib as tb
def initialize(context):
set_commission(PerTrade(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
set_option(‘use_real_price’, True)
g.buy_stock_count = 3
g.kama_days = 13
g.new_public_days = 150
g.return_radio = 0.999
def handle_data(context, data):
hour = context.current_dt.hour
minute = context.current_dt.minute
def market_end(context):
g.days_counter = g.days_counter +1;
#過濾停牌 st
def filter_paused_and_st_stock(stock_list):
current_data = get_current_data()
return [stock for stock in stock_list if not current_data[stock].paused
and not current_data[stock].is_st and ‘ST’ not in current_data[stock].
name and ‘*’ not in current_data[stock].name and ‘退’ not in current_data[stock].name]
def filter_gem_stock(context, stock_list):
return [stock for stock in stock_list if stock[0:3] != ‘300’]
#篩選股票, 次新最小市值排序獲取前N個
def select_stocks(context,data):
q = query(valuation.code,
valuation.circulating_market_cap
).order_by(
valuation.circulating_market_cap.asc()
).filter(
valuation.circulating_market_cap <= 99
).limit(100)
def adjust_position(context,data, buy_stocks):
#賣
for stock in context.portfolio.positions.keys():
last_prices = history(1, ‘1m’, ‘close’, security_list=context.portfolio.positions.keys())
if stock not in buy_stocks:
curr_data = get_current_data()
if last_prices[stock][-1] < curr_data[stock].high_limit:
position = context.portfolio.positions[stock]
order_target_value(stock, 0)
#買
for stock in buy_stocks:
position_count = len(context.portfolio.positions)
if g.buy_stock_count > position_count:
value = context.portfolio.cash / (g.buy_stock_count - position_count)
if context.portfolio.positions[stock].total_amount == 0:
order_target_value(stock, value)
def get_kama_single(context,security):
period= g.kama_days46
close_long = get_price(security, end_date=context.current_dt, frequency=‘10m’, fields=[‘close’], count= period +246 )[‘close’].values;
#close_short = get_price(g.security, end_date=context.previous_date, frequency=‘1d’, fields=[‘close’], count= short_days+10 )[‘close’].values;
kama_long = tb.KAMA(close_long,timeperiod= period);
#kama_short = tb.KAMA(close_short,timeperiod= short_days);
# if( (kama_short[-1] > kama_long[-1]) & (kama_short[-2] <= kama_long[-2]) & (kama_short[-1] > kama_short[-2])):
# return 1
# if(( kama_short[-1] < kama_long[-1]) & (kama_short[-2] >= kama_long[-2]) & (kama_short[-1] < kama_short[-2])):
# return 0
#std = np.std(kama_long[long_days:]);
總結
- 上一篇: 网络流量监控器mrtg全攻略
- 下一篇: crh寄存器_STM32 学习笔记(寄存