COCO研究院

 找回密碼
 註冊
搜索
查看: 2828|回復: 4

[範例程式碼] SafeZone指標 ::走進我的交易室::

[複製鏈接]
發表於 12-11-7 14:31 | 顯示全部樓層 |閱讀模式
本帖最後由 zaqimon 於 12-11-7 14:30 編輯

好像沒看到有人發表過SafeZone指標
“走進我的交易室”書上提到使用SafeZone作為停損使用
這是網路找的然後經過自己修改過


coco10.gif

  1. /*

  2. from htt[delete_me]p:/[delete_me]/w[delete_me]ww.amibroker.com/library/detail.php?id=569

  3. [20121107] zaqimon
  4. Some mods and tidy up.
  5. There is also another version with STDEV for its channel width, but I'm OK with this simple original method.

  6. */
  7. Pd=Param("Period",10,1,50,1);
  8. ShowStop = ParamToggle("Show Stop Breakout?","No|Yes",0);
  9. AvgUpPenMult=Param("Up Multiplier",2,1,4,0.1);
  10. AvgDnPenMult=Param("Down Multiplier",2,1,4,0.1);
  11. DaysInTrade=Param("Days in trade",3,1,50,1);
  12. colorSZShort = ParamColor("Color Safe Zone Short", colorRed );
  13. colorSZLong = ParamColor("Color Safe Zone Long", colorBrightGreen );

  14. H1=Ref(H,-1);
  15. L1=Ref(L,-1);
  16. UpPen=IIf(H>H1,H-H1,0);
  17. DnPen=IIf(L<L1,L1-L,0);
  18. UpPenSum=Sum(UpPen,Pd);
  19. DnPenSum=Sum(DnPen,Pd);
  20. UpPenCount=Sum(H>H1,Pd);
  21. DnPenCount=Sum(L<L1,Pd);
  22. /*
  23. we need to avoid divide-by-zero.
  24. i choose using (yesterday) Close for Stop Price if there is no available value.
  25. you can happily exit anytime and take profit after such a rarely occured winning streak.
  26. or you should be more cautious about your position after such kind of rally.
  27. */
  28. AvgUpPen=IIf(UpPenCount>0,UpPenSum/UpPenCount,0);
  29. AvgDnPen=IIf(DnPenCount>0,DnPenSum/DnPenCount,0);
  30. StopShort=H+(AvgUpPen*AvgUpPenMult);
  31. StopLong=L-(AvgDnPen*AvgDnPenMult);
  32. // This is the SafeZone "Dot"
  33. SafeZoneStopShort=LLV(StopShort,DaysInTrade);
  34. SafeZoneStopLong=HHV(StopLong,DaysInTrade);
  35. // This calculates the Stop "Line"
  36. // Only move Stop closer except Stop Breakout, then we move Stop (farther) to SafeZone "Dot"
  37. ShortStop=SafeZoneStopShort;
  38. LongStop=SafeZoneStopLong;
  39. for (i=1;i < BarCount; i++)
  40. {
  41.     if(H[i] < ShortStop[i-1] && SafeZoneStopShort[i] > ShortStop[i-1])
  42.     {
  43.         ShortStop[i] = ShortStop[i-1];
  44.     }
  45.     if(L[i] > LongStop[i-1] && SafeZoneStopLong[i] < LongStop[i-1])
  46.     {
  47.         LongStop[i] = LongStop[i-1];
  48.     }
  49. }

  50. Plot(SafeZoneStopShort, "Safe Zone Short", colorSZShort, styleDots|styleNoLine);
  51. Plot(SafeZoneStopLong, "Safe Zone Long", colorSZLong, styleDots|styleNoLine);
  52. // EncodeColor force Title color unchanging, or it changes according to the right-most bar, which is irritating.
  53. // Grey line means we move Stop farther, while in real trade, you should always move Stop closer.
  54. Plot(ShortStop, EncodeColor(ColorBlend(colorSZShort,colorBlack))+"Stop Short", IIf(ShortStop>Ref(ShortStop,-1), colorGrey50, ColorBlend(colorSZShort, colorBlack)), ParamStyle("Style Stop Short", styleLine));
  55. Plot(LongStop, EncodeColor(ColorBlend(colorSZLong,colorBlack))+"Stop Long", IIf(LongStop<Ref(LongStop,-1), colorGrey50, ColorBlend(colorSZLong, colorBlack)), ParamStyle("Style Stop Long", styleLine));
  56. if(ShowStop)
  57. {
  58.     // touch Yesterday's Stop price then exit, showing a hollow triangle
  59.     PlotShapes(IIf(H>=Ref(ShortStop,-1), shapeHollowSmallUpTriangle, shapeNone), colorSZShort, 0, H, 12);
  60.     PlotShapes(IIf(L<=Ref(LongStop,-1), shapeHollowSmallDownTriangle, shapeNone), colorSZLong, 0, L, 12);
  61. }
複製代碼


評分

參與人數 3金錢 +4 收起 理由
heavenweaver + 1 按一個讚!
enochyu + 2 按一個讚!
manhavecoco + 1 按一個讚!

查看全部評分

發表於 12-11-7 14:46 | 顯示全部樓層
謝謝版大分享
發表於 12-11-7 14:52 | 顯示全部樓層
謝謝分享好東西
發表於 12-11-7 19:23 | 顯示全部樓層
這對我來說好像火星文  
發表於 12-11-7 20:26 | 顯示全部樓層
好東西謝謝分享SafeZone指標
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-4-30 07:14

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook