量化交易策略一

1.设置股票为沪深300的所有成分股

2.如果当前股价小于10元/股且当前不持仓,则买入

3.如果当前股价比买入时上涨了25%,则清仓止盈

4.如果当前股价比买入时下跌了10%,则清仓止损

 

import jqdata

def initialize(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,close_today_commission=0, min_commission=5),type='stock')

#每天执行一次
def handle_data(context,data):
    #print(get_current_data()['601318.XSHG'].day_open)
    #print(attribute_history('601318.XSHG',5))
    #这里的100是股数
    #order('601318.XSHG',100)
    #这里的10000 是价格
    #order_value('601318.XSHG',10000)
    tobuy = []
    for stock in g.security:
        p = get_current_data()[stock].day_open
        amount = context.portfolio.positions[stock].total_amount
        
        #平均开仓成本
        cost = context.portfolio.positions[stock].avg_cost
        
        if 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)
        
    cash_per_stock = context.portfolio.available_cash/len(tobuy)
    #buy stock
    for stock in tobuy:
        order_value(stock,cash_per_stock)
    

 

你可能感兴趣的:(量化交易)