BirdstudioNZ
BirdstudioNZ

Reputation: 1

Try to code risk-reward ratio of 1:3 with PineScript

unable to get the reward part working

Hi, I'm trying to calculate the profit price target based on the distance between entry price and recentlow, then times 3 to get 1 to 3 risk-reward setup working for back test, the code below will only execute the stop loss at lowestLow but ignore profit taking order when target hit, can anyone help please, thanks in advance.

if (longcondition and strategy.opentrades == 0)

    strategy.entry(id="MR_Long", direction=strategy.long)

    currEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)

    Profit_Price = currEntryPrice + (currEntryPrice - lowestLow) * 3

    strategy.exit("Exit Long", "MR_Long", limit = Profit_Price, stop = lowestLow)

Upvotes: 0

Views: 85

Answers (1)

vitruvius
vitruvius

Reputation: 21342

Updating your exit prices conditionally when place your entry order is generally bad practice if you use strategy.* built-in variables to get any price values.

That is because when (longcondition and strategy.opentrades == 0) is true and you place your order within that if block, strategy.opentrades.entry_price will return na because there is no open order yet. Your order will either be opened on candle close or on the open of the next bar.

Take those calculations and strategy.exit() outside.

if (longcondition and strategy.opentrades == 0)
    strategy.entry(id="MR_Long", direction=strategy.long)

currEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
Profit_Price = currEntryPrice + (currEntryPrice - lowestLow) * 3
strategy.exit("Exit Long", "MR_Long", limit = Profit_Price, stop = lowestLow)

Upvotes: 0

Related Questions