资源加载中... loading...

商品期货等差网格策略

Author: 扫地僧, Date: 2021-12-02 13:59:51
Tags:

一、摘要

在商品期货交易中,趋势跟踪、日内短线、手工炒单、套利、高频是大家比较常见的几种交易方法,但是除了这些方法之外,网格交易也是一种不错的选择,商品期货量化交易也是可以使用网格策略的,那具体怎么操作呢?本篇我们就用优宽量化(youquant.com)交易平台来实现一个简单的商品期货网格策略,希望大家阅读完这篇文章后可以有所收获,话不多说,一起来看看吧。

二、什么是网格交易

网格交易又称渔网交易,它也是量化交易的一种策略,简单的说,网格交易是利用行情震荡波动来赚钱的交易方法,在价格不断上下波动中,通过价格波动的上下区间布置网格低吸高抛获取利润。网格交易不依赖人的主观思考,完全是一种机械行为,比较适合用量化的方式进行交易,利用价格波动在网格区间内低买高卖,通过反复循环差价赚取利润,这种赚取差价的获利方式,可以参照下面这个图片:

img

三、如何设计网格

网格交易本质上是一种空间变时间的玩法,其秉持的原则是“仓位策略比择时策略更重要”。简单的网格是以某个价位为基准点,当价格上涨戓下跌一定的点数或者一定的比例,挂N手数量空单戓多单,每一个格子即是盈利点位,但通常并不设置止损,当价格朝向与持仓有利的方向运动并达到网格点位时获利平仓,并且在该点位挂同样的买单戓卖单。这样这些交易订单就像渔网一样阵列,在行情的波动中来回盈利。


'''backtest
start: 2016-01-01 00:00:00
end: 2021-11-28 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["space",50],["init_amount",1]]
'''


def cancel_order():
    orders = exchange.GetOrders()
    if orders and len(orders) > 0:
        for i in orders:
            exchange.CancelOrder(i.Id)


def trade(type, price, unit):
    exchange.SetDirection(type)
    if type == 'buy' or type == 'closesell':
        exchange.Buy(price, unit)
    elif type == 'sell' or type == 'closebuy':
        exchange.Sell(price, unit)


def on_bar():
    global buy_line, sell_line
    position = exchange.GetPosition()
    depth = exchange.GetDepth()
    if not depth:
        return
    ask = depth['Asks'][0].Price
    bid = depth['Bids'][0].Price
    if len(position) == 0:
        if bid > top:
            trade('sell', bid, unit * init_amount)
            # Log(contract_code, '到达开空区域, 买入空头底仓')
        else:
            trade('buy', ask, unit * init_amount)
            # Log(contract_code, '到达开多区域, 买入多头底仓')
    if len(position) != 1:
        return
    if position[0]["Type"] == 1:
        if ask < bottom:
            # Log(contract_code, '空单全部止盈反手')
            trade('closesell', ask, position[0].Amount)
        else:
            orders = exchange.GetOrders()
            if len(orders) == 0:
                trade('sell', sell_line, unit)
                trade('closesell', buy_line, unit)
            if len(orders) == 1:
                if orders[0]["Type"] == 1: #止盈成交
                    # Log(contract_code, '网格减仓, 当前份数:', position[0].Amount)
                    cancel_order()
                    buy_line = buy_line - space
                    sell_line = sell_line - space
                if orders[0]["Type"] == 0:
                    # Log(contract_code, '网格加仓, 当前份数:', position[0].Amount)
                    cancel_order()
                    buy_line = buy_line + space
                    sell_line = sell_line + space
    if position[0]["Type"] == 0:
        if bid > top:
            # Log(contract_code, '多单全部止盈反手')
            trade('closebuy', bid, position[0].Amount)
        else:
            orders = exchange.GetOrders()
            if len(orders) == 0:
                trade('buy', buy_line, unit)
                trade('closebuy', sell_line, unit)
            if len(orders) == 1:
                if orders[0]["Type"] == 0:
                    # Log(contract_code, '网格减仓, 当前份数:', position[0].Amount)
                    cancel_order()
                    buy_line = buy_line + space
                    sell_line = sell_line + space
                if orders[0]["Type"] == 1:
                    # Log(contract_code, '网格加仓, 当前份数:', position[0].Amount)
                    cancel_order()
                    buy_line = buy_line - space
                    sell_line = sell_line - space


def initialize():
    global buy_line, sell_line
    while not exchange.IO("status"):
        Sleep(1000)
    while not exchange.SetContractType(contract_code):
        Sleep(1000)
    if _G('buy_line') and _G('sell_line'):
        buy_line = _G('buy_line')
        sell_line = _G('sell_line')
        # Log('恢复网格')
    else:
        while True:
            ticker = exchange.GetTicker()
            if ticker:
                break
            Sleep(1000)
        buy_line = ticker["Last"] - space
        sell_line = ticker["Last"] + space
        _G('buy_line', buy_line)
        _G('sell_line', sell_line)
        # Log('重置网格')


def main():
    initialize()
    while True:
        on_bar()
        Sleep(1000)




更多内容