커뮤니티

변수의 이전값 참조

프로필 이미지
teargas
2006-11-14 18:43:57
1674
글번호 11294
답변완료
안녕하세요? 변수의 이전값참조 문제로 다시 질문 드리게되었습니다 아래 소스코드의 중간쯤,, HP = 0.5 * ( 1 + Alpha ) * OneBarDiff + Alpha * HP[1] ; ..부분인데요, 변수 자신의 과거값을 참조하여 값을 갱신하는 문장입니다. 여러모로 돌려서 해결을 시도했지만 먹혀들지 않더군요. 해결 부탁드리겠습니다. p.s. 다른 툴과는 다르게 예스랭귀지에선 왜 이런 현상이 생기는지 의문입니다. 이건 버그인가요? 아님 의도된 설정인가요?? // 소스코드 ----------------------------------------------------------- inputs: MALength(20), RSILen(8), StochLen(8), WAveLen(5) ; //vars: StochRSI(0), Trigger(0); variables: Price(0), SMA(0), Mom(0), SmoothMom(0), ITrend(0), DegPerBar(0), CosineValue(0), Alpha(0), OneBarDiff(0), HP(0), SmoothHP(0), Synth(0), CU(0), CD(0), Count(0), CntPlus1(0), CUAve(0), CDAve(0), RSI0(0), RSILenLess1(0), HiR(0), LoR(0), StochR(0), StochRSI(0), StochRSIDiv(0), Trigger(0), temp(0) ; Price = close; SMA = ema( Price, MALength ) ; //{SMA = HMA(Price, MALength);} Mom = Price[0] - Price[MALength - 1]; SmoothMom = ( Mom + 2 * Mom[1] + 2 * Mom[2] + Mom[3] ) / 6 ; ITrend = SMA + 0.5 * SmoothMom ; DegPerBar = 360 / MALength ; CosineValue = Cos( DegPerBar ) ; if CosineValue <> 0 then Alpha = ( 1 - Sin ( DegPerBar ) ) / CosineValue ; OneBarDiff = Price - Price[1] ; HP = 0.5 * ( 1 + Alpha ) * OneBarDiff + Alpha * HP[1] ; //HP = (0.5 * ( 1 + Alpha ) * OneBarDiff) + (Alpha * HP); SmoothHP = ( HP + 2 * HP[1] + 2 * HP[2] + HP[3] ) / 6 ; if index <= 1 then SmoothHP = 0; else if index < 4 then SmoothHP = OneBarDiff ; Synth = ITrend + SmoothHP ; CU = 0 ; CD = 0 ; for Count = 0 to RSILen - 1 begin CntPlus1 = Count + 1 ; if Synth[Count] > Synth[CntPlus1] then CU = CU + Synth[Count] - Synth[CntPlus1]; else if Synth[Count] < Synth[CntPlus1] then CD = CD + Synth[CntPlus1] - Synth[Count] ; end CU = CU / RSILen ; CD = CD / RSILen ; CUAve = CU ; CDAve = CD ; if CUAve <> -CDAve then RSI0 = CUAve / ( CUAve + CDAve ) ; if index > 1 then begin RSILenLess1 = RSILen - 1 ; CUAve = ( CUAve[1] * RSILenLess1 + CU ) / RSILen ; CDAve = ( CDAve[1] * RSILenLess1 + CD ) / RSILen ; end if CUAve <> -CDAve then RSI0 = CUAve / ( CUAve + CDAve ) ; HiR = Highest( RSI0, StochLen ) ; LoR = Lowest( RSI0, StochLen ) ; if HiR <> LoR then StochR = ( RSI0 - LoR ) / ( HiR - LoR ) ; StochRSI = 0 ; StochRSIDiv = 0 ; for Count = 0 to WAveLen - 1 begin StochRSI = StochRSI + ( WAveLen - Count ) * StochR[Count] ; StochRSIDiv = StochRSIDiv + Count + 1 ; end if StochRSIDiv <> 0 then StochRSI = StochRSI / StochRSIDiv ; else StochRSI = 0; //StochRSI = EhlersStoRSIModel(MALength, RSILen, StochLen, WAveLen); //Trigger = 0.05 + 0.9 * StochRSI[1] ; plot1(StochRSI, "EhlersStoRSI"); //{plot2(Trigger, "Trigger");}
지표
답변 1
프로필 이미지

