ChartArt

Moving Average Consecutive Up/Down Strategy (by ChartArt)

This simple strategy goes long (or short) if there are several consecutive increasing (or decreasing) moving average values in a row in the same direction. The bars can be colored using the raw moving average trend. And the background can be colored using the consecutive moving average trend setting. In addition a experimental line of the moving average change can be drawn.

The strategy is based upon the "Consecutive Up/Down Strategy" which was created by Tradingview.

All trading involves high risk; past performance is not necessarily indicative of future results. Hypothetical or simulated performance results have certain inherent limitations. Unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not actually been executed, the results may have under- or over-compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profits or losses similar to those shown.
Mã nguồn mở

Với tinh thần TradingView, tác giả của tập lệnh này đã xuất bản nó dưới dạng mã nguồn mở, vì vậy các nhà giao dịch có thể hiểu và xác minh nó. Chúc mừng tác giả! Bạn có thể sử dụng mã này miễn phí, nhưng việc sử dụng lại mã này trong một ấn phẩm chịu sự điều chỉnh của Nội quy nội bộ. Bạn có thể yêu thích nó để sử dụng nó trên biểu đồ.

Thông báo miễn trừ trách nhiệm

Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?
//@version=2
strategy("Moving Average Consecutive Up/Down Strategy (by ChartArt)", overlay=true)

// ChartArt's Moving Average Consecutive Up/Down Strategy
//
// Version 1.0
// Idea by ChartArt on December 30, 2015.
//
// This strategy goes long (or short) if there are several
// consecutive increasing (or decreasing) moving average
// values in a row in the same direction.
//
// The bars can be colored using the raw moving average trend.
// And the background can be colored using the consecutive
// moving average trend setting. In addition a experimental
// line of the moving average change can be drawn.
//
// The strategy is based upon the "Consecutive Up/Down Strategy"
// created by Tradingview.


// Input
Switch1 = input(true, title="Enable Bar Color?")
Switch2 = input(true, title="Enable Background Color?")
Switch3 = input(false, title="Enable Moving Average Trend Line?")

ConsecutiveBars = input(4,title="Consecutive Trend in Bars",minval=1)

// MA Calculation
MAlen = input(1,title="Moving Average Length: (1 = off)",minval=1)
SelectMA = input(2, minval=1, maxval=4, title='Moving Average: (1 = SMA), (2 = EMA), (3 = WMA), (4 = Linear)')
Price = input(close, title="Price Source")
Current =
 SelectMA == 1 ? sma(Price, MAlen) :
 SelectMA == 2 ? ema(Price, MAlen) :
 SelectMA == 3 ? wma(Price, MAlen) :
 SelectMA == 4 ? linreg(Price, MAlen,0) :
 na
Last =
 SelectMA == 1 ? sma(Price[1], MAlen) :
 SelectMA == 2 ? ema(Price[1], MAlen) :
 SelectMA == 3 ? wma(Price[1], MAlen) :
 SelectMA == 4 ? linreg(Price[1], MAlen,0) :
 na

// Calculation
MovingAverageTrend = if Current > Last
    1
else
    0

ConsecutiveBarsUp = MovingAverageTrend > 0.5 ? nz(ConsecutiveBarsUp[1]) + 1 : 0
ConsecutiveBarsDown = MovingAverageTrend < 0.5 ? nz(ConsecutiveBarsDown[1]) + 1 : 0
BarColor = MovingAverageTrend > 0.5 ? green : MovingAverageTrend < 0.5 ? red : blue
BackgroundColor = ConsecutiveBarsUp >= ConsecutiveBars ? green : ConsecutiveBarsDown >= ConsecutiveBars ? red : gray
MovingAverageLine = change(MovingAverageTrend) != 0 ? close : na

// Strategy
if (ConsecutiveBarsUp >= ConsecutiveBars)
    strategy.entry("ConsUpLE", strategy.long, comment="Bullish")
    
if (ConsecutiveBarsDown >= ConsecutiveBars)
    strategy.entry("ConsDnSE", strategy.short, comment="Bearish")

// output
barcolor(Switch1?BarColor:na)
bgcolor(Switch2?BackgroundColor:na)
plot(Switch3?MovingAverageLine:na, color=change(MovingAverageTrend)<0?green:red, linewidth=4)
//plot(strategy.equity, title="equity", color=red, linewidth=2, style=areabr)