Trying To Convert a Pinescript Function From V4 to V5 But Stumped - pine-script-v5

I am trying to convert what seems like a simple function in V4 to V5 but getting nowhere. The docs seem relatively vague. Any ideas?
V4
bcwsma(s,l,m) =>
_s = s
_l = l
_m = m
_bcwsma = (_m*_s+(_l-_m)*nz(_bcwsma[1]))/_l
_bcwsma
V5
bcwsma(s,l,m) =>
_s = s
_l = l
_m = m
_bcwsma = (_m*_s+(_l-_m)*nz(_bcwsma[1]))/_l
_bcwsma
The readback in v5 is the infamous undeclared identifier error.
line 14: Undeclared identifier '_bcwsma'

Try this:
bcwsma(s, l, m) =>
_bcwsma = float(na)
_s = s
_l = l
_m = m
_bcwsma := (_m * _s + (_l - _m) * nz(_bcwsma[1])) / _l
_bcwsma

Related

How to convert from Pinescript version 2 to version 5

I have tried to convert the following from version 2 to version 5 without success. I have been getting various errors showing up. Also, the current converter on the Pine Script v5 User Manual doesn't go below Version 3. I would appreciate any help to do this. Much appreciated and thanks.
//#version=2
//Both fisher and macdl MTF
resCustom = input(title="Timeframe", type=resolution, defval="60" )
//--------macdl
src=close
shortLength = input(12, title="Fast Length")
longLength = input(26, title="Slow Length")
sigLength = input(9, title="Signal Length")
ma(s,l) => ema(s,l)
sema = ma( src, shortLength )
lema = ma( src, longLength )
i1 = sema + ma( src - sema, shortLength )
i2 = lema + ma( src - lema, longLength )
macdl = i1 - i2
macdl2 = security(tickerid, resCustom,macdl)
macd=sema-lema
//-------end
//---------fisher
len = input(34, minval=1, title="Fisher")
round_(val) => val > .99 ? .999 : val < -.99 ? -.999 : val
high_ = highest(hl2, len)
low_ = lowest(hl2, len)
value = round_(.66 * ((hl2 - low_) / max(high_ - low_, .001) - .5) + .67 * nz(value[1]))
fish1 = .5 * log((1 + value) / max(1 - value, .001)) + .5 * nz(fish1[1])
fish2 = security(tickerid, resCustom,fish1)
//------------end
sw1=iff(fish2<-6 and macdl2>macdl2[1],1,0)
sw2=iff(fish2>6 and macdl2<macdl2[1],-1,0)
final=sw1+sw2
swap=final==1 or final==-1?fuchsia:green
plot(fish2, color=swap, title="Fisher",style=histogram)
hline(0, color=orange)

Loop to open netcdf file and read variables matlab

i want to optimize my code, and I would like to get a loop for my netcdf files. These are the files I want to open and the variables I want to use:
hydrofile2 = 'C:\modelana\netcdf_2019\westcoms2_20190402_0002.nc';
hydrofile3 = 'C:\modelana\netcdf_2019\westcoms2_20190403_0003.nc';
hydrofile4 = 'C:\modelana\netcdf_2019\westcoms2_20190404_0004.nc';
hydrofile5 = 'C:\modelana\netcdf_2019\westcoms2_20190405_0005.nc';
hydrofile6 = 'C:\modelana\netcdf_2019\westcoms2_20190406_0006.nc';
hydrofile7 = 'C:\modelana\netcdf_2019\westcoms2_20190407_0007.nc';
hydrofile8 = 'C:\modelana\netcdf_2019\westcoms2_20190408_0008.nc';
hydrofile9 = 'C:\modelana\netcdf_2019\westcoms2_20190409_0009.nc';
hydrofile10 = 'C:\modelana\netcdf_2019\westcoms2_20190410_0010.nc';
hydrofile11 = 'C:\modelana\netcdf_2019\westcoms2_20190411_0011.nc';
hydrofile12 = 'C:\modelana\netcdf_2019\westcoms2_20190412_0012.nc';
hydrofile13 = 'C:\modelana\netcdf_2019\westcoms2_20190413_0013.nc';
hydrofile14 = 'C:\modelana\netcdf_2019\westcoms2_20190414_0014.nc';
hydrofile15 = 'C:\modelana\netcdf_2019\westcoms2_20190415_0015.nc';
%velocity
u2 = ncread(hydrofile2,'u');
u3 = ncread(hydrofile3,'u');
u4 = ncread(hydrofile4,'u');
u5 = ncread(hydrofile5,'u');
u6 = ncread(hydrofile6,'u');
u7 = ncread(hydrofile7,'u');
u8 = ncread(hydrofile8,'u');
u9 = ncread(hydrofile9,'u');
u10 = ncread(hydrofile10,'u');
u11 = ncread(hydrofile11,'u');
u12 = ncread(hydrofile12,'u');
u13 = ncread(hydrofile13,'u');
u14 = ncread(hydrofile14,'u');
u15 = ncread(hydrofile15,'u');
%salinity
s2 = ncread(hydrofile2,'salinity');
s3 = ncread(hydrofile3,'salinity');
s4 = ncread(hydrofile4,'salinity');
s5 = ncread(hydrofile5,'salinity');
s6 = ncread(hydrofile6,'salinity');
s7 = ncread(hydrofile7,'salinity');
s8 = ncread(hydrofile8,'salinity');
s9 = ncread(hydrofile9,'salinity');
s10 = ncread(hydrofile10,'salinity');
s11 = ncread(hydrofile11,'salinity');
s12 = ncread(hydrofile12,'salinity');
s13 = ncread(hydrofile13,'salinity');
s14 = ncread(hydrofile14,'salinity');
s15 = ncread(hydrofile15,'salinity');
%temperature
t2 = ncread(hydrofile2,'temp');
t3 = ncread(hydrofile3,'temp');
t4 = ncread(hydrofile4,'temp');
t5 = ncread(hydrofile5,'temp');
t6 = ncread(hydrofile6,'temp');
t7 = ncread(hydrofile7,'temp');
t8 = ncread(hydrofile8,'temp');
t9 = ncread(hydrofile9,'temp');
t10 = ncread(hydrofile10,'temp');
t11 = ncread(hydrofile11,'temp');
t12 = ncread(hydrofile12,'temp');
t13 = ncread(hydrofile13,'temp');
t14 = ncread(hydrofile14,'temp');
t15 = ncread(hydrofile15,'temp');
So far, I've just been able to do this:
%% add hydrofiles
clear
myFolder = ('C:\modelana\netcdf_2019\');
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
for k = 1:14
ncFilename = sprintf('westcoms2_20190402_0002.nc',k);
if isempty(ncFilename == 1)
continue;
else
ncfile=([myFolder ncFilename]);
s = ncread(ncfile,'salinity') ;
t = ncread(ncfile,'temp') ;
u=ncread(ncfile,'u');
end
end
So I just get one .nc file open with its corresponding variables. But what I want is to be able to get all the files and the variables by using the loop.
Thanks and apologize for this basic question
You almost have it, you are missing basic string concatenation/file reading.
You can concatenate strings as [str1, str2, 'random charaters']. e.g. you can do
['westcoms2_2019040', num2str(k), '_000', num2str(k) ,'.nc']
This you can do if you have obvious file formats and some constrains. If you want to read all of them, you can do
fileList = dir([myFolder '*.nc']);
To just get all files with that extension.
Also, remember to store variables independently. i.e.
t(k,:) = ncread(ncfile,'temp') ; % if it is an array
t{k} = ncread(ncfile,'temp') ; % if it is some complex structure

How do I use numba's "guvectorize" decorator to change two arrays in the same function?

I'm using numba's #guvectorize to change two different arrays. The code is:
#guvectorize([(int64[:], int64[:], int64[:], int64[:])], '(n),(n)->(n),(n)', target= 'parallel')
def g(x, y, res, res_two):
res = x
for i in range(x.shape[0]-1):
var = np.random.poisson((2),1)[0]
res_two[i] = var
res[i+1] = res[i] + res_two[i]
print("res[i+1] is", res[i+1], "for x[i] is", x[i])
q = (np.arange(5)) * 0
q[0] = 5
r = (np.arange(5)) * 0
g(q,r)
print("q is", q)
print("r is", r)
And the results printed out are:
As one can see, q is changing, but r isn't.
What must I do to use guvectorize to input two arrays and change those two arrays?

JS push operation does something funny to repeated array arguments

I don't understand js's push. It does this funny thing
var x = []
var y = [1]
x.push(y) // x is [[1]]
x.push(y) // x is [[1],[]] Why?
Using your code I get:
x is [[1],[1]]
as the output - run fiddle at bottom...
I have no idea how you could be getting a different output?
var x = [];
var y = [1];
x.push(y) // x is [[1]]
x.push(y) // x is [[1],[]]
var divOut = document.getElementById('divOut');
divOut.innerText = 'x is ' + JSON.stringify(x);
<div id="divOut"></div>

Undefined function 'log' for input arguments of type 'uint8'

i have been trying generate an image.
C1 = imread(InputImage);
NumberOfGrayLevels=32;
I= 0.299*C1(:,:,1)+0.587*C1(:,:,2)+0.114*C1(:,:,3);
C = 0;
I=(C*log(I+1))';
new=uint8(mat2gray(I)*(NumberOfGrayLevels-1));
[m,n]= size(new);
rgb = zeros(m,n,3);
rgb(:,:,1) = new;
rgb(:,:,2) = rgb(:,:,1);
rgb(:,:,3) = rgb(:,:,2);
new = rgb/256;
imshow(new,[]);
no9=figure;
image(new);
the error is showing at I=(C*log(I+1))';..can you tell me how to solve this?
Most probably C1 is of type uint8. You should convert it, i.e.:
C1 = imread(InputImage);
C1 = double(C1);
NumberOfGrayLevels = 32;
I = 0.299*C1(:,:,1) + 0.587*C1(:,:,2) + 0.114*C1(:,:,3);
.....
if you don't convert C1 to double, then I will also be of type uint8 - it will not contain what you want and log function will not work with it.