Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # SM_MultipleIndicatorLabels
- #
- #
- # version 1.0
- #
- input Show_Impulse_Extras = yes;
- input Show_ATR_Trailing_Stop = yes;
- input Show_MACD_Trend = yes;
- ####################################
- #
- # Exponential moving average (uses 13 period by default)
- #
- ####################################
- input EMA_Length = 13;
- def EMA_price = close;
- def EMA_calculation = ExpAverage(EMA_price, EMA_Length);
- def EMA_above = if close > EMA_calculation then 1 else 0;
- def EMA_below = if close <= EMA_calculation then 1 else 0;
- ####################################
- #
- # MACD + MACD Histogram
- #
- ####################################
- input MACD_fastLength = 12;
- input MACD_slowLength = 26;
- input MACDLength = 9;
- input MACD_averageType = AverageType.EXPONENTIAL;
- def MACD_Value = MovingAverage(MACD_averageType, close, MACD_fastLength) - MovingAverage(MACD_averageType, close, MACD_slowLength);
- def MACD_Avg = MovingAverage(MACD_averageType, MACD_Value, MACDLength);
- def MACD_Diff = MACD_Value - MACD_Avg;
- def ZeroLine = 0;
- def MACD_PositiveAndUp;
- def MACD_PositiveAndDown;
- def MACD_NegativeAndDown;
- def MACD_NegativeAndUp;
- def MACD_GoingUp;
- def MACD_GoingDown;
- if MACD_Diff >= 0 {
- if MACD_Diff > MACD_Diff[1] {
- MACD_PositiveAndUp = 1;
- MACD_PositiveAndDown = 0;
- MACD_NegativeAndUp = 0;
- MACD_NegativeAndDown = 0;
- MACD_GoingUp = 1;
- MACD_GoingDown = 0;
- } else {
- MACD_PositiveAndUp = 0;
- MACD_PositiveAndDown = 1;
- MACD_NegativeAndUp = 0;
- MACD_NegativeAndDown = 0;
- MACD_GoingUp = 0;
- MACD_GoingDown = 1;
- }
- } else {
- if MACD_Diff < MACD_Diff[1] {
- MACD_PositiveAndUp = 0;
- MACD_PositiveAndDown = 0;
- MACD_NegativeAndUp = 0;
- MACD_NegativeAndDown = 1;
- MACD_GoingUp = 0;
- MACD_GoingDown = 1;
- } else {
- MACD_PositiveAndUp = 0;
- MACD_PositiveAndDown = 0;
- MACD_NegativeAndUp = 1;
- MACD_NegativeAndDown = 0;
- MACD_GoingUp = 1;
- MACD_GoingDown = 0;
- }
- }
- ####################################
- #
- # Impulse
- #
- ####################################
- def Impulse_Up;
- def Impulse_Down;
- def Impulse_Neutral;
- if EMA_above == 1 { # cases 1, 2, 3
- Impulse_Down = 0; # Impulse down requires EMA_Down
- Impulse_Up = if MACD_GoingUp == 1 then 1 else 0;
- Impulse_Neutral = if MACD_GoingDown == 1 then 1 else 0;
- } else { # Cases 4, 5, 6
- Impulse_Up = 0;
- Impulse_Down = if MACD_GoingDown == 1 then 1 else 0;
- Impulse_Neutral = if MACD_GoingUp == 1 then 1 else 0;
- }
- AddLabel(Impulse_Up, "IMPULSE", COLOR.DARK_GREEN);
- AddLabel(Impulse_Down, "IMPULSE", COLOR.DARK_RED);
- AddLabel(Impulse_Neutral, "IMPULSE", COLOR.GRAY);
- ####################################
- #
- # EMA Label
- #
- ####################################
- def show_EMA_up;
- def show_EMA_down;
- if Show_Impulse_Extras == yes {
- show_EMA_up = if EMA_above == 1 then 1 else 0;
- show_EMA_down = if EMA_below == 1 then 1 else 0;
- } else {
- show_EMA_up = 0;
- show_EMA_down = 0;
- }
- AddLabel(show_EMA_up, "EMA", COLOR.DARK_GREEN);
- AddLabel(show_EMA_down, "EMA", COLOR.DARK_RED);
- ####################################
- # MACD Histogram label
- ####################################
- def show_MACD_PU;
- def show_MACD_PD;
- def show_MACD_NU;
- def show_MACD_ND;
- if Show_Impulse_Extras == yes {
- show_MACD_PU = if MACD_PositiveAndUp == 1 then 1 else 0;
- show_MACD_PD = if MACD_PositiveAndDown == 1 then 1 else 0;
- show_MACD_NU = if MACD_NegativeAndUp == 1 then 1 else 0;
- show_MACD_ND = if MACD_NegativeAndDown == 1 then 1 else 0;
- } else {
- show_MACD_PU = 0;
- show_MACD_PD = 0;
- show_MACD_NU = 0;
- show_MACD_ND = 0;
- }
- AddLabel(show_MACD_PU, "MACD Histogram", COLOR.DARK_GREEN);
- AddLabel(show_MACD_PD, "MACD Histogram", COLOR.DARK_RED);
- AddLabel(show_MACD_NU, "MACD Histogram", COLOR.DARK_GREEN);
- AddLabel(show_MACD_ND, "MACD Histogram", COLOR.DARK_RED);
- ####################################
- # MACD Trend label
- ####################################
- def MACD_Positive = if MACD_Diff > 0 then 1 else 0;
- def MACD_Negative = if MACD_Diff <= 0 then 1 else 0;
- def show_MACD_P;
- def show_MACD_N;
- if Show_MACD_Trend == yes {
- show_MACD_P = if MACD_Positive == 1 then 1 else 0;
- show_MACD_N = if MACD_Negative == 1 then 1 else 0;
- } else {
- show_MACD_P = 0;
- show_MACD_N = 0;
- }
- AddLabel(show_MACD_P, "MACD Trend", Color.DARK_GREEN);
- AddLabel(show_MACD_N, "MACD Trend", COLOR.DARK_RED);
- ####################################
- #
- # ATR Trailing Stop
- #
- ####################################
- input trailType = {default modified, unmodified};
- input ATRPeriod = 9;
- input ATRFactor = 2.9;
- input firstTrade = {default long, short};
- input averageType = AverageType.EXPONENTIAL;
- Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);
- def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
- def HRef = if low <= high[1] then high - close[1] else (high - close[1]) - 0.5 * (low - high[1]); def LRef = if high >= low[1]
- then close[1] - low
- else (close[1] - low) - 0.5 * (low[1] - high);
- def trueRange;
- switch (trailType) {
- case modified:
- trueRange = Max(HiLo, Max(HRef, LRef));
- case unmodified:
- trueRange = trueRange(high, close, low);
- }
- def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
- def state = {default init, long, short};
- def trail;
- switch (state[1]) {
- case init:
- if (!IsNaN(loss)) {
- switch (firstTrade) {
- case long:
- state = state.long;
- trail = close - loss;
- case short:
- state = state.short;
- trail = close + loss;
- }
- } else {
- state = state.init;
- trail = Double.NaN;
- }
- case long:
- if (close > trail[1]) {
- state = state.long;
- trail = Max(trail[1], close - loss);
- } else {
- state = state.short;
- trail = close + loss;
- }
- case short:
- if (close < trail[1]) {
- state = state.short;
- trail = Min(trail[1], close + loss);
- } else {
- state = state.long;
- trail = close - loss;
- }
- }
- def ATRLabelLong = if state == state.long then 1 else 0;
- def ATRLabelShort = if state != state.long then 1 else 0;
- def ShowATRLabelLong;
- def ShowATRLabelShort;
- if Show_ATR_Trailing_Stop == yes {
- ShowATRLabelLong = if ATRLabelLong == 1 then 1 else 0;
- ShowATRLabelShort = if ATRLabelShort == 1 then 1 else 0;
- } else {
- ShowATRLabelLong = 0;
- ShowATRLabelShort = 0;
- }
- AddLabel(ShowATRLabelLong, "ATR Trailing Stop", COLOR.DARK_GREEN);
- AddLabel(ShowATRLabelShort, "ATR Trailing Stop", COLOR.DARK_RED);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement