Loading ...

测试现货买卖操作(分限价、市价两种情况)

Author: 韬奋量化, Date: 2020-03-18 16:09:36
Tags:

回测火币数据,以及在wexapp模拟盘交易,都得到类似下面的运行结果:

如果限价下单,那么exchange.Buy(6840, 5)就是以6840的价格买入5个btc。 如果市价下单,那么exchange.Buy(-1, 5)就是以市价、花费5个usdt买入btc。

策略代码:https://www.fmz.com/m/edit-strategy/191349

2020年4月5日


/*backtest
start: 2020-01-01 00:00:00
end: 2020-04-01 00:00:00
period: 1d
exchanges: [{"eid":"Huobi","currency":"BTC_USD","balance":1000000,"stocks":0}]
*/

var id, order, buyAmount, lastPrice;

function main() {
    Log(exchange.GetAccount());

    lastPrice = parseInt(exchange.GetTicker().Last);
    id = exchange.Buy(lastPrice + 50, 5); // 限价买入5个BTC,买入价是当前最新价格+50          
    Log(order = exchange.GetOrder(id));
    buyAmount = parseFloat(order.DealAmount);
    Log(exchange.GetAccount());

    Sleep(1000);
    last_price = parseInt(exchange.GetTicker().Last);
    id = exchange.Sell(lastPrice - 50, buyAmount); // 限价卖出5个BTC,卖出价是当前最新价格-50    
    Log(order = exchange.GetOrder(id));
    Log(exchange.GetAccount());

    Sleep(1000);
    id = exchange.Buy(-1, 5); // 市价买入BTC,成交量是5个usdt    
    Sleep(1000);    
    Log(order = exchange.GetOrder(id));
    buyAmount = parseFloat(order.DealAmount);    
    Log(exchange.GetAccount());

    Sleep(1000);    
    id = exchange.Sell(-1, buyAmount); // 市价卖出BTC,成交量是刚才买入的BTC   
    Sleep(1000);    
    Log(order = exchange.GetOrder(id));
    Log(exchange.GetAccount());

}

More