예스스탁 예스스탁 답변

2006-11-15 17:16:03

안녕하세요 예스스탁입니다. OneBarDiff = Price - Price[1] ; HP = 0.5 * ( 1 + Alpha ) * OneBarDiff + Alpha * HP[1] ; 차트상 첫봉에 Price[1]이 없으므로 OneBarDiff 는 N/A을 가지게 됩니다. 이 N/A가 Hp식에 들어가면 HP값이 N/A가 됩니다. 두번째 봉에서 OneBarDiff는 Price[1]이 있으므로 특정값을 가지게 됩니다. 하지만 HP는 전봉의 N/A 다시 참고하여 계산하므로 또 N/A가 나오게 됩니다. 이렇게 연속적으로 계속 HP는 N/A값을 가지게 되므로 수식이 제대로 계산되지 않았습니다. 식을 수정하여 첫봉에서는 OneBarDiff,HP 가 0을 가지게 변경했습니다. 즐거운 하루되세요 inputs: MALength(20), RSILen(8), StochLen(8), WAveLen(5) ; //vars: StochRSI(0), Trigger(0); variables: Price(0), SMA(0), Mom(0), SmoothMom(0), ITrend(0), DegPerBar(0), CosineValue(0), Alpha(0), OneBarDiff(0), HP(0), SmoothHP(0), Synth(0), CU(0), CD(0), Count(0), CntPlus1(0), CUAve(0), CDAve(0), RSI0(0), RSILenLess1(0), HiR(0), LoR(0), StochR(0), StochRSI(0), StochRSIDiv(0), Trigger(0), temp(0) ; Price = close; SMA = ema( Price, MALength ) ; //{SMA = HMA(Price, MALength);} Mom = Price[0] - Price[MALength - 1]; SmoothMom = ( Mom + 2 * Mom[1] + 2 * Mom[2] + Mom[3] ) / 6 ; ITrend = SMA + 0.5 * SmoothMom ; DegPerBar = 360 / MALength ; CosineValue = Cos(DegPerBar); if CosineValue <> 0 then{ Alpha = ( 1 - Sin ( DegPerBar ) ) / CosineValue ; } if index() > 0 then{ OneBarDiff = Price - Price[1] ; HP = 0.5 * ( 1 + Alpha ) * OneBarDiff + Alpha * HP[1] ; } else{ OneBarDiff = 0; HP = 0; } if index <= 1 then SmoothHP = 0; else if index < 4 then SmoothHP = OneBarDiff ; Synth = ITrend + SmoothHP ; CU = 0 ; CD = 0 ; for Count = 0 to RSILen - 1 begin CntPlus1 = Count + 1 ; if Synth[Count] > Synth[CntPlus1] then CU = CU + Synth[Count] - Synth[CntPlus1]; else if Synth[Count] < Synth[CntPlus1] then CD = CD + Synth[CntPlus1] - Synth[Count] ; end CU = CU / RSILen ; CD = CD / RSILen ; CUAve = CU ; CDAve = CD ; if CUAve <> -CDAve then RSI0 = CUAve / ( CUAve + CDAve ) ; if index > 1 then begin RSILenLess1 = RSILen - 1 ; CUAve = ( CUAve[1] * RSILenLess1 + CU ) / RSILen ; CDAve = ( CDAve[1] * RSILenLess1 + CD ) / RSILen ; end if CUAve <> -CDAve then RSI0 = CUAve / ( CUAve + CDAve ) ; HiR = Highest( RSI0, StochLen ) ; LoR = Lowest( RSI0, StochLen ) ; if HiR <> LoR then StochR = ( RSI0 - LoR ) / ( HiR - LoR ) ; StochRSI = 0 ; StochRSIDiv = 0 ; for Count = 0 to WAveLen - 1 begin StochRSI = StochRSI + ( WAveLen - Count ) * StochR[Count] ; StochRSIDiv = StochRSIDiv + Count + 1 ; end if StochRSIDiv <> 0 then StochRSI = StochRSI / StochRSIDiv ; else StochRSI = 0; //StochRSI = EhlersStoRSIModel(MALength, RSILen, StochLen, WAveLen); //Trigger = 0.05 + 0.9 * StochRSI[1] ; plot1(StochRSI, "EhlersStoRSI"); //{plot2(Trigger, "Trigger");} =========================================================================== > teargas 님이 쓴 글입니다. > 제목 : 변수의 이전값 참조 > 안녕하세요? 변수의 이전값참조 문제로 다시 질문 드리게되었습니다 아래 소스코드의 중간쯤,, HP = 0.5 * ( 1 + Alpha ) * OneBarDiff + Alpha * HP[1] ; ..부분인데요, 변수 자신의 과거값을 참조하여 값을 갱신하는 문장입니다. 여러모로 돌려서 해결을 시도했지만 먹혀들지 않더군요. 해결 부탁드리겠습니다. p.s. 다른 툴과는 다르게 예스랭귀지에선 왜 이런 현상이 생기는지 의문입니다. 이건 버그인가요? 아님 의도된 설정인가요?? // 소스코드 ----------------------------------------------------------- inputs: MALength(20), RSILen(8), StochLen(8), WAveLen(5) ; //vars: StochRSI(0), Trigger(0); variables: Price(0), SMA(0), Mom(0), SmoothMom(0), ITrend(0), DegPerBar(0), CosineValue(0), Alpha(0), OneBarDiff(0), HP(0), SmoothHP(0), Synth(0), CU(0), CD(0), Count(0), CntPlus1(0), CUAve(0), CDAve(0), RSI0(0), RSILenLess1(0), HiR(0), LoR(0), StochR(0), StochRSI(0), StochRSIDiv(0), Trigger(0), temp(0) ; Price = close; SMA = ema( Price, MALength ) ; //{SMA = HMA(Price, MALength);} Mom = Price[0] - Price[MALength - 1]; SmoothMom = ( Mom + 2 * Mom[1] + 2 * Mom[2] + Mom[3] ) / 6 ; ITrend = SMA + 0.5 * SmoothMom ; DegPerBar = 360 / MALength ; CosineValue = Cos( DegPerBar ) ; if CosineValue <> 0 then Alpha = ( 1 - Sin ( DegPerBar ) ) / CosineValue ; OneBarDiff = Price - Price[1] ; HP = 0.5 * ( 1 + Alpha ) * OneBarDiff + Alpha * HP[1] ; //HP = (0.5 * ( 1 + Alpha ) * OneBarDiff) + (Alpha * HP); SmoothHP = ( HP + 2 * HP[1] + 2 * HP[2] + HP[3] ) / 6 ; if index <= 1 then SmoothHP = 0; else if index < 4 then SmoothHP = OneBarDiff ; Synth = ITrend + SmoothHP ; CU = 0 ; CD = 0 ; for Count = 0 to RSILen - 1 begin CntPlus1 = Count + 1 ; if Synth[Count] > Synth[CntPlus1] then CU = CU + Synth[Count] - Synth[CntPlus1]; else if Synth[Count] < Synth[CntPlus1] then CD = CD + Synth[CntPlus1] - Synth[Count] ; end CU = CU / RSILen ; CD = CD / RSILen ; CUAve = CU ; CDAve = CD ; if CUAve <> -CDAve then RSI0 = CUAve / ( CUAve + CDAve ) ; if index > 1 then begin RSILenLess1 = RSILen - 1 ; CUAve = ( CUAve[1] * RSILenLess1 + CU ) / RSILen ; CDAve = ( CDAve[1] * RSILenLess1 + CD ) / RSILen ; end if CUAve <> -CDAve then RSI0 = CUAve / ( CUAve + CDAve ) ; HiR = Highest( RSI0, StochLen ) ; LoR = Lowest( RSI0, StochLen ) ; if HiR <> LoR then StochR = ( RSI0 - LoR ) / ( HiR - LoR ) ; StochRSI = 0 ; StochRSIDiv = 0 ; for Count = 0 to WAveLen - 1 begin StochRSI = StochRSI + ( WAveLen - Count ) * StochR[Count] ; StochRSIDiv = StochRSIDiv + Count + 1 ; end if StochRSIDiv <> 0 then StochRSI = StochRSI / StochRSIDiv ; else StochRSI = 0; //StochRSI = EhlersStoRSIModel(MALength, RSILen, StochLen, WAveLen); //Trigger = 0.05 + 0.9 * StochRSI[1] ; plot1(StochRSI, "EhlersStoRSI"); //{plot2(Trigger, "Trigger");}