input price1 = hl2;
input length = 10;
def num = fold count = 1 to length with num_temp = 0 do
num_temp + (1 + count) * price1[count];
def denom = fold count2 = 1 to length with denom_temp = 0 do
denom_temp + price1[count2];
def Ehler_CG = If(denom == 0, 0, -num / denom);
def CG = Ehler_CG;
def CG1 = Ehler_CG[1];
input pricehurst = hl2;
input lengthHurst = 10;
input InnerValue = 0.02;
input OuterValue = 0.03;
input ExtremeValue = 0.035;
#def displacement = 0;
def displacement = (-lengthHurst / 2) + 1;
def dPrice = pricehurst[displacement];
rec CMA = if !IsNaN(dPrice) then Average(dPrice, AbsValue(lengthHurst)) else CMA[1] + (CMA[1] - CMA[2]);
def OscValue =
if high >= high[1] and low <= low[1]
then
if close >= close[1] # high >= high[2]
then high
else low
else
if high > high[1]
then high
else
if low < low[1]
then low
else
if close > close[1]
then high
else
if close < close[1]
then low
else (high + low) / 2;
def HurstOsc = (100 * OscValue / CMA) - 100;
def CenterLine = 0;
def UpperExtremeBand = ExtremeValue;
def LowerExtremeBand = - ExtremeValue;
def UpperOuterBand = OuterValue;
def LowerOuterBand = - OuterValue;
def UpperInnerBand = InnerValue;
def LowerInnerBand = - InnerValue;
def up3 = HurstOsc <= - ExtremeValue;
def down3 = HurstOsc >= ExtremeValue;
#
plot UP1 = if CG crosses above CG1 and up3 then 1 else 0;
UP1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UP1.SetLineWeight(5);
UP1.SetDefaultColor(Color.WHITE);
Alert (UP1, "UP", Alert.BAR, Sound.Ring);
#
plot down1 = if CG crosses below CG1 and down3 then 1 else 0;
down1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
down1.SetLineWeight(5);
down1.SetDefaultColor(Color.WHITE);
Alert(down1, "DOWN", Alert.BAR, Sound.Ring);
input price = close;
input stdevLength = 5;
input avgOfStdevLength = 9;
input ZMLength = 14;
input ZMLengthLowerLimit = 3;
input ZMLengthUpperLimit = 30;
input Over_Bought = 90;
input Over_sold = 10;
Assert(ZMLengthLowerLimit > 0, "'ZM length lower limit' must be positive: " + ZMLengthLowerLimit);
Assert(ZMLength between ZMLengthLowerLimit and ZMLengthUpperLimit, "'ZM length' must be between lower and upper limit: " + ZMLength);
def std = StDev(price, stdevLength);
def ratio = std / Average(std, avgOfStdevLength);
def dynamicLength = Floor(ZMLength / ratio);
def limitedLength = if dynamicLength between ZMLengthLowerLimit and ZMLengthUpperLimit then dynamicLength else 0;
def sf = 2 / (limitedLength + 1);
def bn = Max(BarNumber(), 0);
# 10^-5 precision for ema multiplier
def expIndex = if limitedLength == 0 then 1 else Max(1, bn - Ceil(-5 / Lg(1 - sf)));
def fromIndex = if IsNaN(expIndex) then 1 else expIndex;
def chg = price - price[1];
def absChg = AbsValue(chg);
def netChgAvg = fold indexN = fromIndex to bn + 1 with accuN do sf * (if IsNaN(GetValue(chg, bn - indexN)) then 0 else GetValue(chg,
bn - indexN)) + (1 - sf) * accuN;
def totChgAvg = fold indexT = fromIndex to bn + 1 with accuT do sf * (if IsNaN(GetValue(absChg, bn - indexT)) then 0 else GetValue
(absChg, bn - indexT)) + (1 - sf) * accuT;
def RSI = if totChgAvg != 0 and limitedLength != 0 then 50 * (netChgAvg / totChgAvg + 1) else RSI[1];
#############################
def ZM = RSI;
def OverBought = Over_Bought;
def OverSold = Over_sold;
def centr1 = 50;
def centr2 = 50;
plot Up2 = ZM crosses below OverSold and up3;
Up2.SetStyle(Curve.POINTS);
Up2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up2.SetLineWeight(5);
Up2.SetDefaultColor(Color.DARK_GREEN);
Alert (Up2, "UP", Alert.BAR, Sound.Ring);
AddLabel(Up2, "Вверх", if Up2 then Color.DARK_GREEN else Color.RED);
plot Down2 = ZM crosses above OverBought and down3;
Down2.SetStyle(Curve.POINTS);
Down2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Down2.SetLineWeight(5);
Down2.SetDefaultColor(Color.RED);
Alert(Down2, "DOWN", Alert.BAR, Sound.Ring);
AddLabel(Down2, "Вниз", if Down2 then Color.RED else Color.DARK_GREEN);
input over_boughtp = 80.0;
input over_soldp = 20.0;
input percentDLength = 4;
input percentKLength = 6;
def min_low = Lowest(low, percentKLength);
def max_high = Highest(high, percentKLength);
def rel_diff = close - (max_high + min_low) / 2;
def diff = max_high - min_low;
def avgrel = ExpAverage(ExpAverage(rel_diff, percentDLength), percentDLength);
def avgdiff = ExpAverage(ExpAverage(diff, percentDLength), percentDLength);
def SMI = (avgrel / (avgdiff / 2) + 1) * 50;
def AvgSMI = ExpAverage(SMI, percentDLength);
plot up4 = if SMI > AvgSMI and SMI[1]<=AvgSMI[1] and SMI < over_soldp and up3 then 1 else 0;
up4.setLineWeight(5);
up4.setDefaultColor(color.DARK_GREEN);
up4.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
Alert (up4,"UP",Alert.Bar,Sound.ring);
AddLabel(up4, "Вверх", if up4 then Color.DARK_GREEN else Color.RED);
plot down4 = if SMI < AvgSMI and SMI[1]>=AvgSMI[1] and SMI > over_boughtp and down3 then 1 else 0;
down4.setLineWeight(5);
down4.setDefaultColor(color.red);
down4.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
Alert(down4,"DOWN",Alert.Bar,Sound.ring);
AddLabel(down4, "Вниз", if down4 then Color.RED else Color.DARK_GREEN);
#######CCI
input longLength = 14;
input CCIOversold = -200;
input CCIOverbought = 200;
def pricecc = close;
def LCCI = CCI(length = longLength);
#def sCCI_BUY = LCCI crosses above CCIOverbought;
#def sCCI_SELL = LCCI crosses below CCIOversold;
#########WATERFALL
def WaterfallDn = if (close[3] <= open[3] and close[2] <= open[2] and close[1] <= open[1] and close[0] <= close[1]) then 1 else 0;
def WaterfallUp = if (close[3] >= open[3] and close[2] >= open[2] and close[1] >= open[1] and close[0] >= close[1]) then 1 else 0;
rec up = if Up2 or UP1 or up4 then 1 else 0;
rec down = if Down2 or DOWN1 or down4 then 1 else 0;
##########---------------------------TEST OPTIONS---------------------------------------------
#hint timetest: Test according to timeperiods
#hint expiry: Numbers of candles after signal (1 = next candle after signal, 2 = second candle)
#hint secondcandle: Use only if candleexpiry = 1. Adding signals taking second candle after loss
#hint moneycount: display labels with profit/loss in $
#hint invest: amount of investment per trade
#hint RPI: Return Per Investment (payout)
input expiry = 1;
input secondcandle = no;
input moneycount = no;
input invest = 25;
input RPI = 0.80;
input timetest = no;
########-------------------------------------------TIME FILTER-----------------------------------------------
#hint TradingOpen1: EST NY TIME
input TradingOpen1 = 330;
input TradingClose1 = 530;
input TradingOpen2 = 0930;
input TradingClose2 = 1130;
#hint input AsiaOpenTime1 = 2000;
#hint input AsiaCloseTime1 = 2359;
#hint input AsiaOpenTime2 = 0000;
#hint input AsiaCloseTime2 = 0200;
#hint input EUOpenTime = 330;
#hint input EUCloseTime = 530;
#hint input USOpenTime = 0930;
#hint input USCloseTime = 1130;
# Set below to 'True' to allow for DST difference adjustment if you are setting
# windows up for a european instrument like the euro.
input EnableDSTAutoAdjust = {default "Yes", "No"};
input DSTOffsetHours = 1;
#==========================================================================
# Figure out DST adjustment for diffence between USA and europe
#==========================================================================
#basically, if we are after the 2nd Sunday in March and before
#the last Sunday in March, we need to offset the Trading Windows by 1hr forward
#if we are after the last Sunday in Oct, until the 1st Sunday in Nov
# we need to offset the trading windows by 1hr backward...
def CurrentYear = GetYear();
def CurrentMonth = GetMonth();
def CurrentDOM = GetDayOfMonth(GetYYYYMMDD());
#What is the first day of the week for the 1st of this month?
def Day1DOW1 = GetDayOfWeek(CurrentYear * 10000 + CurrentMonth * 100 + 1);
def FirstSundayDOM1 = if Day1DOW1 < 7
then 7 - Day1DOW1 + 1
else 1;
def SecondSundayOfMonth = FirstSundayDOM1 + 7;
def isMarch = If (CurrentMonth == 3, 1, 0);
def SpringDSTShiftStart = If (isMarch and (SecondSundayOfMonth <= CurrentDOM), 1, 0);
#last sunday in march...
def SpringDSTShiftStop = If (isMarch and ((SecondSundayOfMonth + 14) > CurrentDOM), 1, 0);
def DoSpringShift = If (SpringDSTShiftStart and SpringDSTShiftStop and EnableDSTAutoAdjust, 1, 0);
def isOctober = If (CurrentMonth == 10, 1, 0);
def isNovember = If (CurrentMonth == 11, 1, 0);
def FallDSTShiftStart = If (isOctober and ((SecondSundayOfMonth + 14) <= CurrentDOM), 1, 0);
def FallDSTShiftStop = If (isNovember and FirstSundayDOM1 > CurrentDOM, 1, 0);
def DoFallShift = If (FallDSTShiftStart or FallDSTShiftStop and EnableDSTAutoAdjust, 1, 0);
def isToday = If(GetDay() == GetLastDay(), 1, 0);
#PITA. SecondsTillTime only takes constants... Need to manually adjust for DST if it's enabled.
def DSTAdjust = If (DoSpringShift, DSTOffsetHours * 3600, If (DoFallShift, -DSTOffsetHours * 3600, 0));
def TradingOpenTime1 = If((SecondsTillTime(TradingOpen1) + DSTAdjust > 0), 0, 1);
def TradingCloseTime1 = If((SecondsTillTime(TradingClose1) + DSTAdjust > 0), 0, 1);
def TradingWindow1 = If(TradingOpenTime1 and !TradingCloseTime1, 1, 0);
def TradingOpenTime2 = If((SecondsTillTime(TradingOpen2) + DSTAdjust > 0), 0, 1);
def TradingCloseTime2 = If((SecondsTillTime(TradingClose2) + DSTAdjust > 0), 0, 1);
def TradingWindow2 = If(TradingOpenTime2 and !TradingCloseTime2, 1, 0);
AddVerticalLine(TradingWindow1 and TradingWindow1[1] == 0 and timetest, "TradingOpen 1" , Color.GREEN, Curve.SHORT_DASH);
AddVerticalLine(TradingWindow1 == 0 and TradingWindow1[1] and timetest, "TradingClose 1" , Color.RED, Curve.SHORT_DASH);
AddVerticalLine(TradingWindow2 and TradingWindow2[1] == 0 and timetest, "TradingOpen 2" , Color.GREEN, Curve.SHORT_DASH);
AddVerticalLine(TradingWindow2 == 0 and TradingWindow2[1] and timetest, "TradingClose 2" , Color.RED, Curve.SHORT_DASH);
#####-------------------------------------TESTER-----------------------------------------------------
def ITM1 = if timetest and TradingWindow1 == 0 and TradingWindow2 == 0 then 0 else if (Up[expiry] and close[0] > close[expiry]) or (Down[expiry] and close[0] < close[expiry]) or (secondcandle and Up[2] and close[1] < close[2] and close > close[1]) or (secondcandle and Down[2] and close[1] > close[2] and close < close[1]) then 1 else 0;
def itm = if IsNaN(ITM1) then 0 else ITM1;
def itmsum = TotalSum(itm);
def OTM1 = if timetest and TradingWindow1 == 0 and TradingWindow2 == 0 then 0 else if (Up[expiry] and close[0] < close[expiry]) or (Down[expiry] and close[0] > close[expiry]) or (secondcandle and Up[2] and close[1] < close[2] and close < close[1]) or (secondcandle and Down[2] and close[1] > close[2] and close > close[1]) then 1 else 0;
def otm = if IsNaN(OTM1) then 0 else OTM1;
def otmsum = TotalSum(otm);
def itmrate = RoundUp((itmsum / (itmsum + otmsum) * 100), 2);
def label = 1;
AddLabel(label, Concat("ITM = ", itmsum), Color.GREEN);
AddLabel(label, Concat("OTM = ", otmsum), Color.RED);
AddLabel(label, Concat("ITM RATE = %", itmrate), Color.WHITE);
#####---------Moneycount--------------------------------------------
def PL = invest * itmsum * RPI - invest * otmsum;
def PLColor = if PL > 0 then 6 else 5;
AddLabel(moneycount, Concat("PROFIT/LOSS = $", PL), GetColor(PLColor));