Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TradingView Pine Script : check previous strategy.entry price before new entry

Tags:

pine-script

Someone asked a similar question with no response and I am not allowed to add to it.

Tradingview Pine script save close price at time of strategy entry

I am trying to build a strategy that will buy multiple times (pyramiding) to average down before closing but I want to check the previous entry price to make sure it's less by a configured percentage.

What I have so far:

lastBuy=0

if (condition)
    if (lastBuy==0)
        lastBuy=close
        strategy.entry("buy", true)
    else
        if ((close*1.01)<lastBuy)
            lastBuy=close
            strategy.entry("buy", true)

Each time the code is passed it resets lastBuy back to zero and I never get to check the previous close price. If I don't set this I get undeclared error.

Thanks in advance for any help!

like image 316
rigide Avatar asked Mar 10 '18 21:03

rigide


2 Answers

This works for entry price.

entryPrice = valuewhen(strategy.opentrades == 1, strategy.position_avg_price, 0)

like image 85
CrazedJEW Avatar answered Sep 22 '22 22:09

CrazedJEW


How I am saving the entry price to a variable:

bought = strategy.opentrades[0] == 1 and strategy.position_size[0] > strategy.position_size[1]
entry_price = valuewhen(bought, open, 0)
like image 31
42piratas Avatar answered Sep 20 '22 22:09

42piratas