How to convert from Pinescript version 2 to version 5 - pine-script-v5

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)

Related

increasing the displayed indicator value by a percentage

i'm trying to find a way to make a indicator like rsi or stochastic but be able to adjust the output by a percentage. For example RSI signal that is increased by a percentage of what i would output by default. So it normally show 70 but is increased to 80 e.g.
Is there a way to do this? I've been scrolling through the manual for hours but couldnt find it.
Any help is appreciated
//#version=4
study(title="RSI", shorttitle="RSI", format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
rsiValue = rsi(src, len)
i_perc = input(10, title = "Percentage", type = input.float) * 0.01
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsi_increase_conditon = rsiValue >= rsiValue[1] * (1 + i_perc)
rsi_decrease_conditon = rsiValue <= rsiValue[1] * (1 - i_perc)
plot(rsi, "RSI", color=color.white)
hline(70, "OBBand", color=color.red, linestyle=hline.style_solid)
hline(20, "XOSBand", color=color.blue, linestyle=hline.style_solid)
hline(50, "Mid", color=color.gray, linestyle=hline.style_solid)
hline(80, "XOBBand", color=color.blue, linestyle=hline.style_solid)
hline(30, "OSBand", color=color.red, linestyle=hline.style_solid)
i_perc = input(10, title = "Percentage", type = input.float) * 0.01
...
rsi_increase_conditon = rsiValue >= rsiValue[1] * (1 + i_perc)
rsi_decrease_conditon = rsiValue <= rsiValue[1] * (1 - i_perc)
plotshape(rsi_increase_conditon and not rsi_increase_conditon[1], title = "RSI Increase %", style = shape.triangleup, location = location.belowbar, size = size.normal)
plotshape(rsi_decrease_conditon and not rsi_decrease_conditon[1], title = "RSI Decrease %", style = shape.triangledown, location = location.abovebar, size = size.normal)

How to find the first value of Bollinger Bands when bar open

Actually, the Bollinger Bands code is:
//#version=4
study(title="AAAA", shorttitle="AAAA", overlay=true)
len = 5
multi = 2
bb5med = sma(close, len)
devBB5 = mult2 * stdev(close, len)
bb5top = bb5med + devBB5
bb5bot = bb5med - devBB5
I would want to find the first value of those 3 lines when the new bar comes, means, when close==open.
Also, I need it to work when I change the len to 20, 50 and/or when I change the multi to 3
Please help me. Thank you.
//#version=5
indicator("BB Open", overlay = true)
len = input.int(20)
mult = input.float(2.000)
basis = (math.sum(close, len - 1)[1] + open) / len
float dev_sum = 0.0
for i = 1 to len - 1
dev_sum += math.pow(basis - close[i], 2)
dev_sum += math.pow(basis - open, 2)
stdev = math.sqrt(dev_sum / len)
up = basis + stdev * mult
dn = basis - stdev * mult
plot(basis, color = color.yellow)
plot(up)
plot(dn)
Function :
f_BBopen(_close, _open, _len, _mult) =>
_basis = (math.sum(_close, _len - 1)[1] + _open) / _len
float _dev_sum = 0.0
for i = 1 to _len - 1
_dev_sum += math.pow(_basis - _close[i], 2)
_dev_sum += math.pow(_basis - _open, 2)
_stdev = math.sqrt(_dev_sum / _len)
_up = _basis + _stdev * _mult
_dn = _basis - _stdev * _mult
[_basis, _up, _dn]
[basis, up, dn] = f_BBopen(close, open, len, mult)

Formula of Hijri date

What is the formula of converting "Time since epoch" stamp such as 1525249213 to Hijri date (Year - Month - Day)?
I know there would be one day inaccuracy which is OK for me.
The algorithm I found is to convert the Date to a Julian Date then convert the julian date to Hijri.
P.S. I can not remember where I found it. I am not responsible for any error or miss-accuracy or an consequence bla bla bla of using this code.
unsigned long gregorian_to_julian(const int gyear, const int gmonth, const int gday) {
if (gmonth < 3) {
gyear -= 1;
gmonth += 12;
}
auto a = int(gyear / 100.0f);
auto b = (gyear == 1582 && (gmonth > 10 || (gmonth == 10 && gday > 4)) ? -10 :
(gyear == 1582 && gmonth == 10 ? 0 :
(gyear < 1583 ? 0 : 2 - a + int(a / 4.0f))));
return int(365.25f * (gyear + 4716)) + int(30.6001f * (gmonth + 1)) + gday + b - 1524;
}
std::array<int,3> julian_to_hijri(const unsigned long julian_datestamp) {
std::array<int,3> result;
auto y = 10631.0f / 30.0f;
auto epoch_astro = 1948084;
auto shift1 = 8.01f / 60.0f;
auto z = julian_day - epoch_astro;
auto cyc = int(z / 10631.0f);
z = z - 10631 * cyc;
auto j = int((z - shift1) / y);
z = z - int(j * y + shift1);
result[0] = 30 * cyc + j; //Hijri Year
result[1]= int((z + 28.5001f) / 29.5f); //Hijri Month
if (result[1] == 13) {
result[1]= 12;
}
result[2] = z - int(29.5001f * result[1]- 29);// Hijri day
return result;
}
P.S. there will be +-1 one day error in the result Hijri date.

Encoding - What is this function?

I'm porting and updating an old app and I came across this function. I'd like to know more about it, but I don't actually know what it's called. I'm assuming it has a popular name. Does anyone know?
This version is in Python, although it was originally in Java.
def encode(msg): # msg is a string
msg_len = len(msg)
j = (msg_len + 6) / 7
k = 0
cbytesOutput = [ctypes.c_byte(0)]*(msg_len + j) # return is msg length + j bytes long
for l in xrange(j):
i1 = l * 8
j1 = i1
byte0 = ctypes.c_byte(-128)
byte1 = ctypes.c_byte(1)
k1 = 0
while k1 < 7 and k < msg_len:
byte2 = ctypes.c_byte(ord(msg[k]))
if (byte2.value & 0xffffff80) != 0:
byte0 = ctypes.c_byte(byte0.value | byte1.value)
j1 += 1
cbytesOutput[j1] = ctypes.c_byte(byte2.value | 0xffffff80)
byte1 = ctypes.c_byte(byte1.value << 1)
k += 1
k1 += 1
cbytesOutput[i1] = byte0
return cbytesOutput
Any comments on the algorithm in general? I'm thinking of replacing it. Profiler says it's the worst function in the entire app (it's 60% of the time on the slowest code path) and it bloats the data as well.
Thanks

Run time error 9 when using arrary in Macro

I have been using the followng Macro and it works fine:
Sub PremTable()
Dim i, m, j As Integer
Dim PDFDiv, PDFClass, PDFSex, PDFPlan, LimAge As Variant
Dim FlagD, FlagC, Band, FlagP, FlagB, IssAge, Dur As Integer
PDFClass = Array("N", "S")
PDFSex = Array("M", "F")
PDFDiv = Array("G", "E")
PDFPlan = Array(10, 20, 30)
LimAge = Array(70, 60, 50)
j = 0
For FlagD = 1 To 2
Range("div").Value = PDFDiv(FlagD)
For FlagP = 1 To 3
Range("plan").Value = PDFPlan(FlagP)
For Band = 1 To 3
Range("band").Value = Band
For FlagS = 1 To 2
Range("sex").Value = PDFSex(FlagS)
For FlagC = 1 To 2
Range("class").Value = PDFClass(FlagC)
m = 18
For i = 1 To Range("LimAge").Value - 17
Range("IssAge").Offset(i + j, 0) = m
Range("age").Value = Range("IssAge").Offset(i + j, 0)
Worksheets("input").Range("J4:J76").Copy
Worksheets("Premium Tables").Range("M1").Offset(i + j, 0).PasteSpecial xlPasteValues, Transpose:=True
Range("DIV2").Offset(i + j, 0) = Range("Div")
Range("PLAN2").Offset(i + j, 0) = Range("plan")
Range("BAND2").Offset(i + j, 0) = Range("band")
Range("SEX2").Offset(i + j, 0) = Range("sex")
Range("CLASS2").Offset(i + j, 0) = Range("class")
m = m + 1
Next i
j = j + i - 1
Next FlagC
Next FlagS
Next Band
Next FlagP
Next FlagD
End Sub
Now I have another very similar spreatsheet that I want to use this macro to creat tables, but it always give me the "run time error 9" for all of the arrays having text format variables (for example: Range("class").Value = PDFClass(FlagC) causing an runtime error 9)
Please advise! Thanks very much!