0

请帮我处理这段代码。我想在这段代码中实现以下

  1. 快速 MA 交叉 慢速 MA
  2. 检查 SMA 的斜率(如图所示)。如果坡度开始下降,请执行以下操作

要求

  1. 确定枢轴高点(价格通道的上限)
  2. 下限是从枢轴高点减去 5xATR
  3. 只要价格保持在该限制范围内,更改背景颜色
  4. 当价格超出该限制时,不显示
  5. 如果价格回到那个通道,不要改变背景颜色。
  6. 每次交叉只执行一次。

下面给出了带有标记图表的代码任何建议或帮助将不胜感激

    *******************************CODE*******************************
[enter image description here][2]
//@version=5
indicator(title ="Sideways Market after rally", overlay = true)

// taking user inputs
slowSMAlength = input.int(title="Slow SMA Period", defval=200)
fastSMAlength = input.int(title="Fast SMA Period", defval=21)
ROC_lookback = input.int(title="ROC Lookback Period", defval=15)

//calculating EMAs
fastSMA = ta.sma(close, fastSMAlength)
slowSMA = ta.sma(close, slowSMAlength)

//***********************************************************************************************//
//finding the slope and than smoothing it 
slope_sma = (ta.roc(slowSMA, ROC_lookback))*63.2 //slope of slow moving average
smoothed_slope = ta.sma (slope_sma, 15) // smoothing by taking Smoothed moving average of the slope
isRising = ta.rising(smoothed_slope, 10)
//**********************************************************************************************// 


// initial conditons for long and short entry
longcondition = fastSMA >= slowSMA // uptrend

//Entry Conditions
longentry =  ta.crossover(fastSMA, slowSMA) and longcondition // when fast MA cross above the slow MA and fast MA greater than slow MA 

// finding ATR
atr = ta.atr(14)

//variables for finding the sideways markets
float phigh          = 0.0 
float phigh_1        = 0.0


var bool sideways = false     // variables to verify that code only execute once when the condition is true

//*******************execute this piece of code only once when these condition happens***********************************************

if (long entry and long condition and isRising==0 and sideways==false) // after fast SMA crossover slow SMA and fastSMA is greater than slow SMA and slope starts falling
    sideways:= true                                  //sideways variable becomes true so that this if statement is not executed next time before condition changes
    phigh := ta.pivothigh (high, 15, 5)               // find the pivot high point and it will be the upper limit of the sideways channel
    phigh_1 := phigh - 4*atr                          // find the lower limit of the channel by subtracting 4 ATRs from the pivot high 
//***********************************************************************************************************************************    

if (close > phigh or close < phigh_1 and sideways == true ) // if the price goes above the defined price limits (price channel) and above code is executed
    sideways := false                               // change the state of the variable 

//plot the variables 
bgcolor(close < phigh or close > phigh_1 ? color.new(color.red,90) : na)   // color the background as long as the price remians in the limit "phigh" and "phigh_1"
plot(series=slowSMA, title="Slow EMA", color=color.blue, linewidth=2)      // plot moving averages
plot(series=fastSMA, title="Fast SMA", color=color.yellow, linewidth=1)     // plot moving averages 
***************************************CODE END*************************************************

在此处输入图像描述

4

0 回答 0