# Need help with my script

1046 views
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,
c4ss10p314
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,
Julius

use barssince() to pinpoint where your engulfing candle is and then use strategy.close(when = close<low,...)
split the code into more rows
@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..
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.
@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? :(
c4ss10p314
@c4ss10p314, Research the function barssince(). It should work I use it frequently.

or
clockwork = strategy.openpositions==0 and strategy.openpositions==1 and bullishengulfing condition

this should store the bar for when the conditions were true
@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... :/
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")
@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: