'How pivothigh() and pivotlow() function work on Tradingview Pinescript?
I'm trying to rewrite a script to Python, but I can't figure out how pivothigh() and pivotlow() function work, and I can't find source code, I know how to calculate Pivot Points, but what leftbars and rightbars means in this two function? Please help.
Solution 1:[1]
Leftbars and rightbars are number of bars that the pivot
functions looks when is searching for a pivot.
For example:
pivothigh(10,10)
will search for high price that was not exceeded during 10 bars to the left (past data) and 10 bars to the right (future data). Note that the function won't be able to determine the pivot if there is less than 10 bars to the right.
Solution 2:[2]
I've tried to create a simple version of it in pine script, which does not uses pivothigh/pivotlow - instead does the candlestick comparison.
https://www.tradingview.com/script/BYHsrYPG-Broken-Fractal-Someone-s-broken-dream-is-your-profit
I was also able to convert this into Ruby code (Python code should be as easy)
if (candles[i-1][:h] > candles[i-2][:h]) and (candles[i-1][:h] > candles[i][:h])
puts "DownFractal"
end
if (candles[i-1][:l] < candles[i-2][:l]) and (candles[i-1][:l] < candles[i][:l])
puts "UpFractal"
end
Solution 3:[3]
I too had a need to better understand how the pivothigh()
and pivotlow()
functions work internally so I made the effort to code a Pine Script version (using version 5 of Pine Script) for myself and tested it side-by-side with the ta.pivotlow()
and ta.pivothigh()
functions and it seems to work well. Maybe this will help you as well.
my_pivothigh(float _series = high, int _leftBars, int _rightBars) =>
float _pivotHigh = na
int _pivotRange = ( _leftBars + _rightBars )
float _leftEdgeValue = nz(_series[_pivotRange], na)
if not na(_series) and _leftBars > 0 and _rightBars > 0 and not na(_leftEdgeValue)
float _possiblePivotHigh = _series[_rightBars]
float[] _arrayOfSeriesValues = array.new_float(0)
for _barIndex = _pivotRange to 0
array.push(_arrayOfSeriesValues, _series[_barIndex])
//end for
int _pivotHighRightBars = array.size(_arrayOfSeriesValues) - array.lastindexof(_arrayOfSeriesValues, array.max(_arrayOfSeriesValues)) - 1
_pivotHigh := ( _pivotHighRightBars == _rightBars ) ? _possiblePivotHigh : na
//end if
_pivotHigh
my_pivotlow(float _series = low, int _leftBars, int _rightBars) =>
float _pivotLow = na
int _pivotRange = ( _leftBars + _rightBars )
float _leftEdgeValue = nz(_series[_pivotRange], na)
if not na(_series) and _leftBars > 0 and _rightBars > 0 and not na(_leftEdgeValue)
float _possiblePivotLow = _series[_rightBars]
float[] _arrayOfSeriesValues = array.new_float(0)
for _barIndex = _pivotRange to 0
array.push(_arrayOfSeriesValues, _series[_barIndex])
//end for
int _pivotLowRightBars = array.size(_arrayOfSeriesValues) - array.lastindexof(_arrayOfSeriesValues, array.min(_arrayOfSeriesValues)) - 1
_pivotLow := ( _pivotLowRightBars == _rightBars ) ? _possiblePivotLow : na
//end if
_pivotLow
Solution 4:[4]
I had found this thread after I have searched for this kind of implementation. Here is my own implementation for those that have been utilizing the Binance API. (Written in java)
From my own testings, it has the same results as the pine script.
private boolean checkHighOrLow(Candlestick candlestick , int lengthForCheck, int currentCandleIndex, boolean checkForHigh) {
double currentCandleStickClosePrice = Double.parseDouble(candlestick.getClose());
for (int i = 0; i < lengthForCheck; i++) {
double afterCandleStick = Double.parseDouble(candlestickList.get(currentCandleIndex + i + 1).getClose());
double beforeCandleStick = Double.parseDouble(candlestickList.get(currentCandleIndex - i - 1).getClose());
if(checkForHigh) {
if (afterCandleStick > currentCandleStickClosePrice)
return false;
if (beforeCandleStick > currentCandleStickClosePrice)
return false;
}else{
if(afterCandleStick < currentCandleStickClosePrice)
return false;
if(beforeCandleStick < currentCandleStickClosePrice)
return false;
}
}
return true;
}
public void findHighsAndLows(){
int lengthForCheck = 1;
int numOfCandles = candlestickList.size();
for(int i = lengthForCheck; i < numOfCandles - lengthForCheck; i ++)
{
Candlestick currentCandle = candlestickList.get(i);
if(checkHighOrLow(currentCandle,numOfCandles,lengthForCheck,i,true))
highs.add(currentCandle);
if(checkHighOrLow(currentCandle,numOfCandles,lengthForCheck,i,false))
lows.add(currentCandle);
}
}
Logic still applies. Enjoy
Result:
FOUND LOW | Wed Aug 25 04:20:00 IDT 2021
FOUND HIGH | Wed Aug 25 05:05:00 IDT 2021
FOUND LOW | Wed Aug 25 05:20:00 IDT 2021
FOUND HIGH | Wed Aug 25 05:30:00 IDT 2021
FOUND LOW | Wed Aug 25 05:35:00 IDT 2021
FOUND HIGH | Wed Aug 25 05:45:00 IDT 2021
FOUND LOW | Wed Aug 25 06:15:00 IDT 2021
FOUND HIGH | Wed Aug 25 06:25:00 IDT 2021
FOUND LOW | Wed Aug 25 06:35:00 IDT 2021
FOUND HIGH | Wed Aug 25 06:40:00 IDT 2021
FOUND LOW | Wed Aug 25 06:55:00 IDT 2021
FOUND HIGH | Wed Aug 25 07:05:00 IDT 2021
FOUND LOW | Wed Aug 25 07:25:00 IDT 2021
FOUND HIGH | Wed Aug 25 07:45:00 IDT 2021
FOUND LOW | Wed Aug 25 07:50:00 IDT 2021
FOUND HIGH | Wed Aug 25 08:20:00 IDT 2021
FOUND LOW | Wed Aug 25 08:25:00 IDT 2021
FOUND HIGH | Wed Aug 25 08:35:00 IDT 2021
FOUND LOW | Wed Aug 25 08:45:00 IDT 2021
FOUND HIGH | Wed Aug 25 08:50:00 IDT 2021
FOUND LOW | Wed Aug 25 09:15:00 IDT 2021
FOUND HIGH | Wed Aug 25 09:30:00 IDT 2021
FOUND LOW | Wed Aug 25 09:35:00 IDT 2021
FOUND HIGH | Wed Aug 25 09:40:00 IDT 2021
FOUND LOW | Wed Aug 25 09:55:00 IDT 2021
FOUND HIGH | Wed Aug 25 10:00:00 IDT 2021
FOUND LOW | Wed Aug 25 10:05:00 IDT 2021
FOUND HIGH | Wed Aug 25 10:15:00 IDT 2021
FOUND LOW | Wed Aug 25 10:45:00 IDT 2021
FOUND HIGH | Wed Aug 25 10:50:00 IDT 2021
FOUND LOW | Wed Aug 25 11:15:00 IDT 2021
FOUND HIGH | Wed Aug 25 11:20:00 IDT 2021
FOUND LOW | Wed Aug 25 11:35:00 IDT 2021
FOUND HIGH | Wed Aug 25 11:45:00 IDT 2021
FOUND LOW | Wed Aug 25 11:55:00 IDT 2021
FOUND HIGH | Wed Aug 25 12:15:00 IDT 2021
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | e2e4 |
Solution 2 | Mayank Jain |
Solution 3 | lvinnyl |
Solution 4 |