A single platform balancing strategy

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

This requires building a warehouse, say you have $5,000 in the account, and a coin, and if the value of the coin is greater than the balance in the account is $5,000 and the spread is greater than the depreciation, say the coin is now worth $6,000, you sell it, say $6,000-$5,000, or $6,000-$2,000, or $6,000/$2,000, or $6,000/$2,000, or $6,000/$2,000, or $6,000/$2,000, or $5,000/$4,000, or $4,000/$2,000, or $4,000/$2,000, or $5,000/$4,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $5,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000


/*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

jammingThis is the legendary 50-50 strategy, no need for high frequency.

Quantitative roboticsThis idealization is not feasible, the conditions can only be one-sided. For example, Bitcoin versus USD, the initial condition is that the more money in the account, the less coin can be, if the reverse is the case, will bleed.

hailanyiyeThe idea is unique, learned, thank you for sharing.

momoxJaneway, the phrase is no longer appropriate in this tactic, but simple code, which is actually the most magical tactic, worshiping

South Korea's small woodpeckerThis is supposed to be a bear market, a bull market to make money.