In the last two days, Telegram users have been sending me a private email asking me to give a design example of how to create a new strategy. Sometimes the exchange wants to grab a new coin, so this article will design a simple tool to create a new strategy.
For example, currently an exchange, a trading pair: XXX_USDT, is not listed on the exchange yet. But it will soon be online. You need to use a program to watch the market for this exchange's XXX_USDT. This transaction can be traded once the upper limit is reached.
The need is simple, but for the little ones in the coin circle who don't have a programming background, there may be nothing they can do, so we're going to get to work on it.
The policy parameter is defined as:
Here we define a few parameters to control the following operations.
The code implementation is:
function pendingOrders(ordersNum, price, amount, deltaPrice, deltaAmount) {
var routineOrders = []
var ordersIDs = []
for (var i = 0 ; i < ordersNum ; i++) {
var routine = exchange.Go("Buy", price + i * deltaPrice, amount + i * deltaAmount)
routineOrders.push(routine)
Sleep(ApiReqInterval)
}
for (var i = 0 ; i < routineOrders.length ; i++) {
var orderId = routineOrders[i].wait()
if (orderId) {
ordersIDs.push(orderId)
Log("成功挂单", orderId)
}
}
return ordersIDs
}
function main() {
if (symbol == "null" || pendingPrice == -1 || pendingAmount == -1 || pendingPrice == -1 || deltaPrice == -1 || deltaAmount == -1) {
throw "参数设置错误"
}
exchange.SetCurrency(symbol)
// 屏蔽错误信息
SetErrorFilter("GetDepth")
while (true) {
var msg = ""
var depth = exchange.GetDepth()
if (!depth || (depth.Bids.length == 0 && depth.Asks.length == 0)) {
// 没有深度
msg = "没有深度数据,等待!"
Sleep(500)
} else {
// 获取到深度
Log("并发下单!")
var ordersIDs = pendingOrders(ordersNum, pendingPrice, pendingAmount, deltaPrice, deltaAmount)
while (true) {
var orders = _C(exchange.GetOrders)
if (orders.length == 0) {
Log("当前挂单个数0,停止运行")
return
}
var tbl = {
type: "table",
title: "当前挂单",
cols: ["id", "价格", "数量"],
rows: []
}
_.each(orders, function(order) {
tbl.rows.push([order.Id, order.Price, order.Amount])
})
LogStatus(_D(), "\n`" + JSON.stringify(tbl) + "`")
Sleep(500)
}
}
LogStatus(_D(), msg)
}
}
策略检测交易所API,检测订单薄接口,一旦能获取到订单薄数据,策略就使用exchange.Go函数并发下单。下单之后就循环检测当前挂单的状态。策略没有经过实际测试,这里只是给出一个代码设计参考,有兴趣的可以修改、添加功能使用。
The full strategy:https://www.fmz.com/strategy/358383