- 策略广场
- 商品期货移仓类库
商品期货移仓类库
Author:
雨幕(youquant), Date: 2020-07-06 10:07:52
Tags:
商品期货
// 导出函数(接口)
$.NewTransfer = function (e, taskQueue, IsTrading) {
var self = {}
self.currSymbol = ""
self.e = e
self.q = taskQueue
self.IsTrading = IsTrading
self.arrAction = []
self.process = function(symbolA, symbolB) {
if((typeof(symbolB) == "undefined" && !self.IsTrading(symbolA)) || (typeof(symbolB) != "undefined" && !self.IsTrading(symbolA) && !self.IsTrading(symbolB))) {
return
}
self.arrAction = []
// 检测参数是否有symbolB , 没有即为检测主力合约 , 有则为移仓具体合约
if(typeof(symbolB) == "undefined") {
// 检测symbolA合约是否为XX888 或者 XX000
if(symbolA.indexOf("888") == -1 && symbolA.indexOf("000") == -1) {
throw "缺少symbolB参数!"
}
// 检测是否触发移仓
var insDetail = null
if(self.currSymbol == "") {
insDetail = _C(self.e.SetContractType, symbolA)
self.currSymbol = insDetail.InstrumentID
}
insDetail = _C(self.e.SetContractType, symbolA)
if(self.currSymbol != insDetail.InstrumentID) {
var oldSymbol = self.currSymbol
var pos = self.q.GetPosition(self.e, oldSymbol)
if(pos && pos.Amount > 0) {
// 移仓
self.arrAction = [oldSymbol, insDetail.InstrumentID, pos]
Log("开始移仓:", self.arrAction[0], "->", self.arrAction[1], self.arrAction, "#FF0000")
} else {
self.currSymbol = insDetail.InstrumentID
}
}
} else {
// 检测是否有symbolA持仓
var pos = self.q.GetPosition(self.e, symbolA)
if(pos && pos.Amount > 0) {
// 移仓
self.arrAction = [symbolA, symbolB, pos]
Log("开始移仓:", self.arrAction[0], "->", self.arrAction[1], self.arrAction, "#FF0000")
} else {
Log("没有检测到", symbolA, "的仓位,pos:", pos, "#FF0000")
}
}
if(self.arrAction.length == 3) {
self.q.pushTask(self.e, self.arrAction[0], (self.arrAction[2].Type == PD_LONG || self.arrAction[2].Type == PD_LONG_YD ? "closebuy" : "closesell"), self.arrAction[2].Amount, function(task, ret){
if(!ret) {
Log(self.arrAction[0], "移仓平仓失败!", self.arrAction[0], "->", self.arrAction[1], "#FF0000")
return
}
Log("移仓进度平仓成功,开始开仓!", self.arrAction[1], "数量:", self.arrAction[2].Amount, "#FF0000")
self.q.pushTask(self.e, self.arrAction[1], (self.arrAction[2].Type == PD_LONG || self.arrAction[2].Type == PD_LONG_YD ? "buy" : "sell"), self.arrAction[2].Amount, function(task, ret){
if(!ret) {
Log(self.arrAction[1], "移仓开仓失败!", self.arrAction[0], "->", self.arrAction[1], "#FF0000")
return
}
Log("移仓成功!", self.arrAction[0], "->", self.arrAction[1], "#FF0000")
})
})
}
while(self.q.size() > 0) {
self.q.poll()
Sleep(500)
}
}
return self
}
// 测试函数,在主策略中执行
function main() {
var q = $.NewTaskQueue()
var t = $.NewTransfer(exchange, q, $.IsTrading)
var isTransfer = false
var isFirst = true
var n = 0
while(true) {
if(exchange.IO("status")) {
LogStatus(_D(), "已经连接!")
if(isFirst) {
isFirst = false
exchange.SetContractType("rb1905")
exchange.SetDirection("buy")
var ticker = exchange.GetTicker()
exchange.Buy(ticker.Last + 10, 1)
Log(exchange.GetPosition())
}
// 检测主力合约移仓
t.process("rb888")
// 模拟触发一次
/*
if(n == 20) {
t.process("rb1905", "rb1910")
}
*/
// 检测条件触发移仓
/*
var cmd = GetCommand()
if(cmd) {
isTransfer = true
}
if(isTransfer) {
t.process("rb2010", "rb2101")
isTransfer = false
}
*/
} else {
LogStatus(_D(), "未连接!")
}
n++
Sleep(1000)
}
}
template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6