Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Develop Volatility Stop (Average True Range Trailing Stop) Indicator in Python

$
0
0

I recently started learning Python and trying to develop "Volatility Stop" indicator in Python from Trading View Pine Script.

Following is Trading View Pine Script.

//@version=5indicator("Volatility Stop", "VStop", overlay=true, timeframe="", timeframe_gaps=true)length = input.int(3, "Length", minval = 2)src = input.source(low, "Source")factor = input.float(2.0, "Multiplier", minval = 0.25, step = 0.25)volStop(src, atrlen, atrfactor) =>    if not na(src)        var max     = src        var min     = src        var uptrend = true        var float stop    = na        atrM        = nz(ta.atr(atrlen) * atrfactor, ta.tr)        max         := math.max(max, src)        min         := math.min(min, src)        stop        := nz(uptrend ? math.max(stop, max - atrM) : math.min(stop, min + atrM), src)        uptrend     := src - stop >= 0.0        if uptrend != nz(uptrend[1], true)            max    := src            min    := src            stop   := uptrend ? max - atrM : min + atrM        [stop, uptrend][vStop, uptrend] = volStop(src, length, factor)plot(vStop, "Volatility Stop", style=plot.style_cross, color= uptrend ? #009688 : #F44336)

Following is Python Function.

def Volatility_Stop(Data_Column, High_Price, Low_Price, Close_Price, window, Multiplier):  TR = np.max(pd.concat([(High_Price - Low_Price), abs(High_Price - Close_Price.shift(1)), abs(Low_Price - Close_Price.shift(1))], axis=1), axis=1)  ATR = TR.ewm(alpha=1/window, min_periods=window, adjust=False).mean()  ATR_Multiplier = ATR * Multiplier  ATR_Multiplier = ATR_Multiplier.fillna(TR).tolist()  Up_Trend_List = []  Volatility_Stop_List = []  Max_Price_List = []  Min_Price_List = []  for i in range(0, len(Data_Column)):    if i == 0:      Up_Trend = True      Volatility_Stop = Data_Column[i]      max_price = Data_Column[i]      min_price = Data_Column[i]      Up_Trend_List.append(Up_Trend)      Volatility_Stop_List.append(Volatility_Stop)      Max_Price_List.append(max_price)      Min_Price_List.append(min_price)    if i > 0:      Up_Trend = Data_Column[i] - Volatility_Stop_List[i-1]      if Up_Trend >= 0:        max_price = max(Max_Price_List[i-1], Data_Column[i])        Volatility_Stop = max(Volatility_Stop_List[i-1], max_price - ATR_Multiplier[i])      else:        min_price = min(Min_Price_List[i-1], Data_Column[i])        Volatility_Stop = min(Volatility_Stop_List[i-1], min_price + ATR_Multiplier[i])      if np.isnan(Volatility_Stop) == True:        Volatility_Stop = Data_Column[i]      if Up_Trend != Up_Trend_List[i-1]:        max_price = Data_Column[i]        min_price = Data_Column[i]        if Up_Trend >= 0:          Volatility_Stop = max_price - ATR_Multiplier[i]        else:          Volatility_Stop = min_price + ATR_Multiplier[i]      Up_Trend_List.append(Up_Trend)      Volatility_Stop_List.append(Volatility_Stop)      Max_Price_List.append(max_price)      Min_Price_List.append(min_price)  Volatility_Stop_List = pd.Series(Volatility_Stop_List, index=Data_Column.index)  return Volatility_Stop_List

Python Function Output is NOT matching with Trading View. Only few values (during down tread) is matching.

Following is DataFrame with Python Function and Trading View column.

data['Python Function Output'] = Volatility_Stop(data['Low'], data['High'], data['Low'], data['Close'], 3, 2)
Date Open High Low Close Python Function Output DESIRE Pine Script Output
2018-01-23      9,622.00      9,622.00      9,300.00      9,301.00   9,300.000000   9,300.000000
2018-01-24      9,176.00      9,534.00      9,036.20      9,344.40   9,534.000000   9,534.000000
2018-01-25      9,162.00      9,426.00      9,050.00      9,340.00   9,808.133247   9,534.000000
2018-01-26      9,042.00      9,173.60      8,588.00      8,657.60   9,594.755498   9,534.000000
2018-01-29      8,729.60      8,744.00      8,400.00      8,400.00   9,300.503665   9,317.854815
2018-01-30      8,910.00      8,924.00      8,528.00      8,670.00   9,477.669110   9,317.854815
2018-01-31      8,396.00      8,582.60      8,310.00      8,559.80   9,183.112740   9,190.824362
2018-02-01      8,934.00      8,934.00      8,400.00      8,524.00   9,338.075160   9,190.824362
2018-02-02      8,926.00      9,326.00      8,737.40      9,300.20   9,897.450497   9,190.824362
2018-02-05      9,818.00    10,460.00      9,173.60    10,460.00 10,804.566608   9,190.824362
2018-02-06    10,700.00    11,554.00      9,598.00      9,604.00 11,989.311332   7,205.165558
2018-02-07      9,580.00      9,792.00      9,233.20      9,656.00 11,199.940953   7,630.243705
2018-02-08      9,640.00    10,627.00      8,574.00    10,627.00 11,253.827172   7,630.243705
2018-02-09      9,900.00    11,200.00      9,850.00    10,175.80 12,536.551448   7,630.243705
2018-02-12      9,527.40    10,100.00      9,527.40      9,606.00 11,750.700965   7,630.243705
2018-02-13      9,776.00      9,776.00      9,023.00      9,097.80 11,007.200383   7,865.643201
2018-02-14      8,850.00      8,850.00      8,340.00      8,352.00 10,168.000125   8,021.895467
2018-02-15      8,126.00      8,391.80      7,878.00      7,878.00   9,439.199953   9,434.269688
2018-02-16      7,978.00      8,125.80      7,867.00      8,019.60   9,080.333172   9,080.353126
2018-02-20      8,162.00      8,162.00      7,684.00      8,000.00   8,811.555448   8,811.622084
2018-02-21      7,800.00      7,840.00      7,420.20      7,840.00   8,558.437030   8,558.481389
2018-02-22      8,200.00      8,200.00      7,686.00      7,893.60   8,787.491223   8,521.720926
2018-02-23      7,760.00      7,760.00      7,484.60      7,490.20   8,491.594247   8,427.227284
2018-02-26      7,400.00      7,656.00      7,262.00      7,266.00   8,195.996099   8,196.018189
2018-02-27      7,266.80      7,564.00      7,260.00      7,550.00   8,085.330733   8,085.345460
2018-02-28      7,407.00      7,656.00      7,407.00      7,656.00   8,123.220489   7,976.230306
2018-03-01      7,760.00      8,312.00      7,650.00      8,026.00   8,568.813659   7,976.230306
2018-03-02      8,370.00      8,600.00      7,707.00      7,800.00   8,914.875773   7,976.230306
2018-03-05      7,947.80      7,947.80      7,264.00      7,266.00   8,525.117052   7,976.230306
2018-03-06      7,188.00      7,241.00      7,015.20      7,074.00   8,023.144766   7,976.230306
2018-03-07      7,233.20      7,262.00      6,928.40      6,944.00   7,822.763015   7,822.815349
2018-03-08      6,860.00      7,097.80      6,860.00      7,050.00   7,614.775278   7,614.756899
2018-03-09      7,122.00      7,122.00      6,632.00      6,632.00   7,461.850185   7,461.837933
2018-03-12      6,484.00      6,502.00      6,377.60      6,425.60   7,100.433489   7,100.438622
2018-03-13      6,378.00      6,886.20      6,378.00      6,850.00   7,198.689058   7,100.438622
2018-03-14      6,732.00      6,732.00      6,470.00      6,600.00   7,270.459372   7,100.438622
2018-03-15      6,512.40      6,675.20      6,453.60      6,520.20   7,134.973077   7,058.952184
2018-03-16      6,669.80      6,698.00      6,614.00      6,631.00   7,186.781856   6,950.381456
2018-03-19      6,998.00      7,376.00      6,890.00      7,200.00   7,768.521238   6,950.381456
2018-03-20      7,342.00      7,501.00      7,072.20      7,328.00   7,943.747557   6,200.679353
2018-03-21      7,411.00      7,411.00      7,104.00      7,330.00   7,889.698241   6,318.359569
2018-03-22      7,771.80      8,066.00      7,526.20      8,040.00   8,540.665689   6,511.773046
2018-03-23      8,040.00      8,633.00      8,040.00      8,600.00   9,111.643663   6,661.748697
2018-03-26      7,986.00      8,812.00      7,737.00      7,737.00   9,168.095775   6,661.748697
2018-03-27      7,640.00      9,258.00      7,640.00      8,999.40   9,672.730517   6,661.748697
2018-03-28      9,580.00    10,010.80      9,130.00      9,707.60 11,159.419954   7,064.265540
2018-03-29      9,998.00    10,000.00      8,756.00      8,950.00 10,938.279969   7,064.265540
2018-04-02      9,420.00    10,168.00      9,230.40      9,846.00 11,497.253703   7,064.265540
2018-04-03      9,426.00    10,060.00      9,282.60      9,646.40 11,312.102078   7,242.232012
2018-04-04    10,358.00    10,384.00      9,154.00      9,154.00 11,327.001646   7,242.232012
2018-04-05      8,832.00      8,943.20      8,625.80      8,760.00 10,426.601032   7,476.923116
2018-04-06      8,970.00      9,466.00      8,762.80      9,306.00 10,434.000623   7,608.128744
2018-04-09      9,006.00      9,200.00      8,712.00      9,200.00 10,222.133879   7,770.265829
2018-04-10      8,758.00      8,838.00      8,180.00      8,220.00   9,866.755919   7,770.265829
2018-04-11      8,404.00      8,960.00      8,114.20      8,372.00   9,802.570678   7,770.265829
2018-04-12      8,200.00      8,298.00      8,090.00      8,209.80   9,403.580322   7,968.378764
2018-04-13      8,020.00      8,420.00      7,988.80      8,307.00   9,151.986816   9,152.400824
2018-04-16      8,200.00      8,496.00      8,200.00      8,304.00   9,172.791341   8,961.893883
2018-04-17      8,000.00      8,002.00      7,333.80      7,382.20   8,629.127496   8,629.269255
2018-04-18      7,382.00      7,457.80      7,242.00      7,320.60   8,249.418330   8,249.526170
2018-04-19      7,366.20      7,570.00      7,268.00      7,338.80   8,140.945554   8,115.004113
2018-04-20      7,457.00      7,800.00      7,400.00      7,720.00   8,289.430499   8,115.004113
2018-04-23      7,662.00      8,100.00      7,594.00      8,014.20   8,524.286999   8,115.004113
2018-04-24      7,927.60      8,800.00      7,860.00      8,555.60   9,106.858000   8,115.004113
2018-04-25      8,458.00      9,291.80      8,458.00      8,757.80   9,845.105203   7,070.856471
2018-04-26      8,302.00      8,302.00      7,852.20      7,999.60   9,380.670070   7,070.856471
2018-04-27      7,526.00      8,304.00      7,400.00      7,720.20   9,021.646583   7,070.856471
2018-04-30      7,600.00      7,882.20      7,442.00      7,661.60   8,816.564519   7,083.358955
2018-05-01      7,828.00      7,848.00      7,504.00      7,514.00   8,649.709679   7,312.239303
2018-05-02      7,324.00      7,440.80      7,080.20      7,300.00   8,133.206518   8,133.240465
2018-05-03      7,600.00      7,820.00      7,337.00      7,337.00   8,385.670882   8,128.893643
2018-05-04      7,594.00      7,594.00      6,876.40      6,906.00   8,053.913889   8,053.915762
2018-05-07      6,713.80      6,713.80      6,440.00      6,580.00   7,535.675991   7,535.703841
2018-05-08      6,600.00      6,648.00      6,400.00      6,439.40   7,295.783994   7,295.789228
2018-05-09      6,400.00      6,400.00      6,200.00      6,215.00   6,956.789264   6,956.766152
2018-05-10      6,204.00      6,204.00      5,952.00      6,000.00   6,631.859509   6,631.844101
2018-05-11      5,990.00      6,201.80      5,990.00      6,124.00   6,584.439543   6,546.376067
2018-05-14      6,072.00      6,072.00      5,880.00      6,052.00   6,438.959695   6,438.917378
2018-05-15      6,270.00      6,420.00      6,270.00      6,378.00   6,887.973130   6,438.917378
2018-05-16      6,286.00      6,344.20      6,182.00      6,220.00   6,724.648753   6,422.616613
2018-05-17      6,228.00      6,336.00      6,044.00      6,271.80   6,600.432502   6,422.616613
2018-05-18      6,400.00      6,590.00      6,385.40      6,590.00   6,968.488367   6,422.616613
2018-05-21      6,260.00      6,580.00      6,206.00      6,451.00   6,850.725643   6,422.616613
2018-05-22      6,400.00      6,626.80      6,347.80      6,626.80   6,963.616900   6,422.616613
2018-05-23      6,800.00      6,800.00      6,344.60      6,344.60   7,058.744763   6,422.616613
2018-05-24      6,344.00      6,446.60      6,260.00      6,320.00   6,860.496509   6,422.616613
2018-05-25      6,274.00      6,274.00      6,172.00      6,216.00   6,670.997672   6,378.964978
2018-05-29      6,258.00      6,321.80      6,080.00      6,210.00   6,573.864985   6,373.843319
2018-05-30      6,126.20      6,248.00      6,070.00      6,070.00   6,517.909990   6,327.895546
2018-05-31      6,054.00      6,096.00      5,955.00      6,035.00   6,347.606660   6,272.597030
2018-06-01      5,928.00      5,928.00      5,600.00      5,615.00   6,151.737773   6,151.664687
2018-06-04      5,521.60      5,521.60      5,277.00      5,292.00   5,870.158516   5,870.176458
2018-06-05      5,202.00      5,236.00      5,090.00      5,172.40   5,620.105677   5,620.090972
2018-06-06      5,100.20      5,212.00      5,030.00      5,030.00   5,504.737118   5,504.727315
2018-06-07      5,070.00      5,268.00      4,997.20      5,160.00   5,494.224810   5,494.204876
2018-06-08      5,259.00      5,262.00      5,078.20      5,111.80   5,532.083142   5,451.056584
2018-06-11      5,108.00      5,108.00      4,952.20      4,970.00   5,361.188566   5,361.251056
2018-06-12      4,870.00      4,878.00      4,692.00      4,788.00   5,149.992247   5,150.034037
2018-06-13      4,758.00      4,758.00      4,605.80      4,680.00   5,032.594766   5,032.596025
2018-06-14      4,615.80      4,615.80      4,363.20      4,385.00   4,858.930039   4,859.004017

Please help....


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>