// Define User Inputs
const string group1 = "Calculation"
const string group2 = "UI Settings"
simple int len_dema = input.int(15, minval=1, title = "DEMA Calculation Period", tooltip = "The DEMA is being used as the source for the DMI Calculation", group = group1 )
simple int di_len = input.int(18, minval=1, title = "DI Length", group = group1 )
simple int adx_smoothing_len = input.int(18, minval=1, title = "ADX Smoothing Period", group = group1 )
simple bool showdivergences = input.bool(false, "Show Detected Divergences?", group = 'DIVERGENCES')
simple int lbR = input.int(title="Pivot Lookback Right", defval=10,group = 'DIVERGENCES', inline = "div")
simple int lbL = input.int(title="Pivot Lookback Left", defval=10,group = 'DIVERGENCES', inline = "div")
simple int signalLen = 7
simple bool showoboslvls = input.bool(true, "Show Static High and Low Levels (Extreme DMI Thresholds)", group =group2)
simple bool paintCandles = input.bool(false, "Color Bars According to Trend?", tooltip = "If DMI > 0 bar will be green and turn to red when it is below 0", group =group2)
simple bool show_big_ob_os = input.bool(true, "Show Extreme Value Background Hues?", group =group2)
simple int ob_trigger = input.int(30, "Extreme OB (Upper) Thresholds", group = group2)
simple int os_trigger = input.int(-30, "Extreme OS (Lower) Thresholds", group = group2)
// DEMA DMI Function
dema_dmi(len_dema, di_len, adx_smoothing_len)=>
demah = ta.dema(high, len_dema)
demal = ta.dema(low, len_dema)
u = ta.change(demah)
d = -ta.change(demal)
p = na(u) ? na : (u > d and u > 0 ? u : 0)
m = na(d) ? na : (d > u and d > 0 ? d : 0)
t = ta.rma(ta.tr, di_len)
plus = fixnan(100 * ta.rma(p, di_len) / t)
minus = fixnan(100 * ta.rma(m, di_len) / t)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adx_smoothing_len)
[adx,plus,minus]
Certainly! Below is the converted code from your indicator to a strategy in Pine Script™ v5. This strategy enters a long position when the DMI histogram crosses above zero and enters a short position when the DMI histogram crosses below zero. It also includes customizable stop loss and take profit levels as a percentage of the entry price.
pine
//@version=5
strategy(
title="DEMA Adaptive DMI Strategy [BackQuant]",
shorttitle="DEMA DMI Strategy [BackQuant]",
overlay=false,
initial_capital=100000, // Set your initial capital
default_qty_type=strategy.percent_of_equity,
default_qty_value=10, // Use 10% of equity per trade
pyramiding=0 // Allow only one entry per direction
)
// User Inputs
const string group1 = "Calculation"
const string group2 = "Strategy Settings"
int len_dema = input.int(15, minval=1, title="DEMA Calculation Period", tooltip="The DEMA is being used as the source for the DMI Calculation", group=group1)
int di_len = input.int(18, minval=1, title="DI Length", group=group1)
int adx_smoothing_len = input.int(18, minval=1, title="ADX Smoothing Period", group=group1)
float stopLossPercent = input.float(1.0, "Stop Loss %", minval=0.0, maxval=100.0, step=0.1, group=group2)
float takeProfitPercent = input.float(2.0, "Take Profit %", minval=0.0, maxval=100.0, step=0.1, group=group2)
// DEMA DMI Function
dema_dmi(len_dema, di_len, adx_smoothing_len) =>
demah = ta.dema(high, len_dema)
demal = ta.dema(low, len_dema)
u = ta.change(demah)
d = -ta.change(demal)
p = na(u) ? na : (u > d and u > 0 ? u : 0)
m = na(d) ? na : (d > u and d > 0 ? d : 0)
t = ta.rma(ta.tr, di_len)
plus = fixnan(100 * ta.rma(p, di_len) / t)
minus = fixnan(100 * ta.rma(m, di_len) / t)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adx_smoothing_len)
[adx, plus, minus]
[adx, plus, minus] = dema_dmi(len_dema, di_len, adx_smoothing_len)
// DMI Histogram Calculation
plotosc = plus - minus
// Strategy Conditions
longCondition = ta.crossover(plotosc, 0)
shortCondition = ta.crossunder(plotosc, 0)
// Execute Strategy
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
// Apply Stop Loss and Take Profit
if (strategy.position_size > 0)
strategy.exit("Exit Long", "Long", stop=strategy.position_avg_price * (1 - stopLossPercent / 100), limit=strategy.position_avg_price * (1 + takeProfitPercent / 100))
if (strategy.position_size < 0)
strategy.exit("Exit Short", "Short", stop=strategy.position_avg_price * (1 + stopLossPercent / 100), limit=strategy.position_avg_price * (1 - takeProfitPercent / 100))
// Plotting for Visualization
plot(plotosc, title="DMI Histogram", color=color.blue, style=plot.style_histogram, linewidth=2)
hline(0, "Zero Line", color=color.gray)
// Alert Conditions (Optional)
alertcondition(longCondition, title="DEMA DMI Long", message="DEMA DMI Long {{exchange}}:{{ticker}}")
alertcondition(shortCondition, title="DEMA DMI Short", message="DEMA DMI Short {{exchange}}:{{ticker}}")
Explanation:
Strategy Declaration:
Changed indicator to strategy to convert the script into a strategy.
Set initial_capital, default_qty_type, and default_qty_value for position sizing.
Set pyramiding to 0 to allow only one open position per direction.
Inputs:
Kept the original calculation inputs (len_dema, di_len, adx_smoothing_len).
Added inputs for stopLossPercent and takeProfitPercent to customize risk management.
DEMA DMI Function:
The function dema_dmi calculates the DMI based on DEMA, as in the original script.
Returns adx, plus, and minus values used in the strategy.
Strategy Conditions:
longCondition is true when the DMI histogram crosses above zero.
shortCondition is true when the DMI histogram crosses below zero.
Execute Strategy:
When longCondition is true, the strategy enters a long position.
When shortCondition is true, the strategy enters a short position.
Risk Management:
The strategy.exit function sets stop loss and take profit levels based on the entry price and the percentages provided.
Stop loss and take profit levels are recalculated each time there's an open position.
Plotting:
Plots the DMI histogram (plotosc) for visualization.
Includes a horizontal line at zero to help identify crossovers.
Alerts:
Optional alert conditions are included for when the DMI histogram crosses above or below zero.
Note:
The divergence detection and other plotting features from the original indicator are omitted for simplicity since they don't directly affect the strategy logic.
You can adjust the stopLossPercent and takeProfitPercent inputs to suit your risk management preferences.
Ensure to test the strategy with different settings and timeframes to evaluate its performance before applying it to live trading.