Declare Upper;
input TimeFrame = {default "10-Min", "1-Min", "5-Min", "15-Min", "30-Min", "1-Hour"};
input PreMarketStart = 0730;
input PreMarketEnd = 0929;
input StartTime = 0930;
input BuySignalDelay = 29;
input EndTime = 1600;
input BuySignalStop = 60;
def LastCandleStop;
switch (TimeFrame) {
case "1-min": LastCandleStop = 1;
case "5-Min": LastCandleStop = 5;
case "15-Min": LastCandleStop = 15;
case "30-Min": LastCandleStop = 30;
case "1-Hour": LastCandleStop = 60;
default: LastCandleStop = 10;
}
def TradingDayStart= SecondsFromTime(StartTime);
def BuySignalDelaySeconds = BuySignalDelay * 60;
def SignalStart = TradingDayStart > BuySignalDelaySeconds;
def TradingDayEnd = SecondsTillTime(EndTime);
def EndSignalDelaySeconds = BuySignalStop * 60;
def SignalEnd = TradingDayEnd > EndSignalDelaySeconds;
def TradingDay = SignalStart and SignalEnd;
def LastStopDelaySeconds = LastCandleStop * 60;
def EndDay = TradingDayEnd == LastStopDelaySeconds;
def TradingDayExt = TradingDayEnd > LastStopDelaySeconds;
input ShowTestBubbles = no;
input Ema1Length = 5;
input Ema2Length = 12;
input Ema3Length = 34;
input Ema4Length = 50;
plot FastEMA1 = ExpAverage(close, Ema1Length);
FastEMA1.SetDefaultColor(color.light_green);
FastEMA1.HideBubble();
plot FastEMA2 = ExpAverage(close, Ema2Length);
FastEMA2.SetDefaultColor(color.light_red);
FastEMA2.HideBubble();
AddCloud(FastEMA1, FastEMA2, createcolor(0, 255, 153), createcolor(0, 153, 255));
plot SlowEMA1 = ExpAverage(close, Ema3Length);
SlowEMA1.SetDefaultColor(color.light_green);
SlowEMA1.HideBubble();
plot SlowEMA2 = ExpAverage(close, Ema4Length);
SlowEMA2.SetDefaultColor(color.light_red);
SlowEMA2.HideBubble();
AddCloud(SlowEMA1, SlowEMA2, createcolor(0, 255, 0), createcolor(0, 0, 255));
def FastEMABullish = FastEMA1 > FastEMA2;
def SlowEMABullish = SlowEMA1 > SlowEMA2;
def FastEMABearish = FastEMA1 < FastEMA2;
def SlowEMABearish = SlowEMA1 < SlowEMA2;
def BothEMASBullish = FastEMABullish and SlowEMABullish;
def BothEMASBearish = FastEMABearish and SlowEMABearish;
def FastEMAPctBull = ((FastEMA1 / FastEMA2) * 100) - 100;
def FastEMAPctBullRound = Round(FastEMAPctBull, 2);
def FastEMAPctBear = ((FastEMA2 / FastEMA1) * 100) - 100;
def FastEMAPctBearRound = Round(FastEMAPctBear, 2);
def EMASepThrHld;
switch (TimeFrame) {
case "1-min": EMASepThrHld = 0.1;
case "5-Min": EMASepThrHld = 0.2;
case "15-Min": EMASepThrHld = 0.3;
case "30-Min": EMASepThrHld = 0.4;
case "1-Hour": EMASepThrHld = 0.1;
default: EMASepThrHld = 0.25;
}
def FastEMAPctBullish = BothEMASBullish and FastEMAPctBullRound > EMASepThrHld and FastEMAPctBullRound >= FastEMAPctBullRound[1];
def FastEMAPctBearish = BothEMASBearish and FastEMAPctBearRound > EMASepThrHld and FastEMAPctBearRound >= FastEMAPctBearRound[1];
addchartbubble(if ShowTestBubbles and FastEMAPctBullish == 1 then 1 else 0, SlowEMA2*0.995, FastEMAPctBullRound, color.dark_green, no);
addchartbubble(if ShowTestBubbles and FastEMAPctBearish == 1 then 1 else 0, SlowEMA2*1.005, FastEMAPctBearRound, color.dark_green, yes);
def SlowEMAPctBull = ((SlowEMA1 / SlowEMA2) * 100) - 100;
def SlowEMAPctBullRound = Round(SlowEMAPctBull, 2);
def SlowEMAPctBear = ((SlowEMA2 / SlowEMA1) * 100) - 100;
def SlowEMAPctBearRound = Round(SlowEMAPctBear, 2);
def SlowEMAPctBullish = BothEMASBullish and SlowEMAPctBullRound >= SlowEMAPctBullRound[1];
def SlowEMAPctBearish = BothEMASBearish and SlowEMAPctBearRound <= SlowEMAPctBearRound[1];
def FastEMACrossDown = FastEMA1 crosses below FastEMA2;
def FastEMACrossUp = FastEMA1 crosses above FastEMA2;
def FastEMAPctBullDecrease = if BothEMASBullish and FastEMAPctBullRound < (FastEMAPctBullRound[1] * 0.75) then 1 else 0;
def FastEMAPctBearDecrease = if BothEMASBearish and FastEMAPctBearRound < (FastEMAPctBearRound[1] * 0.75) then 1 else 0;
addchartbubble(if ShowTestBubbles and FastEMAPctBullDecrease == 1 then 1 else 0, SlowEMA2*0.995, FastEMAPctBullRound, color.dark_red, no);
addchartbubble(if ShowTestBubbles and FastEMAPctBearDecrease == 1 then 1 else 0, SlowEMA2*1.005, FastEMAPctBearRound, color.dark_red, yes);
def Height = SlowEMA2 - SlowEMA2[11];
def SlopeDeg = Round((ATan(Height / 10) * 180 / Double.Pi), 0);
def BullSlopeSell = BothEMASBullish and SlopeDeg < SlopeDeg[1];
def BearSlopeSell = BothEMASBearish and SlopeDeg > SlopeDeg[1];
input MACDFast = 10;
input MACDSlow = 22;
input MACDLength = 8;
def MACDValue = ExpAverage(close, MACDFast) - ExpAverage(close, MACDSlow);
def MACDAverage = ExpAverage(MACDValue, MACDLength);
def MACDDiff = MACDValue - MACDAverage;
def ZeroLine = 0;
def MACDBull = MACDDiff >= MACDDiff[1];
def MACDBear = MACDDiff <= MACDDiff[1];
input TSILongLength = 25;
input TSIShortLength = 13;
input TSISignalLength = 8;
def TSIDiff = close - close[1];
def DoubleSmoothedAbsDiff = ExpAverage(ExpAverage(AbsValue(TSIDiff), TSILongLength), TSIShortLength);
def TSIRound = Round((100 * (ExpAverage(ExpAverage(TSIDiff, TSILongLength), TSIShortLength)) / DoubleSmoothedAbsDiff), 2);
def TSIBull = (TSIRound > 10) and (TSIRound >= TSIRound[1]);
def TSIBear = (TSIRound < -10) and (TSIRound <= TSIRound[1]);
def TSICrossDown = TSIRound < (TSIRound[1] * 0.91);
def TSICrossUp = TSIRound > (TSIRound[1] * 0.91);
input ShowPreMarketCloud = yes;
input ShowPreMarketLabel = yes;
def PreMarketTimeRange = secondsFromTime(PreMarketStart) >= 0 and secondsTillTime(PreMarketEnd) >= 0;
def PreMarket = PreMarketTimeRange and !PremarketTimeRange[1];
def Pre_Market_High = compoundValue(1, if((high > Pre_Market_High[1] and PremarketTimeRange) or PreMarket, high, Pre_Market_High[1]), high);
def Pre_Market_Low = compoundValue(1, if((low < Pre_Market_Low[1] and PremarketTImeRange) or PreMarket, low, Pre_Market_Low[1]), low);
plot PreMarketHigh = if ShowPreMarketCloud then Pre_Market_High else double.nan;
PreMarketHigh.SetStyle(curve.short_dash);
PreMarketHigh.SetDefaultColor(color.light_gray);
PreMarketHigh.setLineWeight(1);
plot PreMarketLow = if ShowPreMarketCloud then Pre_Market_Low else double.nan;
PreMarketLow.SetStyle(curve.short_dash);
PreMarketLow.SetDefaultColor(color.light_gray);
PreMarketLow.setLineWeight(1);
AddCloud (PreMarketHigh, PreMarketLow, color.light_gray, color.light_gray);
AddLabel(if ShowPreMarketLabel then yes else no, " ", color.black);
AddLabel(if ShowPreMarketLabel then yes else no, " PM High - $" + PreMarketHigh, color.gray);
AddLabel(if ShowPreMArketLabel then yes else no, " PM Low - $" + PreMarketLow, color.gray);
def PreMarketBull = low > Pre_Market_High;
def PreMarketBear = high < Pre_Market_Low;
addchartbubble(if ShowTestBubbles and PreMarketBull == 1 then 1 else 0, SlowEMA2*0.995, "Pre", color.dark_green, no);
addchartbubble(if ShowTestBubbles and PreMarketBear == 1 then 1 else 0, SlowEMA2*1.005, "Pre", color.dark_green, yes);
def PreMarketBullSell = PreMarketBull[1] and low < Pre_Market_High;
def PreMarketBearSell = PreMarketBear[1] and high > Pre_Market_Low;
addchartbubble(if ShowTestBubbles and PreMarketBullSell == 1 then 1 else 0, SlowEMA2*0.995, "Pre", color.dark_red, no);
addchartbubble(if ShowTestBubbles and PreMarketBearSell == 1 then 1 else 0, SlowEMA2*1.005, "Pre", color.dark_red, yes);
def GetReadyBull = TradingDay and BothEMASBullish and PreMarketBull;
def GetReadyBear = TradingDay and BothEMASBearish and PreMarketBear;
def CallBuyInd = BothEMASBullish and FastEMAPctBullish and MACDBull and TSIBull and PreMarketBull;
def PutBuyInd = BothEMASBearish and FastEMAPctBearish and MACDBear and TSIBear and PreMarketBear;
addchartbubble(if ShowTestBubbles and CallBuyInd == 1 then 1 else 0, SlowEMA2*0.995, "C-Ind", color.light_green, no);
addchartbubble(if ShowTestBubbles and PutBuyInd == 1 then 1 else 0, SlowEMA2*1.005, "P-Ind", color.light_green, yes);
def EMAPctAtCallBuy = if CallBuyInd then FastEMAPctBullRound else EMAPctAtCallBuy[1];
def EMAPctAtPutBuy = if PutBuyInd then FastEMAPctBearRound else EMAPctAtPutBuy[1];
def CallEMAPctSell = FastEMAPctBullRound < EMAPctAtCallBuy * 0.65;
def PutEMAPctSell = FastEMAPctBearRound < EMAPctAtPutBuy * 0.65;
def CallHoldInd = BothEMASBullish and PreMarketBull;
def PutHoldInd = BothEMASBearish and PreMarketBear;
addchartbubble(if ShowTestBubbles and CallHoldInd == 1 then 1 else 0, SlowEMA2*0.995, "CH_Ind", color.light_green, no);
addchartbubble(if ShowTestBubbles and PutHoldInd == 1 then 1 else 0, SlowEMA2*1.005, "PH-Ind", color.light_green, yes);
input ShowOverExtCloud = yes;
def OvrExtUp1 = if ShowOverExtCloud and SignalStart and TradingDayExt and (CallBuyInd or CallHoldInd) then FastEMA1 * 1.01 else Double.NaN;
def OvrExtUp2 = if ShowOverExtCloud and SignalStart and TradingDayExt and (CallBuyInd or CallHoldInd) then FastEMA1 * 1.02 else Double.NaN;
AddCloud(OvrExtUp1, OvrExtUp2, color.light_green, color.light_green);
def OvrExtDn1 = if ShowOverExtCloud and SignalStart and TradingDayExt and (PutBuyInd or PutHoldInd) then FastEMA1 * 0.99 else Double.NaN;
def OvrExtDn2 = if ShowOverExtCloud and SignalStart and TradingDayExt and (PutBuyInd or PutHoldInd) then FastEMA1 * 0.98 else Double.NaN;
AddCloud(OvrExtDn1, OvrExtDn2, color.light_green, color.light_green);
def CallSellInd = FastEMACrossDown or TSICrossDown or CallEMAPctSell;
def PutSellInd = FastEMACrossUp or TSICrossUp or PutEMAPctSell;
addchartbubble(if ShowTestBubbles and CallSellInd == 1 then 1 else 0, SlowEMA2*0.995, "CS-Ind", color.light_red, no);
addchartbubble(if ShowTestBubbles and PutSellInd == 1 then 1 else 0, SlowEMA2*1.005, "PS-Ind", color.light_red, yes);
def CallBuy = TradingDay and CallBuyInd;
def PutBuy = TradingDay and PutBuyInd;
addchartbubble(if ShowTestBubbles and CallBuy == 1 then 1 else 0, SlowEMA2*0.995, "Call", color.green, no);
addchartbubble(if ShowTestBubbles and PutBuy == 1 then 1 else 0, SlowEMA2*1.005, "Put", color.green, yes);
def CallHold = TradingDayExt and CallHoldInd and (CallBuy[1] or CallHold[1]);
def PutHold = TradingDayExt and PutHoldInd and (PutBuy[1] or PutHold[1]);
addchartbubble(if ShowTestBubbles and CallHold == 1 then 1 else 0, SlowEMA2*0.995, "C-Hold", color.green, no);
addchartbubble(if ShowTestBubbles and PutHold == 1 then 1 else 0, SlowEMA2*1.005, "P-Hold", color.green, yes);
def CallSell = TradingDayExt and CallSellInd and !CallSellInd[1] and (CallBuy[1] or CallHold[1]);
def PutSell = TradingDayExt and PutSellInd and !PutSellInd[1] and (PutBuy[1] or PutHold[1]);
addchartbubble(if ShowTestBubbles and CallSell == 1 then 1 else 0, SlowEMA2*0.995, "C-Sell", color.red, no);
addchartbubble(if ShowTestBubbles and PutSell == 1 then 1 else 0, SlowEMA2*1.005, "P-Sell", color.red, yes);
DefineGlobalColor("OpenLongPosition", createcolor (0, 255, 0));
DefineGlobalColor("OpenShortPosition", createcolor (0, 0, 255));
DefineGlobalColor("HoldLongPosition", createcolor (204, 255, 204));
DefineGlobalColor("HoldShortPosition", createcolor (102, 255, 255));
DefineGlobalColor("GetReady", createcolor (204, 153, 0));
DefineGlobalColor("SellPosition", createcolor (255, 0, 0));
DefineGlobalColor("TakeProfit", createcolor (255, 0, 255));
DefineGlobalColor("CriteriaNotMet", createcolor (102, 102, 102));
AssignPriceColor(if CallSell then GlobalColor("SellPosition") else if CallBuy then GlobalColor("OpenLongPosition") else if CallHold then GlobalColor("HoldLongPosition") else if PutSell then GlobalColor("SellPosition") else if PutBuy then GlobalColor("OpenShortPosition") else if PutHold then GlobalColor("HoldShortPosition") else if EndDay then GlobalColor("SellPosition") else if GetReadyBull then GlobalColor("GetReady") else if GetReadyBear then GlobalColor("GetReady") else GlobalColor("CriteriaNotMet"));
AddLabel(yes, " ", color.black);
AddLabel(yes, "PREMKT", if PreMarketBull then GlobalColor("OpenLongPosition") else if PreMarketBear then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "CLOUDS", if BothEMASBullish then GlobalColor("OpenLongPosition") else if BothEMASBearish then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "EMA SEP", if FastEMAPctBullish then GlobalColor("OpenLongPosition") else if FastEMAPctBearish then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "TSI", if TSIBull then GlobalColor("OpenLongPosition") else if TSIBear then GlobalColor("OpenShortPosition") else color.gray);
AddLabel(yes, "MACD", if MACDBull then GlobalColor("OpenLongPosition") else if MACDBear then GlobalColor("OpenShortPosition") else color.gray);
input ShowBuySellBubbles = Yes;
input ShowTkPftBubble = Yes;
def CallBuyBub = TradingDay and !CallSell and (CallBuy[1] or CallBuy[2] or CallBuy[3]) and low <= FastEMA1 and low >= FastEMA2;
addchartbubble(ShowBuySellBubbles and CallBuybub, low * 0.999, "Long", GlobalColor("OpenLongPosition"), no);
def PutBuyBub = TradingDay and !PutSell and (PutBuy[1] or PutBuy[2] or PutBuy[3]) and high >= FastEMA1 and high <=FastEMA2;
addchartbubble(ShowBuySellBubbles and PutBuyBub, high * 1.001, "Short", GlobalColor("OpenShortPosition"), yes);
def CallSellBub = TradingDayExt and (CallBuy[1] or CallHold[1]) and CallSell;
addchartbubble(ShowBuySellBubbles and CallSellBub, low * 0.999, "Sell", GlobalColor("SellPosition"), no);
def PutSellBub = TradingDayExt and (PutBuy[1] or PutHold[1]) and PutSell;
addchartbubble(ShowBuySellBubbles and PutSellBub, high * 1.001, "Sell", GlobalColor("SellPosition"), yes);
def TkPftBubCall = TradingDayExt and (CallBuyInd or CallHoldInd) and high > FastEMA1 * 1.01;
addchartbubble(ShowBuySellBubbles and ShowTkPftBubble and TkPftBubCall, high * 1.001, "Tk Pft", GlobalColor("TakeProfit"), yes);
def TkPftBubPut = TradingDayExt and (PutBuyInd or PutHoldInd) and low < FastEMA1 * 0.99;
addchartbubble(ShowBuySellBubbles and ShowTkPftBubble and TkPftBubPut, low * 0.999, "Tk Pft", GlobalColor("TakeProfit"), no);
input ShowOrders = no;
AddOrder(OrderType.BUY_TO_OPEN, ShowOrders and (CallBuy[1] or CallBuy[2])or CallBuy[3] and low <= FastEMA1 and low >= FastEMA2, FastEMA1, 100, Color.GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_OPEN, ShowOrders and (PutBuy[1] or PutBuy[2] or PutBuy[3]) and high >= FastEMA1 and high <=FastEMA2, FastEMA1, 100, Color.GREEN, Color.LIGHT_GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and TkPftBubCall, high * 0.999, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.SELL_TO_CLOSE, ShowOrders and CallSellBub or EndDay[-1], close, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and TkPftBubPut, low * 1.001, 100, Color.RED, Color.LIGHT_RED);
AddOrder(OrderType.BUY_TO_CLOSE, ShowOrders and PutSellBub or EndDay[-1], close, 100, Color.RED, Color.LIGHT_RED);
input AlertOn = yes;
Alert(AlertOn and (CallSellBub or PutSellBub or TkPftBubCall or TkPftBubPut), "SELL", Alert.BAR, Sound.Bell);
Alert(AlertOn and (CallBuyBub or PutBuyBub), "BUY", Alert.BAR, Sound.Chimes);
input ShowPLLabel = Yes;
AddLabel(if ShowPLLabel then yes else no, " ", color.black);
def CallOpen = if CallBuy and !CallHold then FastEMA1 else CallOpen[1];
def CallEnd = if TkPftBubCall then high * 0.999 else if CallSellBub then close else CallEnd[1];
def CallOpenCost = CallOpen * 100;
def CallCloseCost = CallEnd * 100;
def CallDiff = round(CallCloseCost - CallOpenCost, 2);
AddLabel(if ShowPLLabel then yes else no, "Long Trade P/L - $ " + CallDiff + " ", if CallDiff > 0 then color.light_green else if CallDiff < 0 then color.light_red else color.gray);
def PutOpen = if PutBuy and !PutHold then FastEMA1 else PutOpen[1];
def PutEnd = if TkPftBubPut then low * 1.001 else if PutSellBub then close else PutEnd[1];
def PutOpenCost = PutOpen * 100;
def PutCloseCost = PutEnd * 100;
def PutDiff = round(PutOpenCost - PutCloseCost, 2);
AddLabel(if ShowPLLabel then yes else no, "Short Trade P/L - $ " + PutDiff + " ", if PutDiff > 0 then color.light_green else if PutDiff < 0 then color.light_red else color.gray);
input ShowColorDefLabels = yes;
AddLabel(if ShowColorDefLabels == 1 then yes else no, " ", color.black);
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Long Ind", GlobalColor("OpenLongPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Long Hld", GlobalColor("HoldLongPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Short Ind", GlobalColor("OpenShortPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Short Hld", GlobalColor("HoldShortPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Get Ready", GlobalColor("GetReady"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Sell", GlobalColor("SellPosition"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "Tk Pft", GlobalColor("TakeProfit"));
AddLabel(if ShowColorDefLabels == 1 then yes else no, "No Criteria", GlobalColor("CriteriaNotMet"));
input ShowFlexGridBackgroundColor = no;
AssignBackgroundColor(if ShowFlexGridBackgroundColor and (CallSellBub or PutSellBub) then GlobalColor("SellPosition") else if CallBuyBub then GlobalColor("OpenLongPosition") else if PutBuyBub then GlobalColor("OpenShortPosition") else if ShowFlexGridBackgroundColor and TradingDayExt and (CallBuyInd or CallHoldInd) and (TkPftBubCall or TkPftBubPut) then GlobalColor("TakeProfit") else color.current);
def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
input fplBegin = 0000;
input fplTargetWinLoss = .50;
input fplTargetWinRate = 1;
input fplHidePrice = no;
input fplHideFPL = yes;
input fplShowEntryBubbles = no;
input fplEnableDebugging = no;
def begin = fplBegin;
def targetWinLoss = fplTargetWinLoss;
def targetWinRate = fplTargetWinRate;
def hidePrice = fplHidePrice;
def hideFPL = fplHideFPL;
def showEntryBubbles = fplShowEntryBubbles;
def enableDebugging = fplEnableDebugging;
HidePricePlot(hidePrice);
plot FPL = FPL();
FPL.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
FPL.DefineColor("Positive and Up", Color.GREEN);
FPL.DefineColor("Positive and Down", Color.DARK_GREEN);
FPL.DefineColor("Negative and Down", Color.RED);
FPL.DefineColor("Negative and Up", Color.DARK_RED);
FPL.AssignValueColor(if FPL >= 0
then if FPL > FPL[1]
then FPL.Color("Positive and Up")
else FPL.Color("Positive and Down")
else if FPL < FPL[1]
then FPL.Color("Negative and Down")
else FPL.Color("Negative and Up"));
FPL.SetHiding(hideFPL);
plot ZeroLine1 = if IsNaN(close)
then nan
else 0;
ZeroLine1.SetDefaultColor(Color.GRAY);
ZeroLine1.SetHiding(hideFPL);
script calculateDrawdown {
input plLow = 1;
input plHigh = 1;
def _drawdown = if plHigh == 0
then 0
else (plLow - plHigh) / plHigh;
plot calculateDrawdown = _drawdown;
}
script incrementValue {
input condition = yes;
input increment = 1;
input startingValue = 0;
def _value = CompoundValue(1,
if condition
then _value[1] + increment
else _value[1], startingValue);
plot incrementValue = _value;
}
;
script getDurationInMins {
input gdimBarCount = 1;
def aggPeriod = GetAggregationPeriod();
def _getDurationInMins = Floor((gdimBarCount * aggPeriod) / 60000);
plot getDurationInMins = _getDurationInMins;
}
script formatDuration {
input fdBarCount = 1;
def _fdDuration = getDurationInMins(fdBarCount);
def _formatDuration = if _fdDuration >= 60 and _fdDuration < 1440
then 1
else if _fdDuration >= 1440
then 2
else 0;
plot formatDuration = _formatDuration;
}
script calculateDuration {
input cdBarCount = 1;
def _cdDurationFormat = formatDuration(cdBarCount);
def _cdDuration = getDurationInMins(cdBarCount);
def _calculateDuration = if _cdDurationFormat == 1
then _cdDuration / 60
else if _cdDurationFormat == 2
then Ceil(_cdDuration / 1440)
else _cdDuration;
plot calculateDuration = _calculateDuration;
}
def entry = round (EntryPrice(),2);
def entryPrice = if !IsNaN(entry)
then entry
else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];
def Active = if SecondsTillTime(begin) == 0 and
SecondsFromTime(begin) == 0
then 1
else 0;
def highFPL = HighestAll(FPL);
def lowFPL = LowestAll(FPL);
def fplreturn = (FPL - FPL[1]) / FPL[1];
def cumsum = Sum(fplreturn);
def highBarNumber = CompoundValue(1, if FPL == highFPL
then bn
else highBarNumber[1], 0);
def lowBarNumber = CompoundValue(1, if FPL == lowFPL
then bn
else lowBarNumber[1], 0);
def entryBarsTemp = if hasEntry
then bn
else nan;
def entryBarNum = if hasEntry and isNewEntry
then bn
else entryBarNum[1];
def isEntryBar = entryBarNum != entryBarNum[1];
def entryBarPL = if isEntryBar
then FPL
else entryBarPL[1];
def exitBarsTemp = if !hasEntry
and bn > entryBarsTemp[1]
then bn
else nan;
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1])
then bn
else exitBarNum[1];
def isExitBar = exitBarNum != exitBarNum[1];
def exitBarPL = if isExitBar
then FPL
else exitBarPL[1];
def entryReturn = if isExitBar then exitBarPL - exitBarPL[1] else entryReturn[1];
def isWin = if isExitBar and entryReturn >= 0 then 1 else 0;
def isLoss = if isExitBar and entryReturn < 0 then 1 else 0;
def entryReturnWin = if isWin then entryReturn else entryReturnWin[1];
def entryReturnLoss = if isLoss then entryReturn else entryReturnLoss[1];
def entryFPLWins = if isWin then entryReturn else 0;
def entryFPLLosses = if isLoss then entryReturn else 0;
def entryFPLAll = if isLoss or isWin then entryReturn else 0;
def entryCount = incrementValue(entryFPLAll);
def winCount = incrementValue(isWin);
def lossCount = incrementValue(isLoss);
def highestReturn = if entryReturnWin[1] > highestReturn[1]
then entryReturnWin[1]
else highestReturn[1];
def lowestReturn = if entryReturnLoss[1] < lowestReturn[1]
then entryReturnLoss[1]
else lowestReturn[1];
def winRate = round ( winCount / lossCount,2);
def winLossRatio = winCount / entryCount;
def avgReturn = TotalSum(entryFPLAll) / entryCount;
def avgWin = TotalSum(entryFPLWins) / winCount;
def avgLoss = TotalSum(entryFPLLosses) / lossCount;
def lowestLowBarNumber = HighestAll(if FPL == lowFPL then bn else 0);
def highestHighBarNumber = HighestAll(if FPL == highFPL and FPL != FPL[1] then bn else 0);
def hasPrevLow = lowestLowBarNumber < highestHighBarNumber;
def isPeak = FPL > Highest(FPL[1], 12) and FPL > Highest(FPL[-12], 12);
def isTrough = FPL < Lowest(FPL[1], 12) and FPL < Lowest(FPL[-12], 12);
def _peak = if isPeak then FPL else nan;
def _trough = if isTrough then FPL else nan;
def peak = if !IsNaN(_peak) then FPL else peak[1];
def trough = if !IsNaN(_trough) then FPL else trough[1];
def peakBN = if isPeak then bn else peakBN[1];
def troughBN = if isTrough then bn else troughBN[1];
def ptvDrawdown = if !hasPrevLow then calculateDrawdown(lowFPL, highFPL) else ptvDrawdown[1];
def equityDrawdown = if isTrough and trough < peak then trough - peak else equityDrawdown[1];
def equityDrawdownPercent = if isTrough and trough < peak then calculateDrawdown(trough, peak) else equityDrawdownPercent[1];
def largestEquityDrawdown = LowestAll(equityDrawdown);
def largestEquityDrawdownPercent = LowestAll(equityDrawdownPercent);
def drawdown = if hasPrevLow
then largestEquityDrawdownPercent
else ptvDrawdown;
def equityDrawdownLength = if bn >= peakBN and bn <= troughBN
then troughBN - peakBN
else equityDrawdownLength[1];
def ptvDrawdownLength = if bn >= highestHighBarNumber and bn <= lowestLowBarNumber
then lowestLowBarNumber - highestHighBarNumber
else ptvDrawdownLength[1];
def equityDrawdownDuration = calculateDuration(HighestAll(equityDrawdownLength));
def equityDrawdownDurationFormat = formatDuration(HighestAll(equityDrawdownLength));
def ptvDrawdownDuration = calculateDuration(ptvDrawdownLength);
def ptvDrawdownDurationFormat = formatDuration(ptvDrawdownLength);
def Midnight = if Active then FPL else Midnight[1];
def DaysProfit = Round(FPL - Midnight,2);
AddChartBubble(!hideFPL and showEntryBubbles and isEntryBar, FPL, "Entry: " + entryBarPL + " | " + bn, Color.WHITE);
AddChartBubble(!hideFPL and showEntryBubbles and isExitBar, FPL, "Exit: " + exitBarPL,
color = if isWin
then Color.LIGHT_GREEN
else if isLoss
then Color.DARK_RED
else Color.GRAY,
up = no
);
def tmpMaxLossCount = if isLoss then tmpMaxLossCount[1] + 1 else 0;
def maxLossCount = if iswin then 0 else if tmpMaxLossCount > maxLossCount[1] then tmpMaxLossCount else MaxLossCount[1];
AddLabel(yes,
text = "MaxLossCount " + maxLossCount,
color.ORANGE
);
AddLabel(yes,
text = "LastEntry: " + AsPrice(entryPrice)
);
AddLabel(hasEntry,
text = "Current Trade % Return: " + AsPercent(cumsum),
color = if cumsum > 0
then Color.GREEN
else Color.RED
);
AddLabel(yes,
text = "Total Trades: " + entryCount,
color = Color.WHITE
);
AddLabel(yes,
text = "WinCount: " + winCount +
" | LossCount: " + lossCount +
" | WinRate: " + winRate,
color = if winRate >= targetWinRate
then Color.GREEN
else Color.RED
);
AddLabel(yes,
text = "W/L: " + AsPercent(winLossRatio),
color = if winLossRatio > targetWinLoss
then Color.GREEN
else Color.RED
);
AddLabel(yes,
text = "HighestReturn: " + AsDollars(highestReturn),
color = if highestReturn > 0
then Color.GREEN
else Color.RED
);
AddLabel(yes,
text = "LowestReturn: " + AsDollars(lowestReturn),
color = if lowestReturn > 0
then Color.GREEN
else Color.RED
);
AddLabel(yes,
text = "AvgReturn: " + AsDollars(avgReturn) +
" | AvgWin: " + AsDollars(avgWin) +
" | AvgLoss: " + AsDollars(avgLoss),
color = if avgReturn >= 0
then Color.LIGHT_GREEN
else Color.RED
);
AddLabel(yes,
text = "PeakToValley DD: " + AsPercent(drawdown) +
" | Duration: " + ptvDrawdownDuration +
if ptvDrawdownDurationFormat == 1
then " hours"
else if ptvDrawdownDurationFormat == 2
then " days"
else " mins" ,
color = if drawdown > 0
then Color.GREEN
else Color.RED
);
AddLabel(largestEquityDrawdown < 0,
text = "Largest Equity DD: " + AsDollars(largestEquityDrawdown) +
" | Duration: " + equityDrawdownDuration +
if equityDrawdownDurationFormat == 1
then " hours"
else if equityDrawdownDurationFormat == 2
then " days"
else " mins",
color = if largestEquityDrawdown > 0
then Color.GREEN
else Color.Dark_ORANGE
);
AddLabel(yes,
text = "P&L High" +
(if enableDebugging
then " at bar " + highBarNumber
else "") +
": " + AsDollars(highFPL),
color = Color.GREEN
);
AddLabel(yes,
text = "P&L Low" +
(if enableDebugging
then " at bar " + lowBarNumber
else "") +
": " + AsDollars(lowFPL),
color = Color.RED
);
AddLabel(yes,
text = "Days Profit: $" + DaysProfit,
color = if DaysProfit > 0
then Color.GREEN
else Color.RED
);
AddLabel(yes,
text = "Total Profit: " + AsDollars(FPL),
color = if FPL > 0
then Color.GREEN
else Color.RED
);
AddChartBubble(enableDebugging and isPeak, FPL,
text = "FPL: " + FPL
+ " | Peak: " + peak
+ " | Trough: " + trough[-1]
+ " | Drawdown: " + AsPercent(calculateDrawdown(trough, peak))
+ " | PeakBN: " + peakBN
+ " | BarNumber: " + bn,
color = Color.LIME
);
AddChartBubble(enableDebugging and isTrough, FPL,
text = "FPL: " + FPL
+ " | Peak: " + peak
+ " | Trough: " + trough
+ " | Drawdown: " + AsPercent(calculateDrawdown(trough, peak))
+ " | TroughBN: " + troughBN
+ " | BarNumber: " + bn,
color = Color.LIGHT_RED,
up = no
);
AddVerticalLine(enableDebugging and isEntryBar,
text = "EntryBarNum: " + entryBarNum
+ " | ExitBarNum: " + exitBarNum[-1]
+ " | BarNumber: " + bn,
color = Color.WHITE
);
AddVerticalLine(enableDebugging and isExitBar,
text = "EntryBarNum: " + entryBarNum[1]
+ " | ExitbarNum: " + exitBarNum
+ " | BarNumber: " + bn
+ " | EntryReturn: " + entryReturn,
color = if isWin
then Color.LIGHT_GREEN
else if isLoss
then Color.LIGHT_RED
else Color.LIGHT_GREEN
);