金融数据分析| JoinQuant 量化策略
第一个量化策略:
设置股票池为沪深300的所有成分股,
如果当前股价小于10元/股且当前不持仓, 则买入;
如果当前股价比买入时上涨了25%, 则清仓止盈;
如果当前股价比买入时下跌了10%, 则清仓止损。
https://www.joinquant.com/algorithm/index/list
import jqdata
def initialize(context):
#g.security = '601318.XSHG'
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')
#print(g.security)
def handle_data(context, data):
#print(get_current_data()['601318.XSHG'].day_open)
#print(attribute_history('601318.XSHG', 5))
#order('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)
for stock in tobuy:
order_value(stock, cash_per_stock)