EMA Cross (data chaining template)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Protervus - https://protervus.trade

// [-------------- Start of your indicator's code --------------]
//@version=5
indicator("EMA Cross (data chaining)", overlay = true)

// Create inputs for our two EMAs
FastEmaLen          = input.int(50, title = "Fast EMA Length")
SlowEmaLen          = input.int(200, title = "Fast EMA Length")

// Calculate EMAs
FastEma             = ta.ema(close, FastEmaLen)
SlowEma             = ta.ema(close, SlowEmaLen)

// Create Entry conditions
BullishCross        = ta.crossover(FastEma, SlowEma)
BearishCross        = ta.crossunder(FastEma, SlowEma)

// Create Exit conditions
PriceAboveFastEma   = ta.crossover(close, FastEma)
PriceBelowFastEma   = ta.crossunder(close, FastEma)

// Create Filter conditions
FastAboveSlow       = FastEma > SlowEma
SlowAboveFast       = FastEma < SlowEma

// Show EMAs on chart
plot(FastEma, color = color.new(color.green, 0))
plot(SlowEma, color = color.new(color.red, 0))

// [-------------- End of your indicator's code ----------------]

//===================================================== START OF PROTERVUS TRADING TOOLKIT SNIPPET ========================================= Version 1.2 ===
//                                                  This snippet should be placed at the end of your code
//==========================================================================================================================================================
// CHAINED DATA INPUTS (do not edit)            
//==========================================================================================================================================================
tooltip_source      = 'Receive signals from an chained indicator'
tooltip_signal      = 'Choose whether this indicator works as Signal or Filter for a chained indicator'

SourceGroup         = '🔗 Protervus Chaining'
ChainedIndicator    = input.bool(false, title='Receive Data',                                           group = SourceGroup, inline='input', tooltip = tooltip_source )
ExternalIndicator   = input.source(close, title='',                                                     group = SourceGroup, inline='input'                           )
SignalMode          = input.string(title='Output mode', defval='Signal', options=['Signal', 'Filter'],  group = SourceGroup,                 tooltip = tooltip_signal )

ExtIndicatorData    = nz(ExternalIndicator)
null = 0



//==========================================================================================================================================================
// CONDITIONS INPUTS (Your conditions go here)    <-------------
//==========================================================================================================================================================
// You can null conditions you don't need, as well as adding new ones following the template below.
// Once the code has been compiled, you can enable the conditions by ticking the relevant box in the Settings panel.

EntryCondition_1    = input.bool(true,  'Ema Cross',                                                    group = 'Entry Conditions')
EntryCondition_2    = null //input.bool(false, 'Description of Entry condition',                               group = 'Entry Conditions')
EntryCondition_3    = null //input.bool(false, 'Description of Entry condition',                               group = 'Entry Conditions')

FilterCondition_1   = input.bool(false, 'Trend',                                                        group = 'Filters')
FilterCondition_2   = null //input.bool(false, 'Description of Filter condition',                              group = 'Filters')

ExitCondition_1     = input.bool(false, "Price crosses Fast Ema",                                       group = "Exit Conditions")
ExitCondition_2     = null //input.bool(false, "Description of TakeProfit Condition",                          group = "Exit Conditions")

StopCondition_1     = null //input.bool(false, "Description of Stop Condition",                                group = "Stop Conditions")
StopCondition_2     = null //input.bool(false, "Description of Stop Condition",                                group = "Stop Conditions")



//==========================================================================================================================================================
// ENTRY \ FILTER CONDITIONS                      <-------------
//==========================================================================================================================================================
// You should now associate EntryCondition_X to Entry condition(s) scripted in your indicator - same for eventual Filters.
// By Default, EntryCondition_X can trigger both Long or Short signals, but you can also set conditions for Long Only or Short Only by separating inputs.
// (e.g. "LongEntryCondition_2 = null" and "ShortEntryCondition_2 = YOUR_CONDITION" will void Long Entry, keeping only the Short Entry as valid)


