buja
buja

Reputation: 51

pinescript strategy_direction as a variable?

I want only one strategy.entry() rule in my script (the both directions should be in a variable). The reason for that is, that I want a strategy.exit() rule independent of the strategy.entry() rule. The following code shows what I mean, but it doesn't work.

//@version=6
strategy("Market order strategy", overlay = true, close_entries_rule = "ANY")

float sma14 = ta.sma(close, 14)
float sma28 = ta.sma(close, 28)
entryLong = (sma14 - sma28) > 100
entryShort = (sma14 - sma28) < -100
direction = entryLong ? strategy.long : entryShort ? strategy.short : na

var int tradeNum = 0
int profitDistanceInput = input.int(10000, "Profit distance, in ticks", 1)
int lossDistanceInput   = input.int(5000, "Loss distance, in ticks", 1)

if not na(direction)
    tradeNum += 1
    string entryStr = str.tostring(tradeNum)
    string exitStr = str.format('e {}', tradeNum)
    strategy.entry(entryStr, direction)

if strategy.opentrades > 0
    strategy.exit(exitStr, entryStr, profit=profitDistanceInput, loss=lossDistanceInput)

I also don't want to use for the exit strategy the profit and loss parameters (with ticks), i want to use instead a percent value of the entry price. I read the manual, of course, but I couldn´t realize this.

Upvotes: 0

Views: 58

Answers (1)

Gu5tavo71
Gu5tavo71

Reputation: 1214

Try limiting to 3 operations. something like this:

if entryLong
    tradeNum += 1
    if tradeNum == 1
        strategy.entry('entryLong1', strategy.long, comment = 'LE1')
    if tradeNum == 2
        strategy.entry('entryLong2', strategy.long, comment = 'LE2')
    if tradeNum == 3
        strategy.entry('entryLong3', strategy.long, comment = 'LE3')
        tradeNum := 0


strategy.exit(id = 'Long Exit 1', from_entry = 'entryLong1', profit = profitDistanceInput, loss = lossDistanceInput, comment_loss = 'LxSL1', comment_profit = 'LxTP1')
strategy.exit(id = 'Long Exit 2', from_entry = 'entryLong2', profit = profitDistanceInput, loss = lossDistanceInput, comment_loss = 'LxSL2', comment_profit = 'LxTP2')
strategy.exit(id = 'Long Exit 3', from_entry = 'entryLong3', profit = profitDistanceInput, loss = lossDistanceInput, comment_loss = 'LxSL3', comment_profit = 'LxTP3')

test

Upvotes: 0

Related Questions