The Target specified in strategy.exit is not working in the below Pinescript code - range

The Targets specified in "strategy.exit" is not working in the below Pinescript code. Please pardon if too many errors- this is my 1st Pinescript code.Please help.
Basically, the exit targets specified in the strategy.exit line are not computing.
//#version=5
strategy("MyFirstCode")
var int numBars = 1
var bool isLong=false
var bool isShort=false
var int countTriggered = 0
t = time('D')
if t == t[1]
numBars := nz(numBars[1]) + 1
buyPoint=ta.highest(numBars)
sellPoint=ta.lowest(numBars)
cond1= numBars>9
cond2= numBars<19
cond3= buyPoint>high[numBars-1]
cond4= sellPoint<low[numBars-1]
cond5= close>low[numBars-1]
cond6= close<high[numBars-1]
cond7= not isLong
cond8= not isShort
entryCond= cond1 and cond2 and cond3 and cond4 and cond5 and cond6 and cond7 and cond8
if entryCond
countTriggered := nz(countTriggered[1])+1
if countTriggered==1
rangeHigh=buyPoint
rangeLow=sellPoint
rnge=buyPoint-sellPoint
slLong=sellPoint-0.50
slShort=buyPoint+0.50
tgtLong=0.95*rnge+buyPoint
tgtShort=sellPoint-0.95*rnge
strategy.entry("Long",strategy.long, stop=buyPoint+0.05, oca_name="x", oca_type=strategy.oca.cancel,when= countTriggered==1)
strategy.entry("Short",strategy.short, stop=sellPoint-0.05, oca_name="x", oca_type=strategy.oca.cancel,when= countTriggered==1)
strategy.exit("Long Exit",from_entry="Long", limit=tgtLong, stop=slLong,when= countTriggered==1)
strategy.exit("Short Exit",from_entry="Short", limit=tgtShort, stop=slShort,when= countTriggered==1)
isLong := (strategy.position_size > 0)
isShort := (strategy.position_size < 0)
strategy.cancel_all(numBars==72 and (isLong or isShort))
strategy.close("Long",numBars==72 and isLong)
strategy.close("Short",numBars==72 and isShort)
else
numBars := 1
isLong :=false
isShort :=false
countTriggered :=0

Related

Pine script issues on checking alert conditions when breaching a Fibonacchi line

