Compilation error. Line 10: Extraneous input ':' expecting 'end of line without line continuation' - pine-script-v5

I keep getting compilation errors, I am new to pinescript and cant figure out where I am going wrong.
//#version=5
// Define the RSI period
period = 14
// Calculate the RSI
rsi = rsi(close, period)
// Check for bullish divergence
if (rsi < 30) and (rsi < rsi[1]) and (close > close[1]):
# Bullish divergence detected, go long
strategy.entry("Long", long=true)
strategy.exit("Exit long", "Long", profit=50, stop=-50)
// Check for bearish divergence
if (rsi > 70) and (rsi > rsi[1]) and (close < close[1]):
# Bearish divergence detected, go short
strategy.entry("Short", short=true)
strategy.exit("Exit short", "Short", profit=50, stop=-50)

You error comes from the indentation of your 'if' block.
Change you indentation and your code to :
//#version=5
// Define the RSI period
period = 14
rsi = ta.rsi(close, period)
// Check for bullish divergence
if (rsi < 30) and (rsi < rsi[1]) and (close > close[1])
// Bullish divergence detected, go long
strategy.entry("Long", strategy.long)
strategy.exit("Exit long", "Long", profit=50, stop=-50)
// Check for bearish divergence
if (rsi > 70) and (rsi > rsi[1]) and (close < close[1])
// Bearish divergence detected, go short
strategy.entry("Short", strategy.short)
strategy.exit("Exit short", "Short", profit=50, stop=-50)

Related

How do I use both stop loss and take profit in pinescript v5 ? (Three methods)

I'd like to get the stop loss and take profit to trigger and print on the chart. The stop loss and take profit should both be set to 1% from the entry for both long and short positions.
Method 1: Initiate the take profit order immediately after entry
if longCondition
strategy.entry("BB Long", strategy.long)
strategy.exit("EXIT LONG WIN", from_entry="BB Long", limit=high * 1.01)
Can I initiate both the stop loss and take profit orders in the same way, immediately after the entry? Example:
if longCondition
strategy.entry("BB Long", strategy.long)
strategy.exit("EXIT LONG WIN", from_entry="BB Long", limit=high * 1.01)
strategy.exit("EXIT LONG STOP", from_entry="BB Long", stop=open * 0.99)
So far I'm not able to get it working with method 1 for both stop loss and take profit.
Method 2: I've seen this example in a few scripts. If I can't use both takeprofit and stop loss in method 1, when would I need to use this instead?
if (strategy.position_size > 0)
strategy.exit("EXIT LONG STOP", from_entry="BB Long", stop=open * 0.99)
Using method 1 for the take profit and method 2 for the stop loss, I'm getting varying success. The script still isn't printing the closing of the positions on the chart for both take profit and stop loss.
Method 3: Instead of using strategy.exit() , use strategy.close() . Can someone explain the differences to me?
Can you help me understand what I should be doing to achieve my goal for this script?
For the sake of completeness, here is the script as I have it currently.
//#version=5
strategy(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true)
// Set input parameters
length = input.int(20, minval=1)
mult = input.float(2.5, minval=0.001, maxval=50)
offset = input.int(0, "Offset", minval = -500, maxval = 500)
// Calculate Bollinger Bands using 15 minute data
src = close
middle = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = middle + dev
lower = middle - dev
// Calculate Bollinger Bands using 1 hour data
src1h = request.security(syminfo.tickerid, '60', close, lookahead=barmerge.lookahead_on, gaps=barmerge.gaps_on)
middle1h = ta.sma(src1h, length)
dev1h = mult * ta.stdev(src1h, length)
upper1h = middle1h + dev1h
lower1h = middle1h - dev1h
// Enter long position when 15 minute chart Bollinger Band is touched and the 1 hour band is touched
longCondition = ta.crossover(low, lower) and (ta.crossover(low, lower1h) or ta.crossover(low[1], lower1h))
if longCondition
strategy.entry("BB Long", strategy.long)
strategy.exit("EXIT LONG WIN", from_entry="BB Long", limit=high * 1.01)
// Enter short position when 15 minute chart Bollinger Band is touched and the 1 hour band is touched
shortCondition = ta.crossunder(high, upper) and (ta.crossover(high, upper1h) or ta.crossover(high[1], upper1h))
if shortCondition
strategy.entry("BB Short", strategy.short)
strategy.exit("EXIT SHORT WIN", from_entry="BB Short", limit=low * 0.09)
// Plot Bollinger Bands
plot(upper, color=color.red, linewidth=2)
plot(lower, color=color.red, linewidth=2)
plot(upper1h, color=color.blue, linewidth=2)
plot(lower1h, color=color.blue, linewidth=2)
if (strategy.position_size > 0)
strategy.exit("EXIT LONG STOP", from_entry="BB Long", stop=open * 0.99)
if (strategy.position_size < 0)
strategy.exit("EXIT SHORT STOP", from_entry="BB Short", stop=open * 1.01)
I have written a few scripts from cobbled together pieces of code but I'm now trying to get a better understanding of the functions. I've tried combinations of different methods but I'm still not getting both the take profit and the stop loss to trigger as well as display on the chart.
Thank you!
Ok, so the first solution I found is this:
strategy.exit("TP / SL", from_entry="BB Long", limit=high * 1.01, stop=initialEntryPrice * 0.99)
The take profit and stop loss are placed in the same strategy.exit() function. However, this doesn't plot in on the graph in the way that I want. I would want to see just TP or SL, depending on the case. In this solution, it will plot "TP / SL" whenever it exits a trade, for either reason. This is ok, but if anyone has can answer my other questions I would be grateful!

