Index equilibrium strategy (teaching)

Author: ruby, Created: 2018-10-09 15:15:50, Updated: 2018-10-09 15:16:41

var account = _C(exchanges[0].GetAccount);      // Get the account information before the strategy starts running, and then assign it to the global variable account
var coinValue = {};                             // Declare the global variable coinValue , assign it with an empty object
var totalValue = 0;                             // totalValue
function updateValue(){                         // update Value
    var logString = 'Time: '+ _D() + '\n';      // Declare a variable, log string, initialize it, and record the current time
    totalValue = coinValue[BaseAsset].value;    // Update totalValue 
    logString += BaseAsset + ': ' + _N(coinValue[BaseAsset].value,5) + '\n';    // Add data of current base asset's total value to logString.
    for(var i=0;i<exchanges.length;i++){                                        // Check through exchanges
        coinValue[BaseAsset] = {amount:account.Balance + account.FrozenBalance, value:account.Balance + account.FrozenBalance};  // Initialize amount and value to BaseAsset.
        var account = _C(exchanges[i].GetAccount);                              // Update current indexed account information
        var ticker = _C(exchanges[i].GetTicker);                                // Update current indexed ticker information
        var symbol = exchanges[i].GetCurrency().split('_')[0];                  // Get currency name
        coinValue[symbol].amount = account.Stocks + account.FrozenStocks;       // Record the amount of currency
        coinValue[symbol].value = coinValue[symbol].amount * ticker.Last;       // Record the value of currency
        totalValue += coinValue[symbol].value;                                  // Cumulative total value
        coinValue[symbol].buyPrice = ticker.Buy;                                // Update the latest buying price info of the current indexed exchange object
        coinValue[symbol].sellPrice = ticker.Sell;                              // Update the latest selling price info of the current indexed exchange object
        logString += symbol + ': ' + _N(coinValue[symbol].value,5) + '\n'       // Add the value of the currency of the current indexed exchange to logString.
        Sleep(1000)
    }
    LogStatus(logString);                                            // LogStatus
}
var keepPercent = Ratio.split('|').map(Number);                     // Split the Ratio parameter with the character "|", then call the map function and convert the string to the Number type to return a new array.
if(math.sum(keepPercent) > 1){                                     // Count the sum of the element values in the keepPercent array
    throw 'sum of keep percent should be lower than 1';           // The sum of assets should not exceed 100%
}
var buyPercent = ToBuy.split('|').map(Number);                     // Construct a buy ratio array based on the parameter processing string
var sellPercent = ToSell.split('|').map(Number);                  // Construct sell ratio array based on the parameter processing string
for(var i=0;i<exchanges.length;i++){                             // Check through the array of exchange objects
    var symbol = exchanges[i].GetCurrency().split('_')[0];      // Get currency name in the transaction pair, and assign it to symbol 
    coinValue[symbol] = {amount:0, value:0, buyPrice:0, sellPrice:0, keepPercent:0, buyPercent:0, sellPercent:0};  // Construct data about each currency in transaction pair:amount, value, buyPrice, sellPrice, keepPercent, buyPercent, sellPercent.
    coinValue[symbol].keepPercent = keepPercent[i];            // Initialization, keepPercent
    coinValue[symbol].buyPercent = buyPercent[i];             // Initialization, buyPercent
    coinValue[symbol].sellPercent = sellPercent[i];          // Initialization, sellPercent
}
function CancelPendingOrders(e) {                           // Cancel all pending orders
    var orders = _C(e.GetOrders);
    for (var j = 0; j < orders.length; j++) {
        exchange.CancelOrder(orders[j].Id, orders[j]);
        Sleep(300);
    }
}
function onTick(){    // the implementation of main function
    updateValue();    // Update currency net value
    for(var i=0;i<exchanges.length;i++){                         // check through exchanges
        var symbol = exchanges[i].GetCurrency().split('_')[0];   // Get the currency name of the current exchange
        if(coinValue[symbol].value > (1+coinValue[symbol].sellPercent)*totalValue*coinValue[symbol].keepPercent){
           var sellAmount = (coinValue[symbol].value - totalValue*coinValue[symbol].keepPercent)/coinValue[symbol].buyPrice
           exchanges[i].Sell(coinValue[symbol].buyPrice, sellAmount)
           CancelPendingOrders(exchanges[i]);
           }
        else if(coinValue[symbol].value < (1-coinValue[symbol].buyPercent)*totalValue*coinValue[symbol].keepPercent){
            var buyAmount = (totalValue*coinValue[symbol].keepPercent - coinValue[symbol].value)/coinValue[symbol].sellPrice
            exchanges[i].Buy(coinValue[symbol].sellPrice, buyAmount);
            CancelPendingOrders(exchanges[i]);
        }        
    }
}
function main() {
    while(true){    // Main loop
        onTick();   // Main function
        Sleep(Interval*1000);
    }
}

More