Loading ...

# Python版简单做多网格策略

Author: 去者伯仁, Date: 2021-12-07 21:51:46
Tags:

```
import json

# 参数
beginPrice = 1000
endPrice = 5000
distance = 5
pointProfit = 25
amount = 0.05
minBalance = 200

# 全局变量
arrNet = []
arrMsg = []
acc = None
tradenum=0

def findOrder (orderId, NumOfTimes, ordersList = []) :
for j in range(NumOfTimes) :
orders = None
if len(ordersList) == 0:
orders = _C(exchange.GetOrders)
else :
orders = ordersList
for i in range(len(orders)):
if orderId == orders[i]["Id"]:
return True
return False

def cancelOrder (price, orderType) :
orders = _C(exchange.GetOrders)
for i in range(len(orders)) :
if price == orders[i]["Price"] and orderType == orders[i]["Type"]:
exchange.CancelOrder(orders[i]["Id"])
Sleep(500)

def checkOpenOrders (orders, ticker) :
global arrNet, arrMsg, tradenum
for i in range(len(arrNet)) :
if arrNet[i]["state"] == "pending" :
if not findOrder(arrNet[i]["id"], 1, orders) :
orderId = exchange.Sell(arrNet[i]["coverPrice"], arrNet[i]["amount"], arrNet[i], ticker)
if orderId :
arrNet[i]["state"] = "cover"
arrNet[i]["id"] = orderId
tradenum+=arrNet[i]["price"]*arrNet[i]["amount"]
else :
# 撤销
cancelOrder(arrNet[i]["coverPrice"], ORDER_TYPE_SELL)
arrMsg.append("挂单失败!" + json.dumps(arrNet[i]) + ", time:" + _D())
elif arrNet[i]["price"]<ticker.Sell-distance*10:
exchange.CancelOrder(arrNet[i]["id"])
arrNet[i]["id"] = -1
arrNet[i]["state"] = "idle"
Log(arrNet[i], "节点撤单，重置为空闲状态。", "#FF0000")

elif arrNet[i]["state"] == "waitingcover" and arrNet[i]["coverPrice"]<=ticker.Sell+distance*10:
if not findOrder(arrNet[i]["id"], 1, orders) :
orderId = exchange.Sell(arrNet[i]["coverPrice"], arrNet[i]["amount"], arrNet[i], ticker)
if orderId :
arrNet[i]["state"] = "cover"
arrNet[i]["id"] = orderId
else :
# 撤销
cancelOrder(arrNet[i]["coverPrice"], ORDER_TYPE_SELL)
arrMsg.append("挂单失败!" + json.dumps(arrNet[i]) + ", time:" + _D())
def checkCoverOrders (orders, ticker) :
global arrNet, arrMsg, tradenum
for i in range(len(arrNet)) :
if arrNet[i]["state"] == "cover" :
if not findOrder(arrNet[i]["id"], 1, orders):
arrNet[i]["id"] = -1
arrNet[i]["state"] = "idle"
tradenum+=arrNet[i]["coverPrice"]*arrNet[i]["amount"]
Log(arrNet[i], "节点平仓，重置为空闲状态。", "#FF0000")
elif arrNet[i]["coverPrice"]>ticker.Sell+distance*10:
exchange.CancelOrder(arrNet[i]["id"])
arrNet[i]["state"] = "waitingcover"
Log(arrNet[i], "节点撤单，重置为待平仓状态。", "#FF0000")

def onTick () :
global arrNet, arrMsg, acc

ticker = _C(exchange.GetTicker)
for i in range(len(arrNet)):
if i != len(arrNet) - 1 and arrNet[i]["state"] == "idle" and ticker.Sell > arrNet[i]["price"] and ticker.Sell < arrNet[i + 1]["price"]:
acc = _C(exchange.GetAccount)
if acc.Balance < minBalance :
arrMsg.append("资金不足" + json.dumps(acc) + "!" + ", time:" + _D())
break

orderId = exchange.Buy(arrNet[i]["price"], arrNet[i]["amount"], arrNet[i], ticker)
if orderId :
arrNet[i]["state"] = "pending"
arrNet[i]["id"] = orderId
else :
# 撤单
cancelOrder(arrNet[i]["price"], ORDER_TYPE_BUY)
arrMsg.append("挂单失败!" + json.dumps(arrNet[i]) + ", time:" + _D())

Sleep(1000)
orders = _C(exchange.GetOrders)
checkOpenOrders(orders, ticker)
Sleep(1000)
orders = _C(exchange.GetOrders)
checkCoverOrders(orders, ticker)

tbl = {
"type" : "table",
"title" : "网格状态",
"cols" : ["节点索引", "详细信息"],
"rows" : [],
}

for i in range(len(arrNet)) :
tbl["rows"].append([i, json.dumps(arrNet[i])])

errTbl = {
"type" : "table",
"title" : "记录",
"cols" : ["节点索引", "详细信息"],
"rows" : [],
}

orderTbl = {
"type" : "table",
"title" : "orders",
"cols" : ["节点索引", "详细信息"],
"rows" : [],
}

while len(arrMsg) > 20 :
arrMsg.pop(0)

for i in range(len(arrMsg)) :
errTbl["rows"].append([i, json.dumps(arrMsg[i])])

for i in range(len(orders)) :
orderTbl["rows"].append([i, json.dumps(orders[i])])

LogStatus(_D(), "\n", acc, "\n","交易额:", tradenum, "\n","arrMsg length:", len(arrMsg), "\n", "`" + json.dumps([tbl, errTbl, orderTbl]) + "`")

def main ():
global arrNet
for i in range(int((endPrice - beginPrice) / distance)):
arrNet.append({
"price" : beginPrice + i * distance,
"amount" : amount,
"state" : "idle",    # pending / cover / idle
"coverPrice" : beginPrice + i * distance + pointProfit,
"id" : -1,
})
Log('初始化完成')
while True:
onTick()
LogReset(100000)
LogVacuum()
Sleep(400)
```

More