Strategy · Pine Script v4 · On the chart

MC Strategy - Aggressive

by Metzenas

This strategy implements an automated dollar-cost averaging (DCA) trading system inspired by Market Cipher indicators.

View on GitHub

GitHub stars2on the repository
Pine versionv4older, still runs
Lines of code245
Settings21inputs
LicenceNone statedfile: MPL-2.0
Last update5 years ago8 Apr 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

245 lines of Pine Script v4 from strategies/mc_strategy_aggressive.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

mc_strategy_aggressive.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©Metzenas

// @version=4
strategy("MC Strategy - Aggressive", shorttitle="MC Strategy - Aggressive", format=format.price, overlay=true, default_qty_type=strategy.cash, default_qty_value=350, commission_type=strategy.commission.percent, commission_value=.1, slippage=0, calc_on_order_fills=false, initial_capital=8700, currency=currency.NONE)

// Input variables
botType = input(title="Bot Type", defval="Long", options=["Long", "Short"])
baseOrder = input(title="Base Order Size", type=input.float, minval=0, defval=350)
safetyOrder = input(title="Safety Order Size", type=input.float, minval=0, defval=350)
maxSafetyOrders = input(title="Max safety order count", type=input.float, minval=1, defval=8)
priceDeviation = input(title="Price deviation in % to open safety orders", type=input.float, step=0.1, minval=0.1, defval=1) // the less the more profit
volumeScaleInput = input(title="Safety Order volume scale", type=input.float, step=0.1, minval=0.1, defval=1.3) // the more the more profit
safetyOrderStepScale = input(title="Safety Order step scale", type=input.float, step=0.1, minval=0.1, defval=1.1) // the more the less profit
targetProfit = input(title="Target profit (%)", type=input.float, step=0.1, minval=0.1, defval=2)
startTime = input(title="Start Time", defval=timestamp("1 Mar 2021"), type=input.time)
endTime = input(title="End Time", defval=timestamp("31 Dec 2021"), type=input.time)

dealStartType = input(title="Deal Start Condition", defval="Indicator", options=["ASAP", "Script", "Indicator"])
sourceDealStart = input(title="Deal Start Source", defval=close, type=input.source)
sourceDealStartValue = input(title="Deal Start Value", defval=0, type=input.float)

n1 = input(10, "Channel Length")
n2 = input(21, "Average Length")
src = input(title="Source", type=input.source, defval=hlc3)
// ema21 = input(21, "EMA 21")

// MACD Input
// fast_length = input(title="Fast Length", type=input.integer, defval=12)
// slow_length = input(title="Slow Length", type=input.integer, defval=26)

// signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
// sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=false)
// sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=false)

// Levels
obLevel1 = input(60, "Over Bought Level 1")
obLevel2 = input(53, "Over Bought Level 2")
osLevel1 = input(-60, "Over Sold Level 1")
osLevel2 = input(-53, "Over Sold Level 2")

