Skip to content

Latest commit

 

History

History
148 lines (125 loc) · 3.73 KB

合约下单.md

File metadata and controls

148 lines (125 loc) · 3.73 KB

Name

合约下单

Author

一拳男孩

Strategy Description

$.openPos(buy|sell, amount);

开仓

$.closePos(buy|sell, amount);

平仓

Strategy Arguments

Argument Default Description
SLIP_POINT 0.01 滑点
TRADE_TIMEOUT 10 交易超时
FACE_VALUE 10 面值
PRICE_PRECISION 4 价格精度
AMOUNT_PRECISION 8 交易量精度

Source (javascript)

/*backtest
start: 2020-01-08 00:00:00
end: 2020-04-06 00:00:00
period: 1d
exchanges: [{"eid":"Futures_OKCoin","currency":"ETH_USD","stocks":1400}]
args: [["PRICE_PRECISION",2],["AMOUNT_PRECISION",4]]
*/

var tempOrders = [];

var COLOR = {
    GREEN: '#00a500',
    RED: '#ff0000',
    GRAY: '#b2b2b2',
    ORANGE: '#ff9e38'
}

function cancelOrder(ex,order_id){
    if (!order_id) return Log('不正确的订单id', order_id);
	Log("取消订单" + order_id);
	ex.CancelOrder(order_id);
	Sleep(500);
	var orders=_C(ex.GetOrders);
	var find=false;
	for (var i=0;i<orders.length;++i){
		if (orders[i].Id===order_id){
			find=true;
			break;
		}
	}
	if (find){
		cancelOrder(ex.order_id);
	}
}

var trade = function(direction, amount) {
    var ticker = _C(exchange.GetTicker);
    exchange.SetDirection(direction);
    var price = 0
    if (_.contains(['buy', 'closesell'], direction)) {
        price = _N(new Decimal(ticker.Last).plus(SLIP_POINT).toNumber(), PRICE_PRECISION);
    } else {
        price = _N(new Decimal(ticker.Last).minus(SLIP_POINT).toNumber(), PRICE_PRECISION);
    }
    var orderInfo = null;
    var avgPrice = null;
    var oid = null;
    var beginTime = null;
    var dealAmount = _.reduce(tempOrders, function(p, n) {
        return new Decimal(p).plus(n.DealAmount).toNumber();
    }, 0);
    var remainAmount = Math.floor(new Decimal(amount).minus(dealAmount).toNumber());
    if (remainAmount < 1) {
        avgPrice = _N(new Decimal(_.reduce(tempOrders, function(p, n) {
            return new Decimal(p).plus(new Decimal(n.AvgPrice).mul(n.DealAmount).mul(FACE_VALUE)).toNumber()
        }, 0)).div(new Decimal(dealAmount).mul(FACE_VALUE)).toNumber(), PRICE_PRECISION);
        orderInfo = {
            Id: _.last(tempOrders).Id,
            Type: _.contains(['buy', 'closebuy'], direction) ? PD_LONG : PD_SHORT,
            Price: price,
            AvgPrice: avgPrice,
            Amount: amount,
            DealAmount: dealAmount,
            Orders: _.pluck(tempOrders, 'Id')
        }
        tempOrders = [];
        return orderInfo;
    } else {
        oid = _.contains(['buy', 'closesell'], direction) ? exchange.Buy(price, remainAmount) : exchange.Sell(price, remainAmount);
        if (oid) {
            beginTime = Unix();
            while(1) {
                orderInfo = _C(exchange.GetOrder, oid);
                if (orderInfo.Status !== ORDER_STATE_PENDING) {
                    tempOrders.push(orderInfo);
                    return trade(direction, amount);
                }
                if (Unix() - beginTime >= TRADE_TIMEOUT) {
                    Log('交易超时, 撤销订单' + oid + COLOR.RED);
                    cancelOrder(exchange, oid);
                    continue;
                }
                Sleep(250);
            }
        } else {
            return null;
        }
    }
}
$.OpenPos = function(direction, amount) {
    return trade(direction, amount);
};

$.ClosePos = function(direction, amount) {
    return trade('close' + direction, amount);
}

function main() {
    exchange.SetContractType('this_week');
    exchange.SetPrecision(PRICE_PRECISION, 0);
    Log($.OpenPos('buy', 2));
    $.OpenPos('sell', 1);
    $.ClosePos('buy', 1);
    $.ClosePos('buy', 1);
    $.ClosePos('sell', 1);
    Log(exchange.GetPosition());
}

Detail

https://www.fmz.com/strategy/195453

Last Modified

2020-09-19 18:56:34