Loading ...

Moving Average Bot 30 lines

Author: 小草, Date: 2018-07-03 15:16:29
Tags: Trend Study

“Talk is cheap. Show me the code”

Just for learning, Be carefully when using in real market.

Only run on digital currency exchanges.

Note: this strategy included template. (function with $.)

Beginers can start with this strategy, learn how to code, know the difference between backtesting and real market.

img


function main() {
    Log('started');
    var initAccount = _C(exchange.GetAccount);
    var ticker = exchange.GetTicker();
    var InitValue = (initAccount.Stocks + initAccount.FrozenStocks)*ticker.Last + initAccount.Balance + initAccount.FrozenBalance;
    while (true) {
        var records = _C(exchange.GetRecords);
        ticker =_C(exchange.GetTicker);
        var FastRecords = TA.MA(records,FastPeriod);
        var SlowRecords = TA.MA(records,SlowPeriod);
        var NowAccount = _C(exchange.GetAccount);
        var n = _Cross(FastRecords, SlowRecords);
        if (n >= EnterPeriod && NowAccount.Balance > 0) {
            var Price = _N(ticker.Sell+Slippage, 2);
            var Amount = _N(0.99*NowAccount.Balance/Price, 3);
            if(Amount>0.1){
               var id = exchange.Buy(Price, Amount);
               if(exchange.GetOrders(id).Status == ORDER_STATE_PENDING){exchange.CancelOrder(id);}
               LogProfit((NowAccount.Stocks + NowAccount.FrozenStocks)*ticker.Last + NowAccount.Balance + NowAccount.FrozenBalance - InitValue);
            }
        }
        if(n <= -EnterPeriod && NowAccount.Stocks > 0) {
            var Price = _N(ticker.Buy-Slippage, 2);
            var Amount = _N(NowAccount.Stocks, 3);
            if(Amount>0.1){
                var id = exchange.Sell(Price, Amount);
                if(exchange.GetOrders(id).Status == ORDER_STATE_PENDING){exchange.CancelOrder(id);}
                LogProfit((NowAccount.Stocks + NowAccount.FrozenStocks)*ticker.Last + NowAccount.Balance + NowAccount.FrozenBalance - InitValue);
            }
        }
        Sleep(Interval*1000);
    }
}

Related

More

zhllxk n>=EnterPeriod是什么意思?没有定义啊

小草 复制代码就知道了,参数