admin管理员组

文章数量:1295300

I am trying to create a Pine Script Strategy (using ChatGPT and/or Claude AI) and am having some trouble getting it to work. Here is the outline of what I want the strategy to test for:

  1. Price starts above the 100 EMA (meaning the low of that candle did not touch the 100 EMA).

  2. Price then touches or goes below the 100 EMA (meaning that at least the low of that candle touched or went below the 100 EMA)

  3. Price then goes again above the 100 EMA (meaning the low of that candle did not touch the 100 EMA).

  4. We then check if both stochastic lines are at or above 50.

    1. If yes - place an order.
    2. If no - we check the next candle.
      1. If the low of that candle is ABOVE the 100 EMA and the stochastic lines are at or above 50, place the order
      2. If the low of that candle is ABOVE the 100 EMA and the stochastic lines are below 50, then check the next candle
      3. If the low of that candle is at or below the 100 EMA, then wait for the next candle where the low is ABOVE the 100 EMA and start the process again.

When I run this script, however, Trading View says that no orders were placed (although in my latest attempt I got ONE order placed). No matter what I do (or try to get ChatGPT/Claud to do), I can't get it to work.

Here is my last Pine Script attempt:

//@version=6
strategy("Touch & Recovery - isAboveEMA Based on Previous Bar", overlay=true, initial_capital=400000, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=1)

// 1) Inputs & Basic Calcs
emaLen  = input.int(100,"EMA Length")
stLen   = input.int(14,"Stochastic Length")
stSig   = input.int(3,"Stoch Signal Len")
ema100  = ta.ema(close, emaLen)
kVal    = ta.stoch(high, low, close, stLen)
dVal    = ta.sma(kVal,  stSig)

// 2) Persistent Flags
var bool isAboveEMA    = false 
var bool inRecoveryMode = false

// 3) On each new bar, update isAboveEMA based on the previous bar’s data
if barstate.isconfirmed
    if isAboveEMA
        // Lose "above" status if the previous bar's high was below its EMA
        if close < ema100
            isAboveEMA := false
    else
        // Gain "above" status if the previous bar's low was above its EMA
        if low > ema100
            isAboveEMA := true

// 4) Touch Event on current bar if isAboveEMA is true (meaning the previous bar was above) 
//    AND current bar's low is <= EMA
bool touchEvent  = isAboveEMA and (low <= ema100)
if touchEvent
    inRecoveryMode := true

// 5) Candidate Candle & Stochastic Condition
bool candidateCandle = (open > ema100) and (close > ema100)
bool stochCondition  = (kVal > 50) and (dVal > 50)

// 6) Enter Trade if in recovery + both conditions. Then exit recovery mode
if inRecoveryMode and candidateCandle and stochCondition
    strategy.entry("Long", strategy.long)
    inRecoveryMode := false

// 7) Exit if high >= 2× entry price (on bar close)
if strategy.position_size > 0
    float avgPrice = strategy.position_avg_price
    if high >= avgPrice * 2
        strategy.close("Long", comment="TP by strategy.close")

// VISUAL AIDS (uncomment if desired)
// barcolor(inRecoveryMode ? color.green : color.red)
plot(ema100, color=color.yellow, linewidth=2, title="EMA 100")
plotshape(isAboveEMA, style=shape.circle, location=location.abovebar, color=color.yellow, size=size.tiny, title="isAboveEMA")
plotshape(touchEvent, style=shape.circle, location=location.abovebar, color=color.green, size=size.tiny, title="touchEvent")
plotshape(inRecoveryMode, style=shape.circle, location=location.top, color=color.orange, size=size.tiny, title="inRecoveryMode")

Right now there seem to be two problems with the script.

One is that it doesn't note all touch events.
The second is that right now it's not registering that inRecoveryMode is true.

When inRecoveryMode is true, then (and only then) do we check to see if the stochastic lines are at or above 50 and the low is above the 100 EMA. In other words, if inRecoveryMode is set to false, then we can never place an order.

Any idea on how I can modify this script to get it to work?

P.S. Here is a link to the chart: /?symbol=AMEX%3ASPY

[note: this is my first time sharing a chart on tradingview, I hope it works :-)]

本文标签: Pine Script Trouble Implementing a Touch amp Recovery’ StrategyStack Overflow