I have converted the code from Pine Script to Python, but I am facing some problems. I'm not good in Python.
import pandas as pd
import pandas_ta as ta
close = pd.read_csv('close-MATIC.csv')
def Z_4(src, length, smooth):
mean = ta.sma(src, length)
vlo = src - mean
std = ta.stdev(vlo, length)
value = (src - mean) / std
Wema = ta.ema(ta.ema(value, smooth), smooth)
return [Wema, value]
R4 = Z_4(close, 8, 5)
print(R4)
Sample close-MATIC.csv
:
1.1154
1.1133
1.114
1.1131666666666669
1.1158444444444446
1.1207629629629632
1.1231753086419756
1.1252835390946503
1.1277223593964336
1.1305149062642892
1.1350099375095262
1.1363399583396843
1.1352933055597896
1.1354622037065263
1.132108135804351
1.1314387572029008
1.1310258381352671
1.1331172254235113
1.1350114836156744
1.135007655743783
The original Pine Script:
f_zscore(_src, _length, _smooth)=>
_mean = sma(_src, _length)
_std = stdev(_src-_mean, _length)
_value = (_src - _mean) / _std
_dema = ema(ema(_value, _smooth), _smooth)
[_value, _dema]
smooth = input(3)
[z0, d0] = f_zscore(close, input(6), smooth)
The errors:
Traceback (most recent call last):
File "c:/Users/MO/OneDrive/Desktop/New folder (21)/TOP4.py", line 14, in <module>
R4= Z_4(close,8,5)
File "c:/Users/MO/OneDrive/Desktop/New folder (21)/TOP4.py", line 8, in Z_4
vlo=(src-mean)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\ops\common.py", line 65, in new_method
return method(self, other)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\arraylike.py", line 97, in __sub__
return self._arith_method(other, operator.sub)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 5982, in _arith_method
new_data = self._dispatch_frame_op(other, op, axis=axis)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 6008, in _dispatch_frame_op bm = self._mgr.apply(array_op, right=right)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\managers.py", line 425, in apply
applied = b.apply(f, **kwargs)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\blocks.py", line 378, in apply
result = func(self.values, **kwargs)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\ops\array_ops.py", line 189, in arithmetic_op
res_values = _na_arithmetic_op(lvalues, rvalues, op)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\ops\array_ops.py", line 149, in _na_arithmetic_op
result = _masked_arith_op(left, right, op)
File "C:\Users\MO\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\ops\array_ops.py", line 111, in _masked_arith_op
result[mask] = op(xrav[mask], y)
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'
I would be thankful and grateful for all the help.
CodePudding user response:
Here is your code fixed, but I'm not sure if the results are valid though:
import pandas as pd
import pandas_ta as ta
def Z_4(src, length, smooth):
mean = ta.sma(src, length)
vlo = src - mean
std = ta.stdev(vlo, length)
value = vlo / std
Wema = ta.ema(
ta.ema(value, smooth),
smooth
)
return [Wema, value]
close = pd.read_csv('close-MATIC.csv', header=None, names=['Sources'])
R4 = Z_4(close.Sources, 8, 5)
print(R4)
The main problem is that you are using a DataFrame
everywhere a Series
is expected.
For the sample CSV
below:
1.1154
1.1133
1.114
1.1131666666666669
1.1158444444444446
1.1207629629629632
1.1231753086419756
1.1252835390946503
1.1277223593964336
1.1305149062642892
1.1350099375095262
1.1363399583396843
1.1352933055597896
1.1354622037065263
1.132108135804351
1.1314387572029008
1.1310258381352671
1.1331172254235113
1.1350114836156744
1.135007655743783
The code will return the following:
[0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
8 NaN
9 NaN
10 NaN
11 NaN
12 NaN
13 NaN
14 -0.030459
15 -0.063999
16 -0.121392
17 -0.146357
18 -0.106888
19 -0.021776
Name: EMA_5, dtype: float64, 0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
8 NaN
9 NaN
10 NaN
11 NaN
12 NaN
13 NaN
14 -0.030459
15 -0.332315
16 -0.446383
17 -0.116503
18 0.308728
19 0.501244
dtype: float64]