Giá trị là 0.418.

Tác giả:Kakababy, Ngày: 2016-02-14 00:09:10
Tags:Số dư

Thiết lập một khoản vốn để đầu tư, chẳng hạn như 10.000 đô la. Thiết lập tỷ lệ giá trị của tiền và Bitcoin, chẳng hạn như 50:50; Theo giá hiện tại, duy trì tỷ lệ giá trị của tiền và Bitcoin.

Ví dụ, đầu tư ban đầu là 10.000 đô la, tỷ lệ tiền và Bitcoin được thiết lập là 30:70 và mua 7.000 đô la Bitcoin trong giao dịch đầu tiên (giả sử giá đồng tiền là 1.000 đô la, mua 7 đồng tiền), số tiền còn lại là 3.000 đô la, giữ tỷ lệ 30:70).

Trong chu kỳ giao dịch tiếp theo, giả sử giá đồng xu thay đổi thành 1500 USD, tổng giá trị sẽ thay đổi thành 1500 USD.7 + 3000 = 13500, nên giữ số tiền trong tay là 135000.3 = 4050 đô la, bạn nên bán 4050 - 3000 = 1050 Bitcoin, số lượng bán là 1050/1500 = 0.7, số Bitcoin còn lại trong tay là 7-0.7 = 6.3, giá trị đồng tiền là 6.3 * 1500 = 9450;; tỷ lệ tiền với giá trị Bitcoin là 4050:9450 = 30:70;;

Cũng như vậy, khi giá đồng tiền giảm, họ mua để giữ tỷ lệ không thay đổi.


var initAccount = null;
var initPrice = 0;
var stockHold = 0;
var moneyHold = 0;
var lastOrderID = null;
var prices = [];

function AdjustFloat(v) {
    return Math.floor(v * 1000) / 1000;
}

function GetAccount() {
    var account = null;
    while (!(account = exchange.GetAccount())) {
        Log('Get Account Error');
        Sleep(ErrorInterval);
    }
    return account;
}

function GetCurrentPrice() {
    var ticker = null;
    while (!(ticker = exchange.GetTicker())) {
        Log('Get Ticker Error');
        Sleep(ErrorInterval);
    }
    return AdjustFloat(ticker.Last);
}

function GetOrders() {
    var orders = null;
    while (!(orders = exchange.GetOrders())) {
        Log('Get Orders Error');
        Sleep(ErrorInterval);
    }
    return orders;
}

function CancelPendingOrders() {
    while (true) {
        var orders = GetOrders();
        if (orders.length === 0) {
            return;
        }

        for (var i = 0; i < orders.length; i++) {
            exchange.CancelOrder(orders[i].Id);
            if (i < (orders.length - 1)) {
                Sleep(ErrorInterval);
            }
        }
    }
}

function GetStatus() {
    var price = GetCurrentPrice();
    var initStockValue = InitStock + InitMoney / initPrice;
    var initMoneyValue = InitMoney + InitStock * initPrice;
    var stockValueNow = stockHold + moneyHold / price;
    var moneyValueNow = stockHold * price + moneyHold;
    var stockEarned = stockValueNow - initStockValue;
    var moneyEarned = moneyValueNow - initMoneyValue;
    var averagePrice = 0;
    var alpha = (stockHold - InitStock) * (price - initPrice);
    var beta = moneyEarned - alpha;

    var sum = 0;
    if (prices.length > 0) {
        for (var i = prices.length - 1; i >= 0; i--) {
            sum += prices[i];
        }
        averagePrice = sum / prices.length;
    }

    var logString = 'Stock Holding:' + AdjustFloat(stockHold) + '\n';
    logString += 'Money Holding:' + AdjustFloat(moneyHold) + '\n';
    logString += 'Stock Earned:' + AdjustFloat(stockEarned) + '\n';
    logString += 'Money Earned:' + AdjustFloat(moneyEarned) + '\n';
    logString += 'History Average price:' + averagePrice + '\n';
    logString += 'Current Price:' + price + '\n';
    logString += 'Alpha:' + alpha + '\n';
    logString += 'Beta:' + beta;

    LogProfit(moneyEarned);

    return logString;
}

