In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
本篇内容主要讲解"如何移植一个my语言策略",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何移植一个my语言策略"吧!
对于趋势策略移植来说是非常简单的,我们可以使用一段范例代码,填充驱动策略的数据计算部分代码,填充交易信号触发条件即可。
可复用的范例代码:
以用于OKEX期货的策略为例。
// 全局变量var IDLE = 0var LONG = 1var SHORT = 2var OPENLONG = 3var OPENSHORT = 4var COVERLONG = 5var COVERSHORT = 6 var BREAK = 9var SHOCK = 10 var _State = IDLEvar Amount = 0 // 记录持仓数量var TradeInterval = 500 // 轮询间隔var PriceTick = 1 // 价格一跳var Symbol = "this_week" function OnTick(){ // 驱动策略的行情处理部分 // 待填充... // 交易信号触发处理部分 // 待填充... // 执行交易逻辑 var pos = null var price = null var currBar = records[records.length - 1] if(_State == OPENLONG){ pos = GetPosition(PD_LONG) // 判断是不是 满足状态,如果满足 修改状态 if(pos[1] >= Amount){ _State = LONG Amount = pos[1] // 更新实际量 return } price = currBar.Close - (currBar.Close % PriceTick) + PriceTick * 2 Trade(OPENLONG, price, Amount - pos[1], pos, PriceTick) // (Type, Price, Amount, CurrPos, PriceTick) } if(_State == OPENSHORT){ pos = GetPosition(PD_SHORT) if(pos[1] >= Amount){ _State = SHORT Amount = pos[1] // 更新实际量 return } price = currBar.Close - (currBar.Close % PriceTick) - PriceTick * 2 Trade(OPENSHORT, price, Amount - pos[1], pos, PriceTick) } if(_State == COVERLONG){ pos = GetPosition(PD_LONG) if(pos[1] == 0){ _State = IDLE return } price = currBar.Close - (currBar.Close % PriceTick) - PriceTick * 2 Trade(COVERLONG, price, pos[1], pos, PriceTick) } if(_State == COVERSHORT){ pos = GetPosition(PD_SHORT) if(pos[1] == 0){ _State = IDLE return } price = currBar.Close - (currBar.Close % PriceTick) + PriceTick * 2 Trade(COVERSHORT, price, pos[1], pos, PriceTick) }} // 交易逻辑部分function GetPosition(posType) { var positions = _C(exchange.GetPosition) var count = 0 for(var j = 0; j
< positions.length; j++){ if(positions[j].ContractType == Symbol){ count++ } } if(count >1){ throw "positions error:" + JSON.stringify(positions) } for (var i = 0; i
< positions.length; i++) { if (positions[i].ContractType == Symbol && positions[i].Type === posType) { return [positions[i].Price, positions[i].Amount]; } } Sleep(TradeInterval); return [0, 0];} function CancelPendingOrders() { while (true) { var orders = _C(exchange.GetOrders) for (var i = 0; i < orders.length; i++) { exchange.CancelOrder(orders[i].Id); Sleep(TradeInterval); } if (orders.length === 0) { break; } }} function Trade(Type, Price, Amount, CurrPos, OnePriceTick){ // 处理交易 if(Type == OPENLONG || Type == OPENSHORT){ // 处理开仓 exchange.SetDirection(Type == OPENLONG ? "buy" : "sell") var pfnOpen = Type == OPENLONG ? exchange.Buy : exchange.Sell var idOpen = pfnOpen(Price, Amount, CurrPos, OnePriceTick, Type) Sleep(TradeInterval) if(idOpen) { exchange.CancelOrder(idOpen) } else { CancelPendingOrders() } } else if(Type == COVERLONG || Type == COVERSHORT){ // 处理平仓 exchange.SetDirection(Type == COVERLONG ? "closebuy" : "closesell") var pfnCover = Type == COVERLONG ? exchange.Sell : exchange.Buy var idCover = pfnCover(Price, Amount, CurrPos, OnePriceTick, Type) Sleep(TradeInterval) if(idCover){ exchange.CancelOrder(idCover) } else { CancelPendingOrders() } } else { throw "Type error:" + Type }} function main() { // 设置合约 exchange.SetContractType(Symbol) while(1){ OnTick() Sleep(1000) }} 举例:双均线策略的移植 麦语言回测: 麦语言策略代码: MA5^^MA(C,5);MA15^^MA(C,15);CROSSUP(MA5,MA15),BPK;CROSSDOWN(MA5,MA15),SPK; 移植为JavaScript策略 首先给可复用的范例代码填充上行情获取、指标计算部分: // 驱动策略的行情处理部分var records = _C(exchange.GetRecords) if (records.length < 15) { return } var ma5 = TA.MA(records, 5)var ma15 = TA.MA(records, 15)var ma5_pre = ma5[ma5.length - 3]var ma15_pre = ma15[ma15.length - 3]var ma5_curr = ma5[ma5.length - 2]var ma15_curr = ma15[ma15.length - 2] 可以看到,双均线策略非常简单,只是首先获取K线数据records,然后使用TA函数库的均线函数TA.MA计算出5日均线、15日均线(回测界面上可以看到,K线周期设置的是日K线,所以TA.MA(records, 5)计算出的就是5日均线,TA.MA(records, 15)15日均线)。 然后获取ma5指标数据的倒数第二个点ma5_curr(指标值),倒数第三个点ma5_pre(指标值),ma15指标数据同理。然后就可以使用这些指标数据去判断金叉死叉了,如图: 只要形成这样的状态,即为确定的金叉死叉。 那么判断信号的部分就可以写成: if(_State == IDLE && ma5_pre < ma15_pre && ma5_curr >ma15_curr){ _State = OPENLONG Amount = 1} if(_State == IDLE && ma5_pre > ma15_pre && ma5_curr
< ma15_curr){ _State = OPENSHORT Amount = 1} if(_State == LONG && ma5_pre >ma15_pre && ma5_curr
< ma15_curr){ _State = COVERLONG Amount = 1} if(_State == SHORT && ma5_pre < ma15_pre && ma5_curr >ma15_curr){ _State = COVERSHORT Amount = 1}
This is OK, you can test it back:
You can see that the backtest results are basically the same, so if you want to continue to add interactive features to the strategy, add data processing (such as K-line synthesis), and add custom chart drawings, you can do it.
Backtesting JavaScript policies
Backtest configuration:
Backtest results:
Back testing of my language
At this point, I believe everyone has a deeper understanding of "how to transplant a my language strategy," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.