I am trying to set an alert when two conditions are met. For the UP case, I want to check first for the price to break above a certain level (UPLabel is true), then I want to set off the alarm when the price is breaching the Fib line (BreachUpFiboAlert is true). Somehow, the code is not working properly after I tried to check the conditions. I am quite new to the Pine Script so, any pointers will be greatly appreciated.
if barstate.isconfirmed and nySession == false and nyaSession == false and nyeSession == false
HHcheck = high[LastBar-1]
LLcheck = low[LastBar-1]
HHidrcheck = close[LastBar-1]
LLidrcheck = open[LastBar-1]
HfiboVal = HHidrcheck
LfiboVal = LLidrcheck
if useDRforFibo
HfiboVal := HHcheck
LfiboVal := LLcheck
if close[LastBar-1] > open[LastBar-1]
HHidrcheck := close[LastBar-1]
LLidrcheck := open[LastBar-1]
else
HHidrcheck := open[LastBar-1]
LLidrcheck := close[LastBar-1]
for i = 0 to FirstBar-LastBar
if high[LastBar+i] > HHcheck
HHcheck := high[LastBar+i]
if low[LastBar+i] < LLcheck
LLcheck := low[LastBar+i]
if close[LastBar+i] > open[LastBar+i] and close[LastBar+i] > HHidrcheck
HHidrcheck := close[LastBar+i]
if close[LastBar+i] < open[LastBar+i] and open[LastBar+i] > HHidrcheck
HHidrcheck := open[LastBar+i]
if close[LastBar+i] > open[LastBar+i] and open[LastBar+i] < LLidrcheck
LLidrcheck := open[LastBar+i]
if close[LastBar+i] < open[LastBar+i] and close[LastBar+i] < LLidrcheck
LLidrcheck := close[LastBar+i]
if UseWick
if high > (HHcheck + ((HHcheck/100)*BreachThreshold)) and high[1] <= HHcheck
if not UPLabel and not DOWNLabel
UPLabel := true
BreachesUP := BreachesUP+1
if low < (LLcheck - ((LLcheck/100)*BreachThreshold)) and low[1] >= LLcheck
if not UPLabel and not DOWNLabel
DOWNLabel := true
BreachesDown := BreachesDown+1
else
if close > (HHcheck + ((HHcheck/100)*BreachThreshold)) and close[1] <= HHcheck
if not UPLabel and not DOWNLabel
UPLabel := true
BreachesUP := BreachesUP+1
if close < (LLcheck - ((LLcheck/100)*BreachThreshold)) and close[1] >= LLcheck
if not UPLabel and not DOWNLabel
DOWNLabel := true
BreachesDown := BreachesDown+1
for i = 0 to LastBar
if UseWick
if high[i] > (HHcheck + ((HHcheck/100)*BreachThreshold)) and high[i+1] <= HHcheck
BreachesUP := BreachesUP+1
if low[i] < (LLcheck - ((LLcheck/100)*BreachThreshold)) and low[i+1] >= LLcheck
BreachesDown := BreachesDown+1
// add check if the wick is breaching the fiboAlertLevels
if high[i] > LfiboVal + ((HfiboVal-LfiboVal)*fiboDownAlertLevel) and high[i+1] <= LfiboVal + ((HfiboVal-LfiboVal)*fiboDownAlertLevel)
BreachesDownFiboAlert := BreachesDownFiboAlert+1
BreachDownFiboAlert := true
if low[i] < LfiboVal + ((HfiboVal-LfiboVal)*fiboUpAlertLevel) and low[i+1] >= LfiboVal + ((HfiboVal-LfiboVal)*fiboUpAlertLevel)
BreachesUpFiboAlert := BreachesUpFiboAlert+1
BreachUpFiboAlert := true
else
if close[i] > (HHcheck + ((HHcheck/100)*BreachThreshold)) and close[i+1] <= HHcheck
BreachesUP := BreachesUP+1
if close[i] < (LLcheck - ((LLcheck/100)*BreachThreshold)) and close[i+1] >= LLcheck
BreachesDown := BreachesDown+1
// add check if the price close is breaching the fiboAlertLevels
if DOWNLabel
if close[i] > LfiboVal + ((HfiboVal-LfiboVal)*fiboDownAlertLevel) and close[i+1] <= LfiboVal + ((HfiboVal-LfiboVal)*fiboDownAlertLevel)
BreachesDownFiboAlert := BreachesDownFiboAlert+1
BreachDownFiboAlert := true
if UPLabel
if close[i] < LfiboVal + ((HfiboVal-LfiboVal)*fiboUpAlertLevel) and close[i+1] >= LfiboVal + ((HfiboVal-LfiboVal)*fiboUpAlertLevel)
BreachesUpFiboAlert := BreachesUpFiboAlert+1
BreachUpFiboAlert := true
I attached a screenshot describing what I want to do. I also tried to plotshape per the code below on the graph to visualise it. Unfortunately, the plot showed that the condition, BreachUp&Down, check doesn't work.
plotshape(BreachUpFiboAlert, title="Breach Fib Up Alert", text="", style=shape.triangleup, location=location.belowbar, color=color.rgb(8, 73, 253), textcolor=color.white) plotshape(BreachDownFiboAlert, title="Breach Fib Down Alert", text="", style=shape.triangledown, location=location.abovebar, color=color.rgb(255, 1, 213), textcolor=color.white)
I'm suspecting that the for loop causing me this problem but I am not quite sure how to fix this. Note that you can look at the entire code here. https://www.tradingview.com/script/K2NPoa5y-Lukenum-TheMas7er-V-4-5min-promuckaj/

Pascal Calculator

I am now developing a calculator in Pascal and I am getting this error that while the exponent is negative the result is always 0. Can someone help me with this?
Here is the piece of code for this function that I wrote so far:
5: Begin
clrscr;
Writeln('POWER');
Write('Base: ');
Readln(pot1);
Write('Exponent: ');
Readln(pot2);
res5 := 1;
if pot2 < 0 then
Begin
pot1 := 1 div pot1;
pot2 := -pot2;
res5 := 1 / res5;
End;
while pot2 > 0 do
Begin
res5 := res5 * pot1;
pot2 := pot2 - 1;
End;
Writeln('The result is: ', res5:0:4);
Readkey;
End;

