以文本方式查看主题 - 金字塔客服中心 - 专业程序化交易软件提供商 (http://222.73.7.161/bbs/index.asp) -- 公式模型编写问题提交 (http://222.73.7.161/bbs/list.asp?boardid=4) ---- [求助]请老师帮忙把海龟策略的仓位,修改为固定总资金30% (http://222.73.7.161/bbs/dispbbs.asp?boardid=4&id=160393) |
-- 作者:liurunning -- 发布时间:2017/12/13 16:06:20 -- [求助]请老师帮忙把海龟策略的仓位,修改为固定总资金30% 请老师帮忙把海龟策略的仓位,修改为固定总资金30%,非常感谢 nput:inb(20,1,500,1),outb(10,1,500,1),risk(2,0,10,0.1); //和风版海龟源码 variable:times=0,i=0,n=0; rn:=ema(ref(tr,1),20); n:=valuewhen(holding=0,rn); rh:=ref(h,1); rl:=ref(l,1); h1:hhv(rh,inb); h2:hhv(rh,outb),linedot; l1:llv(rl,inb); l2:llv(rl,outb),linedot; lotst:asset*risk*0.01/(n*2*multiplier),linethick0; lots:=if(risk=0,1,lotst); //如果risk取0,表示固定开1手 tbc:=h<>l;//判断是否停板 partline(holding>0,enterprice-2*n); if barpos<inb+1 then exit; if holding=0 and tbc then //不是停板才可以交易 begin if h>h1 then //开多 begin buyp:=max(o,h1); buy(1,lots,limitr,buyp); times:=1; while h>enterprice+n*0.5 and times<4 do begin buyp:=max(o,enterprice+n*0.5); buy(1,lots,limitr,buyp); times:=times+1; end;//连续开仓 end;//开多结束 else if l<l1 then //开空 begin sellp:=min(o,l1); buyshort(1,lots,limitr,sellp); times:=1; while l<enterprice-n*0.5 and times<4 do begin sellp:=min(o,enterprice-n*0.5); buyshort(1,lots,limitr,sellp); times:=times+1; end;//连续开仓 end; end;//holding=0 if holding>0 and tbc then //已有多仓 begin exitlongp:=max(enterprice-2*n,l2); if l<exitlongp and enterbars<>0 then //出场 begin exitp:=min(o,exitlongp); sell(1,0,limitr,exitp); times:=0; end;//出场 else begin while h>enterprice+n*0.5 and times<4 do //开多 begin buyp:=max(o,enterprice+n*0.5); buy(1,lots,limitr,buyp); times:=times+1; end;//连续开仓 end;//else end;//holding>0
if holding<0 and tbc then //已有空仓 begin exitlongp:=min(enterprice+2*n,h2); if h>exitlongp and enterbars<>0 then //出场 begin exitp:=max(o,exitlongp); sellshort(1,0,limitr,exitp); times:=0; end;//出场 else begin while l<enterprice-n*0.5 and times<4 do //开多 begin sellp:=min(o,enterprice-n*0.5); buyshort(1,lots,limitr,sellp); times:=times+1; end;//连续开仓 end;//else end;//holding<0 |
-- 作者:FireScript -- 发布时间:2017/12/13 16:31:02 -- 是要每次开仓按照总资产30%开仓? |
-- 作者:liurunning -- 发布时间:2017/12/13 16:38:20 -- 是的,老师。 |
-- 作者:FireScript -- 发布时间:2017/12/13 16:45:40 -- buy函数第二个参数 写成百分比就行了。开空buyshort也是一样的道理。 你可以看下函数说明对第二个参数的说明。 |
-- 作者:liurunning -- 发布时间:2017/12/13 16:58:35 -- 谢谢老师,但盈利是不加仓的,亏损也不减仓,到亏损幅度后,清仓。开进去后,永远保持30%仓位 [此贴子已经被作者于2017/12/13 16:59:27编辑过]
|
-- 作者:FireScript -- 发布时间:2017/12/14 9:13:27 -- 你这意思是无论怎么开仓加仓都要保存当前持仓占资金的30% |
-- 作者:客人 -- 发布时间:2017/12/14 11:18:30 -- 是的,一直保持30%左右仓位,谢谢了 |
-- 作者:qq代人发帖 -- 发布时间:2017/12/14 11:23:24 -- buy(cond,30%,marketr),PERTRADER; 当前资金的30%开仓 |
-- 作者:liurunning -- 发布时间:2017/12/14 15:09:39 -- 老师,运行不了嘛。
|
-- 作者:FireScript -- 发布时间:2017/12/14 15:34:30 -- 总持仓量不超过总资金的30%,可以理解为占用保证金/总权益<=30%对吧。 可以添加一个开仓条件 cond:TACCOUNT(28) / TACCOUNT( 6) <=0.3;
你的开仓,加仓都添加一个cond的条件就行了。 这2个函数直接是读取的是实际账号的持仓。 [此贴子已经被作者于2017/12/14 15:36:32编辑过]
|