// Assign LONG ENTRY conditions   --------------------------------------------------------------------------------------------------------------------------
LongEntryCondition_1    = BullishCross  // Replace "null" with the condition of your Indicator or leave "null" to void this signal
LongEntryCondition_2    = null  // Replace "null" with the condition of your Indicator or leave "null" to void this signal

LongFilterCondition_1   = FastAboveSlow  // Replace "null" with the condition of your Indicator or leave "null" to void this signal
LongFilterCondition_2   = null  // Replace "null" with the condition of your Indicator or leave "null" to void this signal



// Prepare LONG ENTRY Conditions
LongCondition = SignalMode == 'Signal' ? 
 EntryCondition_1 and LongEntryCondition_1
 or 
 EntryCondition_2 and LongEntryCondition_2


// If you don't use Filters, just leave "LongFilterCondition_X = null" and don't select "Filter" as Output mode
 : SignalMode == "Filter" ?
 FilterCondition_1 and LongFilterCondition_1
 or 
 FilterCondition_2 and LongFilterCondition_2
 
 
 : na // don't comment this
 
// Finalize LONG ENTRY signal (do not edit)
LongEntry  = ChainedIndicator ? (ExtIndicatorData == 1 or ExtIndicatorData == 6) and LongCondition : LongCondition



// Assign SHORT ENTRY conditions   ------------------------------------------------------------------------------------------------------------------------
ShortEntryCondition_1   = BearishCross  // Replace "null" with the condition of your Indicator or leave "null" to void this signal
ShortEntryCondition_2   = null  // Replace "null" with the condition of your Indicator or leave "null" to void this signal

ShortFilterCondition_1  = SlowAboveFast  // Replace "null" with the condition of your Indicator or leave "null" to void this signal
ShortFilterCondition_2  = null  // Replace "null" with the condition of your Indicator or leave "null" to void this signal



// Prepare SHORT ENTRY Conditions
ShortCondition = SignalMode == 'Signal' ? 
 EntryCondition_1 and ShortEntryCondition_1
 or 
 EntryCondition_2 and ShortEntryCondition_2


// If you don't use Filters, just leave "ShortFilterCondition_X = null" and don't select "Filter" as Output mode
 : SignalMode == "Filter" ?
 FilterCondition_1 and ShortFilterCondition_1
 or 
 FilterCondition_2 and ShortFilterCondition_2
 
 
 : na // don't comment this
 

// Finalize SHORT ENTRY signal (do not edit)
ShortEntry = ChainedIndicator ? (ExtIndicatorData == -1 or ExtIndicatorData == 6) and ShortCondition : ShortCondition




//==========================================================================================================================================================
// EXIT CONDITIONS (only in Signal mode)        <-------------
//==========================================================================================================================================================
// Just like Entry conditions, you can add more Exit conditions by adding "or ExitCondition_X and LongExitCondition_X" in the Finalize section.


// Assign LONG EXIT conditions -----------------------------------------------------------------------------------------------------------------------------
LongExitCondition_1     = PriceBelowFastEma  // Replace "null" with the condition of your Indicator or leave "null" to void this signal
LongExitCondition_2     = null  // Replace "null" with the condition of your Indicator or leave "null" to void this signal


// Prepare LONG EXIT conditions
LongExit = 
 ExitCondition_1 and LongExitCondition_1
 or 
 ExitCondition_2 and LongExitCondition_2



// Assign SHORT EXIT conditions ----------------------------------------------------------------------------------------------------------------------------
ShortExitCondition_1    = PriceAboveFastEma  // Replace "null" with the condition of your Indicator or leave "null" to void this signal
ShortExitCondition_2    = null  // Replace "null" with the condition of your Indicator or leave "null" to void this signal


// Prepare SHORT EXIT conditions
ShortExit = 
 ExitCondition_1 and ShortExitCondition_1
 or 
 ExitCondition_2 and ShortExitCondition_2




