//** //* depth -> length -> zero //* lengthが最高値・最安値であるかを確認 //* trueならbar_indexと価格を返す //* pivots(src, length, isHigh) => p = nz(src[length])
if length == 0 [bar_index, p] else isFound = true // length -> zeroの最高値・最安値を確認 for i = 0 to length - 1 if isHigh and src[i] > p isFound := false if not isHigh and src[i] < p isFound := false // depth -> lengthの最高値・最安値を確認 for i = length + 1 to 2 * length if isHigh and src[i] >= p isFound := false if not isHigh and src[i] <= p isFound := false
// lengthが最高値・最安値だった場合は値を返す if isFound and length * 2 <= bar_index [bar_index[length], p] // そうでない場合はnaを返す else [int(na), float(na)]
// 直近のpivotの情報を格納 var line lineLast = na // lineのid (実際はidではなく識別している何か) var int iLast = 0 // bar_index var float pLast = 0 // price var bool isHighLast = true // High か Low か // 描画したラインの数 var int linesCount = 0
//** //* Lineを描画する関数 //* pivotFound(dev, isHigh, index, price) => // zigzagの頂点を更新する if isHighLast == isHigh and not na(lineLast) if isHighLast ? price > pLast : price < pLast if linesCount <= 1 line.set_xy1(lineLast, index, price) line.set_xy2(lineLast, index, price) [lineLast, isHighLast, false] else [line(na), bool(na), false]
// zigzagの方向を変える(zigzagの頂点) else // 一番最初のLine if na(lineLast) id = line.new(index, price, index, price, color=color.red, width=2) [id, isHigh, true] else // 変化率が設定値以上であることを確認 if abs(dev) >= dev_threshold id = line.new(iLast, pLast, index, price, color=color.red, width=2) [id, isHigh, true] else [line(na), bool(na), false]
//** //* 描画処理 //*
// iH iL のデータある // iH と iL の値(bar_index)が同じ // 高値と安値の描画処理を行う if not na(iH) and not na(iL) and iH == iL dev1 = calc_dev(pLast, pH) [id2, isHigh2, isNew2] = pivotFound(dev1, true, iH, pH) if isNew2 linesCount := linesCount + 1 if not na(id2) lineLast := id2 isHighLast := isHigh2 iLast := iH pLast := pH
dev2 = calc_dev(pLast, pL) [id1, isHigh1, isNew1] = pivotFound(dev2, false, iL, pL) if isNew1 linesCount := linesCount + 1 if not na(id1) lineLast := id1 isHighLast := isHigh1 iLast := iL pLast := pL
else // iH の値がある if not na(iH) dev1 = calc_dev(pLast, pH) [id, isHigh, isNew] = pivotFound(dev1, true, iH, pH) if isNew linesCount := linesCount + 1 if not na(id) lineLast := id isHighLast := isHigh iLast := iH pLast := pH // iL の値がある else if not na(iL) dev2 = calc_dev(pLast, pL) [id, isHigh, isNew] = pivotFound(dev2, false, iL, pL) if isNew linesCount := linesCount + 1 if not na(id) lineLast := id isHighLast := isHigh iLast := iL pLast := pL =====