ap = hlc3 
esa = ema(ap, n1)
d = ema(abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ema(ci, n2)
// ema21_ = ema(hlc3, ema21)
 
wt1 = tci
wt2 = sma(wt1,3)
cross_condition = cross(wt1, wt2)

////////////////
// Money Flow //
////////////////
length = input(title="Length", type=input.integer, defval=60, minval=1, maxval=2000)
// src = hlc3
upper = sum(volume * (change(src) <= 0 ? 0 : src), length)
lower = sum(volume * (change(src) >= 0 ? 0 : src), length)
_rsi(upper, lower) =>
    if lower == 0
        100
    if upper == 0
        0
    100.0 - (100.0 / (1.0 + upper / lower))
mf = _rsi(upper, lower)
// diverge_long = abs(ema21_ - open) / open

//////////
// MACD //
//////////
// fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
// slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
// macd = fast_ma - slow_ma
// signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
// hist = macd - signal

// // Intermediate
// long_base = wt2 <= 0 and mf <= 40
// short_base = wt2 >= 40 and mf >= 55
// // long_diverge = mf <= 50 and diverge_long > 0.008
// long_macd_signal = hist < -40
// short_macd_signal = hist < 0

// Filters
body_size = abs(close - open) / (high - low)

// Base
long_signal_base = cross_condition and (wt2 - wt1 <= 0) // and body_size > 0.1 // and (long_base) and long_macd_signal
short_signal_base = cross_condition and (wt2 - wt1 > 0) // and (short_base) and body_size >= 0.2 and short_macd_signal

// Execute
// strategy.entry("long_base", true, 1, when = long_signal_base)
// strategy.close("long_base", when = short_signal_base)

////////////////////////////////
// STRATEGY TO TEST
///////////////////////////////

// Set dealStartCondition to your buy condition and dealStopCondition to your sell condition
bool dealStartCondition = false
bool dealStopCondition = na


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////
// DCA STRATEGY CODE
///////////////////////////////
if (dealStartType == "ASAP")
    dealStartCondition := true
else if (dealStartType == "Indicator")
    dealStartCondition := long_signal_base

bool isLongBot = botType == "Long"

window()  => time >= startTime and time <= endTime ? true : false

// Declaring key DCA variables
var float entry_price = 0.0
entry_price := na(entry_price[1]) ? na : entry_price[1]
var float baseOrderPrice = 0.0

//main_stop = strategy.position_avg_price - strategy.position_avg_price*(D_sl/100)
float main_takeprofit = 0
if (isLongBot)
    main_takeprofit := strategy.position_avg_price + strategy.position_avg_price*(targetProfit/100)
else
    main_takeprofit := strategy.position_avg_price - strategy.position_avg_price*(targetProfit/100)
slCondition = false //close <= main_stop
exitConditions = dealStopCondition or slCondition
var float firstBaseOrderPrice = na

///////////////////////////
// UTILITY FUNCTIONS
///////////////////////////
// safety order calculation functions
calcSafetySize(i) => safetyOrder*(pow(volumeScaleInput, i-1))
calcPriceDeviation(index) =>
    var float totalDeviation = 0.0 
    totalDeviation := 0
    for i = 0 to (index-1)
        totalDeviation := totalDeviation + priceDeviation*pow(safetyOrderStepScale,i)
calcSafetyPrice(index) =>
    if (isLongBot)
        baseOrderPrice - (baseOrderPrice * (calcPriceDeviation(index)/100))
    else
        baseOrderPrice + (baseOrderPrice * (calcPriceDeviation(index)/100))


///////////////////////////
// ORDER PLACING
//////////////////////////
// place base order
if(dealStartCondition and strategy.opentrades == 0 and window()) 
    entry_price := close
    if (isLongBot)
        strategy.order('DCA Order', true, qty=baseOrder/entry_price, limit=entry_price, when = window(), comment="Base Order")
    else
        strategy.order('DCA Order', false, qty=baseOrder, limit=entry_price, when = window(), comment="Base Order")
    baseOrderPrice := entry_price
    if (na(firstBaseOrderPrice))
        firstBaseOrderPrice := close

    for i = 1 to maxSafetyOrders
        nextEntryPrice = calcSafetyPrice(i)
        if (isLongBot)
            strategy.order('DCA Order'+tostring(i), true, qty=calcSafetySize(i)/nextEntryPrice, limit=nextEntryPrice, when = window(), comment="Safety Order")
        else
            strategy.order('DCA Order'+tostring(i), false, qty=calcSafetySize(i), limit=nextEntryPrice, when = window(), comment="Safety Order")
        entry_price := nextEntryPrice


if (strategy.opentrades >= 1)
    if (isLongBot)
        // add sell order for whole position and update target price
        strategy.order('DCA Sell', false, qty=strategy.position_size, limit=main_takeprofit, comment="DCA Sell")
    else 
        strategy.order('DCA Buy', true, qty=abs(strategy.position_size), limit=main_takeprofit, comment="DCA Buy")
        
      
// cancel all open orders if we just closed all our open trades
if ((strategy.opentrades[1] > 0 and strategy.opentrades == 0) or exitConditions)
    strategy.close_all()
    strategy.cancel_all()
    baseOrderPrice := 0.0
    entry_price := 0
        

/////////////////////////////////
// DCA PLOTTING
/////////////////////////////////
shouldPlot = strategy.opentrades > 0 and not exitConditions
col = (shouldPlot[1] and shouldPlot) ? color.white : color.new(color.white, 100)
avePlot = plot(strategy.position_avg_price, title="Average Buy Price", color=col, linewidth=2)

desiredSafetyCol = strategy.opentrades[1] > maxSafetyOrders ? color.orange : color.red
safetyCol = (shouldPlot[1] and shouldPlot) ? desiredSafetyCol : color.new(color.white, 100)
nextSafetyPrice = calcSafetyPrice(min(strategy.opentrades, maxSafetyOrders))
safetyPlot = plot(nextSafetyPrice, title="Next Safety Order", color=safetyCol, linewidth=2)

tpCol = (shouldPlot[1] and shouldPlot) ? color.green : color.new(color.white, 100)
tpPlot = plot(main_takeprofit, title="Take Profit", color=tpCol, linewidth=2)

fill(tpPlot, avePlot, color=color.green, transp=80) 
fill(avePlot, safetyPlot, color=color.red, transp=80)


//////////////////////////////
// RESULTS LABEL 
//////////////////////////////
// label to hold the results of the backtest
var label  _resultsLabel    = na
var label  _instructionsLabel = na
var string _labelText      = na
var string _title = " RESULTS\n------------\n\n"
var string _maxUsageText = na
var float  _maxUsage = baseOrder

// -- build our label text
if (na(_labelText))
    for i = 1 to maxSafetyOrders
        _maxUsage := _maxUsage + calcSafetySize(i)
    _maxUsageText := "Max amount for bot usage: " + tostring(_maxUsage, "###,###.########")
   
requiredInitialCapital = _maxUsage
if (not isLongBot)
    requiredInitialCapital := requiredInitialCapital * firstBaseOrderPrice


_labelText := _title 
     + "Profit Percentage: " + tostring(strategy.netprofit / requiredInitialCapital, "#.##%") + "\n"
     + "Total Profit: " + tostring(strategy.netprofit, "###,###.########") + "\n"
     + _maxUsageText + "\n"
     + "Covered Deviation: " + tostring(calcPriceDeviation(maxSafetyOrders)/100, "#.##%") + "\n"
     + "Required Initial Capital: " + tostring(requiredInitialCapital, "###,###.########") + "\n"
     + "\n--------------------------------\n"
     + "Please adjust your initial capital amount to match the \n'Required Initial Capital' amount to get accurate tester results"

f_offsetTime(offset) => time + (time - time[1]) * offset

if (na(_resultsLabel))
    _resultsLabel := label.new(time,close,text=_labelText,xloc=xloc.bar_time,color=color.black, style=label.style_label_left,textcolor=color.white, textalign=text.align_left, size=size.normal)
label.set_xy(_resultsLabel,f_offsetTime(4), high)
label.set_text(_resultsLabel, _labelText)

Overview

MC Strategy - Aggressive calculates WaveTrend oscillator crossovers and a volume-weighted Money Flow index to trigger initial base orders, supporting both long and short deal types. When price moves against the position, it executes scaled safety orders according to configurable price deviation steps and volume multipliers. Positions exit once a defined target profit percentage is reached. Drawn directly on the price chart, it features no stop-loss mechanism.

Details

Published by

Metzenas on GitHub

Type

Strategy, Pine Script v4

Draws

On the price chart

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

  • The strategy can add several entries in the same direction
  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Bot TypestringLongbotType
Base Order Sizefloat350baseOrder
Safety Order Sizefloat350safetyOrder
Max safety order countfloat8maxSafetyOrders
Price deviation in % to open safety ordersfloat1priceDeviation
Safety Order volume scalefloat1.3volumeScaleInput
Safety Order step scalefloat1.1safetyOrderStepScale
Target profit (%)float2targetProfit
Start Timetimetimestamp("1 Mar 2021")startTime
End Timetimetimestamp("31 Dec 2021")endTime
Deal Start ConditionstringIndicatordealStartType
Deal Start SourcesourceclosesourceDealStart
Show all 21 settings
SettingTypeDefaultVariable
Deal Start Valuefloat0sourceDealStartValue
Channel Lengthint10n1
Average Lengthint21n2
Sourcesourcehlc3src
Over Bought Level 1int60obLevel1
Over Bought Level 2int53obLevel2
Over Sold Level 1int-60osLevel1
Over Sold Level 2int-53osLevel2
Lengthint60length
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

Alert BB + RSI [Alorse] is a multi-symbol mean-reversion strategy designed for cryptocurrency trading. It evaluates Bollinger Bands and the Relative Strength Index (RSI) across up to 20 user-defined ticker symbols simultaneously. The strategy enters long posit

Strategy v4 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of Alert BB + RSI [Alorse]
285 stars

Mean Reversion Strategy is an overlay strategy that identifies pullbacks using a long-term EMA, RSI, and candlestick patterns. It generates long signals when a bullish engulfing candle forms below the EMA while RSI is oversold, and short signals when a bearish

Strategy v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Strategy

This strategy draws on the price chart and combines Stochastic RSI, the Money Flow Index, and an exponential moving average to open long or short trades. Longs use the lower oscillator thresholds and shorts use the upper ones, with direction limited to longs,

Strategy v5 Oscillators & momentumMoving averages Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3 Indicator Strategy (StochRSI, MFI & EMA) With Safety Orders
104 stars

This strategy combines Bollinger Bands with a short RSI to fade moves that leave the bands and then return. A long requires the prior bar below the lower band and oversold, then a close back inside the band while RSI is between 30 and 50. Shorts mirror that at

Strategy v5 Oscillators & momentumVolatility & bands
Updated 3 years ago · MIT GitHub repository of Bollinger Bands and RSI Strategy
53 stars

This strategy implements Larry Connors' 2-period RSI mean-reversion algorithm overlaid on the price chart. It filters trades using a long-term Simple Moving Average (SMA), entering long positions when the price trades above the SMA and the short-term RSI drops

Strategy v6 Oscillators & momentumMean reversion
Updated 2 months ago · MPL-2.0 GitHub repository of Larry Connors 2-Period RSI Strategy

More Oscillators & momentum scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers