The resource loading... loading...

Index balancing strategies (teaching)

Author: Inventors quantify - small dreams, Date: 2018-10-08 10:36:23
Tags: PortfolioStudyBalance


var coinValue = {};                             // 声明 全局变量 coinValue  ,用一个空对象赋值
var totalValue = 0;                             // 总价值
function updateValue(){                         // 更新 价值
    var logString = 'Time: '+ _D() + '\n';      // 声明一个变量 , 日志字符串并且初始化, 初始化记录上 当前时间
    var account = _C(exchanges[0].GetAccount);  // 获取 为了给 coinValue的 BaseAsset 属性 (也就是基础币种资产)初始化的账户数据。
    coinValue[BaseAsset] = {amount:account.Balance + account.FrozenBalance, value:account.Balance + account.FrozenBalance};  // amount , value 属性 初始化 均为 总计价币数。BaseAsset 为 基准货币
    totalValue = coinValue[BaseAsset].value;    // 更新  总价值 
    logString += BaseAsset + ': ' + _N(coinValue[BaseAsset].value,5) + '\n';    // 给日志字符串 添加上 当前 基准货币 总价值 数据
    for(var i=0;i<exchanges.length;i++){                                        // 遍历 交易所对象
        var account = _C(exchanges[i].GetAccount);                              // 更新当前索引的账户信息
        coinValue[BaseAsset] = {amount:account.Balance + account.FrozenBalance, value:account.Balance + account.FrozenBalance};  // amount , value 属性 初始化 均为 总计价币数。BaseAsset 为 基准货币
        var ticker = _C(exchanges[i].GetTicker);                                // 更新当前索引的账户行情信息
        var symbol = exchanges[i].GetCurrency().split('_')[0];                  // 获取操作币 名称
        coinValue[symbol].amount = account.Stocks + account.FrozenStocks;       // 记录操作币的数量
        coinValue[symbol].value = coinValue[symbol].amount * ticker.Last;       // 记录操作币的价值(BaseAsset计价)
        totalValue += coinValue[symbol].value;                                  // 累计总价值
        coinValue[symbol].buyPrice = ticker.Buy;                                // 更新 当前索引的 交易所对象上的 买一价格数据
        coinValue[symbol].sellPrice = ticker.Sell;                              // 更新 当前索引的 交易所对象上的 卖一价格数据
        logString += symbol + ': ' + _N(coinValue[symbol].value,5) + '\n'       // 给日志字符串添加当前索引的交易所 的操作币的价值。
        Sleep(1000)
    }
    LogStatus(logString);                                                       // 在状态栏输出
}
var keepPercent = Ratio.split('|').map(Number);                                 // 用字符 "|" 分割 Ratio 参数,然后调用 map 函数 ,转换字符串为 Number 类型 返回一个新的数组。
if(math.sum(keepPercent) > 1){                                                  // 统计 keepPercent 数组中的元素值总和
    throw 'sum of keep percent should be lower than 1';                         // 各资产 占比总和 不能超过 100%
}
var buyPercent = ToBuy.split('|').map(Number);                                  // 根据参数处理字符串,构造 买入比例 数组
var sellPercent = ToSell.split('|').map(Number);                                // ... 构造 卖出比例 数组
for(var i=0;i<exchanges.length;i++){                                            // 遍历交易所对象数组
    var symbol = exchanges[i].GetCurrency().split('_')[0];                      // 获取 交易对 中 交易币的 名称,赋值给 symbol 
    coinValue[symbol] = {amount:0, value:0, buyPrice:0, sellPrice:0, keepPercent:0, buyPercent:0, sellPercent:0};  // 构造每个 交易对对象 交易币的 相关数据: 数量,价值,买入价格,卖出价格,持有百分比,买入百分比,卖出百分比
    coinValue[symbol].keepPercent = keepPercent[i];   // 初始化 持有百分比
    coinValue[symbol].buyPercent = buyPercent[i];     // 初始化 买入百分比
    coinValue[symbol].sellPercent = sellPercent[i];   // 初始化 卖出百分比
}
function CancelPendingOrders(e) {                     // 取消所有挂单
    var orders = _C(e.GetOrders);
    for (var j = 0; j < orders.length; j++) {
        e.CancelOrder(orders[j].Id, orders[j]);
        Sleep(300);
    }
}
function onTick(){    // 主要逻辑函数 实现
    updateValue();    // 更新 操作币 净值
    for(var i=0;i<exchanges.length;i++){                                              // 遍历交易所
        var symbol = exchanges[i].GetCurrency().split('_')[0];                        // 获取当前交易所对象的 操作币名称
        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){    // 主循环
        onTick();   // 主要逻辑函数
        Sleep(Interval*1000);
    }
}

Related

More

The Little MoustacheThe 40th line should be e.CancelOrder ((orders[j].Id, orders[j]);

panmingThis tactic has not been implemented - all sorts of mistakes have been reported.

Inventors quantify - small dreamsOh, yes, thank you for the reminder.

Inventors quantify - small dreamsIf you have any questions, please leave a comment.

panmingGood. Thank you, little dream.

Inventors quantify - small dreamsHi, this is indeed a bit of a problem, it has been corrected, please try again. Strategic teaching is primarily about learning how to think and design strategic structures.

panmingHello, previously it was due to server time issue, now it has been replaced, after the replacement the re-run appears: TypeError: cannot read property 'value' of undefined at updateValue (__FILE__:6) at onTick (__FILE__:44) at main (__FILE__:61)

Inventors quantify - small dreamsThis strategy is a bit special, it requires adding multiple exchange objects, one exchange object controls one currency. You can send a screenshot of specific error messages, and I'll help you see.