Flash collapse robot (teaching)

Author: ruby, Created: 2018-10-09 15:18:13, Updated:

function CancelPendingOrders(orders) {                             // Cancel all pending orders
    for (var j = 0; j < orders.length; j++) {                      // Based on the list of pending orders sent by the parameters, cancel the orders one by one.
        exchange.CancelOrder(orders[j].Id, orders[j]);
        Sleep(300);                                                // Interval of 300 milliseconds
    }
}
function LogOrders(orders){
    var buyString = '';                                            // buyString
    var sellString = '';                                           // sellString
    orders.sort(function(x, y){return x.Price - y.Price;});        // Sort orders from big to small according to the Price attribute of orders
    for (var j = 0; j < orders.length; j++) {                      // Check through orders, and note that they have been sorted.
        if (orders[j].Type == ORDER_TYPE_SELL){                    // If the order type is sell order, then
            sellString += String(orders[j].Price) + ' ' + String(orders[j].Amount) + '|';    // Store it in the sellString , separate it by |
        }else{
            buyString += String(orders[j].Price) + ' ' + String(orders[j].Amount) + '|';     // If not, store it in the buyString , separate it by |
        }
    }
    LogStatus('买单:' + buyString + '\n' + '卖单:' + sellString);                             // Output these order information in the status bar.
}
function main() {                                           // main function
    while(true){                                            // main loop
        var orders = _C(exchange.GetOrders);                // Get all pending order info, orders is an array.
        CancelPendingOrders(orders);                        // Cancel all pending orders
        var ticker = _C(exchange.GetTicker);                // Get latest ticker info
        var account = _C(exchange.GetAccount);              // Get current account info
        var midPrice = (ticker.Buy + ticker.Sell) / 2;      // Calculate the average price in the price gap.
        var buyAmount  = 0;                                 // Declare buyAmount(the amount planned to buy) and initialize it to 0.
        var sellAmount = 0;                                 // Declare sellAmount(the amount planned to sell) and initialize it to 0.
        var amount = OrderSize;                             // Assign OrderSize(the size of each order) to amount.
        var buyPrice = midPrice  - Spread;                  // Set buyPrice within the scope of midPrice and Spread.
        var sellPrice = midPrice + Spread;                  // ....
        while((buyAmount < TotalBuy) && (account.Balance > amount*buyPrice) && buyPrice > 0){       // When buyAmount is less than TotalBuy, account balance is more than buyPrice(funds for this order),
                                                                                                    //and buyPrice is bigger than 0(prevent midPrice - Spread from being less than 0),
            if(exchange.Buy(buyPrice, amount)){             // Place order. If return null, execute the code block inside "else"; if return id, execute the code block of if.
                buyAmount += amount;                        // Cumulative amount of buying orders
                account.Balance -= amount*buyPrice;         // Update the number of coins in the account
                buyPrice -= Spread;                         // update buyPrice
                amount = amount * SpreadTimes;              // Update the amount of orders, and it increases progressively according to SpreadTimes.
            
            }else{                                          // If the order fails, update the account information for the judgment of the current while loop condition.
                account = _C(exchange.GetAccount);
            }
            Sleep(500);                                     // Used to control the pending order frequency
        }
        amount = OrderSize;                                 // Reset the variable amount to OrderSize
        while((sellAmount < TotalSell) && (account.Stocks*(1-fee/100) > amount)){     // When sellAmount is less than TotalSell and the amount of currency available 
                                                                                     //for the account minus the transaction fee is greater than the quantity per order, execute while loop.
            if(exchange.Sell(sellPrice, amount)){           // Place the order, return the order ID, and update the relevant variables.
                sellAmount += amount;                       // Cumulative amount of pending orders
                account.Stocks -= amount;                   // Update available currency
                sellPrice += Spread;                        // update sellPrice
                amount = amount * SpreadTimes;              // Update the amount of orders, and it increases progressively according to SpreadTimes.
            }else{
                account = _C(exchange.GetAccount);          // If the order fails, update account info.
            }
            Sleep(500);
        }
        orders = _C(exchange.GetOrders);                    // Get pending orders info
        LogOrders(orders);                                  // Output pending order info in the status bar
        Sleep(Interval*1000);                               // Interval of polling, control the frequency of policy operations.
    }
}

More