Loading ...

经典均线策略V1.0_区班主注释简化

Author: tomjava, Date: 2019-10-11 14:41:29
Tags:

“Talk is cheap. Show me the code”

教学性质, 实盘慎用.

注: 策略使用了交易模板类库

希望新手从此策略入门, 一步步学习编写策略, 并体验到模拟与真实环境对交易系统的影响

img


/*backtest
start: 2019-01-01 00:00:00
end: 2019-10-10 00:00:00
period: 1d
exchanges: [{"eid":"OKEX","currency":"ETH_USDT","stocks":0}]
args: [["OpMode",1,10989],["MaxAmount",1,10989],["TradeFee",0.001,10989]]
*/
//注册币乎后https://m.bihu.com/signup?i=1ewtKO&s=4&c=4
//搜索 物联网区块链 可以联系到作者区班主
function main() {
    var STATE_IDLE  = -1;
    var state = STATE_IDLE; //表示状态
    var entryPrice = 0;
    var initAccount = _C(exchange.GetAccount);
    Log(initAccount);
    while (true) {
        if (state === STATE_IDLE) {  //初始化状态为默认仓;默认为空仓,只买入
            var n = $.Cross(FastPeriod, SlowPeriod);//模板函数返回EMA指标快线、慢线交叉结果
            if (Math.abs(n) >= EnterPeriod) { //EnterPeriod入市观察区,等待上拉一定时,再入场
                var account = _C(exchange.GetAccount);
                var ticker = _C(exchange.GetTicker);
                var obj;
                //账户现金乘以比例,除以当前价格,保留小数前3位
                if(n > 0){
                   obj = $.Buy(_N(account.Balance * PositionRatio / ticker.Sell, 3));
                   if (obj) { //如果购买成功,就标志开仓
                      opAmount = obj.amount;
                      entryPrice = obj.price;
                      state = PD_LONG;
                      account = _C(exchange.GetAccount);
                      Log("开仓:购买量", opAmount);
                      Log("目前持币数", account.Stocks, "交叉周期", n);
                   }
                }
            }
        } else { //state为非空闲状态;处理平仓检测
            var n = $.Cross(ExitFastPeriod, ExitSlowPeriod);
            //这个判断条件有点长,先看Math.abs(n) >= ExitPeriod,n的绝对值大于等于 离市观察期 这是触发条件1,并且
            //(n > 0)两者中至少有个为真
            //需要离市平仓
            var needCover = Math.abs(n) >= ExitPeriod && (n<0);
            if (needCover) { //离市平仓
                var nowAccount = _C(exchange.GetAccount);
                var obj = $.Sell(_N(nowAccount.Stocks, 3));
                if(obj){
                    state=STATE_IDLE;
                    Log("平仓:卖出量",nowAccount.Stocks);
                    nowAccount = _C(exchange.GetAccount);
                    Log("目前资金",nowAccount.Balance, "交叉周期", n,"盈利",nowAccount.Balance - initAccount.Balance);
                }
                //打印收益
                //LogProfit(nowAccount.Balance - initAccount.Balance, '钱:', nowAccount.Balance, '币:', nowAccount.Stocks, '平仓详情:', obj, "交叉周期", n);
            }
        }
        Sleep(Interval*1000);
    }
}

More

belman 没设置止损啊,只写了个参数StopLossRatio

BBboxing 為什麼沒有适合外匯交易平台MetaTrader 的mql語言編寫的程序呢?

BBboxing 為什麼沒有适合外匯交易平台MetaTrader 的mql語言編寫的程序呢?