Strategy · Pine Script v5 · On the chart

Diverlong F002

by ruckard

Diverlong F002 is a price-chart strategy that enters on Squeeze Momentum divergences and exits on Curie closing moving-average lines.

View on GitHub

GitHub stars13on the repository
Pine versionv5runs as is
Lines of code1,509
Settings40inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago2 Mar 2025

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

1,509 lines of Pine Script v5 from scripts/diverlong/diverlong_f_adxatr.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

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

//@version=5

// Current version: v20221030 ( F002 )
// Tradingview Public description - BEGIN
// 
// This strategy serves as a template for Ruckard strategies that interact with Zignaly.
// This specific strategy also includes Risk Management.
// 
// It was originally tested at BTCUSDT pair and 1H timeframe.
// Divergences trigger entry points.
// Curie Closing lines trigger exit points.
// We finally make sure that it's an actual divergence by checking two pairs at the same time.
// More details about how divergence is found:
// This strategy checks changes between the different sections of the Squeeze Momentum Indicator.
// Light-Green to Dark-Green change is compared to former Light-Green to Dark-Green change.
// If the squeeze value is smaller and at the same time the price is higher then we short.
// Light-Red to Dark-Red change is compared to former Light-Red to Dark-Red change.
// If the squeeze value is higher and at the same time the price is lower then we long.
//
// Before using this documentation it's recommended that you:
// [LIST]
// [*] Use default TradingView strategy script or another script and setup its associated alert manually. Just make the alert pop-up in the screen.
// [*] Create a 'Copy-Trader provider' (or Signal Provider) in Zignaly and send signals to it either thanks to your browser or with some basic programming.
// [/LIST]

// [B]SETTINGS[/B]
// [B]__ SETTINGS - Capital[/B]
// [LIST]
// [*] (CAPITAL) Capital quote invested per order in USDT units {100.0}. This setting is only used when '(ZIG) Provider type' is set to 'Signal Provider'.
// [*] (CAPITAL) Capital percentage invested per order (%) {25.0}. This setting is only used when '(ZIG) Provider type' is set to 'Copy Trader Provider'.
// [/LIST]
// [B]__ SETTINGS - Misc[/B]
// [LIST]
// [*] (ZIG) Enable Alert message {True}: Whether to enable alert message or not.
// [*] (ZIG) Service Secret Key {MYSECRETKEY}: Your service secret key. You can find it in your service Edit tab.
// [*] (DEBUG) Enable debug on order comments {False}: Whether to show alerts on order comments or not.
// [*] Number of decimal digits for Prices {2}.
// [*] (DECIMAL) Maximum number of decimal for contracts {3}.
// [/LIST]
// [B]__ SETTINGS - Zignaly[/B]
// [LIST]
// [*] (ZIG) Integration type {TradingView only}: [b]Hybrid[/b]: Both TradingView and Zignaly handle take profit, trailing stops and stop losses. Useful if you are scared about TradingView not firing an alert. It might arise problems if TradingView and Zignaly get out of sync. [b]TradingView only[/b]: TradingView sends entry and exit orders to Zignaly so that Zignaly only buys or sells. Zignaly won't handle stop loss or other settings on its own.
// [*] (ZIG) Zignaly Alert Type {WebHook}: 'Email' or 'WebHook'.
// [*] (ZIG) Provider type {Profit Sharing Provider}: 'Profit Sharing Provider', 'Copy Trader Provider', or 'Signal Provider'. 'Copy Trader Provider' or 'Profit Sharing Provider' sends a percentage to manage. 'Signal Provider' sends a quote to manage.
// [*] (ZIG) Exchange {Zignaly}: 'Zignaly', 'Binance' or 'Kucoin'.
// [*] (ZIG) Exchange Type {Futures}: 'Spot' or 'Futures'.
// [*] (ZIG) Leverage {1}. Set it to '1' when '(ZIG) Exchange Type' is set to 'Spot'.
// [/LIST]
// [B]__ SETTINGS - Strategy[/B]
// [LIST]
// [*] (STRAT) Strategy Type: 'Long and Short', 'Long Only' or 'Short Only'.
// [*] (STRAT) Strategy Start Time {2021-01-01 00:00:00}: When to start the strategy if '(STRAT) Strategy Start Time forced?' setting is set to true.
// [*] (STRAT) Strategy Stop Time {2021-12-31 00:00:00}: When to stop the strategy if '(STRAT) Strategy Stop Time forced?' setting is set to true.
// [*] (STRAT) Strategy Start Time forced? {True}: Whether to start the strategy from a given point in time.
// [*] (STRAT) Strategy Stop Time forced? {False}: Whether to stop the strategy after a given point in time.
// [*] (STOPTAKE) Take Profit? {false}: Whether to enable Take Profit.
// [*] (STOPTAKE) Stop Loss? {True}: Whether to enable Stop Loss.
// [*] (TRAILING) Enable Trailing Take Profit (%) {True}: Whether to enable Trailing Take Profit.
// [*] (STOPTAKE) Take Profit % {3.0}: Take profit percentage. This setting is only used when '(STOPTAKE) Take Profit?' setting is set to true.
// [*] (STRAT) Risk/Reward Ratio % {1.5}. Take Profit is StopLoss times Risk/Reward Ratio set here.
// [*] (STOPTAKE) Stop Loss % {2.0}: Stop loss percentage. This setting is only used when '(STOPTAKE) Stop Loss?' setting is set to true.
// [*] (TRAILING) Trailing Take Profit Trigger (%) {2.5}: Trailing Stop Trigger Percentage. This setting is only used when '(TRAILING) Enable Trailing Take Profit (%)' setting is set to true.
// [*] (TRAILING) Trailing Take Profit as a percentage of Trailing Take Profit Trigger (%) {25.0}: Trailing Stop Distance Percentage. This setting is only used when '(TRAILING) Enable Trailing Take Profit (%)' setting is set to true.
// [*] (RECENT) Number of minutes to wait to open a new order after the previous one has been opened {6}.
// [/LIST]
// [B]____ SETTINGS - Risk Management [/B]
// [list]
// [*] (RISKM) Risk Management Type: 'Safe', 'Somewhat safe compound' or 'None'. '[b]Safe[/b]': Calculations are always done with the initial capital (1000) in mind. The maximum losses per trade/day/week/month are taken into account. '[b]Somewhat safe compound[/b]': Calculations are done with initial capital (1000) or a higher capital if it increases. The maximum losses per trade/day/week/month are taken into account. '[b]None[/b]': In each order all the current capital is gambled and only the default stop loss per order is taken into account. That means that the maximum losses per trade/day/week/month are not taken into account. [b]Default[/b]: 'Somewhat safe compound'.
// [*] (RISKM) Maximum loss per trade % {1.0}.
// [*] (RISKM) Maximum loss per day % {6.0}.
// [*] (RISKM) Maximum loss per week % {8.0}.
// [*] (RISKM) Maximum loss per month % {10.0}.
// [/list]

// [B]DEFAULT SETTINGS[/B]

// By default this strategy has been setup with these beginner settings:
// [LIST]
// [*] '(ZIG) Integration type' : TradingView only
// [*] '(ZIG) Provider type'    : 'Copy Trader Provider'
// [*] '(ZIG) Exchange'         : 'Binance'
// [*] '(ZIG) Exchange Type'    : 'Spot'
// [*] '(STRAT) Strategy Type'  : 'Long Only'
// [*] '(ZIG) Leverage'         : '1' (Or no leverage)
// [/LIST]
// but you can change those settings if needed.


// [B]FIRST STEP[/B]
// [LIST]
// [*] For both future or spot markets you should make sure to change '(ZIG) Zignaly Alert Type' to match either WebHook or Email. If you have a non paid account in TradingView as in October 2020 you would have to use Email which it's free to use.
// [/LIST]

// [B]RECOMMENDED SETTINGS[/B]
// [B]__ RECOMMENDED SETTINGS - Spot markets[/B]
// [LIST]
// [*] '(ZIG) Exchange Type'   setting should be set to 'Spot'
// [*] '(STRAT) Strategy Type' setting should be set to 'Long Only'
// [*] '(ZIG) Leverage'        setting should be set to '1'
// [/LIST]
// [B]__ RECOMMENDED SETTINGS - Future markets[/B]
// [LIST]
// [*] '(ZIG) Exchange Type'   setting should be set to 'Futures'
// [*] '(STRAT) Strategy Type' setting should be set to 'Long and Short'
// [*] '(ZIG) Leverage'        setting might be changed if desired.
// [/LIST]
// [B]__ RECOMMENDED SETTINGS - Signal Providers[/B]
// [LIST]
// [*] '(ZIG) Provider type'   setting should be set to 'Signal Provider'
// [*] '(CAPITAL) Capital quote invested per order in USDT units' setting might be changed if desired.
// [/LIST]
// [B]__ RECOMMENDED SETTINGS - Copy Trader Providers[/B]
// [LIST]
// [*] '(ZIG) Provider type'   setting should be set to 'Copy Trader Provider'
// [*] '(CAPITAL) Capital percentage invested per order (%)' setting might be changed if desired.
// [*] Strategy Properties setting: 'Initial Capital' might be changed if desired.
// [/LIST]

// [B]INTEGRATION TYPE EXPLANATION[/B]
// [LIST]
// [*] 'Hybrid': Both TradingView and Zignaly handle take profit, trailing stops and stop losses. Useful if you are scared about TradingView not firing an alert. It might arise problems if TradingView and Zignaly get out of sync.
// [*] 'TradingView only': TradingView sends entry and exit orders to Zignaly so that Zignaly only buys or sells. Zignaly won't handle stop loss or other settings on its own.
// [/LIST]

// [B]HOW TO USE THIS STRATEGY[/B]
// [LIST]
// [*] Beginner: Copy and paste the strategy and change it to your needs. Turn off '(DEBUG) Enable debug on order comments' setting.
// [*] Medium: Reuse functions and inputs from this strategy into your own as if it was a library.
// [*] Advanced: Check Strategy Tester. List of trades. Copy and paste the different suggested 'alert_message' variable contents to your script and change them to your needs.
// [*] Expert: You needed a way to pass data from TradingView script to the alert. Now you know it's the 'alert_message' variable. You can find several examples in the source code using the variable. You also have seen 'ALERTS SETUP' explanation below. An additional quick look at 'Strategy Tester', 'List of Trades' and you are ready to go.
// [/LIST]


// [B] ALERTS SETUP[/B]
// This is the important piece of information that allows you to connect TradingView to Zignaly in a semi-automatic manner.
// [B] __ ALERTS SETUP - WebHook[/B]
// [LIST]
// [*] Webhook URL: https : // zignaly . com / api / signals.php?key=MYSECRETKEY
// [*] Message: {{strategy.order.alert_message}}
// [/LIST]
// [B] __ ALERTS SETUP - Email[/B]
// [LIST]
// [*] Setup a new Hotmail account
// [*] Add it as an 'SMS email' in TradingView Profile settings page.
// [*] Confirm your own the email address
// [*] Create a rule in your Hotmail account that 'Redirects' (not forwards) emails to 'signals @ zignaly . email' when (1): 'Subject' includes 'Alert', (2): 'Email body' contains string 'MYZIGNALYREDIRECTTRIGGER' and (3): 'From' contains 'noreply @ tradingview . com'.
// [*] In 'More Actions' check: Send Email-to-SMS
// [*] Message: {{strategy.order.alert_message}}
// MYZIGNALYREDIRECTTRIGGER
// [/LIST]


// '(DEBUG) Enable debug on order comments' is turned on by default so that you can see in the Strategy Tester. List of Trades. The different orders alert_message that would have been sent to your alert. You might want to turn it off it some many letters in the screen is problem.

// [B]STRATEGY ADVICE[/B]
// [LIST]
// [*] If you turn on 'Take Profit' then turn off 'Trailing Take Profit'.
// [/LIST]
//
// [B]ZIGNALY SIDE ADVICE[/B]
// [LIST]
// [*] If you are a 'Signal Provider' make sure that 'Allow reusing the same signalId if there isn't any open position using it?' setting in the profile tab is set to true.
// [*] You can find your 'MYSECRETKEY' in your 'Copy Trader/Signal' provider Edit tab at 'Signal URL'.
// [/LIST]
//
// [B]ADDITIONAL ZIGNALY DOCUMENTATION[/B]
// This beginner's guide is quite basic and meant to be an introduction. Please read additional documentation to learn what you actually can do with Zignaly.
// [LIST]
// [*] docs . zignaly . com / signals / how-to -- How to send signals to Zignaly
// [*] help . zignaly . com / hc/en-us/categories/360002703720-Sending-Signals -- Sending zignals
// [/LIST]
//
// [B]FINAL REMARKS[/B]
// [LIST]
// [*] This strategy tries to match the Pine Script Coding Conventions as best as possible.
// [*] You can check my 'Ruckard TradingLatino' strategy for a more complex strategy that it's also integrated with Zignaly. Unfortunatley it does not use Pine Script Coding Conventions as much.
// [/LIST]

// Tradingview Public description - END

strategy('Diverlong F002', shorttitle='F002Diverlong', overlay=true, max_bars_back=5000, calc_on_order_fills=false, calc_on_every_tick=false, pyramiding=0, initial_capital=1000, slippage=1, commission_type=strategy.commission.percent, commission_value=0.04)

import ruckard/RiskManagementBeta/4 as rm
import ruckard/ZignalyBeta/21 as zig

// INPUTS - BEGIN

// Strategy - Inputs
i_enableZignalyAlert = input(true, '(ZIG) Enable Alert message {True}')
i_zignalyServiceSecretKey = input.string('MYSECRETKEY', '(ZIG) Service Secret Key {MYSECRETKEY}')
_ZIGHYBRIDINTEGRATION_ = 'Hybrid', _ZIGTVONLYINTEGRATION_ = 'TradingView only'
i_zignalyIntegrationTypePre = input.string(_ZIGHYBRIDINTEGRATION_, '(ZIG) Integration type', options=[_ZIGTVONLYINTEGRATION_, _ZIGHYBRIDINTEGRATION_])
i_zignalyIntegrationType = (i_zignalyIntegrationTypePre == _ZIGHYBRIDINTEGRATION_) ? 'hybrid' : 'nonhybrid'
_ZIGSIGNALPROVIDER_ = 'Signal Provider', _ZIGCOPYTRADERPROVIDER_ = 'Profit Sharing/Copy Trader Provider'
i_zignalyProviderTypePre = input.string(_ZIGCOPYTRADERPROVIDER_, '(ZIG) Provider type', options=[_ZIGSIGNALPROVIDER_, _ZIGCOPYTRADERPROVIDER_])
i_zignalyProviderType = (i_zignalyProviderTypePre == _ZIGCOPYTRADERPROVIDER_) ? 'copytrader' : 'signalprovider'
L_S = 'Long and Short', _L_ = 'Long Only', _S_ = 'Short Only', _REVERSE_ = 'Reverse'
i_strategyType = input.string(_L_, '(STRAT) Strategy Type', options=[L_S, _L_, _S_, _REVERSE_])
i_enableLongStrictStrategy = input.bool(true, '(STRAT) (L) Strict mode {True}')
i_enableShortStrictStrategy = input.bool(false, '(STRAT) (S) Strict mode {False}')
i_strategyStartTime = input.time(timestamp('01 Jan 2021'), '(STRAT) Strategy Start Time {2021-01-01 00:00:00}')
i_strategyStopTime = input.time(timestamp('31 Dec 2021'), '(STRAT) Strategy Stop Time {2021-12-31 00:00:00}')
i_strategyStartTimeOn = input.bool(true, '(STRAT) Strategy Start Time forced? {True}')
i_strategyStopTimeOn = input.bool(false, '(STRAT) Strategy Stop Time forced? {False}')

// Curie Closing Lines - Inputs
i_enableCurieClosingLines = input.bool(true, title = 'Use {True}', group='Curie Closing Lines', inline='CurieEnableShow')
i_showCurieClosingLines = input.bool(true, title = 'Show {True}', group='Curie Closing Lines', inline='CurieEnableShow')

i_curieHighClosingMaType = input.string(defval = 'SMA', options = ['SMA', 'EMA', 'VWMA', 'RMA'], title = 'High MA {SMA/20/ohlc4}', group='Curie Closing Lines', inline='High Closing Ma')
i_curieHighClosingMaLength = input.int(20, minval = 1,  title = '', group='Curie Closing Lines', inline='High Closing Ma')
i_curieHighClosingMaSource = input.source(ohlc4, title = '', group='Curie Closing Lines', inline='High Closing Ma')
i_curieHighestClosingMaLength = input.int(5, minval = 1,  title = 'Highest MA Length {5}', group='Curie Closing Lines')

i_curieLowClosingMaType = input.string(defval = 'SMA', options = ['SMA', 'EMA', 'VWMA', 'RMA'], title = 'Low MA {SMA/25/ohlc4}', group='Curie Closing Lines', inline='Low Closing Ma')
i_curieLowClosingMaLength = input.int(25, minval = 1,  title = '', group='Curie Closing Lines', inline='Low Closing Ma')
i_curieLowClosingMaSource = input.source(ohlc4, title = '', group='Curie Closing Lines', inline='Low Closing Ma')
i_curieLowestClosingMaLength = input.int(10, minval = 1,  title = 'Lowest MA Length {10}', group='Curie Closing Lines')

i_curieSource = input.source(ohlc4, title = 'Source {ohlc4}', group='Curie Closing Lines')

// Order comments based debug
i_enableOrderCommentDebug = input.bool(false, title='(DEBUG) Enable debug on order comments {False}')
i_enableTakeProfit = input.bool(false, '(STOPTAKE) Take Profit? {false}')
i_enableStopLoss = input.bool(true, '(STOPTAKE) Stop Loss? {True}')
i_enableTrailingTakeProfit = input.bool(false, title='(TRAILING) Enable Trailing Take Profit (%) {False}')
i_enableBreakEven = input.bool(false, title='(BREAKEVEN) Enable Break Even (%) {False}')
i_enableCustomBreakEven = input.bool(false, title='(BREAKEVEN) Enable Custom Break Even (%) {False}')
i_enableRiskRewardRatio = input.bool(false, title='(STOPTAKE) Calculate Take Profit from R/R Ratio (%) {False}')
i_takeProfit = input.float(1.1, title='(STOPTAKE) Take Profit % {3.0}')
i_takeProfitPerOne = i_takeProfit / 100
i_positionRatio = input.float(1.5, title='(STRAT) Risk/Reward Ratio % {1.5}', minval=0.01)
i_stopLoss = input.float(7.0, title='(STOPTAKE) Stop Loss % {7.0}', minval=0.01) / 100
_SLConstant_ = 'Constant', _SLATR_ = 'ATR'
i_stopLossTypePre = input.string(_SLATR_, '(STOPTAKE) Stop Loss type', options=[_SLConstant_, _SLATR_])
i_stopLossType = (i_stopLossTypePre == _SLConstant_) ? 'constant' : (i_stopLossTypePre == _SLATR_) ? 'atr' : ''
i_atrStopLossLength = input.int(14, minval = 1,  title = '(STOPTAKE) ATR Stop Loss Length [14]')
i_atrStopLossSource = input.source(close, title = '(STOPTAKE) ATR Stop Loss Source [close]')
i_atrStopLossMultiplier = input.float(2.0, title='(STOPTAKE) ATR Stop Loss Multiplier % {2.0}', minval=0.01)
// Trailing Take Profit - Inputs
i_trailingTakeProfitPercentageTrigger = input.float(1.2, '(TRAILING) Trailing Take Profit Trigger (%) {2.5}', minval=0, step=0.01)
i_trailingTakeProfitPercentageTriggerPerOne = i_trailingTakeProfitPercentageTrigger * 0.01
i_trailingTakeProfitPercentageOffset = input.float(25.0, '(TRAILING) Trailing Take Profit as a percentage of Trailing Take Profit Trigger (%) {25.0}', minval=0, step=0.01)
i_trailingTakeProfitPercentageOffsetPerOne = i_trailingTakeProfitPercentageOffset * 0.01
// Break Even - Inputs
i_breakEvenTrigger = input.float(2.15, '(BREAKEVEN) Break Even Trigger (%) {2.15}', minval=0, step=0.01)
i_breakEvenTriggerPerOne = i_breakEvenTrigger * 0.01
i_breakEven = input.float(0.25, '(BREAKEVEN) Break Even Stop Loss (%) {0.25}', minval=0, step=0.01)
i_breakEvenPerOne = i_breakEven * 0.01
// Zignaly - Email (zignalye)
_ZIGNALYE_ = 'Email', _ZIGNALYW_ = 'WebHook'
i_zignalyAlertTypePre = input.string(_ZIGNALYW_, '(ZIG) Zignaly Alert Type {WebHook}', options=[_ZIGNALYW_, _ZIGNALYE_])
i_zignalyAlertType = (i_zignalyAlertTypePre == _ZIGNALYW_) ? 'webhook' : 'email'
_ZIGEXZIGNALY_ = 'Zignaly', _ZIGEXBINANCE_ = 'Binance', _ZIGEXKUCOIN_ = 'Kucoin'
i_zignalyExchange = input.string(_ZIGEXZIGNALY_, '(ZIG) Exchange {Zignaly}', options=[_ZIGEXZIGNALY_, _ZIGEXBINANCE_, _ZIGEXKUCOIN_])
_ZIGEXTYPESPOT_ = 'spot', _ZIGEXFUTURES_ = 'futures'
i_zignalyExchangeType = input.string(_ZIGEXFUTURES_, '(ZIG) Exchange Type {Futures}', options=[_ZIGEXFUTURES_, _ZIGEXTYPESPOT_])
i_zignalyLeverage = input.int(1, '(ZIG) Leverage {1}', minval=1, step=1, maxval=125)
var i_zignalyLeverage_str = str.tostring(i_zignalyLeverage)  // It needs to run only once
i_priceDecimalDigits = input.int(2, 'Number of decimal digits for Prices {2}', minval=0, maxval=10, step=1)
// Decimal - Inputs
i_contractMaximumDecimalNumber = input.int(3, title='(DECIMAL) Maximum number of decimal for contracts {3}', minval=0)
i_tooRecentMinutesNumber = input.int(0, '(RECENT) Number of minutes to wait to open a new order after the previous one has been opened {0}', minval=0, maxval=100, step=1)
i_CapitalQuote = input.float(100.0, title='(CAPITAL) Capital quote invested per order in USDT units {100.0}')
i_CapitalPercentage = input.float(25.0, title='(CAPITAL) Capital percentage invested per order (%) {25.0}', minval=0, step=0.01) * 0.01
// Risk Management - Inputs
_SAFE_ = 'Safe', _SCOMPOUND_ = 'Somewhat safe compound', _NONE_ = 'None', _CUSTOM_ = 'Custom', _FORCED_ = 'Forced'
i_riskManagementTypePre = input.string(_SCOMPOUND_, '(RISKM) Risk Management Type', options=[_SAFE_, _SCOMPOUND_, _NONE_, _CUSTOM_, _FORCED_])
i_riskManagementType = (i_riskManagementTypePre == _SAFE_) ? 'SAFE' : (i_riskManagementTypePre == _SCOMPOUND_) ? 'SCOMPOUND' : (i_riskManagementTypePre == _NONE_) ? 'NONE' : (i_riskManagementTypePre == _CUSTOM_) ? 'CUSTOM' : 'FORCED'
i_riskManagementMaximumLossPerTrade = input.float(1.0, title='(RISKM) Maximum loss per trade % {1.0}') / 100
i_riskManagementMaximumLossPerDay = input.float(6.0, title='(RISKM) Maximum loss per day % {6.0}') / 100
i_riskManagementMaximumLossPerWeek = input.float(8.0, title='(RISKM) Maximum loss per week % {8.0}') / 100
i_riskManagementMaximumLossPerMonth = input.float(10.0, title='(RISKM) Maximum loss per month % {10.0}') / 100

i_enableVolatilityProtection = input.bool(false, title='Enable {False}', group='ATR Volatility Protection')
i_showAtrVolatilityMultiplier = input.bool(false, title='Show {False}', group='ATR Volatility Protection')

i_atrVolatilityLength = input.int(14, minval = 1,  title='{14/close/1.10 %}', group='ATR Volatility Protection', inline='ATRVolatilitySettings')
i_atrVolatilitySource = input.source(close, title='', group='ATR Volatility Protection', inline='ATRVolatilitySettings')
i_atrVolatilityMinimumMultiplier = input.float(1.10, title='Min/Max {1.10/2.00}', minval=0.01, group='ATR Volatility Protection', inline='ATRVolatilityMinMax')
i_atrVolatilityMaximumMultiplier = input.float(2.00, title='', minval=0.01, group='ATR Volatility Protection', inline='ATRVolatilityMinMax')

i_higherTimeframeTrendMaEnforce = input.bool(false, title='Use {False}', group='Enforce Higher Timeframe Trend', inline='HTTimeframeUseShow')
i_showHigherTimeframeTrendMa = input.bool(false, title='Show {False}', group='Enforce Higher Timeframe Trend', inline='HTTimeframeUseShow')

i_higherTimeframeTrendMaTimeframe = input.timeframe('60', title='Timeframe/Offset {1 hour/0 bars}', group='Enforce Higher Timeframe Trend', inline='HTTimeframeOffset')

i_higherTimeframeTrendMaOffset = input.int(0, title='', group='Enforce Higher Timeframe Trend', inline='HTTimeframeOffset')

i_higherTimeframeTrendFastMaType = input.string(defval = 'VWMA', options = ['SMA', 'EMA', 'VWMA', 'RMA', 'WMA'], title = 'Fast MA {EMA/10/close}', group='Enforce Higher Timeframe Trend', inline='HTFastMa')
i_higherTimeframeTrendFastMaLength = input.int(10, minval = 1,  title = '', group='Enforce Higher Timeframe Trend', inline='HTFastMa')
i_higherTimeframeTrendFastMaSource = input.source(close, title = '', group='Enforce Higher Timeframe Trend', inline='HTFastMa')