How to find the bar which is the highest high green bar within 5 bars, excluding the current bar?

I would like to find the green bar which is the highest high bar of last 5 bars, and not the current bar (the current bar may be the highest green one, so I need to exclude it)
I tried this but it seems not correct
find = high == highest(high, 5) and close > open and bar_index > 0 and bar_index < 6
numb = barssince(find)
high_of_that_bar = high[numb]
low_of_that_bar = low[numb])
Thank you for helping me
This should do what you want.
//#version=5
indicator("My script")
i_bars = input.int(5, "Bars back")
var float green_hi = 0
var float green_lo = 0
var label lbl = label.new(na, na, "")
if barstate.islast
green_hi := 0
for i = 1 to i_bars
if close[i] > open[i] and high[i] > green_hi
green_hi := high[i]
green_lo := low[i]
label.set_xy(lbl, bar_index+1, high)
label.set_text(lbl, str.format("green within last {2} bars\nhigh = {0}\nlow = {1}", green_hi, green_lo, i_bars))
This shows the fractional bar-index for intraday timeframes
//#version=5
indicator("My script")
i_bars = input.int(5, "Bars back")
var float green_hi = 0
var float green_lo = 0
var label lbl = label.new(na, na, "")
var int mybarindex = na
var int mybar = na
var int myi = na
if barstate.islast
green_hi := 0
for i = 1 to i_bars
if close[i] > open[i] and high[i] > green_hi
green_hi := high[i]
green_lo := low[i]
mybarindex := bar_index
mybar := bar_index - i
myi := i
label.set_xy(lbl, bar_index+1, high)
label.set_text(lbl, str.format("green within last {2} bars\nhigh = {0,number,#.#######}\nlow = {1,number,#.#######}\nmybar = {3}\nmybarindex = {4}\nmyi = {5}", green_hi, green_lo, i_bars, mybar, mybarindex, myi))
I got a second opinion, and it seems bar_index is not fractional after all.
str.format() just separates thousands with commas.
This makes it clear:
//#version=5
indicator("My script")
i_bars = input.int(5, "Bars back")
var float green_hi = 0
var float green_lo = 0
var label lbl = label.new(na, na, "")
var int mybarindex = na
var int mybar = na
var int myi = na
if barstate.islast
green_hi := 0
for i = 1 to i_bars
if close[i] > open[i] and high[i] > green_hi
green_hi := high[i]
green_lo := low[i]
mybarindex := bar_index
mybar := bar_index - i
myi := i
label.set_xy(lbl, bar_index+1, high)
label.set_text(lbl, str.format("green within last {2} bars\nhigh = {0,number,#.#######}\nlow = {1,number,#.#######}\nmybar = {3}\nmybarindex = {4}\nmyi = {5}", green_hi, green_lo, i_bars, mybar, mybarindex, myi))
if barstate.islastconfirmedhistory
label.new(bar_index - 10, high, str.format("{0}", bar_index + 0.1))

PineScript - zigzag trend change counter

