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

单平台均衡策略

Author: Zero, Date: 2014-08-14 20:20:48
Tags: Balance

这个需要建仓,比如账户有5000块钱,跟1个币,如果币的价值大于账户的余额5000了并且差价超过阀值,比如币现在值6000块钱,就卖掉(6000-5000)/6000/2个币,说明币升值了,把钱兑换回来,如果币贬值了,比如4000块钱了,就买入(5000-4000)/4000/2个币, 币跌的时候买一些回来,如果再涨了,就再卖掉,好像天平一样,两边不同的对冲,所以我命名为均衡策略


/*backtest
start: 2018-03-01 00:00:00
end: 2018-08-01 11:00:00
period: 15m
exchanges: [{"eid":"OKCoin_EN","currency":"BTC"}]
*/

function CancelPendingOrders() {
    var ret = false;
    while (true) {
        var orders = null;
        while (!(orders = exchange.GetOrders())) {
            Sleep(Interval);
        }

        if (orders.length == 0) {
            return ret;
        }

        for (var j = 0; j < orders.length; j++) {
            exchange.CancelOrder(orders[j].Id);
            ret = true;
            if (j < (orders.length-1)) {
                Sleep(Interval);
            }
        }
    }
    return ret;
}

var InitAccount = null;

function onTick() {
    var acc = _C(exchange.GetAccount);
    var ticker = _C(exchange.GetTicker);
    var spread = ticker.Sell - ticker.Buy;
    var diffAsset = (acc.Balance - (acc.Stocks * ticker.Sell)) / 2;
    var ratio = diffAsset / acc.Balance;
    LogStatus('ratio:', ratio, _D());
    if (Math.abs(ratio) < threshold) {
        return false;
    }
    if (ratio > 0) {
        var buyPrice = _N(ticker.Sell + spread, ZPrecision);
        var buyAmount = _N(diffAsset / buyPrice, XPrecision);
        if (buyAmount < MinStock) {
            return false;
        }
        exchange.Buy(buyPrice, buyAmount, diffAsset, ratio);
    } else {
        var sellPrice = _N(ticker.Buy - spread, ZPrecision);
        var sellAmount = _N(-diffAsset / sellPrice, XPrecision);
        if (sellAmount < MinStock) {
            return false;
        }
        exchange.Sell(sellPrice, sellAmount, diffAsset, ratio);
    }
    return true;
}

function main() {
    InitAccount = _C(exchange.GetAccount);
    LoopInterval = Math.max(LoopInterval, 1);
    while (1) {
        if (onTick()) {
            Sleep(1000);
            CancelPendingOrders();
            Log(_C(exchange.GetAccount));
        }
        Sleep(LoopInterval * 1000);
    }
}

Related

More

RunningBits 历久弥新,经典

jamming 这个是传说中的50-50策略啊,不需要高频啊。

量化机器人 这个理想化,是不行的,条件只能是单行情。例如比特币对usd涨,初始条件是账户里的钱要多,币少才可以,如果反过来,就会吐血的

hailanyiye 想法很独特,学习了,谢谢分享

momox 大道至简,这句话用在这个策略再合适不过,简单的代码,居然是最神奇的策略,膜拜ing

南朝小木瓜 这个应该是熊市的时候吸币,牛市的时候赚钱