-1

Dears,

I'm new in Pine Script and looking for your kind help.

I have this beginner-level script that gives (buy signal) when fast MA crossed over slow MA and kept higher than crossing price for 3 days which I call it confirmed bars.

However, I'm not able to generate take profit and stop loss signals.

I need TP signal when X% is generated. For instance, If the buy price is 100, it will be 110 (if x=10%)

the same thing with stop loss when the price is dropped down for a specific percentage.


//@version=4
strategy("MovingAvg Cross", overlay=true)
length = input(100)
confirmBars = input(3)

price = close
ma = sma(price, length)
bcond = price > ma
bcount = 0
bcount := bcond ? nz(bcount[1]) + 1 : 0
if (bcount == confirmBars)
    strategy.entry("MACrossLE", strategy.long, comment="MACrossLE")

Pls help me.

mkoism
  • 63
  • 1
  • 1
  • 3

1 Answers1

0

I am sorry but I am not a programmer myself so my knowledge is limited. This is the method I am using at the moment, you need to calculate your TP and SL variables and then write the exit conditions:

Long=(buy condition, such as "crossover(price,ma)")
if Long
        takeprofit := (your calculation)
        stoploss := (your calculation)
        strategy.entry("Long", strategy.long, when = Long)
        strategy.exit("TP/SL", "Long", stop=stoploss, limit=takeprofit)

A reference link https://kodify.net/tradingview/orders/strategy-close/

Berni
  • 1
  • 1