I've made an indicator that is similiar to zigzag. I want to write a formula that will count number of up trends or number of trend changes (from up to down and from down to up). I have problem with it, because my variable is still setting to 0. Could you help me to correct it?
//#version=3
study("ZigZag Poker", overlay=true)
//INPUTS
trend = 0
trend := na(trend[1]) ? 1 : trend[1] //Beggining trend set to up
LL = 0.0
LL := na(LL[1]) ? low : LL[1] //LastLow
HH = 0.0
HH := na(HH[1]) ? high : HH[1] //LastHigh
LO = 0.0
LO := na(LO[1]) ? open : LO[1] //LastOpen
LC = 0.0
LC := na(LC[1]) ? close : LC[1] //LastClose
LOLO = 0.0
LOLO := na(LOLO[1]) ? low : LOLO[1] //LowestLow
HIHI = 0.0
HIHI := na(HIHI[1]) ? high : HIHI[1] //HighestHigh
zigzag = na
kolor = 0 //variable that counts number of trend changes
imp = input(true, "Alt imp")
kolor := imp == true ? 2 : 0
if (trend > 0) // trend is up, look for new swing low
if close >= min(LO, LC)
LC := close
LL := low
LO := open
LOLO := low
HIHI := high
else
zigzag := HIHI
trend := -1
HH := high
HIHI := high > HIHI ? high : HIHI
LC := close
LL := low
LO := open
LOLO := low
kolor := kolor[1] + 1
else // trend is down, look for new swing high
if close <= max(LO, LC)
HH := high
HIHI := high
LC := close
LL := low
LO := open
LOLO := low < LOLO ? low : LOLO
else
zigzag := LOLO
trend := 1
HH := high
LC := close
LL := low
LO := open
kolor: = kolor[1] + 1
plot(kolor)
plot(zigzag, color = trend < 0 ? blue : orange, linewidth=2, offset=-1)
I know it's too late to help the OP, but the error is in the line
kolor := imp == true ? 2 : 0, that always sets the value of kolor to 2 or 0, for all the candles that are in the current trend.
What is missing is copying the last kolor's value on every loop, so kolor[1] can have a valid counter.
Replacing that line with kolor := na(kolor[1])? 0: kolor[1] will do it.

Got "Boolean" expected "LongInt" pascal

I get this error on my insertion sort algorithm:
insertionsort.lpr(19,17) Error: Incompatible types: got "Boolean" expected "LongInt"
Here's the line 19 of my code
while j > 0 and A[j]>key do
I have tried googling all over the internet but i couldn't find any syntax errors or anything.
Here's the full code if it helps :
program instert;
uses crt;
const
N = 5;
var
i:integer;
j:integer;
key:integer;
A : Array[1..N] of Integer;
procedure insertionsort;
begin
for i := 2 to N do
begin
key := A[1];
j:= i - 1;
while j > 0 and A[j]>key do
begin
A[j+1] := A[j] ;
j := j-1;
end;
A[j+1] := key ;
end;
end;
begin
A[1]:= 9;
A[2]:= 6;
A[3]:= 7;
A[4]:= 1;
A[5]:= 2;
insertionsort;
end.
I also get the same error on the bubble sort algorithm i did. Here's the error line
bubblesort.lpr(26,14) Error: Incompatible types: got "Boolean" expected "LongInt"
Here's line 26 of my algorithm:
until flag = false or N = 1 ;
Here's the full code:
program bubblesort;
uses crt;
var
flag:boolean;
count:integer;
temp:integer;
N:integer;
A : Array[1..N] of Integer;
procedure bubblesort ;
begin
Repeat
flag:=false;
for count:=1 to (N-1) do
begin
if A[count] > A[count + 1] then
begin
temp := A[count];
A[count] := A[count + 1];
A[count] := temp;
flag := true;
end;
end;
N := N - 1;
until flag = false or N = 1 ;
end;
begin
A[1]:= 9;
A[2]:= 6;
A[3]:= 7;
A[4]:= 1;
A[5]:= 2;
N := 5;
bubblesort;
end.
In Pascal, boolean operators and and or have higher precedence than the comparison operators >, =, etc. So in the expression:
while j > 0 and A[j] > key do
Given that and has higher precedence, Pascal sees this as:
while (j > (0 and A[j])) > key do
0 and A[j] are evaluated as a bitwise and (since the arguments are integers) resulting in an integer. Then the comparison, j > (0 and A[j]) is evaluated as a boolean result, leaving a check of that with > key, which is boolean > longint. You then get the error that a longint is expected instead of the boolean for the arithmetic comparison.
The way to fix it is to parenthesize:
while (j > 0) and (A[j] > key) do ...
The same issue applies with this statement:
until flag = false or N = 1 ;
which yields an error because or is higher precedence than =. So you can parenthesize:
until (flag = false) or (N = 1);
Or, more canonical for booleans:
until not flag or (N = 1); // NOTE: 'not' is higher precedence than 'or'
When in doubt about the precedence of operators, parenthesizing is a good idea, as it removes the doubt about what order operations are going to occur.