0

Right now, I'm trying out Strategy Alert but it's a hit or miss. One trade, it performs fine according to my strategy. Then it starts randomly entering and exiting no matter what my strategy says.

//@version=5
strategy("Entry Price", overlay=true)

var float entryprice = na
var float tp = na
var float sl = na

var bool entryCondition = false

if and close[1] < open[1] and close[0] > open[0]
    entryCondition := true

if entryCondition and barstate.isconfirmed and strategy.opentrades == 0
    strategy.entry("Go Long", strategy.long, qty=1, oca_type=strategy.oca.cancel, alert_message="Go Long!")
    entryprice := close[0]
    entryCondition := false  // Reset entryCondition after entering the trade

if strategy.opentrades > 0
    if na(tp)
        tp := strategy.position_avg_price + 5 * syminfo.mintick
    if na(sl)
        sl := strategy.position_avg_price - 5 * syminfo.mintick

plot(entryprice, title='Entry', color=color.new(color.gray, 0), style=plot.style_stepline_diamond)
plot(tp, "Profit", color=color.green, style=plot.style_cross, linewidth=2)
plot(sl, "Stop", color=color.red, style=plot.style_cross, linewidth=2)

strategy.exit("Exit!", from_entry = "Go Long", qty=1, limit=tp, stop=sl)

if strategy.opentrades == 0
    tp := na
    sl := na

I also tried alertcondition() and pretty much the same thing but I wanted to use Strategy Alert so I can use the price that I entered to measure my stop and profit. Would changing strategy.exit() to strategy.close()? The last trade it took was on par with my strategy. But it never took the exit when my strategy indicated it supposed to. What could I be doing wrong? Any help would be very much appreciated!

Nishi
  • 1
  • 2

1 Answers1

0

In strategy.exit try alert_profit and alert_loss

Something like this:

strategy.exit("Exit!", from_entry = "Go Long", qty=1, limit=tp, stop=sl, alert_loss = 'alert sl', alert_profit = 'alert tp')
Gu5tavo71
  • 706
  • 1
  • 4
  • 11