Order down the stairs - set the trigger price

Author: Zero, Date: 2014-09-24 23:57:05
Tags: Trade-aided

If the order is a purchase, the price of the first order is the highest, the price of the next one decreases, the price of the first order is the cheapest, and the price of the next one increases.


function adjustFloat(v) {
    return Math.floor(parseFloat(v.toFixed(10))*1000)/1000;
}

function GetAccount(e) {
    var account;
    while (!(account = exchange.GetAccount())) {
        Sleep(Interval);
    }
    return account;
}

function GetTicker() {
    var ticker;
    while (!(ticker = exchange.GetTicker())) {
        Sleep(1000);
    }
    return ticker;
}

function main() {
    var ticker = GetTicker();
    var InitPrice = ticker.Last;
    if (EnableTrigger) {
        Log('当前价格: ', InitPrice, InitPrice > TriggerPrice ? '价格跌破' : '价格涨超', TriggerPrice, '时触发阶梯下单');
        while (true) {
            if (InitPrice > TriggerPrice && ticker.Last < TriggerPrice) {
                Log('当前价格', ticker.Last, '价格跌破 ', TriggerPrice, '元, 开始下单');
                break;
            } else if (InitPrice < TriggerPrice && ticker.Last > TriggerPrice) {
                Log('当前价格', ticker.Last, '价格涨超 ', TriggerPrice, '元, 开始下单');
                break;
            }
            ticker = GetTicker();
            Sleep(1000);
        }
    }
    var account = GetAccount();
    var needMoney = 0;
    var needStocks = 0;
    for (var i = 0; i < OrderNum; i++) {
        needMoney += (StartPrice - (i * PriceDiff)) * Amount;
        needStocks += Amount;
    }

    if (OpType == 0) {
        if (needMoney > account.Balance) {
            throw "没有足够的钱下单";
        }
        for (var i = 0; i < OrderNum; i++) {
            exchange.Buy(adjustFloat(StartPrice - (i * PriceDiff)), Amount);
        }
    } else {
        if (needStocks > account.Stocks) {
            throw "没有足够的币下单";
        }
        for (var i = 0; i < OrderNum; i++) {
            exchange.Sell(adjustFloat(StartPrice + (i * PriceDiff)), Amount);
        }        
    }
    Log("全部委托完成");
}

Related

More