Need help with my script

Hello world,

this is a somewhat simplified version of what I am trying to accomplish.
The script:
When having correctly identified a trend, I want to look for engulfing candles and buy at the next open.
That works for now.

My problem:
I want to set SL and TP depending on the size of that engulfing candle.
When price drops below the low (for short high) of the engulfing candle, I want to exit the trade.
When price crosses above 1.25 x the size of the engulfing candle, I want to close the position.

Can anyone help?

Im quite new to this..

All the best,
Release Notes: Script now enters at correct time.
Though it doesn't stop out correctly.

Therefore I'm still stuck with the question:
How can I use the previous candle size as basis for TP/SL calculation?

Would be greatful for your help!

All the best,
Remove from Favorite Scripts Add to Favorite Scripts


use barssince() to pinpoint where your engulfing candle is and then use strategy.close(when = close<low,...)
split the code into more rows
+1 Reply
c4ss10p314 greenmask9
@greenmask9, Thank you very much for the reply! I dont quite know how to identify the engulfing candles. Also im not sure how to call the "low"/"high" of it..
greenmask9 c4ss10p314
@c4ss10p314, In such a scenario, I usually use paper and test until I find the right mathematical operation. You can call high and low just like close or open.
+1 Reply
c4ss10p314 greenmask9
@greenmask9, Well, its always the last candle before entry. Is there no way to express this in pine? "Use low of position.entry"? Or storing the previous candle size in a variable? :(
greenmask9 c4ss10p314
@c4ss10p314, Research the function barssince(). It should work I use it frequently.

clockwork = barssince(condition for buy)
clockwork = strategy.openpositions==0 and strategy.openpositions==1 and bullishengulfing condition

this should store the bar for when the conditions were true
c4ss10p314 greenmask9
@greenmask9, You are of great help! Can one say something like:
EngSize = clockwork.low - clockwork.high
stopLoss = cross(close, clockwork.low)
TakeProfit = cross(close, EngSize * 1.25)
That's basically the code I am looking for. Just cant get the syntax right... :/
greenmask9 c4ss10p314
@c4ss10p314, Try it like this.

EngSize = high - low
stoplong = low
profitlong = close + EngSize*1.25
strategy.close("Close Long", from_entry = "Long", when = close < stoplong, comment="SL")
strategy.close("Close Long", from_entry = "Long", when = close > profitlong, comment="TP")
c4ss10p314 greenmask9
@greenmask9, Somewhat my last comment didnt go through. Thank you!

So, even though it seems to be correct, it doesnt work. Very frustrating.

What I did:

clockwork = barssince(bullEngulfing)
Engsize = abs(open(clockwork) - close (clockwork)) / syminfo.mintick
execProfitTarget = cross(close, close + Engsize)
execStopLoss = cross(close, close - Engsize)

if execProfitTarget
- strategy.close...
greenmask9 c4ss10p314
@c4ss10p314, Mby. It can also be that not every engulfing candle opens a new position. clockwork always targets the last engulfing candle. Can you somehow specify the last engulfing candle?
Home Stock Screener Forex Screener Crypto Screener Economic Calendar How It Works Chart Features Pricing Refer a friend House Rules Help Center Website & Broker Solutions Widgets Charting Solutions Lightweight Charting Library Blog & News Twitter
Profile Profile Settings Account and Billing Referred friends Coins My Support Tickets Help Center Ideas Published Followers Following Private Messages Chat Sign Out