简体   繁体   中英

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

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).

迟到的和期望的交易

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM