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/
Related
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;
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))
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
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.
I'm working with TwinCAT 3 and ST to save data from a socket connection. The sockets work and parts of the saving too, but not all of it. The first array I try to save works fine. But if i want to save another one, it fails. The FB_FileClose doesn't go bBusy.
IF reset THEN // I reset the FB after it saved one array.
bSuccess := FALSE;
iError := 0;
step := 1;
reset := FALSE;
MEMSET(ADR(saveArray), 0, SIZEOF(saveArray));
RETURN;
END_IF
CASE step OF
1:
IF path = '' THEN
bSuccess := FALSE;
iError := 12;
step := 1;
END_IF
fbFileOpen.sPathName := path;
fbFileOpen.nMode := FOPEN_MODEAPPEND OR FOPEN_MODEPLUS;
fbFileOpen.bExecute := TRUE;
fbFileOpen.tTimeout := T#2S;
fbFileOpen();
step := 2;
2:
fbFileOpen(bExecute := FALSE);
IF NOT fbFileOpen.bBusy AND NOT fbFileOpen.bError THEN
step := 3;
ELSE
iError := fbFileOpen.nErrId;
END_IF
3:
fbWriteFile.hFile := fbFileOpen.hFile;
fbWriteFile.bExecute := TRUE;
fbWriteFile.pWriteBuff := ADR(saveArray);
fbWriteFile.cbWriteLen := SIZEOF(saveArray);
fbWriteFile.tTimeout := T#2S;
fbWriteFile();
step := 4;
4:
fbWriteFile(bExecute := FALSE);
IF NOT fbWriteFile.bBusy AND NOT fbWriteFile.bError THEN
step := 5;
END_IF
5:
fbCloseFile.hFile := fbFileOpen.hFile;
fbCloseFile.bExecute := TRUE;
fbCloseFile.tTimeout := T#3S;
fbCloseFile();
IF fbCloseFile.bBusy THEN //Gets suck here at the second run. And if I remove it, the FB doesn't get busy and doesn't close my hFile.
step := 6;
END_IF
6:
fbCloseFile(bExecute := FALSE);
IF NOT fbCloseFile.bBusy AND NOT fbCloseFile.bError THEN
bSuccess := TRUE;
ELSE
iError := fbCloseFile.nErrId;
END_IF
END_CASE
I also noticed that FB_FileOpen opens the same hFile two times in a row. The second one of it can't get closed from the FB_FileClose. The next run it gets a new hFile and then it can save the data. Next one it can't, and so on. What's my error in this one?
Thank you!
Found the solution by myself after a bit of hackaround. Before I set all the parameters for FB_FileOpen, FB_FileWrite and FB_FileClose I set the execute to false like this:
1:
IF path = '' THEN
bSuccess := FALSE;
iError := 12;
step := 1;
END_IF
fbFileOpen(bExecute := FALSE); // Set this to false before.
fbFileOpen.sPathName := path;
fbFileOpen.nMode := FOPEN_MODEAPPEND OR FOPEN_MODEPLUS;
fbFileOpen.bExecute := TRUE;
fbFileOpen.tTimeout := T#2S;
fbFileOpen();
step := 2;
Now it's working.