Loading ...

合约下单

Author: 一拳男孩, Date: 2020-04-05 21:24:29
Tags:

$.openPos(buy


/*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());
}

More

胜天半子 /upload/asset/11d6077a5d7d32a32d709.png /upload/asset/11dfaef590e0c31a2b72b.png

胜天半子 开仓后 再平仓 提示仓位不足 麻烦大神看看

bamsmen 请问 Id: _.last(tempOrders).Id 这里是不是有问题?报错main:58:35 - TypeError: Cannot read property 'Id' of undefined

bth2020 这个模板怎么引用啊?