// 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]
To convert your indicator into a strategy, you'll need to change the script type from indicator to strategy and implement entry and exit conditions using strategy.entry and strategy.exit functions. Below is the converted strategy code based on your original script:
Script Type Change: Changed from indicator to strategy to enable backtesting and trading functionalities.
Entry Conditions: Implemented strategy.entry to open positions when the plotosc crosses over zero for longs and crosses under zero for shorts.
Position Management: By using the same order ID "Trade" in strategy.entry, the strategy automatically handles position reversal when a new signal is generated.
Removed Unnecessary Variables: Omitted variables and conditions related to plotting that are not essential for the strategy execution.
Alerts Update: Modified alert conditions to correspond with the new strategy logic.
How It Works:
Long Entry: When the plotosc histogram crosses above zero, the strategy enters a long position.
Short Entry: When the plotosc histogram crosses below zero, the strategy enters a short position.
Position Reversal: Using the same order ID allows the strategy to reverse positions automatically without the need for explicit exit commands.
Plotting and Visuals: The plotting code is retained for visualization purposes, so you can see the DMI histogram and divergence signals on the chart.
Note:
Position Sizing: Adjust the default_qty_value to control the percentage of equity used per trade.
Testing Period: Use the strategy tester to evaluate performance over different timeframes and market conditions.
Risk Management: Consider implementing stop-loss and take-profit levels for better risk management.
Feel free to adjust the parameters and conditions to better suit your trading style and to optimize the strategy's performance.