//==========================================================================================================================================================
// STOP CONDITIONS (only in Signal mode)        <-------------
//==========================================================================================================================================================
// Just like Entry and Exit conditions, you can add more Stop conditions by adding "or StopCondition_X and StopExitCondition_X" in the Finalize section.
// Also, If you want to use Exit Conditions as general closing signal, you can set the Stop = Exit, the rest of the code will handle that situation.
// Example: "LongStop = LongExit" and "ShortStop = ShortExit"


// Assigning LONG STOP conditions --------------------------------------------------------------------------------------------------------------------------
LongStopCondition_1     = PriceBelowFastEma  // Replace "null" with the condition of your Indicator or leave "null" to void this signal
LongStopCondition_2     = null  // Replace "null" with the condition of your Indicator or leave "null" to void this signal


// Prepare LONG STOP conditions
LongStop = 
 StopCondition_1 and LongStopCondition_1
 or 
 StopCondition_2 and LongStopCondition_2




// Assigning SHORT STOP conditions -------------------------------------------------------------------------------------------------------------------------
ShortStopCondition_1    = PriceAboveFastEma  // Replace "null" with the condition of your Indicator or leave "null" to void this signal
ShortStopCondition_2    = null  // Replace "null" with the condition of your Indicator or leave "null" to void this signal


// Prepare SHORT STOP conditions
ShortStop = 
 StopCondition_1 and ShortStopCondition_1
 or 
 StopCondition_2 and ShortStopCondition_2




//==========================================================================================================================================================
// Signal Output (do not edit)
//==========================================================================================================================================================
// Prepare output signal and handle situations where more conditions are valid at the same time. Protervus Trading Toolkit will know what to do. 

Signal 
 = LongEntry and not ShortEntry ? 1
 : ShortEntry and not LongEntry ? -1
 : LongExit ? 2
 : ShortExit ? -2
 : LongStop ? 3
 : ShortStop ? -3
 : 0

// This part must be separated from the initial Signal operation, otherwise the code will stop at the first condition when running.
if LongEntry and ShortEntry
    Signal := 6
if (LongExit and ShortStop) or (ShortExit and LongStop) or (LongExit and LongStop) or (ShortExit and ShortStop)
    Signal := 4
if LongEntry and (LongExit or LongStop or ShortExit and ShortStop)
    Signal := 5
if ShortEntry and (ShortExit or ShortStop or LongExit and LongStop)
    Signal := -5

// Send data via plot, ready to be received from a chained indicator or Protervus Trading Toolkit
plot(Signal, title='🔗 Chained Data', display=display.none)




//==========================================================================================================================================================
// Preventing issues with Signal plot          <-------------
//==========================================================================================================================================================
// Since we plot the Signal for Chained Data as "invisible", it cannot be the only plot in the code, otherwise it will lead to an error.
// So, if your indicator DOES NOT plot anything on the chart, please un-comment the line below:
// plot(close < 0 ? close : na, title='Dummy Plot')




//==========================================================================================================================================================
// SIGNALS LABEL (optional)                    <-------------
//==========================================================================================================================================================
// Optionally show labels for your signals. To add markers for Exits or Stops, just clone a label and replace variables (e.g. LongEntry --> LongExit),
// making sure to give labels unique names and text.


// LongLabel = LongEntry and SignalMode == 'Signal' ? 
//  label.new(bar_index, 
//  y=close,
//  text='L',
//  color=color.green,
//  textcolor=color.white,
//  style=label.style_label_upper_right,
//  yloc=yloc.price,
//  size=size.normal)
//  : na
// label.delete(not LongEntry ? LongLabel : na)

// ShortLabel = ShortEntry and SignalMode == 'Signal' ? 
//  label.new(bar_index, 
//  y=close,
//  text='S',
//  color=color.red,
//  textcolor=color.white,
//  style=label.style_label_lower_right,
//  yloc=yloc.price,
//  size=size.normal)
//  : na
// label.delete(not ShortEntry ? ShortLabel : na)



