분류
공개스크립트
매수 후 보유 지표
페이지 정보
조회
39,181
본문
특정 종목을 매수 후 보유했을때의 수익률과 연 복리, 손실폭을 보여주는 지표입니다.
전략의 수익률(실험군)과 비교해볼 수 있습니다.
전략과의 비교 뿐만 아니라 종목이나 ETF를 선택할때도 활용할 수 있습니다.
//@version=4 study("매수 후 보유 [Indicator]", "매&보 [I]", false, precision=2) // 수익률 계산할 종목 입력 sym = input("", "종목", input.symbol) // 백테스트 기간 설정 startYear = input(1, "시작 년") startMonth = input(1, "시작 월") startDay = input(1, "시작 일") startPeriod = timestamp(startYear, startMonth, startDay, 0, 0) endYear = input(9999, "종료 년") endMonth = input(12, "종료 월") endDay = input(31, "종료 일") endPeriod = timestamp(endYear, endMonth, endDay, 0, 0) testPeriod = time >= startPeriod and time <= endPeriod // 종가 불러오기 price = security(sym=='' ? syminfo.tickerid : sym, timeframe.period, close, barmerge.gaps_off, barmerge.lookahead_on) // 수익률 계산 unitYield = testPeriod ? nz(price/price[1]-1)*100 : 0 // 누적 수익률 계산 var yield = 0.0 yield := (((1+yield/100) * (1+unitYield/100)) - 1) * 100 // 백테스트 기간에만 봉 갯수 세기 var barCount = 0 barCount := barCount + (testPeriod ? 1 : 0) // 연 복리 계산 (일봉 기준일때 주식이면 260.714285, 암호화폐면 365) unitCagr = timeframe.period == "D" ? 260.714285 : timeframe.period == "W" ? 52.142857 : timeframe.period == "M" ? 12 : timeframe.period == "12M" ? 1 : na cagr = (pow(1+yield/100, 1/((barCount)/unitCagr))-1)*100 // 최고 수익률 계산 var maxYield = 0.0 maxYield := max(maxYield, yield) // 손실폭 계산 var drawdown = 0.0 drawdown := (((1+yield/100)/(1+maxYield/100)) - 1) * 100 // 최대 손실폭 계산 var mdd = 0.0 mdd := min(mdd, drawdown) // 결과 출력 plot(testPeriod ? yield : na, "수익률(%)", color.purple) plot(testPeriod ? cagr : na, "연 복리(%)", color.green) plot(testPeriod ? drawdown : na, "손실폭(%)", color.red) plot(testPeriod ? mdd : na, "최대 손실폭(%)", color.orange)
관련자료
등록된 댓글이 없습니다.