i_higherTimeframeTrendSlowMaType = input.string(defval = 'VWMA', options = ['SMA', 'EMA', 'VWMA', 'RMA', 'WMA'], title = 'Slow MA {EMA/55/close}', group='Enforce Higher Timeframe Trend', inline='HTSlowMa')
i_higherTimeframeTrendSlowMaLength = input.int(55, minval = 1,  title = '', group='Enforce Higher Timeframe Trend', inline='HTSlowMa')
i_higherTimeframeTrendSlowMaSource = input.source(close, title = '', group='Enforce Higher Timeframe Trend', inline='HTSlowMa')

// Too old order - Inputs
i_enableTooOldLongOrderClose = input.bool(false, title='Enable/Bars/SL (L) {False/6/1.20 %}', group='Too Old Orders', inline='Too Old Long Orders')
i_tooOldLongBarsNumber = input.int(6, title='', minval=1, maxval=100, step=1, group='Too Old Orders', inline='Too Old Long Orders')
i_tooOldLongStopLoss = input.float(1.20, title='', tooltip='(OLD) Too Old Long order Stop Loss (%) {1.20}', minval=0, step=0.01, group='Too Old Orders', inline='Too Old Long Orders')
i_tooOldLongStopLossPerOne = i_tooOldLongStopLoss * 0.01

i_enableTooOldShortOrderClose = input.bool(false, title='Enable/Bars/SL (S) {False/6/0.60 %}', group='Too Old Orders', inline='Too Old Short Orders')
i_tooOldShortBarsNumber = input.int(6, title='', minval=1, maxval=100, step=1, group='Too Old Orders', inline='Too Old Short Orders')
i_tooOldShortStopLoss = input.float(0.60, title='', tooltip='(OLD) Too Old Short order Stop Loss (%) {0.60}', minval=0, step=0.01, group='Too Old Orders', inline='Too Old Short Orders')
i_tooOldShortStopLossPerOne = i_tooOldShortStopLoss * 0.01

i_enableLongRSIProtection = input.bool(true, title='Enable (L) RSI Protection {True, 45.0}', group='RSI Protection', inline='LongRSIProtection')
i_longRSIProtectionLevel = input.float(45.0, title='', group='RSI Protection', inline='LongRSIProtection', tooltip='Not greater than')
i_enableShortRSIProtection = input.bool(true, title='Enable (S) RSI Protection {True, 58.0}', group='RSI Protection', inline='ShortRSIProtection')
i_shortRSIProtectionLevel = input.float(58.0, title='', group='RSI Protection', inline='ShortRSIProtection', tooltip='Not lower than')
i_enableRSIProtectionCustomTimeframe = input.bool(true, title='Use Custom Timeframe {True, 3 hours}', group='RSI Protection', inline='RSIProtectionTimeframe')
i_RSIProtectionCustomTimeframe = input.timeframe('180', title='', group='RSI Protection', inline='RSIProtectionTimeframe')
i_RSIProtectionLength = input.int(14, minval=1, title='RSI Length', group='RSI Protection')
i_RSIProtectionSource = input.source(close, 'RSI Source', group='RSI Protection')

i_enableLongDynamicRSIProtection = input.bool(true, title='(L) Dynamic RSI Level {True, 0.0, 50.0}', group='RSI Protection', inline='LongDynamicRSIProtection')
i_longDynamicRSIProtectionMinimum = input.float(0, minval=0, maxval=100, title='', group='RSI Protection', inline='LongDynamicRSIProtection')
i_longDynamicRSIProtectionMaximum = input.float(50, minval=0, maxval=100, title='', group='RSI Protection', inline='LongDynamicRSIProtection')
i_enableShortDynamicRSIProtection = input.bool(true, title='(S) Dynamic RSI Level {True, 50.0, 100.0}', group='RSI Protection', inline='ShortDynamicRSIProtection')
i_shortDynamicRSIProtectionMinimum = input.float(50, minval=0, maxval=100, title='', group='RSI Protection', inline='ShortDynamicRSIProtection')
i_shortDynamicRSIProtectionMaximum = input.float(100, minval=0, maxval=100, title='', group='RSI Protection', inline='ShortDynamicRSIProtection')
i_DynamicRSIProtectionLength = input.int(35, minval=1, title='Dynamic Length', group='RSI Protection')
i_DynamicRSIProtectionRound = input.float(5, minval=0.001, title='Dynamic Round', group='RSI Protection')

i_enableLongRSIClosing = input.bool(false, title='Enable (L) RSI Closing {False, 55.0}', group='RSI Closing', inline='LongRSIClosing')
i_longRSIClosingLevel = input.float(55.0, title='', group='RSI Closing', inline='LongRSIClosing', tooltip='Close when crossing under')
i_enableShortRSIClosing = input.bool(true, title='Enable (S) RSI Closing {True, 45.0}', group='RSI Closing', inline='ShortRSIClosing')
i_shortRSIClosingLevel = input.float(45.0, title='', group='RSI Closing', inline='ShortRSIClosing', tooltip='Close when crossing over')
i_enableRSIClosingCustomTimeframe = input.bool(false, title='Use Custom Timeframe {False, 3 hours}', group='RSI Closing', inline='RSIClosingTimeframe')
i_RSIClosingCustomTimeframe = input.timeframe('180', title='', group='RSI Closing', inline='RSIClosingTimeframe')
i_RSIClosingLength = input.int(14, minval=1, title='RSI Length', group='RSI Closing')
i_RSIClosingSource = input.source(close, 'RSI Source', group='RSI Closing')
i_enableLongRSIClosingAutoLevel = input.bool(true, title='(L) Auto Level {True, 55 bars, 42 bars}', group='RSI Closing', inline='longRSIClosingAutoLevel')
i_longRSIClosingAutoBarsNumber = input.int(55, minval=1, title='', group='RSI Closing', inline='longRSIClosingAutoLevel')
i_longRSIClosingLookbackBarsNumber = input.int(42, minval=1, title='', group='RSI Closing', inline='longRSIClosingAutoLevel')
i_enableShortRSIClosingAutoLevel = input.bool(true, title='(S) Auto Level {True, 55 bars, 42 bars}', group='RSI Closing', inline='shortRSIClosingAutoLevel')
i_shortRSIClosingAutoBarsNumber = input.int(55, minval=1, title='', group='RSI Closing', inline='shortRSIClosingAutoLevel')
i_shortRSIClosingLookbackBarsNumber = input.int(42, minval=1, title='', group='RSI Closing', inline='shortRSIClosingAutoLevel')

i_enableLongADXProtection = input.bool(true, title='Enable (L) ADX Protection {True, 20.0, 30.0}', group='ADX Protection', inline='LongADXProtection')
i_longADXProtectionMinimum = input.float(20.0, title='', group='ADX Protection', inline='LongADXProtection', tooltip='Greater than')
i_longADXProtectionMaximum = input.float(30.0, title='', group='ADX Protection', inline='LongADXProtection', tooltip='Less than')
i_enableShortADXProtection = input.bool(false, title='Enable (S) ADX Protection {False, 20.0, 30.0}', group='ADX Protection', inline='ShortADXProtection')
i_shortADXProtectionMinimum = input.float(20.0, title='', group='ADX Protection', inline='ShortADXProtection', tooltip='Greater than')
i_shortADXProtectionMaximum = input.float(30.0, title='', group='ADX Protection', inline='ShortADXProtection', tooltip='Less than')
i_enableADXProtectionCustomTimeframe = input.bool(false, title='Use Custom Timeframe {True, 3 hours}', group='ADX Protection', inline='ADXProtectionTimeframe')
i_ADXProtectionCustomTimeframe = input.timeframe('180', title='', group='ADX Protection', inline='ADXProtectionTimeframe')
i_ADXProtectionDiLength = input.int(14, minval=1, title='ADX DI Length', group='ADX Protection')
i_ADXProtectionSmoothing = input.int(14, title='ADX Smoothing', minval=1, maxval=50, group='ADX Protection')

i_subStrategiesLookback = input.int(1, title='(STRAT) Substrategies Lookback [1]')
i_subStrategiesMinimum = input.int(1, title='(STRAT) Substrategies minimum [1]')

i_showSqzmomDivergences = input.bool(true, title='Show {True}', group='Sqzmom Divergences')
i_enableSqzmomColourConfirmation = input.bool(false, title='Colour confirmation {False}', group='Sqzmom Divergences')

i_sqzmomLength = input.int(20, title='BB Length/MultFactor {20/2.0}', group='Sqzmom Divergences', inline='SqzmomBB')
i_sqzmomMult = input.float(2.0,title='', group='Sqzmom Divergences', inline='SqzmomBB')
i_sqzmomLengthKC=input.int(20, title='KC Length/MultFactor/TR {20/1.5/True}', group='Sqzmom Divergences', inline='SqzmomKC')
i_sqzmomMultKC = input.float(1.5, title='', group='Sqzmom Divergences', inline='SqzmomKC')
i_sqzmomUseTrueRange = input.bool(true, title='', group='Sqzmom Divergences', inline='SqzmomKC')

i_enableLongSqzmomPriceMinimumDifference = input.bool(true, 'Price Minimum Difference (Long) {True/0.15 %}', group='Sqzmom Divergences', inline='SqzmomLongPriceMinimumDifference')
i_longSqzmomPriceMinimumDifference = input.float(0.15, title='', minval=0.01, group='Sqzmom Divergences', inline='SqzmomLongPriceMinimumDifference')
i_enableShortSqzmomPriceMinimumDifference = input.bool(true, 'Price Minimum Difference (Short) {True/0.35 %}', group='Sqzmom Divergences', inline='SqzmomShortPriceMinimumDifference')
i_shortSqzmomPriceMinimumDifference = input.float(0.35, title='', minval=0.01, group='Sqzmom Divergences', inline='SqzmomShortPriceMinimumDifference')

i_enableLongSqzmomValMinimumDifference = input.bool(false, 'Val Minimum Difference (Long) {False/15.00 %}', group='Sqzmom Divergences', inline='SqzmomLongValMinimumDifference')
i_longSqzmomValMinimumDifference = input.float(15.00, title='', minval=0.01, group='Sqzmom Divergences', inline='SqzmomLongValMinimumDifference')
i_enableShortSqzmomValMinimumDifference = input.bool(true, 'Val Minimum Difference (Short) {True/150.00 %}', group='Sqzmom Divergences', inline='SqzmomShortValMinimumDifference')
i_shortSqzmomValMinimumDifference = input.float(150.00, title='', minval=0.01, group='Sqzmom Divergences', inline='SqzmomShortValMinimumDifference')

i_enableSecondaryDivergence = input.bool(false, title='LB/Pair {1/LTC}', group='Secondary Divergence', inline='SDLookbackPair')
i_secondaryDivergenceLookback = input.int(1, title='', tooltip='LookBack Bars Number', group='Secondary Divergence', inline='SDLookbackPair')
i_secondaryDivergencePair = input.symbol('BINANCE:LTCUSDTPERP', title='', group='Secondary Divergence',inline='SDLookbackPair')
// INPUTS - END



// Strategy - INIT - BEGIN

string stringNA = na

onStartTimeInterval = (not i_strategyStartTimeOn) or (time >= i_strategyStartTime)
offStopTimeInterval = (i_strategyStopTimeOn) and (time > i_strategyStopTime)

// order Stop Loss
float orderStopLossPerOne = na
float orderStopLoss = orderStopLossPerOne

float customBreakEvenPerOne = na
float customBreakEven = customBreakEvenPerOne

// Dynamic Trailing Take Profit - Variables

var int buyOrderOpenBarIndex = na
var int sellOrderOpenBarIndex = na

// Breakeven accounting - Variables
var bool breakEvenTriggered = false
var bool breakEvenNeedsTrigger = false
var bool customBreakEvenNeedsTrigger = false

// Handle to avoid two long and short orders being too next to each other
var bool insideABuyOrder = false
var bool insideASellOrder = false

var int lastOrderOpenBarTime = na

// Handle recalculate after Stop Loss / Take Profit order
if (strategy.position_size == 0.0)
    insideABuyOrder := false
    insideASellOrder := false
else
    lastOrderOpenBarTime := time_close

if (strategy.position_size > 0.0)
    insideABuyOrder := true
    insideASellOrder := false
if (strategy.position_size < 0.0)
    insideABuyOrder := false
    insideASellOrder := true

// Zignaly
exchangeTickerID = syminfo.ticker

var string tmpOrderComment = na
var string tmpOrderAlertMessage = na

var int i_tooRecentMinutesNumber_ms = i_tooRecentMinutesNumber * 60 * 1000 // Convert minutes into milliseconds only once

// Strategy - INIT - END

// FUNCTIONS - BEGIN

// Decimals - Functions
f_getContractMultiplier(_contractMaximumDecimalNumber) =>
    _contractMultiplier = 1
    if (_contractMaximumDecimalNumber == 0)
        _contractMultiplier // Return 1
    else
        for _counter = 1 to _contractMaximumDecimalNumber
            _contractMultiplier:= _contractMultiplier * 10
        _contractMultiplier

// Order comment - Functions - BEGIN

f_getOrderComment(_typeOrder, _side, _alertMessage) =>
    // Dependencies: i_enableOrderCommentDebug (initialized in inputs).

    // TODO: Add descriptive comments
    // _typeOrder="entry" or _typeOrder="close"
    // _side: true means long ; false means short ; We can use strategy.long or strategy.short as true and false aliases
    // _alertMessage
    var string _orderComment = ""
    
    if (i_enableOrderCommentDebug)
        _orderComment := _alertMessage
    else
        // TODO: Use human explanations instead of default order comment value (na)
        _orderComment := na
        
    _orderComment

// Order comment - Functions - END

// Too recent orders functions

f_getTooRecentOrderNotOpenAdvice (_lastOrderOpenBarTime) =>
    // Dependencies: i_tooRecentMinutesNumber_ms

    // Condition 1: The order was open
    // Condition 2: The order was long or short
    // Condition 3: Time since order was last open is smaller than minimum between orders
    // Returns boolean
    bool _isTooRecent =  ((time_close) <= (_lastOrderOpenBarTime + i_tooRecentMinutesNumber_ms))

    bool _tooRecentOrderBuyNotOpenAdvice = na
    if (na(_lastOrderOpenBarTime))
        _tooRecentOrderBuyNotOpenAdvice := false
    else
        _tooRecentOrderBuyNotOpenAdvice := _isTooRecent
    _tooRecentOrderBuyNotOpenAdvice

// MA - Functions - BEGIN
f_Ma (_maType, _maSource, _maLength) =>
    _maType == "SMA" ? ta.sma(_maSource, _maLength) : _maType == "EMA" ? ta.ema(_maSource, _maLength) : _maType == "VWMA" ? ta.vwma(_maSource, _maLength) : _maType == "RMA" ? ta.rma(_maSource, _maLength) : _maType == "WMA" ? ta.wma(_maSource, _maLength) : _maType == "HMA" ? ta.hma(_maSource, _maLength) : _maSource
// MA - Functions - END

getTrend (_fastMaType, _fastMaLength, _fastMaSource, _slowMaType, _slowMaLength, _slowMaSource, _offset) =>
    _fastMa = f_Ma (_fastMaType, _fastMaSource, _fastMaLength)
    _slowMa = f_Ma (_slowMaType, _slowMaSource, _slowMaLength)
    _trend = _fastMa >= _slowMa

    _trendOffset = _trend[_offset]
    _trendOffset

// Too old orders functions

getTooOldOrderLongCloseAdvice () =>
    // Condition 1: The order is open
    // Condition 2: The order is long
    // Condition 3: Order age is bigger than the maximum setting
    // Returns boolean
    bool isValidOpenLongOrder = (strategy.position_size > 0)
    bool isTooOld =  (bar_index >= (buyOrderOpenBarIndex + i_tooOldLongBarsNumber))
    tooOldStopLoss = strategy.position_avg_price * (1 - i_tooOldLongStopLossPerOne)

    bool isALosingOrder = close < tooOldStopLoss
    tooOldOrderLongCloseAdvice = isValidOpenLongOrder and isTooOld and isALosingOrder
    tooOldOrderLongCloseAdvice

getTooOldOrderShortCloseAdvice () =>
    // Condition 1: The order is open
    // Condition 2: The order is short
    // Condition 3: Order age is bigger than the maximum setting
    // Returns boolean
    bool is_valid_open_sell_order = (strategy.position_size < 0)
    bool isTooOld =  (bar_index >= (sellOrderOpenBarIndex + i_tooOldShortBarsNumber))
    tooOldStopLoss = strategy.position_avg_price * (1 + i_tooOldShortStopLossPerOne)

    bool isALosingOrder = close > tooOldStopLoss
    tooOldOrderShortCloseAdvice = is_valid_open_sell_order and isTooOld and isALosingOrder
    tooOldOrderShortCloseAdvice

// RSI protection functions
f_RsiModeOccurance(_value, _src, _length, _round, _minimum, _maximum) =>
    int _current_value_occurance = 0
    for _i = 0 to _length
        if ((_src[_i] >= _minimum) and (_src[_i] < _maximum))
            if (_src[_i] == _value)
                _current_value_occurance := _current_value_occurance + 1
    _return = _current_value_occurance

f_RsiMode(_src, _length, _round, _minimum, _maximum) =>
    float _highest_occurence_value = 0.0
    int _highest_occurance_number = 0
    for _i = 0 to _length
        _current_value_occurance = f_RsiModeOccurance(_src[_i], _src, _length, _round, _minimum, _maximum)
        if (_current_value_occurance > _highest_occurance_number)
            _highest_occurance_number := _current_value_occurance
            _highest_occurence_value := _src[_i]
    _return = _highest_occurence_value

// SMILB functions - BEGIN
sqzmom(length, mult, lengthKC, multKC, useTrueRange) =>
    // Calculate BB
    source = close
    basis = ta.sma(source, length)
    dev = mult * ta.stdev(source, length)
    upperBB = basis + dev
    lowerBB = basis - dev

    // Calculate KC
    ma = ta.sma(source, lengthKC)
    _range = useTrueRange ? ta.tr : (high - low)
    rangema = ta.sma(_range, lengthKC)
    upperKC = ma + rangema * multKC
    lowerKC = ma - rangema * multKC

    sqzOn  = (lowerBB > lowerKC) and (upperBB < upperKC)
    sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
    noSqz  = (sqzOn == false) and (sqzOff == false)

    val = ta.linreg(source - math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)),ta.sma(close,lengthKC)), lengthKC,0)

    bcolor = val > 0 ? ( val > nz(val[1]) ? color.lime : color.green ) : ( val < nz(val[1]) ? color.red : color.maroon )
    // TODO: Use string variables for colours so that final user can change the actual colour if he wants to.
    scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray

    sqzon_percentage = (((upperKC-upperBB)/upperKC)*100)*(0.5) + (((lowerBB-lowerKC)/lowerBB)*100)*(0.5)
    sqzoff_percentage = (((upperBB-upperKC)/upperBB)*100)*(0.5) + (((lowerKC-lowerBB)/lowerKC)*100)*(0.5)
    float sqz_percentage = na
    if sqzOn
        sqz_percentage := sqzon_percentage
    else if sqzOff
        sqz_percentage := - sqzoff_percentage

    [val, bcolor, scolor, sqz_percentage]

// FUNCTIONS - END


// Strategy Body - BEGIN

// Higher Timeframe Trend substrategy

higherTimeframeTrend = request.security(syminfo.tickerid, i_higherTimeframeTrendMaTimeframe, getTrend (i_higherTimeframeTrendFastMaType, i_higherTimeframeTrendFastMaLength, i_higherTimeframeTrendFastMaSource, i_higherTimeframeTrendSlowMaType, i_higherTimeframeTrendSlowMaLength, i_higherTimeframeTrendSlowMaSource, i_higherTimeframeTrendMaOffset))

int higherTimeframeTrendMaBarIndex = request.security (syminfo.tickerid, i_higherTimeframeTrendMaTimeframe, bar_index)
bool newHigherTimeframeTrendMaBar = higherTimeframeTrendMaBarIndex[1] != higherTimeframeTrendMaBarIndex[0]

var higherTimeframeTrendMaLong = higherTimeframeTrend
var higherTimeframeTrendMaShort = (not higherTimeframeTrend)

if newHigherTimeframeTrendMaBar
    higherTimeframeTrendMaLong := higherTimeframeTrend
    higherTimeframeTrendMaShort := (not higherTimeframeTrend)

// Volatility substrategy

volatilityLongCondition = true
volatilityShortCondition = true

atrLongVolatilityMinimumPrice = i_atrVolatilitySource + (ta.atr(i_atrVolatilityLength) * i_atrVolatilityMinimumMultiplier)
atrLongVolatilityMaximumPrice = i_atrVolatilitySource + (ta.atr(i_atrVolatilityLength) * i_atrVolatilityMaximumMultiplier)
barIsVolatileLong = (close[0] >= open[0]) and (close[0] >= atrLongVolatilityMinimumPrice[1]) and (close[0] < atrLongVolatilityMaximumPrice[1])

if barIsVolatileLong or barIsVolatileLong[1]
    volatilityLongCondition := false

atrShortVolatilityMinimumPrice = i_atrVolatilitySource - (ta.atr(i_atrVolatilityLength) * i_atrVolatilityMinimumMultiplier)
atrShortVolatilityMaximumPrice = i_atrVolatilitySource - (ta.atr(i_atrVolatilityLength) * i_atrVolatilityMaximumMultiplier)
barIsVolatileShort = (close[0] <= open[0]) and (close[0] <= atrShortVolatilityMinimumPrice[1]) and (close[0] > atrShortVolatilityMaximumPrice[1])

if barIsVolatileShort or barIsVolatileShort[1]
    volatilityShortCondition := false

// Curie Closing Lines substrategy
curieHighClosingMa = f_Ma (i_curieHighClosingMaType, i_curieHighClosingMaSource, i_curieHighClosingMaLength)
curieLowClosingMa = f_Ma (i_curieLowClosingMaType, i_curieLowClosingMaSource, i_curieLowClosingMaLength)

curieHighestClosingMA = ta.highest(curieHighClosingMa, i_curieHighestClosingMaLength)
curieLowestClosingMA = ta.lowest(curieLowClosingMa, i_curieLowestClosingMaLength)

// Too old logic
tooOldOrderLongCloseAdvice = getTooOldOrderLongCloseAdvice ()
tooOldOrderShortCloseAdvice = getTooOldOrderShortCloseAdvice ()

// RSI protection substrategy
rsiProtectionLongCondition = true
rsiProtectionShortCondition = true

float currentTimeframeProtectionRsi = ta.rsi(i_RSIProtectionSource, i_RSIProtectionLength)
float higherTimeframeProtectionRsi = request.security(syminfo.tickerid, i_RSIProtectionCustomTimeframe, ta.rsi(i_RSIProtectionSource, i_RSIProtectionLength))

float protectionRsi = na
if (i_enableRSIProtectionCustomTimeframe)
    protectionRsi := higherTimeframeProtectionRsi
else
    protectionRsi := currentTimeframeProtectionRsi

float roundedProtectionRsi = math.floor(protectionRsi / i_DynamicRSIProtectionRound) * i_DynamicRSIProtectionRound

float dynamicLongRSIProtection = f_RsiMode(roundedProtectionRsi, i_DynamicRSIProtectionLength, i_DynamicRSIProtectionRound, i_longDynamicRSIProtectionMinimum, i_longDynamicRSIProtectionMaximum)
float dynamicShortRSIProtection = f_RsiMode(roundedProtectionRsi, i_DynamicRSIProtectionLength, i_DynamicRSIProtectionRound, i_shortDynamicRSIProtectionMinimum, i_shortDynamicRSIProtectionMaximum)

float longRSIProtectionLevel = na
float shortRSIProtectionLevel = na

if (i_enableLongDynamicRSIProtection)
    longRSIProtectionLevel := dynamicLongRSIProtection
else
    longRSIProtectionLevel := i_longRSIProtectionLevel

if (i_enableShortDynamicRSIProtection)
    shortRSIProtectionLevel := dynamicShortRSIProtection
else
    shortRSIProtectionLevel := i_shortRSIProtectionLevel

if (protectionRsi > longRSIProtectionLevel)
    rsiProtectionLongCondition := false
if (protectionRsi < shortRSIProtectionLevel)
    rsiProtectionShortCondition := false

// RSI Closing substrategy
float currentTimeframeClosingRsi = ta.rsi(i_RSIClosingSource, i_RSIClosingLength)
float higherTimeframeClosingRsi = request.security(syminfo.tickerid, i_RSIClosingCustomTimeframe, ta.rsi(i_RSIClosingSource, i_RSIClosingLength))

float closingRsi = na
if (i_enableRSIClosingCustomTimeframe)
    closingRsi := higherTimeframeClosingRsi
else
    closingRsi := currentTimeframeClosingRsi

float longRSIClosingLevel = na
float shortRSIClosingLevel = na

float longPreAutoRSIClosingLevel = math.max(closingRsi, i_longRSIClosingAutoBarsNumber)
float shortPreAutoRSIClosingLevel = math.min(closingRsi, i_shortRSIClosingAutoBarsNumber)
float longAutoRSIClosingLevel = longPreAutoRSIClosingLevel[i_longRSIClosingLookbackBarsNumber]
float shortAutoRSIClosingLevel = shortPreAutoRSIClosingLevel[i_shortRSIClosingLookbackBarsNumber]

if (i_enableLongRSIClosingAutoLevel)
    longRSIClosingLevel := longAutoRSIClosingLevel
