Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pine Script Strategy - How to trigger entry when *price* (not close) crosses a certain value? Currently entering trade one candle too late

Tags:

pine-script

Just playing around, learning how to write strategies. The one I'm trying right now is (pseudo-code)...

if(previousCandle == red 
   ... AND previousCandle.high >= sma
   ... AND previousCandle.low <= sma 
   ... AND currentPrice > previousCandle.high)
    
    enter trade

What I have in Pine Script is...

redTouch = close < open and high >= ma and low <= ma

longCond = redTouch[1] and close > high[1]

strategy.entry("Long", strategy.long, when = longCond)

The redTouch candles are all identified correctly (checked previously using BG colors), but for the longCond I don't want close > high[1], because that enters the trade only on the next candle (and too late).

The following screenshot shows where the trade is currently being entered (blue line on red candle), and where I'd like it to trigger/enter (yellow line on green candle).

Late and desired trades

How do I change close > high[1] to price > high[1] or a similar intra-candle crossover trigger? Or can you only enter trades in the next candle in Pine Script?

like image 493
Birrel Avatar asked Oct 27 '25 08:10

Birrel


1 Answers

You'll need to do a few things to get the behavior you're looking for.

  1. Add process_orders_on_close=true to your strategy declaration statement to get the Strategy tester to process on the bar that triggers the condition instead of the next bar's open.
  2. Store the price you want to enter in a variable, maybe high[1]+somenumber
  3. Add limit=YourEntryPriceVariable to your strategy.entry statement

This will create a limit order to enter at the price you specify on the bar that it happens.

like image 189
kmarryat Avatar answered Oct 28 '25 22:10

kmarryat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!