一个简单的固定价格止损机器人,可以帮助你止损。也是个很好的入门数字货币量化交易的策略。
### 参数:
StopPrice:Fixed price to stop loss
Intervel: Intervel(second) of checking coin price
function CancelPendingOrders() { var orders = _C(exchange.GetOrders); for (var j = 0; j < orders.length; j++) { exchange.CancelOrder(orders[j].Id, orders[j]); } } function StopLoss(){ var done = false while(!done){ var ticker = _C(exchange.GetTicker) account = _C(exchange.GetAccount) var price = ticker.Buy var amount = account.Stocks try{ if(amount > 0){ var id = exchange.Sell(price, amount); if(id){ exchange.CancelOrder(id) } else{ Log('all coins are sold') Log('total coins: ', account.Stocks) done = true } } }catch(err){ Log('error, stop') done = true } Sleep(1000); } } function main() { Log('robot starts to run') CancelPendingOrders() var account = _C(exchange.GetAccount) Log('total coins: ', account.Stocks); while(true){ var ticker = _C(exchange.GetTicker) if(ticker.Last <= StopPrice){ Log('Last price is:', ticker.Last, 'Stop price is: ', StopPrice) Log('the stop price has reached, start to sell') StopLoss(); break; } Sleep(Intervel*1000) } Log('robot has stopped') }
lerylin 是不是把自己的单子挂出去,有可能你的挂单卖家已经大于实时价格了,那么你这个单子就不会成交,所以立即取消掉,重新检测最新卖价,重新用最新卖家挂单
xh69 有的疑问,为什么卖出后,立即要再去取消这个卖出单那?是为了防止什么情况: var id = exchange.Sell(price, amount); if(id){ exchange.CancelOrder(id) }
lerylin 是不是把自己的单子挂出去,有可能你的挂单卖家已经大于实时价格了,那么你这个单子就不会成交,所以立即取消掉,重新检测最新卖价,重新用最新卖家挂单