关于python:Python-一起来对网格做下回测

50次阅读

共计 1974 个字符,预计需要花费 5 分钟才能阅读完成。

max_consume_money = consume_money
for day_up_down in data.index:

open = data.loc[day_up_down].values[0]
high = data.loc[day_up_down].values[1]
low = data.loc[day_up_down].values[2]
# close = data.loc[day_up_down].values[3]
# 盘前
# 如果 opt 为空,没有任何操作,基准价 > 开盘价,触发买入
# if len(opt) == 0 and benchmark > open:
if benchmark * (1 - grid) > open:
    # 一手买入 或者 倍数买入
    # 买入
    # 基准价变更
    benchmark = open
    print(day_up_down, "收盘买入", benchmark)
    # 计算的操作
    consume_money += Decimal(benchmark) * Decimal(count)
    if consume_money.compare(max_consume_money) > 0:
        max_consume_money = consume_money
    # 增加记录
    h = history.History(stock_code, 1, open, count)
    opt.append(h)
    opt_b.append([day_up_down, benchmark, 1])
elif benchmark * (1 + grid) <= open:
    if len(opt) > 0:
        # 卖出
        # 基准价变更
        benchmark = open
        # 计算的操作
        # 利润
        temp = float(((Decimal(benchmark) - Decimal(opt[len(opt) - 1].price)) * count).quantize(Decimal('0.00')))
        profit += temp
        consume_money -= Decimal(benchmark) * Decimal(count)
        print(day_up_down, "收盘卖出", benchmark, opt[len(opt) - 1].price, "收益", temp)
        # 批改记录
        h = history.History(stock_code, -1, benchmark, count)
        opt.pop()
        opt_s.append([day_up_down, benchmark, -1])
while benchmark * (1 - grid) >= low:
    # 盘中
    # 一手买入 或者 倍数买入
    # 买入
    # 基准价变更
    benchmark = float(Decimal(benchmark * (1 - grid)).quantize(Decimal('0.000')))
    print(day_up_down, "盘中买入", benchmark)
    # 计算的操作
    consume_money += Decimal(benchmark) * Decimal(count)
    if consume_money.compare(max_consume_money) > 0:
        max_consume_money = consume_money
    # 增加记录
    h = history.History(stock_code, 1, benchmark, count)
    opt.append(h)
    opt_b.append([day_up_down, benchmark, 1])
# open = high 开盘价就是最高价的状况 [Skrill 下载](https://www.gendan5.com/wallet/Skrill.html) 到时候再触发 low 会多买,这是一个假收益
# 不会那么巧吧开盘价跟最高价一样
if len(opt) > 0 and open != high:
    while len(opt) > 0 and benchmark * (1 + grid) <= high:
        # 卖出
        # 基准价变更
        benchmark = float(Decimal(benchmark * (1 + grid)).quantize(Decimal('0.000')))
        # 计算的操作
        temp = float(((Decimal(benchmark) - Decimal(opt[len(opt) - 1].price)) * count)
                .quantize(Decimal('0.00')))
        profit += temp
        consume_money -= Decimal(benchmark) * Decimal(count)
        print(day_up_down, "盘中卖出", benchmark, opt[len(opt) - 1].price, "收益", temp)
        # 批改记录
        h = history.History(stock_code, -1, benchmark, count)
        opt.pop()
        opt_s.append([day_up_down, benchmark, -1])

正文完
 0