pinescript newbie: problem to implement RR as TP! can you check my script? :)

var float loLows = na
is_new_position = (strategy.position_size[1] != strategy.position_size) // This will only be true on the bar of the entry
loLows := is_new_position // Only update the value if it is a new position. Keep the old value otherwise
TPL=(strategy.position_avg_price/RR)-(loLows/RR)+strategy.position_avg_price
TPS=(strategy.position_avg_price/RR)-(hiHighs/RR)-strategy.position_avg_price
if inDateRange and (close > (ema200) and close > (ema992) and ta.crossover(ema13,ema62) and ta.crossover(ema21,ema62))
strategy.entry("entry long", strategy.long)
strategy.exit("long exit", profit=TPL, stop=loLows)
if inDateRange and(close < (ema200) and close < (ema992) and ta.crossunder(ema13,ema62) and ta.crossunder(ema21,ema62))
strategy.entry("entry short", strategy.short)
strategy.exit("short exit", profit=TPS , stop=hiHighs)

Pinescript question, sell 50% at profit and let 50% run

I am trying to have 2 sell orders but I want to have a profit and stop as well based on pips not price. If I use strategy.exit it would look like this for my stop and profit but I can't figure out how to split the sells.
strategy.exit("exit", "long", stop = 100, profit = 100)
Is this possible? I know my coding isnt very good any help is greatly appreciated.
shortCondition = close < lowerBand and close[1] > lowerBand
if (shortCondition)
strategy.entry("short", strategy.short, qty=pos_size)
strategy.order("short1", strategy.long, qty=size_trim, comment="closefirst50%")
strategy.order("short2", strategy.long, qty=size_trim, comment="closesecond50%")
longCondition = close > upperBand and close[1] < upperBand
if (longCondition)
strategy.entry("long", strategy.long, qty=pos_size)
strategy.order("long1", strategy.short, qty=size_trim, comment="closefirst50%")
strategy.order("long2", strategy.short, qty=size_trim, comment="closesecond50%")
The solution is to use 2 strategy.exit indeed
if (shortCondition)
strategy.entry("short", strategy.short, qty=pos_size)
if strategy.position_size < 0
strategy.exit("exit short TP", qty=size_trim, profit = 100, comment="closefirst50%")
strategy.exit("exit short SL", loss = 100, comment="close the remaining 50%")

Move stop loss after take profit is hit

I would like to move my stop loss to where my first take profit is IF it reaches it. And also as of now when my stop loss is hit it only sells half the position and then the left is left lingering until it hits the second take profit if it ever does. Any suggestions are greatly appreciated.
shortCondition = close < lowerBand and close[1] > lowerBand
if (shortCondition)
strategy.entry("short", strategy.short, qty=pos_size)
if strategy.position_size < 0
strategy.exit("exit short TP", qty=size_trim, profit = 400, comment="close first 50%")
strategy.exit("exit short SL", loss = 400, profit = 800, comment="SL/last50%")
longCondition = close > upperBand and close[1] < upperBand
if (longCondition)
strategy.entry("long", strategy.long, qty=pos_size)
if strategy.position_size > 0
strategy.exit("exit long TP", qty=size_trim, profit = 400, comment="close first 50%")
strategy.exit("exit long SL", loss = 400, profit = 800, comment="SL/last50%")

Pine Script V5, How can i DELAY 5 seconds for my LONG & SHORT Entry?

I want to delay 5 seconds for my LONG & SHORT entries.
for example,
if the "longCondition" is true at 12:00:00, I want to execute LONG entry at 12:00:05
the same for Short entry: if the "shortCondition" is true at 12:00:00, I want to execute SHORT entry at 12:00:05
Can anyone help me, please?
//#version=5
strategy('My Strategy', overlay=true)
// Indicators
shortSMA = ta.sma(close, 10)
longSMA = ta.sma(close, 30)
rsi = ta.rsi(close, 14)
// Conditions
longCondition = ta.crossover(shortSMA, longSMA) and (rsi > 50)
shortCondition = ta.crossunder(shortSMA, longSMA) and (rsi < 50)
/// LONG
strategy.entry("long", strategy.long, when=longCondition, comment="Entry Long") //## How can i delay 5 seconds for this entry ???
strategy.close("long", when=shortCondition, comment = "Exit Long")
/// SHORT
strategy.entry("short", strategy.short, when = shortCondition, comment="Entry Short") //## How can i delay 5 seconds for this entry ???
strategy.close("short", when=longCondition, comment="Exit Short")