function CaculateHoldings() {
    CancelPendingOrders();

    if (lastOrderID) {
        var order = exchange.GetOrder(lastOrderID);
        Log('last order:', order);
        if (order.Status === ORDER_STATE_CLOSED) {
            if (order.Type === ORDER_TYPE_BUY) { //Buy
                stockHold += order.DealAmount;
                moneyHold -= order.DealAmount * order.Price;
                Log("Order Buy. stockHold +=:", order.DealAmount, ',Money -=:', order.DealAmount * order.Price);
            } else { //Sell
                stockHold -= order.DealAmount;
                moneyHold += order.DealAmount * order.Price;
                Log("Order Sell. stockHold -=:", order.DealAmount, ',Money +=:', order.DealAmount * order.Price);
            }
        }
    }
}

function OnTick() {
    var currentPrice = GetCurrentPrice();
    prices.push(currentPrice);
    //Log('History prices:',prices);

    var stockValue = 0;
    var totalValue = 0;
    var ratio = 0;

    stockValue = stockHold * currentPrice;
    totalValue = stockValue + moneyHold;

    ratio = stockValue / totalValue;
    Log('Current Price:', currentPrice);
    Log('stockHold:', stockHold);
    Log('moneyHold:', moneyHold);
    Log('stockValue:', stockValue);
    Log('Current Ratio:', ratio);

    if (ratio > StockRatio) { //Sell
        Log('Current ratio > StockRatio');
        var sellAmt = (stockValue - totalValue * StockRatio) / (currentPrice - SlidePrice);
        lastOrderID = exchange.Sell(currentPrice - SlidePrice, sellAmt);
    } else { //Buy
        Log('Current ratio < StockRatio');
        var buyAmt = (totalValue * StockRatio - stockValue) / (currentPrice + SlidePrice);
        lastOrderID = exchange.Buy(currentPrice + SlidePrice, buyAmt);
    }
    Log('Order ID:', lastOrderID);
    CaculateHoldings();

}

function main() {
    if (InitStock < 0) {
        Log('Stock less than zero.');
        return;
    }

    if (InitMoney < 0) {
        Log('Money less than zero.');
    }

    //if(InvestInterval < 0){
    //    Log('InvestInterval less than zero.');
    //}

    if (ErrorInterval < 0) {
        Log('ErrorInterval less than zero.');
    }

    if (StockRatio < 0 || StockRatio > 1) {
        Log('StockRatio should be a number from 0 to 1.');
    }

    EnableLogLocal(true);
    SetFailover(true);
    exchange.IO("websocket");

    Log(exchange.GetName(), exchange.GetCurrency());

    initAccount = _G('InitAccount');
    if (initAccount === null) {
        initAccount = GetAccount();
        _G('InitAccount', initAccount);
    }
    Log('InitAccount', initAccount);

    initPrice = _G('InitPrice');
    if (initPrice === null) {
        initPrice = GetCurrentPrice();
        _G('InitPrice', initPrice);
    }
    Log('InitPrice', initPrice);

    if (UseAccount) {
        InitMoney = initAccount.Balance + initAccount.FrozenBalance;
        InitStock = initAccount.Stocks + initAccount.FrozenStocks;
    }
    Log('InitMoney:', InitMoney);
    Log('InitStock:', InitStock);

    stockHold = _G('StockHold');
    if (stockHold === null) {
        stockHold = InitStock;
        Log('Set Stock Hold', stockHold);
    }

    moneyHold = _G('MoneyHold');
    if (moneyHold === null) {
        moneyHold = InitMoney;
        Log('Set Money Hold', moneyHold);
    }

    if (_G('Prices') !== null) {
        prices = _G('Prices');
    }

    while (true) {
        OnTick();
        LogStatus(GetStatus());
        Sleep(InvestInterval * 1000 * 60);
    }
}

function onexit() {
    _G('StockHold', stockHold);
    _G('MoneyHold', moneyHold);
    //_G('Prices',prices);
    Log('Set Stock Hold', stockHold);
    Log('Set Money Hold', moneyHold);
    Log('Set prices:', prices);
    Log('Robot Stopped!#ff0000@');
}

Có liên quan

Thêm nữa