Poly familiar frame width _

Familiar with the wide frame using polyethylene

Description simple strategy:

  • Benchmark: CSI 300 constituent stocks as a benchmark
  • Jiancang Standard: Select Low CSI 300 stocks in the stock to sell (stock <4 million)
  • Only the profit criterion: only the profit sell stock holdings when yields> = 25%
  • Stop standards: when yields <= - sell the stock at 10% stop-loss positions

Code:

# 导入函数库
from jqdata import *

# 初始化函数
def initialize(context):
    # 设置基准
    set_benchmark('000300.XSHG')
    # 动态复权
    set_option('use_real_price', True)
    # 设置手续费
    # 股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
    set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
    # 用户定义
    # get_index_stocks 获取成分股
    g.security = get_index_stocks('000300.XSHG')

def handle_data(context, data):
    # 账户总资金
    t_value = context.portfolio.total_value
    for stock in g.security:
        # 前一天收盘价
        p = attribute_history(stock, count=1, df=False)['close'][0]
        # 持仓总数
        amount = context.portfolio.positions[stock].total_amount
        # 平均成本
        cost = context.portfolio.positions[stock].avg_cost
        # 持仓标准:低价买入
        if p < 4 and amount == 0:
            order_target_value(stock, 0.1 * t_value)
        # 止盈标准:收益率>25%
        elif amount > 0 and p >= cost * 1.25:
            order_target(stock, 0)
        # 止损标准:收益率<10%
        elif amount > 0 and p <= cost * 0.9:
            order_target(stock, 0)

Select the time you have finished editing the code, you can see the results of backtesting run

Poly familiar frame width _
Backtesting Results:
Poly familiar frame width _

Guess you like

Origin blog.51cto.com/tobeys/2445812