伪高频策略PLUS版本,增加附加状态栏,会显示包括策略运行期间的挂单价格,止盈止损价格,交易期间内成功和失败的次数信息的统计,收益曲线会显示每笔交易的盈亏变化,图表会显示进场挂单价格,入场的位置和仓位类型,开仓成功以后,会显示止盈止损价格和出场的位置,以及平仓是否盈利或者损失。大家可以挑选自己喜欢的品种,更改参数或者策略的逻辑,来实现属于你的高频策略,有好的想法大家也可以留言在评论区,我们可以共同探讨创建更好的高频策略。 (已更新:添加加锁功能)
/*backtest
start: 2023-08-02 09:00:00
end: 2023-08-02 15:00:17
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES","depthDeep":20}]
mode: 1
*/
function cancelOrders(symbol, offset) {
var orders = null;
while (1) {
orders = _C(exchange.GetOrders);
if (orders.length == 0) {
break;
}
for (var i = 0; i < orders.length; i++) {
if ((orders[i].ContractType == symbol && orders[i].Offset == offset) || typeof(offset) == "undefined") {
exchange.CancelOrder(orders[i].Id, orders[i]);
Sleep(interval);
}
}
Sleep(interval);
}
return orders;
}
function trade(distance, price, amount) {
var tradeFunc = null;
if (distance == "buy") {
tradeFunc = exchange.Buy;
} else if (distance == "sell") {
tradeFunc = exchange.Sell;
} else if (distance == "closebuy" || distance == "closebuy_today") {
tradeFunc = exchange.Sell;
} else if (distance == "closesell" || distance == "closesell_today") {
tradeFunc = exchange.Buy;
}
exchange.SetDirection(distance);
return tradeFunc(price, amount);
}
function getOrderBySymbol(symbol, orders) {
var ret = [];
_.each(orders, function(order) {
if (order.ContractType == symbol) {
ret.push(order);
}
});
return ret;
}
function hasOrder(orders, type, offset) {
var ret = false;
_.each(orders, function(order) {
if (order.Offset == offset && order.Type == type) {
ret = order;
}
});
return ret;
}
var p = $.NewPositionManager(); //交易类库函数
var success_count = 0; //成功次数统计
var fail_count = 0; //止损次数统计
var orderlong = null; //多头挂单价格
var ordershort = null; //空头挂单价格
var profitprice = null; //止盈挂单价格
var lossprice = null; //止损挂单价格
var tblStatus = {
type: "table",
title: "策略运行状态信息",
cols: ["合约名称", "当前价格", "多头挂单", "空头挂单", "止盈价格","止损价格", "持仓方向", "持仓价格", "持仓数量", "持仓盈亏", "止损次数", "成功次数"],
rows: []
};
function main() {
var initAccount = _C(exchange.GetAccount);
var preprofit = 0 //先前权益
var curprofit = 0 //当前权益
var holdPrice = null //持仓价格
var holdType = null //持仓类型
var holdAmount = null //持仓数量
var holdProfit = null //持仓盈亏
var isLock = false //止盈挂单锁
while (true) {
if (exchange.IO("status")) {
exchange.SetContractType(symbol);
var t = exchange.GetTicker();
var r = exchange.GetRecords();
var ContractType = symbol
var curprice = r[r.length-1].Close
var positions = _C(exchange.GetPosition);
var pos = [p.GetPosition(symbol, PD_LONG, positions), p.GetPosition(symbol, PD_SHORT, positions)];
var orders = getOrderBySymbol(symbol, _C(exchange.GetOrders));
if (orders.length == 0 && (!pos[0] && !pos[1])) {
profitprice = null
lossprice = null
isLock = false //解锁
holdPrice = ''
holdType = ''
holdAmount = ''
holdProfit = ''
preprofit = curprofit
curprofit = exchange.GetAccount().Balance - initAccount.Balance
LogProfit(curprofit, "权益", '&');
if(preprofit < curprofit){
success_count += 1
$.PlotFlag(r[r.length-2].Time, '止盈离场', '止盈离场')
}
if(preprofit > curprofit){
fail_count += 1
$.PlotFlag(r[r.length-2].Time, '止损离场', '止损离场')
}
orderlong = t.Buy - priceTick * deviation
ordershort = t.Sell + priceTick * deviation
// 当前没有挂单,没有持仓,基于盘口挂单
trade("buy", orderlong, 1);
trade("sell", ordershort, 1);
} else if (pos[0] || pos[1]) {
// 只要有持仓
orderlong = null
ordershort = null
var cur_pos = pos[0] ? pos[0] : pos[1]
holdPrice = cur_pos.Price
holdType = pos[0] ? '多头方向' : '空头方向'
holdAmount = cur_pos.Amount
holdProfit = cur_pos.Profit
if ((pos[1] && hasOrder(orders, ORDER_TYPE_BUY, ORDER_OFFSET_OPEN)) || (pos[0] && hasOrder(orders, ORDER_TYPE_SELL, ORDER_OFFSET_OPEN))) {
$.PlotFlag(r[r.length-1].Time, pos[0] ? '多单进场' : '空单进场', pos[0] ? '多单进场' : '空单进场')
cancelOrders(symbol, ORDER_OFFSET_OPEN);
Log('删除挂单:', pos[0] ? '空单' : '多单');
}
var longCoverOrder = hasOrder(orders, ORDER_TYPE_SELL, ORDER_OFFSET_CLOSE);
var shortCoverOrder = hasOrder(orders, ORDER_TYPE_BUY, ORDER_OFFSET_CLOSE);
if (pos[0] && !longCoverOrder && isLock == false) {
profitprice = t.Sell + priceTick * profit
lossprice = t.Sell - priceTick * maxCoverDeviation
trade("closebuy", profitprice, pos[0].Amount);
isLock = true //加锁
}
if (pos[1] && !shortCoverOrder && isLock == false) {
// 有空头持仓
profitprice = t.Buy - priceTick * profit
lossprice = t.Buy + priceTick * maxCoverDeviation
trade("closesell", profitprice, pos[1].Amount);
isLock = true //加锁
}
// 重新平仓条件
if (pos[0] && longCoverOrder && t.Sell < lossprice) {
Log('多头止损');
cancelOrders(symbol);
p.Cover(symbol);
}
if (pos[1] && shortCoverOrder && t.Buy > lossprice) {
Log('空头止损');
cancelOrders(symbol);
p.Cover(symbol);
}
}
$.PlotRecords(r, "K线数据")
$.PlotLine("多头挂单", orderlong, r[r.length - 1].Time)
$.PlotLine("空头挂单", ordershort, r[r.length - 1].Time)
$.PlotLine("挂单盈利线", profitprice, r[r.length - 1].Time)
$.PlotLine("挂单止损线", lossprice, r[r.length - 1].Time)
tblStatus.rows = [];
tblStatus.rows.push([ContractType, curprice, orderlong, ordershort, profitprice, lossprice, holdType, holdPrice, holdAmount, holdProfit, fail_count, success_count]);
lastStatus = '`' + JSON.stringify([tblStatus]) + '`';
LogStatus(lastStatus);
} else {
LogStatus("未连接", _D());
}
Sleep(interval);
}
}