“MtParabolicSAR,跨周期拋物線轉向ParabolicSAR函數”出爐
作者:開拓者 TB 來源:cxh99.com 發布時間:2014年05月29日
- 咨詢內容:
本帖最后由 扶老二 于 2014-5-11 19:34 編輯
“MtParabolicSAR,跨周期拋物線轉向ParabolicSAR函數”出爐
有需要的帥哥美女拿去用吧,續貼到“追漲殺跌”的帖子里了:
http://bbs.tb18.net/thread-15184-24-1.html
有問題歡迎交流 QQ: 149561420- //------------------------------------------------------------------------
- // MtParabolicSAR,跨周期拋物線轉向ParabolicSAR函數
- // 返回 當前bar的“周期索引”,比如當前在1分鐘周期里,折算成15分鐘周期的話,從凌晨00:00分鐘開始,第1根bar是1,第2根bar是2......第15根bar是15,第16根bar又變成1,......
- // 除了函數本身的返回值,還通過oParCl、oParOpen、oPosition、oTransition這四個引用參數把對應Bar的停損值、對應Bar的停損值下一根的停損值、對應Bar建議的持倉狀態(1 - 多頭,-1 - 空頭)、對應Bar的狀態是否發生反轉(1 或 -1 為反轉,0 為保持不變)
- // 版本 20140511_183500
- //------------------------------------------------------------------------
- Params
- Numeric TimeFrame(1440); //目標時間周期參數,參數說明參見MtBar
- Numeric BarsBack(1); //目標時間周期BAR偏移參數,說明見MtBar函數
- Numeric AfStep(0.02);
- Numeric AfLimit(0.2);
- NumericRef oParClose;
- NumericRef oParOpen;
- NumericRef oPosition;
- NumericRef oTransition;
- Vars
- NumericSeries barCnt;
- NumericSeries CurBar;
- NumericSeries barCntSum;
- NumericSeries HighHT;
- NumericSeries LowHT;
- Numeric CurTime;
- Numeric PreTime;
- bool condition(false);
- Numeric n;
- Numeric i;
- NumericSeries Af(0);
- NumericSeries ParOpen(0);
- NumericSeries ParClose(0);
- NumericSeries Position(0);
- NumericSeries HHValue(0);
- NumericSeries LLValue(0);
- NumericSeries Transition(0);
-
- Begin
- if(TimeFrame == 40320) { //月線
- CurTime = Month;
- PreTime = Month[1];
- } else if(TimeFrame == 10080) { //周線
- CurTime = IntPart(DateDiff(19700105, TrueDate(0)) / 7);
- PreTime = IntPart(DateDiff(19700105, TrueDate(1)) / 7);
- } else { //其他時間周期
- CurTime = IntPart((DateDiff(19700105, TrueDate(0)) * 1440 + Hour * 60 + Minute) / TimeFrame);
- PreTime = IntPart((DateDiff(19700105, TrueDate(1)) * 1440 + Hour[1] * 60 + Minute[1]) / TimeFrame);
- }
- condition = CurTime != PreTime;
- if(CurrentBar == 0) { //如果是第一根Bar, CurBar=0
- barCnt = 0;
- CurBar = 0;
- HighHT = High[1];
- LowHT = Low[1];
-
- Position = 1;
- Transition = 1;
- Af = AfStep;
- HHValue = HighHT;
- LLValue = LowHT;
- ParClose = LLValue;
- ParOpen = ParClose + Af * (HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
- } else {
- if(condition) { //如果在目標周期下,屬于另一根K線,則CurBar加1
- barCnt = 1;
- CurBar = CurBar[1] + 1;
- HighHT = High[1];
- LowHT = Low[1];
- for n = 2 to TimeFrame {
- HighHT = Max(HighHT, High[n]);
- LowHT = Min(LowHT, Low[n]);
- }
- } else { //如果在目標周期下,屬于同一根K線,則CurBar不變,但最高價和最低價要記錄價格的變化,成交量要累加
- barCnt = barCnt[1] + 1;
- CurBar = CurBar[1];
- HighHT = Max(HighHT[1], High);
- LowHT = Min(LowHT[1], Low);
- }
-
- HHValue = Max(HHValue[1], HighHT);
- LLValue = Min(LLValue[1], LowHT);
- if(CurBar == 0) {
- Position = 1;
- Transition = 1;
- Af = AfStep;
- ParClose = LLValue;
- ParOpen = ParClose + Af * (HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
- } else {
- if(condition) { //正好切換到目標周期的下一根k線
- Transition = 0;
- if(Position[TimeFrame] == 1) {
- if(LowHT <= ParOpen[TimeFrame]) {
- Position = -1;
- Transition = -1;
- ParClose = HHValue;
- HHValue = HighHT;
- LLValue = LowHT;
-
- Af = AfStep;
- ParOpen = ParClose + Af * (LLValue - ParClose);
-
- if(ParOpen < HighHT) {
- ParOpen = HighHT;
- }
-
- if(ParOpen < HighHT[TimeFrame]) {
- ParOpen = HighHT[TimeFrame];
- }
- } else {
- Position = Position[TimeFrame];
- ParClose = ParOpen[TimeFrame];
- if(HHValue > HHValue[TimeFrame] && Af[TimeFrame] < AfLimit) {
- if(Af[TimeFrame] + AfStep > AfLimit) {
- Af = AfLimit;
- } else {
- Af = Af[TimeFrame] + AfStep;
- }
- } else {
- Af = Af[TimeFrame];
- }
- ParOpen = ParClose + Af * (HHValue - ParClose);
-
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
-
- if(ParOpen > LowHT[TimeFrame]) {
- ParOpen = LowHT[TimeFrame];
- }
- }
- } else {
- if(HighHT >= ParOpen[TimeFrame]) {
- Position = 1;
- Transition = 1;
-
- ParClose = LLValue;
- HHValue = HighHT;
- LLValue = LowHT;
-
- Af = AfStep;
- ParOpen = ParClose + Af * (HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
-
- if(ParOpen > LowHT[TimeFrame]) {
- ParOpen = LowHT[TimeFrame];
- }
- } else {
- Position = Position[TimeFrame];
- ParClose = ParOpen[TimeFrame];
-
- if(LLValue < LLValue[TimeFrame] && Af[TimeFrame] < AfLimit) {
- if(Af[TimeFrame] + AfStep > AfLimit) {
- Af = AfLimit;
- } else {
- Af = Af[TimeFrame] + AfStep;
- }
- } else {
- Af = Af[TimeFrame];
- }
- ParOpen = ParClose + Af * (LLValue - ParClose);
-
- if(ParOpen < HighHT) {
- ParOpen = HighHT;
- }
-
- if(ParOpen < HighHT[TimeFrame]) {
- ParOpen = HighHT[TimeFrame];
- }
- }
- }
- }
- }
- }
-
- //上面的程序,在每根小周期的K線上,記錄了它所屬的大時間周期下的開高低收等值的變化。
- //接下來,要把在大的時間周期級別上,屬于同一根K線的開高低收這些數據,記錄在這一組小周期K線的最后一根上。
- barCntSum = barCnt;
- if(BarsBack == 0) { //如果Bar偏移參數為0,則取每根小周期K線上保留的大時間周期截止到這根小周期K線為止的BAR數據
- barCntSum = 0;
- } else if(BarsBack == 1) { //如果Bar偏移參數為1,則取大時間周期的上一根K線的BAr數據
- barCntSum = barCnt;
- } else { //如果BAR偏移參數為其他,則取大時間周期的指定偏移后的那根K線的BAR數據
- for i = 2 to BarsBack {
- barCntSum = barCntSum + barCnt[barCntSum];
- }
- }
- //最后將相應的K線數據作為引用參數返回
- oParClose = ParClose[barCntSum];
- oParOpen = ParOpen[barCntSum];
- oPosition = Position[barCntSum];
- oTransition = Transition[barCntSum];
-
- Return true;
- End
- TB技術人員:
群主我用rb的日線跟此函數timeframe1440進行了對比發現不匹配
- TB客服:
duck_arrow 發表于 2014-5-13 15:12
群主我用rb的日線跟此函數timeframe1440進行了對比發現不匹配
跟tb自帶的ParabolicSAR函數比對發現不匹配。其實主要是tb自帶的ParabolicSAR函數代碼里High和Low都用的當前k線的數據導致,這是有問題的,當前k線還沒運行結束之前,High和Low都是未來的屬性,所以直接使用它,會導致實盤時候信號閃爍和使用了未來屬性,我修復了一下tb自帶的ParabolicSAR,用High[1]和Low[1]來取代High和Low,取名ParabolicSAR_ximen,請用以下代碼新建用戶函數,函數名叫ParabolicSAR_ximen,然后在不需要跨周期的時候,建議用ParabolicSAR_ximen來替換ParabolicSAR:- //------------------------------------------------------------------------
- // 西門 ParabolicSAR_ximen
- // 描述 求拋物線轉向,修正系統默認用當前bar會導致閃爍的問題,采用[1]來做判斷依據,比系統默認函數更精確
- // 返回 True 或 false。除了函數本身的返回值,還通過oParCl、oParOpen、oPosition、oTransition這四個引用參數把對應Bar的停損值、對應Bar的停損值下一根的停損值、對應Bar建議的持倉狀態(1 - 多頭,-1 - 空頭)、對應Bar的狀態是否發生反轉(1 或 -1 為反轉,0 為保持不變)
- // 版本 20140511
- //------------------------------------------------------------------------
- Params
- Numeric AfStep(0.02);
- Numeric AfLimit(0.2);
- NumericRef oParClose;
- NumericRef oParOpen;
- NumericRef oPosition;
- NumericRef oTransition;
-
- Vars
- NumericSeries HighHT;
- NumericSeries LowHT;
- NumericSeries Af(0);
- NumericSeries ParOpen(0);
- NumericSeries ParClose(0);
- NumericSeries Position(0);
- NumericSeries HHValue(0);
- NumericSeries LLValue(0);
- NumericSeries Transition(0);
- Begin
- if(CurrentBar == 0) {
- HighHT = High;
- LowHT = Low;
-
- Position = 1;
- Transition = 1;
- Af = AfStep;
- HHValue = HighHT;
- LLValue = LowHT;
- ParClose = LLValue;
- ParOpen = ParClose + Af * ( HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
- } else {
- HighHT = High[1];
- LowHT = Low[1];
-
- HHValue = Max(HHValue[1], HighHT);
- LLValue = Min(LLValue[1], LowHT);
- Transition = 0;
- if(Position[1] == 1) {
- if(LowHT <= ParOpen[1]) {
- Position = -1;
- Transition = -1;
- ParClose = HHValue;
- HHValue = HighHT;
- LLValue = LowHT;
-
- Af = AfStep;
- ParOpen = ParClose + Af * (LLValue - ParClose);
-
- if(ParOpen < HighHT) {
- ParOpen = HighHT;
- }
-
- if(ParOpen < HighHT[1]) {
- ParOpen = HighHT[1];
- }
- } else {
- Position = Position[1];
- ParClose = ParOpen[1];
- if(HHValue > HHValue[1] && Af[1] < AfLimit) {
- if(Af[1] + AfStep > AfLimit) {
- Af = AfLimit;
- } else {
- Af = Af[1] + AfStep;
- }
- } else {
- Af = Af[1];
- }
- ParOpen = ParClose + Af * (HHValue - ParClose);
-
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
-
- if(ParOpen > LowHT[1]) {
- ParOpen = LowHT[1];
- }
- }
- } else {
- if(HighHT >= ParOpen[1]) {
- Position = 1;
- Transition = 1;
-
- ParClose = LLValue;
- HHValue = HighHT;
- LLValue = LowHT;
-
- Af = AfStep;
- ParOpen = ParClose + Af * (HHValue - ParClose);
- if(ParOpen > LowHT) {
- ParOpen = LowHT;
- }
-
- if(ParOpen > LowHT[1]) {
- ParOpen = LowHT[1];
- }
- } else {
- Position = Position[1];
- ParClose = ParOpen[1];
-
- if(LLValue < LLValue[1] && Af[1] < AfLimit) {
- if(Af[1] + AfStep > AfLimit) {
- Af = AfLimit;
- } else {
- Af = Af[1] + AfStep;
- }
- } else {
- Af = Af[1];
- }
- ParOpen = ParClose + Af * (LLValue - ParClose);
-
- if(ParOpen < HighHT) {
- ParOpen = HighHT;
- }
-
- if(ParOpen < HighHT[1]) {
- ParOpen = HighHT[1];
- }
- }
- }
- }
-
- oParClose = ParClose;
- oParOpen = ParOpen;
- oPosition = Position;
- oTransition = Transition;
- Return True;
- End
復制代碼下面是不跨周期的ParabolicSAR_ximen調用的例子:- Params
- Vars
- Numeric oParCl;
- Numeric oParOp;
- Numeric oPosition;
- Numeric oTransition;
-
- begin
- ParabolicSAR_ximen(0.02, 0.2, oParCl, oParOp, oPosition, oTransition);
-
- Commentary("oParCl:" + Text(oParCl));
- Commentary("oParOp:" + Text(oParOp));
- Commentary("oPosition:" + Text(oPosition));
- Commentary("oTransition:" + Text(oTransition));
- End
復制代碼