简体   繁体   English

如何在 pine 脚本上获得条件的第二次出现

[英]How to get the second occurrence of a condition on pine script

I have a strategy in tradingview that enters every time this condition is true, but I only need to enter the second occurrence of this condition我在交易视图中有一个策略,每次这个条件为真时进入,但我只需要输入这个条件的第二次出现

LongCondition = (wasOverSold and crossoverBull) and up_trend == false

在此处输入图像描述

the LongCondition gets true only on the exact candle where the blue cross appears (macd crossover), after that it is gets false all the time. LongCondition仅在出现蓝色十字的确切蜡烛上为真(macd 交叉),之后它一直为假。

I already have tried using the history-referencing operator enter = LongCondition[1]? true: false我已经尝试过使用历史引用运算符enter = LongCondition[1]? true: false enter = LongCondition[1]? true: false but it just delays the buy entry. enter = LongCondition[1]? true: false但它只是延迟买入入场。

how can i find that second occurrence every time after an enter exit?每次进入退出后,我怎样才能找到第二次出现?

You can use a counter for that.您可以为此使用计数器。 Increase it when your condition becomes true , check if it is 2, enter position and reset your counter for next order.当您的条件变为true时增加它,检查它是否为 2,输入 position 并重置您的计数器以进行下一个订单。

Here is an example that enters and exits position whenever a MACD cross takes place the second time.这是一个在 MACD 交叉发生第二次时进入和退出 position 的示例。

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)

n = input.int(2, "Number of occurences")

var bullCrossCnt = 0
var bearCrossCnt = 0

[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

bullCross = ta.crossover(macdLine, signalLine)
bearCross = ta.crossunder(macdLine, signalLine)
bullCrossCnt := bullCross ? bullCrossCnt + 1 : bullCrossCnt
bearCrossCnt := bearCross ? bearCrossCnt + 1 : bearCrossCnt

if (bullCross and bullCrossCnt == n)
    strategy.entry("Long", strategy.long)
    bullCrossCnt := 0  // Reset variable

if (bearCross and bearCrossCnt == n)
    strategy.close("Long")
    bearCrossCnt := 0  // Reset variable

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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