else
    longRSIClosingLevel := i_longRSIClosingLevel

if (i_enableShortRSIClosingAutoLevel)
    shortRSIClosingLevel := shortAutoRSIClosingLevel
else
    shortRSIClosingLevel := i_shortRSIClosingLevel

// ADX protection substrategy
adxProtectionLongCondition = true
adxProtectionShortCondition = true

[currentTimeframeProtectionDiPlus, currentTimeframeProtectionDiMinus, currentTimeframeProtectionAdx] = ta.dmi(i_ADXProtectionDiLength, i_ADXProtectionSmoothing)
[higherTimeframeProtectionDiPlus, higherTimeframeProtectionDiMinus, higherTimeframeProtectionAdx] = request.security(syminfo.tickerid, i_ADXProtectionCustomTimeframe, ta.dmi(i_ADXProtectionDiLength, i_ADXProtectionSmoothing))

float protectionAdx = na
if (i_enableADXProtectionCustomTimeframe)
    protectionAdx := higherTimeframeProtectionAdx
else
    protectionAdx := currentTimeframeProtectionAdx

float longADXProtectionMinimumLevel = na
float longADXProtectionMaximumLevel = na
float shortADXProtectionMinimumLevel = na
float shortADXProtectionMaximumLevel = na

longADXProtectionMinimumLevel := i_longADXProtectionMinimum
longADXProtectionMaximumLevel := i_longADXProtectionMaximum

shortADXProtectionMinimumLevel := i_shortADXProtectionMinimum
shortADXProtectionMaximumLevel := i_shortADXProtectionMaximum

if ((protectionAdx < longADXProtectionMinimumLevel) or (protectionAdx >= longADXProtectionMaximumLevel))
    adxProtectionLongCondition := false
if ((protectionAdx < shortADXProtectionMinimumLevel) or (protectionAdx >= shortADXProtectionMaximumLevel))
    adxProtectionShortCondition := false

// SQMMOMDivergence SubStrategy

getSqzmomDivergences (p_sqzmomLength, p_sqzmomMult, p_sqzmomLengthKC, p_sqzmomMultKC, p_sqzmomUseTrueRange, p_shortSqzmomPriceMinimumDifference, p_enableShortSqzmomValMinimumDifference, p_shortSqzmomValMinimumDifference, p_enableShortSqzmomPriceMinimumDifference, p_longSqzmomPriceMinimumDifference, p_enableLongSqzmomValMinimumDifference, p_longSqzmomValMinimumDifference, p_enableLongSqzmomPriceMinimumDifference, p_colourConfirmation) =>
    // Short divergence means that next top height is smaller than previous top height and at the same time price is bigger than in the previous top height.
    // Long divergence means that next bottom height is higher than previous bottom height and at the same time price is smaller than in the previous bottom height.

    // Needed variables
    var int sqzmom_top_changes = 0
    var float sqzmom_top_val_a = na
    var float sqzmom_top_price_a = na
    var float sqzmom_top_val_b = na
    var float sqzmom_top_price_b = na

    var int sqzmom_bottom_changes = 0
    var float sqzmom_bottom_val_a = na
    var float sqzmom_bottom_price_a = na
    var float sqzmom_bottom_val_b = na
    var float sqzmom_bottom_price_b = na

    [sqzmom_val, sqzmom_bcolor, sqzmom_scolor, sqz_percentage] = sqzmom(p_sqzmomLength, p_sqzmomMult, p_sqzmomLengthKC, p_sqzmomMultKC, p_sqzmomUseTrueRange)

    // Gather data

    bool sqzmom_top_change = ( sqzmom_bcolor[1] == color.lime )  and ( sqzmom_bcolor[0] == color.green )
    bool sqzmom_bottom_change = ( sqzmom_bcolor[1] == color.red )  and ( sqzmom_bcolor[0] == color.maroon )

    if (sqzmom_top_change)
        sqzmom_top_changes := sqzmom_top_changes + 1

    if (sqzmom_bottom_change)
        sqzmom_bottom_changes := sqzmom_bottom_changes + 1

    bool sqzmom_top_is_a = (sqzmom_top_changes % 2) == 0
    bool sqzmom_top_is_b = (sqzmom_top_changes % 2) == 1

    bool sqzmom_bottom_is_a = (sqzmom_bottom_changes % 2) == 0
    bool sqzmom_bottom_is_b = (sqzmom_bottom_changes % 2) == 1

    if (sqzmom_top_change and sqzmom_top_is_a)
        sqzmom_top_val_a := sqzmom_val[1]
        sqzmom_top_price_a := close[1]

    if (sqzmom_top_change and sqzmom_top_is_b)
        sqzmom_top_val_b := sqzmom_val[1]
        sqzmom_top_price_b := close[1]

    if (sqzmom_bottom_change and sqzmom_bottom_is_a)
        sqzmom_bottom_val_a := sqzmom_val[1]
        sqzmom_bottom_price_a := close[1]

    if (sqzmom_bottom_change and sqzmom_bottom_is_b)
        sqzmom_bottom_val_b := sqzmom_val[1]
        sqzmom_bottom_price_b := close[1]

    // Implement logic

    bool _sqzmomDivergenceLong = false
    bool _sqzmomDivergenceShort = false

    // Short divergence - Section A
    if (sqzmom_top_change and sqzmom_top_is_a and (sqzmom_top_val_a < sqzmom_top_val_b) and (sqzmom_top_price_a > sqzmom_top_price_b))
        _sqzmomDivergenceShort := true
        if (p_enableShortSqzmomPriceMinimumDifference)
            shortSectionASqzmomMinimumPrice = sqzmom_top_price_b + (p_shortSqzmomPriceMinimumDifference * 0.01 * sqzmom_top_price_b)
            shortSectionASqzmomPriceIsOverMinimum = sqzmom_top_price_a >= shortSectionASqzmomMinimumPrice
            _sqzmomDivergenceShort := _sqzmomDivergenceShort and shortSectionASqzmomPriceIsOverMinimum
        if (p_enableShortSqzmomValMinimumDifference)
            shortSectionASqzmomMinimumVal = sqzmom_top_val_a + (p_shortSqzmomValMinimumDifference * 0.01 * sqzmom_top_val_a)
            shortSectionASqzmomValIsOverMinimum = sqzmom_top_price_b >= shortSectionASqzmomMinimumVal
            _sqzmomDivergenceShort := _sqzmomDivergenceShort and shortSectionASqzmomValIsOverMinimum
    // Short divergence - Section B
    if (sqzmom_top_change and sqzmom_top_is_b and (sqzmom_top_val_b < sqzmom_top_val_a) and (sqzmom_top_price_b > sqzmom_top_price_a))
        _sqzmomDivergenceShort := true
        if (p_enableShortSqzmomPriceMinimumDifference)
            shortSectionBSqzmomMinimumPrice = sqzmom_top_price_a + (p_shortSqzmomPriceMinimumDifference * 0.01 * sqzmom_top_price_a)
            shortSectionBSqzmomPriceIsOverMinimum = sqzmom_top_price_b >= shortSectionBSqzmomMinimumPrice
            _sqzmomDivergenceShort := _sqzmomDivergenceShort and shortSectionBSqzmomPriceIsOverMinimum
        if (p_enableShortSqzmomValMinimumDifference)
            shortSectionBSqzmomMinimumVal = sqzmom_top_val_b + (p_shortSqzmomValMinimumDifference * 0.01 * sqzmom_top_val_b)
            shortSectionBSqzmomValIsOverMinimum = sqzmom_top_val_a >= shortSectionBSqzmomMinimumVal
            _sqzmomDivergenceShort := _sqzmomDivergenceShort and shortSectionBSqzmomValIsOverMinimum
    // Long divergence - Section A
    if (sqzmom_bottom_change and sqzmom_bottom_is_a and (sqzmom_bottom_val_a > sqzmom_bottom_val_b) and (sqzmom_bottom_price_a < sqzmom_bottom_price_b))
        _sqzmomDivergenceLong := true
        if (p_enableLongSqzmomPriceMinimumDifference)
            longSectionASqzmomMinimumPrice = sqzmom_bottom_price_a + (p_longSqzmomPriceMinimumDifference * 0.01 * sqzmom_bottom_price_a)
            longSectionASqzmomPriceIsOverMinimum = sqzmom_bottom_price_b >= longSectionASqzmomMinimumPrice
            _sqzmomDivergenceLong := _sqzmomDivergenceLong and longSectionASqzmomPriceIsOverMinimum
        if (p_enableLongSqzmomValMinimumDifference)
            longSectionASqzmomMinimumVal = sqzmom_bottom_val_b + (p_longSqzmomValMinimumDifference * 0.01 * math.abs(sqzmom_bottom_val_b))
            longSectionASqzmomValIsOverMinimum = sqzmom_bottom_val_a >= longSectionASqzmomMinimumVal
            _sqzmomDivergenceLong := _sqzmomDivergenceLong and longSectionASqzmomValIsOverMinimum
    // Long divergence - Section B
    if (sqzmom_bottom_change and sqzmom_bottom_is_b and (sqzmom_bottom_val_b > sqzmom_bottom_val_a) and (sqzmom_bottom_price_b < sqzmom_bottom_price_a))
        _sqzmomDivergenceLong := true
        if (p_enableLongSqzmomPriceMinimumDifference)
            longSqzmomSectionBMinimumPrice = sqzmom_bottom_price_b + (p_longSqzmomPriceMinimumDifference * 0.01 * sqzmom_bottom_price_b)
            longSectionBSqzmomPriceIsOverMinimum = sqzmom_bottom_price_a >= longSqzmomSectionBMinimumPrice
            _sqzmomDivergenceLong := _sqzmomDivergenceLong and longSectionBSqzmomPriceIsOverMinimum
        if (p_enableLongSqzmomValMinimumDifference)
            longSectionBSqzmomMinimumVal = sqzmom_bottom_val_a + (p_longSqzmomValMinimumDifference * 0.01 * math.abs(sqzmom_bottom_val_a))
            longSectionBSqzmomValIsOverMinimum = sqzmom_bottom_val_b >= longSectionBSqzmomMinimumVal
            _sqzmomDivergenceLong := _sqzmomDivergenceLong and longSectionBSqzmomValIsOverMinimum
    bool r_sqzmomDivergenceLong = na
    bool r_sqzmomDivergenceShort = na
    if (p_colourConfirmation)
        r_sqzmomDivergenceLong := _sqzmomDivergenceLong[1] and (sqzmom_bcolor[1] == sqzmom_bcolor[0])
        r_sqzmomDivergenceShort := _sqzmomDivergenceShort[1] and (sqzmom_bcolor[1] == sqzmom_bcolor[0])
    else
        r_sqzmomDivergenceLong := _sqzmomDivergenceLong
        r_sqzmomDivergenceShort := _sqzmomDivergenceShort
    [r_sqzmomDivergenceLong, r_sqzmomDivergenceShort]

[sqzmomDivergenceLong, sqzmomDivergenceShort] = getSqzmomDivergences (i_sqzmomLength, i_sqzmomMult, i_sqzmomLengthKC, i_sqzmomMultKC, i_sqzmomUseTrueRange, i_shortSqzmomPriceMinimumDifference, i_enableShortSqzmomValMinimumDifference, i_shortSqzmomValMinimumDifference, i_enableShortSqzmomPriceMinimumDifference, i_longSqzmomPriceMinimumDifference, i_enableLongSqzmomValMinimumDifference, i_longSqzmomValMinimumDifference, i_enableLongSqzmomPriceMinimumDifference, i_enableSqzmomColourConfirmation)

// Secondary Pair Divergence logic
[currentSecondaryDivergenceLong, currentSecondaryDivergenceShort] = request.security(i_secondaryDivergencePair, timeframe.period, getSqzmomDivergences(i_sqzmomLength, i_sqzmomMult, i_sqzmomLengthKC, i_sqzmomMultKC, i_sqzmomUseTrueRange, i_shortSqzmomPriceMinimumDifference, i_enableShortSqzmomValMinimumDifference, i_shortSqzmomValMinimumDifference, i_enableShortSqzmomPriceMinimumDifference, i_longSqzmomPriceMinimumDifference, i_enableLongSqzmomValMinimumDifference, i_longSqzmomValMinimumDifference, i_enableLongSqzmomPriceMinimumDifference, i_enableSqzmomColourConfirmation))

bool currentSecondaryDivergenceLongFound = false
for i = (i_secondaryDivergenceLookback - 1) to 0
    if currentSecondaryDivergenceLong[i]
        currentSecondaryDivergenceLongFound := true
bool currentSecondaryDivergenceShortFound = false
for i = (i_secondaryDivergenceLookback - 1) to 0
    if currentSecondaryDivergenceShort[i]
        currentSecondaryDivergenceShortFound := true

bool secondaryDivergenceLong = currentSecondaryDivergenceLongFound
bool secondaryDivergenceShort = currentSecondaryDivergenceShortFound

// Common Logic to go LONG/LONG or go SHORT/SHORT - BEGIN
// Define here your, at most, 5 long conditions. Use false if not used. - BEGIN
bool longCondition1 = sqzmomDivergenceLong
bool longCondition2 = false
bool longCondition3 = false
bool longCondition4 = false
bool longCondition5 = false
// Define here your, at most, 5 long conditions. Use false if not used. - END

// Define here your, at most, 5 short conditions. Use false if not used. - BEGIN
bool shortCondition1 = sqzmomDivergenceShort
bool shortCondition2 = false
bool shortCondition3 = false
bool shortCondition4 = false
bool shortCondition5 = false
// Define here your, at most, 5 short conditions. Use false if not used. - END

bool longCondition1Found = false
bool longCondition2Found = false
bool longCondition3Found = false
bool longCondition4Found = false
bool longCondition5Found = false

bool shortCondition1Found = false
bool shortCondition2Found = false
bool shortCondition3Found = false
bool shortCondition4Found = false
bool shortCondition5Found = false


longConditions = 0
shortConditions = 0
for i = (i_subStrategiesLookback - 1) to 0
    if longCondition1[i]
        longCondition1Found := true
    if longCondition2[i]
        longCondition2Found := true
    if longCondition3[i]
        longCondition3Found := true
    if longCondition4[i]
        longCondition4Found := true
    if longCondition5[i]
        longCondition5Found := true
    if shortCondition1[i]
        shortCondition1Found := true
    if shortCondition2[i]
        shortCondition2Found := true
    if shortCondition3[i]
        shortCondition3Found := true
    if shortCondition4[i]
        shortCondition4Found := true
    if shortCondition5[i]
        shortCondition5Found := true

if longCondition1Found
    longConditions := longConditions + 1
if longCondition2Found
    longConditions := longConditions + 1
if longCondition3Found
    longConditions := longConditions + 1
if longCondition4Found
    longConditions := longConditions + 1
if longCondition5Found
    longConditions := longConditions + 1
if shortCondition1Found
    shortConditions := shortConditions + 1
if shortCondition2Found
    shortConditions := shortConditions + 1
if shortCondition3Found
    shortConditions := shortConditions + 1
if shortCondition4Found
    shortConditions := shortConditions + 1
if shortCondition5Found
    shortConditions := shortConditions + 1

bool longCondition = na
bool shortCondition = na

longCondition := (longConditions >= i_subStrategiesMinimum)
shortCondition := (shortConditions >= i_subStrategiesMinimum)

if i_enableVolatilityProtection
    longCondition := longCondition and volatilityLongCondition
    shortCondition := shortCondition and volatilityShortCondition

if (i_higherTimeframeTrendMaEnforce)
    longCondition := longCondition and higherTimeframeTrendMaLong
    shortCondition := shortCondition and higherTimeframeTrendMaShort

if (i_enableLongRSIProtection)
    longCondition := longCondition and rsiProtectionLongCondition
if (i_enableShortRSIProtection)
    shortCondition := shortCondition and rsiProtectionShortCondition

if (i_enableLongADXProtection)
    longCondition := longCondition and adxProtectionLongCondition
if (i_enableShortADXProtection)
    shortCondition := shortCondition and adxProtectionShortCondition

if (i_enableSecondaryDivergence)
    longCondition := longCondition and secondaryDivergenceLong
    shortCondition := shortCondition and secondaryDivergenceShort
// Common Logic to go LONG/LONG or go SHORT/SHORT - END

// Volatility - Plot - BEGIN
atrVolatilityColor = i_showAtrVolatilityMultiplier ? color.maroon : na
atrLongVolatilityMinimumPricePlot = plot(atrLongVolatilityMinimumPrice, color = atrVolatilityColor, title = "Long Volatility Minimum Price")
atrShortVolatilityMinimumPricePlot = plot(atrShortVolatilityMinimumPrice, color = atrVolatilityColor, title = "Short Volatility Minimum Price")
atrLongVolatilityMaximumPricePlot = plot(atrLongVolatilityMaximumPrice, color = atrVolatilityColor, title = "Long Volatility Maximum Price")
atrShortVolatilityMaximumPricePlot = plot(atrShortVolatilityMaximumPrice, color = atrVolatilityColor, title = "Short Volatility Maximum Price")
// Volatility - Plot - END

// Curie Closing Lines - Plot - BEGIN
curieHighestClosingLowestColor = i_showCurieClosingLines ? color.yellow : na
curieHighestClosingMAPlot = plot(curieHighestClosingMA, color = curieHighestClosingLowestColor, title = 'Upper Curie Closing line')
curieLowestClosingMAPlot = plot(curieLowestClosingMA, color = curieHighestClosingLowestColor, title = 'Lower Curie Closing Line')
// Curie Closing Lines - Plot - END

// Higher Timeframe Trend - Plot - BEGIN
higherTimeframeTrendMaLongColor = i_showHigherTimeframeTrendMa ? color.new(#00c3ff, transp=0) : na
higherTimeframeTrendMaShortColor = i_showHigherTimeframeTrendMa ? color.new(#ff0062, transp=0) : na
plotshape(higherTimeframeTrendMaLong, title='SideMA Up', style=shape.arrowup, color=higherTimeframeTrendMaLongColor, location=location.top)
plotshape(higherTimeframeTrendMaShort, title='SideMA Down', style=shape.arrowdown, color=higherTimeframeTrendMaShortColor, location=location.top)
// Higher Timeframe Trend - Plot - END

// Squeeze Momentum Divergence - Plot - BEGIN
sqzmomDivergenceLongColor = i_showSqzmomDivergences ? color.new(#00c3ff, transp=0) : na
sqzmomDivergenceShortColor = i_showSqzmomDivergences ? color.new(#ff0062, transp=0) : na
plotshape(sqzmomDivergenceLong, text='D', title='Divergence Up', style=shape.labelup, color=sqzmomDivergenceLongColor, location=location.bottom)
plotshape(sqzmomDivergenceShort, text='D', title='Divergence Down', style=shape.labeldown, color=sqzmomDivergenceShortColor, location=location.bottom)
// Squeeze Momentum Divergence - Plot - END

// Position price calculation - BEGIN
float positionPrice = na
if (insideASellOrder or insideABuyOrder)
    positionPrice := strategy.position_avg_price
else
    positionPrice := close
    // Make sure to reset breakeven triggered if we are not in an order
    breakEvenTriggered := false
    breakEvenNeedsTrigger := false
    customBreakEvenNeedsTrigger := false
    
// Position price calculation - END

tooRecentOrderNotOpenAdvice = f_getTooRecentOrderNotOpenAdvice (lastOrderOpenBarTime)

// Decide if we are going to LONG/Go LONG - BEGIN
PRE_LONG = false
if (i_tooRecentMinutesNumber == 0)
    PRE_LONG := onStartTimeInterval and longCondition
else
    PRE_LONG := onStartTimeInterval and longCondition and (not tooRecentOrderNotOpenAdvice)

// LONG = PRE_LONG[1] and PRE_LONG // Wait for confirmation
LONG = PRE_LONG // Do not wait for confirmation

// Decide if we are going to LONG/Go LONG - END

// Decide if we are going to SHORT/Go SHORT - BEGIN
PRE_SHORT = false
if (i_tooRecentMinutesNumber == 0)
    PRE_SHORT := onStartTimeInterval and shortCondition
else
    PRE_SHORT := onStartTimeInterval and shortCondition and (not tooRecentOrderNotOpenAdvice)

// SHORT = PRE_SHORT[1] and PRE_SHORT // Wait for confirmation
SHORT = PRE_SHORT // Do not wait for confirmation

// Decide if we are going to SHORT/Go SHORT - END

// Decide if a LONG/LONG entry should be closed - BEGIN
LONG_CLOSE = true
if (i_enableLongStrictStrategy)
    LONG_CLOSE := offStopTimeInterval
else
    LONG_CLOSE := offStopTimeInterval or shortCondition

if i_enableCurieClosingLines
    LONG_CLOSE := LONG_CLOSE or ( ta.crossunder(i_curieSource, curieHighestClosingMA) and (close >= (positionPrice * (1 + i_breakEvenPerOne))) )

if i_enableTooOldLongOrderClose
    LONG_CLOSE := LONG_CLOSE or tooOldOrderLongCloseAdvice

if i_enableLongRSIClosing
    LONG_CLOSE := LONG_CLOSE or ( ta.crossunder(closingRsi, longRSIClosingLevel) )

// Let's make sure LONG does not trigger if LONG_CLOSE would tell us to close the LONG
if LONG_CLOSE
    LONG := false
// Decide if a LONG/LONG entry should be closed - END

// Decide if a SHORT/SHORT entry should be closed - BEGIN
SHORT_CLOSE = true
if (i_enableShortStrictStrategy)
    SHORT_CLOSE := offStopTimeInterval
else
    SHORT_CLOSE := offStopTimeInterval or longCondition

if i_enableCurieClosingLines
    SHORT_CLOSE := SHORT_CLOSE or ( ta.crossover(i_curieSource, curieLowestClosingMA) and (close <= (positionPrice * (1 - i_breakEvenPerOne))) )

if i_enableTooOldShortOrderClose
    SHORT_CLOSE := SHORT_CLOSE or tooOldOrderShortCloseAdvice

if i_enableShortRSIClosing
    SHORT_CLOSE := SHORT_CLOSE or ( ta.crossover(closingRsi, shortRSIClosingLevel) )

// Let's make sure SHORT does not trigger if SHORT_CLOSE would tell us to close the SHORT
if SHORT_CLOSE
    SHORT := false
// Decide if a SHORT/SHORT entry should be closed - END

// orderStopLoss is just here just in case
// we implement i_longStopLoss and i_shortStopLoss
// in the future
atrLongStopLossPrice = i_atrStopLossSource - (ta.atr(i_atrStopLossLength) * i_atrStopLossMultiplier)
atrShortStopLossPrice = i_atrStopLossSource + (ta.atr(i_atrStopLossLength) * i_atrStopLossMultiplier)
if LONG
    if ( i_stopLossType == 'constant' )
        orderStopLossPerOne := i_stopLoss
    else if ( i_stopLossType == 'atr' )
        orderStopLossPerOne := (close - atrLongStopLossPrice) / close

    orderStopLoss := orderStopLossPerOne * 100
else
    if ( i_stopLossType == 'constant' )
        orderStopLossPerOne := i_stopLoss
    else if ( i_stopLossType == 'atr' )
        orderStopLossPerOne := (atrShortStopLossPrice - close) / atrShortStopLossPrice

    orderStopLoss := orderStopLossPerOne * 100

var float LongBreakEvenTrigger = na
var float ShortBreakEvenTrigger = na

if ((not insideABuyOrder[1]) and insideABuyOrder)
    LongBreakEvenTrigger := positionPrice * (1 + i_breakEvenTriggerPerOne)
if ((not insideASellOrder[1]) and insideASellOrder)
    ShortBreakEvenTrigger := positionPrice * (1 - i_breakEvenTriggerPerOne)

if (insideABuyOrder and insideABuyOrder[1])
    if ( false )
        customBreakEvenNeedsTrigger := true
if (insideASellOrder and insideASellOrder[1])
    if ( false )
        customBreakEvenNeedsTrigger := true

if insideABuyOrder
    if (high >= LongBreakEvenTrigger)
        breakEvenNeedsTrigger := true
if insideASellOrder
    if (low <= ShortBreakEvenTrigger)
        breakEvenNeedsTrigger := true

if (insideABuyOrder and LONG_CLOSE)
    breakEvenTriggered := false
    breakEvenNeedsTrigger := false
    customBreakEvenNeedsTrigger := false
if (insideASellOrder and SHORT_CLOSE)
    breakEvenTriggered := false
    breakEvenNeedsTrigger := false
    customBreakEvenNeedsTrigger := false

// How much to LONG/GO LONG/SHORT/GO SHORT - BEGIN

// Custom take profit and stop loss
var float LongStopLoss = na
var float ShortStopLoss = na

// Initial StopLoss value based on last closed candle
if (i_enableStopLoss and (not insideABuyOrder))
    LongStopLoss := close * (1 - orderStopLossPerOne)
if (i_enableStopLoss and (not insideASellOrder))
    ShortStopLoss := close * (1 + orderStopLossPerOne)

// Final StopLoss value based on actual position price
if (i_enableStopLoss and ((not insideABuyOrder[1]) and (insideABuyOrder)))
    LongStopLoss := positionPrice * (1 - orderStopLossPerOne)
if (i_enableStopLoss and ((not insideASellOrder[1]) and (insideASellOrder)))
    ShortStopLoss := positionPrice * (1 + orderStopLossPerOne)

// Make sure LongStopLoss and ShortStopLoss are undefined
// by default when i_enableStopLoss is set to false
if (not i_enableStopLoss)
    LongStopLoss := na
    ShortStopLoss := na

// Override StopLoss with BreakEven if needed
if ( i_enableBreakEven and insideABuyOrder and (not breakEvenTriggered) and breakEvenNeedsTrigger )
    LongStopLoss := positionPrice * (1 + i_breakEvenPerOne)
if ( i_enableBreakEven and insideASellOrder and (not breakEvenTriggered) and breakEvenNeedsTrigger )
    ShortStopLoss := positionPrice * (1 - i_breakEvenPerOne)

if ( i_enableCustomBreakEven and insideABuyOrder and customBreakEvenNeedsTrigger )
    LongStopLoss := positionPrice * (1 + i_breakEvenPerOne)
    customBreakEvenPerOne := (LongStopLoss - close) / close
    customBreakEven := customBreakEvenPerOne * 100
if ( i_enableCustomBreakEven and insideASellOrder and customBreakEvenNeedsTrigger )
    ShortStopLoss := positionPrice * (1 - i_breakEvenPerOne)
    customBreakEvenPerOne := (close - ShortStopLoss) / ShortStopLoss
    customBreakEven := customBreakEvenPerOne * 100

var float takeProfit = na
var float takeProfitPerOne = na

if i_enableRiskRewardRatio
    takeProfit := orderStopLoss * i_positionRatio
    takeProfitPerOne := takeProfit / 100
else
    takeProfit := i_takeProfit
    takeProfitPerOne := i_takeProfitPerOne

var float LongTakeProfit = na
var float ShortTakeProfit = na

// Initial TakeProfit value based on last closed candle
if (not insideABuyOrder)
    LongTakeProfit := close * (1 + takeProfitPerOne)
if (not insideASellOrder)
    ShortTakeProfit := close * (1 - takeProfitPerOne)

// Final TakeProfit value based on actual position price
if ((not insideABuyOrder[1]) and (insideABuyOrder))
    LongTakeProfit := positionPrice * (1 + takeProfitPerOne)
if ((not insideASellOrder[1]) and (insideASellOrder))
    ShortTakeProfit := positionPrice * (1 - takeProfitPerOne)

float entryQuantity = na
float currentCapital = strategy.initial_capital + strategy.netprofit

var float customQuantity = na


if (i_zignalyProviderType == 'copytrader')
    entryQuantity := i_CapitalPercentage * currentCapital
else // _ZIGSIGNALPROVIDER_
    entryQuantity := i_CapitalQuote

if ( (i_riskManagementType == 'CUSTOM') or (i_riskManagementType == 'FORCED') )
    customQuantity := entryQuantity
else
    customQuantity := na

if (i_zignalyProviderType == 'copytrader') // Risk management is only implemented for CopyTrader Providers
    var currentDayCapital = 0.0
    var currentDay = 0
    var currentWeekCapital = 0.0
    var currentWeek = 0
    var currentMonthCapital = 0.0
    var currentMonth = 0

    currentDay := rm.getCurrentDay ()
    currentWeek := rm.getCurrentWeek ()
    currentMonth := rm.getCurrentMonth ()

    [__entryQuantity, __currentDay, __currentDayCapital, __currentWeek, __currentWeekCapital, __currentMonth, __currentMonthCapital] = rm.advisedEntryQuantity (currentCapital, currentDay, currentDayCapital, currentWeek, currentWeekCapital, currentMonth, currentMonthCapital, i_riskManagementMaximumLossPerDay, i_riskManagementMaximumLossPerWeek, i_riskManagementMaximumLossPerMonth, i_riskManagementMaximumLossPerTrade, orderStopLossPerOne, i_riskManagementType, customQuantity)

    entryQuantity := __entryQuantity
    currentDayCapital := __currentDayCapital
    currentWeekCapital := __currentWeekCapital
    currentMonthCapital := __currentMonthCapital

float entryQuantityContractsReducer = 0.99

contractMultiplier = f_getContractMultiplier(i_contractMaximumDecimalNumber)

// We assume the current price is current bar close

entryQuantityContracts = (math.floor(((entryQuantity / close) * entryQuantityContractsReducer) * contractMultiplier) / contractMultiplier)
entryQuantityContractsUSDT = entryQuantityContracts * (close)

// How much to LONG/GO LONG/SHORT/GO SHORT - END


// GO LONG only logic - BEGIN

var float tmpPositionBeforeOpeningOrder = na

if (i_strategyType == _L_)
    if LONG
        tmpPositionBeforeOpeningOrder := strategy.position_avg_price
        if (not insideABuyOrder) // Entry
            tmpOrderAlertMessage := zig.longEntry(key=i_zignalyServiceSecretKey,positionSizeQuote=entryQuantityContractsUSDT, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, integration=i_zignalyIntegrationType, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            strategy.entry("[L]", strategy.long, qty=entryQuantityContracts, alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
        if (na(tmpPositionBeforeOpeningOrder)) // bar_index should only be saved when there is no order opened
            buyOrderOpenBarIndex := bar_index + 1
        0 // Workaround for making every if branch to return the same type of value
    else if LONG_CLOSE
        tmpOrderAlertMessage := zig.longExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
        tmpOrderComment := f_getOrderComment("close", strategy.long, tmpOrderAlertMessage)
        strategy.close("[L]", alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
        0 // Workaround for making every if branch to return the same type of value
    if ((insideABuyOrder) and (not LONG_CLOSE))
        if ( i_enableCustomBreakEven and customBreakEvenNeedsTrigger ) // Update Custom Breakeven
            tmpOrderAlertMessage := zig.updateLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableCustomBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = customBreakEven, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
        else if (i_enableBreakEven and breakEvenNeedsTrigger and (not(breakEvenTriggered))) // Update Breakeven
            tmpOrderAlertMessage := zig.updateLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = i_breakEvenTrigger, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
            breakEvenTriggered := true
        else // Update StopLoss
            tmpOrderAlertMessage := zig.updateLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
// GO LONG only logic - END

// GO SHORT only logic - BEGIN
if (i_strategyType == _S_)
    if SHORT
        tmpPositionBeforeOpeningOrder := strategy.position_avg_price
        if (not insideASellOrder) // Entry
            tmpOrderAlertMessage := zig.shortEntry(key=i_zignalyServiceSecretKey,positionSizeQuote=entryQuantityContractsUSDT, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, integration=i_zignalyIntegrationType, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            strategy.entry("[S]", strategy.short, qty=entryQuantityContracts, alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
        if (na(tmpPositionBeforeOpeningOrder)) // bar_index should only be saved when there is no order opened
            sellOrderOpenBarIndex := bar_index + 1
        0 // Workaround for making every if branch to return the same type of value
    else if SHORT_CLOSE
        tmpOrderAlertMessage := zig.shortExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
        tmpOrderComment := f_getOrderComment("close", strategy.short, tmpOrderAlertMessage)
        strategy.close("[S]", alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
        0 // Workaround for making every if branch to return the same type of value
    if ((insideASellOrder) and (not SHORT_CLOSE))
        if ( i_enableCustomBreakEven and customBreakEvenNeedsTrigger ) // Update Custom Breakeven
            tmpOrderAlertMessage := zig.updateShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableCustomBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = customBreakEven, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
        else if (i_enableBreakEven and breakEvenNeedsTrigger and (not(breakEvenTriggered))) // Update Breakeven
            tmpOrderAlertMessage := zig.updateShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = i_breakEvenTrigger, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
            breakEvenTriggered := true
        else // Update StopLoss
            tmpOrderAlertMessage := zig.updateShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
// GO SHORT only logic - END

// GO LONG and SHORT logic - BEGIN
if (i_strategyType == L_S)
// Asumption: Above we have:
// if LONG_CLOSE
//    LONG := false
// if SHORT_CLOSE
//    SHORT := false

    // LONG and SHORT cannot be true at the same time
    // Anyways we will take it into account
    if (LONG == true) and (SHORT == true)
        LONG := true
        SHORT := false
        SHORT_CLOSE := true

    if LONG
        tmpPositionBeforeOpeningOrder := strategy.position_avg_price
        if (not insideABuyOrder) // Entry
            tmpOrderAlertMessage := zig.longEntry(key=i_zignalyServiceSecretKey,positionSizeQuote=entryQuantityContractsUSDT, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, integration=i_zignalyIntegrationType, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            strategy.entry("[L]", strategy.long, qty=entryQuantityContracts, alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
        if (na(tmpPositionBeforeOpeningOrder)) // bar_index should only be saved when there is no order opened
            buyOrderOpenBarIndex := bar_index + 1
    if LONG_CLOSE
        tmpOrderAlertMessage := zig.longExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
        tmpOrderComment := f_getOrderComment("close", strategy.long, tmpOrderAlertMessage)
        strategy.close("[L]", alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
    if ((insideABuyOrder) and (not LONG_CLOSE))
        if ( i_enableCustomBreakEven and customBreakEvenNeedsTrigger ) // Update Custom Breakeven
            tmpOrderAlertMessage := zig.updateLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableCustomBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = customBreakEven, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
        else if (i_enableBreakEven and breakEvenNeedsTrigger and (not(breakEvenTriggered))) // Update Breakeven
            tmpOrderAlertMessage := zig.updateLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = i_breakEvenTrigger, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
            breakEvenTriggered := true
        else // Update StopLoss
            tmpOrderAlertMessage := zig.updateLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
    if SHORT
        tmpPositionBeforeOpeningOrder := strategy.position_avg_price
        if (not insideASellOrder) // Entry
            tmpOrderAlertMessage := zig.shortEntry(key=i_zignalyServiceSecretKey,positionSizeQuote=entryQuantityContractsUSDT, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, integration=i_zignalyIntegrationType, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            strategy.entry("[S]", strategy.short, qty=entryQuantityContracts, alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
        if (na(tmpPositionBeforeOpeningOrder)) // bar_index should only be saved when there is no order opened
            sellOrderOpenBarIndex := bar_index + 1
    if SHORT_CLOSE
        tmpOrderAlertMessage := zig.shortExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
        tmpOrderComment := f_getOrderComment("close", strategy.short, tmpOrderAlertMessage)
        strategy.close("[S]", alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
    if ((insideASellOrder) and (not SHORT_CLOSE))
        if ( i_enableCustomBreakEven and customBreakEvenNeedsTrigger ) // Update Custom Breakeven
            tmpOrderAlertMessage := zig.updateShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableCustomBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = customBreakEven, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
        else if (i_enableBreakEven and breakEvenNeedsTrigger and (not(breakEvenTriggered))) // Update Breakeven
            tmpOrderAlertMessage := zig.updateShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = i_breakEvenTrigger, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
            breakEvenTriggered := true
        else // Update StopLoss
            tmpOrderAlertMessage := zig.updateShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
// GO LONG and SHORT logic - END

// Reverse logic - BEGIN
if (i_strategyType == _REVERSE_)
// Asumption: Above we have:
// if LONG_CLOSE
//    LONG := false
// if SHORT_CLOSE
//    SHORT := false

    // LONG and SHORT cannot be true at the same time
    // Anyways we will take it into account
    if (LONG == true) and (SHORT == true)
        LONG := true
        SHORT := false

    if (LONG and (not LONG_CLOSE))
        tmpPositionBeforeOpeningOrder := strategy.position_avg_price
        if (not insideABuyOrder) // Entry
            tmpOrderAlertMessage := zig.reverseLongEntry(key=i_zignalyServiceSecretKey,positionSizeQuote=entryQuantityContractsUSDT, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, integration=i_zignalyIntegrationType, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            strategy.entry("[L]", strategy.long, qty=entryQuantityContracts, alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
        if (na(tmpPositionBeforeOpeningOrder)) // bar_index should only be saved when there is no order opened
            buyOrderOpenBarIndex := bar_index + 1
    if (LONG_CLOSE and (not ((SHORT and (not SHORT_CLOSE)))))
        tmpOrderAlertMessage := zig.reverseLongExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
        tmpOrderComment := f_getOrderComment("close", strategy.long, tmpOrderAlertMessage)
        strategy.close("[L]", alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
    if ((insideABuyOrder) and (not LONG_CLOSE))
        if ( i_enableCustomBreakEven and customBreakEvenNeedsTrigger ) // Update Custom Breakeven
            tmpOrderAlertMessage := zig.updateReverseLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableCustomBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = customBreakEven, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
        else if (i_enableBreakEven and breakEvenNeedsTrigger and (not(breakEvenTriggered))) // Update Breakeven
            tmpOrderAlertMessage := zig.updateReverseLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = i_breakEvenTrigger, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
            breakEvenTriggered := true
        else // Update StopLoss
            tmpOrderAlertMessage := zig.updateReverseLongEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.long, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
    if (SHORT and (not SHORT_CLOSE))
        tmpPositionBeforeOpeningOrder := strategy.position_avg_price
        if (not insideASellOrder) // Entry
            tmpOrderAlertMessage := zig.reverseShortEntry(key=i_zignalyServiceSecretKey,positionSizeQuote=entryQuantityContractsUSDT, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, integration=i_zignalyIntegrationType, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            strategy.entry("[S]", strategy.short, qty=entryQuantityContracts, alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
        if (na(tmpPositionBeforeOpeningOrder)) // bar_index should only be saved when there is no order opened
            sellOrderOpenBarIndex := bar_index + 1
    if (SHORT_CLOSE and (not (LONG and (not LONG_CLOSE))))
        tmpOrderAlertMessage := zig.reverseShortExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
        tmpOrderComment := f_getOrderComment("close", strategy.short, tmpOrderAlertMessage)
        strategy.close("[S]", alert_message=i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)
    if ((insideASellOrder) and (not SHORT_CLOSE))
        if ( i_enableCustomBreakEven and customBreakEvenNeedsTrigger ) // Update Custom Breakeven
            tmpOrderAlertMessage := zig.updateReverseShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableCustomBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = customBreakEven, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
        else if (i_enableBreakEven and breakEvenNeedsTrigger and (not(breakEvenTriggered))) // Update Breakeven
            tmpOrderAlertMessage := zig.updateReverseShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableBreakEven, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, breakEvenPercentage = i_breakEvenTrigger, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
            breakEvenTriggered := true
        else // Update StopLoss
            tmpOrderAlertMessage := zig.updateReverseShortEntry(key=i_zignalyServiceSecretKey, currentCapital=currentCapital, alertType=i_zignalyAlertType, stopLoss=i_enableStopLoss, takeProfit=i_enableTakeProfit, takeProfitPercentage1=takeProfit, trailingStopLoss=i_enableTrailingTakeProfit, trailingStopTriggerPercentage=i_trailingTakeProfitPercentageTrigger, trailingStopDistancePercentage=i_trailingTakeProfitPercentageOffset, priceDecimalDigits=i_priceDecimalDigits, exchange=i_zignalyExchange, exchangeAccountType=i_zignalyExchangeType, pair=exchangeTickerID, leverage=i_zignalyLeverage, providerType=i_zignalyProviderType, stopLossPercentage=orderStopLoss, signalId=stringNA, partial=false)
            // tmpOrderComment := f_getOrderComment("entry", strategy.short, tmpOrderAlertMessage)
            alert(message=( i_enableZignalyAlert and (i_zignalyIntegrationType == 'hybrid') ) ? tmpOrderAlertMessage : na)
// Reverse logic - END

// Handle STOP LOSS (and similar) order exits - BEGIN

// Quick Paste - BEGIN
float longTrailingTakeProfitPrice = na
float longTrailingTakeProfitPoints = na
float longTrailingTakeProfitOffset = na
float shortTrailingTakeProfitPrice = na
float shortTrailingTakeProfitPoints = na
float shortTrailingTakeProfitOffset = na

// Calculate trailing_take_profit_offset based on tick units
longTmpTtpBaseValue = positionPrice * (1 + i_trailingTakeProfitPercentageTriggerPerOne)
longTmpTtpTop_value = longTmpTtpBaseValue * (1 + i_trailingTakeProfitPercentageOffsetPerOne)
longTrailingTakeProfitOffsetFloat = longTmpTtpTop_value - longTmpTtpBaseValue
float longTrailingTakeProfitOffsetTick = math.floor (longTrailingTakeProfitOffsetFloat / syminfo.mintick)

shortTmpTtpBaseValue = positionPrice * (1 - i_trailingTakeProfitPercentageTriggerPerOne)
shortTmpTtpBottomValue = shortTmpTtpBaseValue * (1 - i_trailingTakeProfitPercentageOffsetPerOne)
shortTrailingTakeProfitOffsetFloat = shortTmpTtpBaseValue - shortTmpTtpBottomValue
float shortTrailingTakeProfitOffsetTick = math.floor (shortTrailingTakeProfitOffsetFloat / syminfo.mintick)

if i_enableTrailingTakeProfit
    longTrailingTakeProfitPrice := positionPrice * (1 + i_trailingTakeProfitPercentageTriggerPerOne)
    longTrailingTakeProfitOffset := longTrailingTakeProfitOffsetTick
    shortTrailingTakeProfitPrice := positionPrice * (1 - i_trailingTakeProfitPercentageTriggerPerOne)
    shortTrailingTakeProfitOffset := shortTrailingTakeProfitOffsetTick
else
    longTrailingTakeProfitPrice := na
    longTrailingTakeProfitOffset := na
    shortTrailingTakeProfitPrice := na
    shortTrailingTakeProfitOffset := na
// Quick Paste - END

// This might trigger additional close orders than needed in zignaly
// Better close an existing order twice than regreting zignaly not having closed it.

if (not (i_strategyType == _REVERSE_))
    tmpOrderAlertMessage := zig.longExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
else // REVERSE
    tmpOrderAlertMessage := zig.reverseLongExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
tmpOrderComment := f_getOrderComment("close", strategy.long, tmpOrderAlertMessage)

bool i_enableAnyStopLoss = i_enableStopLoss or i_enableBreakEven or i_enableCustomBreakEven

// Exit the signal if
// * We are not in reverse mode
// * We are in Long reverse mode and there is not a new Short reverse entry that would have closed it.
if (
     (not (i_strategyType == _REVERSE_))
     or
     ((i_strategyType == _REVERSE_) and (not (SHORT and (not SHORT_CLOSE))))
   )
    strategy.exit("[L]-[SL/TP]", "[L]", stop= i_enableAnyStopLoss ? LongStopLoss : na, limit= i_enableTakeProfit ? LongTakeProfit : na, trail_price= i_enableTrailingTakeProfit ? longTrailingTakeProfitPrice : na, trail_offset = i_enableTrailingTakeProfit ? longTrailingTakeProfitOffset : na, alert_message= i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)

if (not (i_strategyType == _REVERSE_))
    tmpOrderAlertMessage := zig.shortExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
else // REVERSE
    tmpOrderAlertMessage := zig.reverseShortExit(key=i_zignalyServiceSecretKey,exchange=i_zignalyExchange,exchangeAccountType=i_zignalyExchangeType,pair=exchangeTickerID,leverage=i_zignalyLeverage,alertType=i_zignalyAlertType, signalId=stringNA, partial=false)
tmpOrderComment := f_getOrderComment("close", strategy.short, tmpOrderAlertMessage)

// Exit the signal if
// * We are not in reverse mode
// * We are in Short reverse mode and there is not a new Long reverse entry that would have closed it.
if (
     (not (i_strategyType == _REVERSE_))
     or
     ((i_strategyType == _REVERSE_) and (not (LONG and (not LONG_CLOSE))))
   )
    strategy.exit("[S]-[SL/TP]", "[S]", stop= i_enableAnyStopLoss ? ShortStopLoss : na, limit= i_enableTakeProfit ? ShortTakeProfit : na, trail_price= i_enableTrailingTakeProfit ? shortTrailingTakeProfitPrice : na, trail_offset = i_enableTrailingTakeProfit ? shortTrailingTakeProfitOffset : na, alert_message= i_enableZignalyAlert ? tmpOrderAlertMessage : na, comment=tmpOrderComment)

// Handle STOP LOSS (and similar) order exits - END

// Strategy Body - END

Overview

A short is taken when a light-green to dark-green squeeze section is smaller while price is higher; a long is taken on the opposite red-section pattern, and a second pair is checked to confirm the divergence. Users choose long-only, short-only, or both, strict mode, and a date window. Stop loss is on by default; take profit, trailing profit, and break-even are off. It can send Zignaly order alerts.

Details

Published by

ruckard on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Divergence, Oscillators & momentum, Moving averages

Markets

Crypto

Timeframes

M15-H1

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
(ZIG) Enable Alert message {True}booltruei_enableZignalyAlert
(ZIG) Service Secret Key {MYSECRETKEY}stringMYSECRETKEYi_zignalyServiceSecretKey
(ZIG) Integration typestring_ZIGHYBRIDINTEGRATION_i_zignalyIntegrationTypePre
(ZIG) Provider typestring_ZIGCOPYTRADERPROVIDER_i_zignalyProviderTypePre
(STRAT) Strategy Typestring_L_i_strategyType
(STRAT) (L) Strict mode {True}booltruei_enableLongStrictStrategy
(STRAT) (S) Strict mode {False}boolfalsei_enableShortStrictStrategy
(STRAT) Strategy Start Time {2021-01-01 00:00:00}timetimestamp('01 Jan 2021')i_strategyStartTime
(STRAT) Strategy Stop Time {2021-12-31 00:00:00}timetimestamp('31 Dec 2021')i_strategyStopTime
(STRAT) Strategy Start Time forced? {True}booltruei_strategyStartTimeOn
(STRAT) Strategy Stop Time forced? {False}boolfalsei_strategyStopTimeOn
Use {True}booltruei_enableCurieClosingLines
Show all 40 settings
SettingTypeDefaultVariable
Show {True}booltruei_showCurieClosingLines
High MA {SMA/20/ohlc4}stringSMAi_curieHighClosingMaType
i_curieHighClosingMaLengthint20i_curieHighClosingMaLength
i_curieHighClosingMaSourcesourceohlc4i_curieHighClosingMaSource
Highest MA Length {5}int5i_curieHighestClosingMaLength
Low MA {SMA/25/ohlc4}stringSMAi_curieLowClosingMaType
i_curieLowClosingMaLengthint25i_curieLowClosingMaLength
i_curieLowClosingMaSourcesourceohlc4i_curieLowClosingMaSource
Lowest MA Length {10}int10i_curieLowestClosingMaLength
Source {ohlc4}sourceohlc4i_curieSource
(DEBUG) Enable debug on order comments {False}boolfalsei_enableOrderCommentDebug
(STOPTAKE) Take Profit? {false}boolfalsei_enableTakeProfit
(STOPTAKE) Stop Loss? {True}booltruei_enableStopLoss
(TRAILING) Enable Trailing Take Profit (%) {False}boolfalsei_enableTrailingTakeProfit
(BREAKEVEN) Enable Break Even (%) {False}boolfalsei_enableBreakEven
(BREAKEVEN) Enable Custom Break Even (%) {False}boolfalsei_enableCustomBreakEven
(STOPTAKE) Calculate Take Profit from R/R Ratio (%) {False}boolfalsei_enableRiskRewardRatio
(STOPTAKE) Take Profit % {3.0}float1.1i_takeProfit
(STRAT) Risk/Reward Ratio % {1.5}float1.5i_positionRatio
(STOPTAKE) Stop Loss % {7.0}float7.0i_stopLoss
(STOPTAKE) Stop Loss typestring_SLATR_i_stopLossTypePre
(STOPTAKE) ATR Stop Loss Length [14]int14i_atrStopLossLength
(STOPTAKE) ATR Stop Loss Source [close]sourceclosei_atrStopLossSource
(STOPTAKE) ATR Stop Loss Multiplier % {2.0}float2.0i_atrStopLossMultiplier
(TRAILING) Trailing Take Profit Trigger (%) {2.5}float1.2i_trailingTakeProfitPercentageTrigger
(TRAILING) Trailing Take Profit as a percentage of Trailing Take Profit Trigger (%) {25.0}float25.0i_trailingTakeProfitPercentageOffset
(BREAKEVEN) Break Even Trigger (%) {2.15}float2.15i_breakEvenTrigger
(BREAKEVEN) Break Even Stop Loss (%) {0.25}float0.25i_breakEven
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

24 stars

This overlay strategy combines a Cipher B WaveTrend oscillator with two optional moving averages on the price chart. It exposes WaveTrend channel and average lengths, overbought and oversold levels, buy, sell and gold dots, and regular or hidden divergence mar

Strategy v5 Oscillators & momentumMoving averages
Updated 4 years ago · MPL-2.0 GitHub repository of Cipher B + EMAs Strategy

This strategy executes long positions by combining a 200-period EMA trend filter, Stochastic RSI oversold crosses, and Williams Alligator retracements, supplemented by RSI divergence detection. It triggers long entries when the price trades above the 200 EMA w

Strategy v4 TrendOscillators & momentum
Updated 3 years ago · Apache-2.0 GitHub repository of 200 EMA + Alligator + Stochastic Long Strategy
330 stars

MACD + Divergences [Alorse] is a momentum strategy that calculates the MACD oscillator and identifies regular and hidden divergences using pivot high and low points. It filters trade signals with dual Exponential Moving Averages (EMA 150 and 600) to ensure tra

Strategy v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]

Awesome Oscillator - Eterna MCP

by EternaHybridExchange
9 stars

This strategy implements Bill Williams' Awesome Oscillator (AO) calculated as the difference between two simple moving averages of the median price. It generates long and short entries using three classic signal triggers: zero-line crossovers, twin peaks diver

Strategy v5 Oscillators & momentumMoving averages
Updated 7 months ago · MIT GitHub repository of Awesome Oscillator - Eterna MCP

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy

This strategy combines Arnaud Legoux Moving Average percentage price change calculations with an adaptive Gaussian Moving Average to generate trend-following signals. Long and short entries are triggered when the ALMA change calculation crosses above or below

Strategy v5 Moving averagesOscillators & momentum
Updated 6 weeks ago · None stated GitHub repository of ESM2024 - ALMA Smoothed Gaussian Moving Average

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers