dap711
Active member
Market Sentiment Strategy (Optimized for META 5Min chart)
Merry Christmas everyone! .. Something I was playing around with and decided to give it as a Christmas present.
Merry Christmas everyone! .. Something I was playing around with and decided to give it as a Christmas present.
C++:
#dap711
#floating P/L by MerryDay
input ShowStategyPositions = yes;
input ShowFloating_pl_Labels = yes;
input MA_length = 200;
input MA_price = close;
input MA_averageType = AverageType.WILDERS;
input MA_Show = yes;
input MS_Oversold = 46;
input MS_Overbought = 71;
input MS_Displace = 1;
input MS_SmoothLength = 5;
plot MA = MovingAverage(MA_averageType, MA_price, MA_length);
MA.SetHiding(!MA_Show);
def temp = MarketSentiment();
def MS = EhlersSuperSmootherFilter(temp, MS_SmoothLength);
def BuyOpen = MS > MS[MS_Displace] and MS > MS_Oversold and close > MA;
def BuyClose = MS < MS[MS_Displace] or close < MA;
AddOrder(OrderType.BUY_TO_OPEN, BuyOpen and ShowStategyPositions, open[-1], 1, Color.CYAN, Color.CYAN, "EA");
AddOrder(OrderType.SELL_TO_CLOSE, BuyClose and ShowStategyPositions, open[-1], 1, Color.CYAN, Color.CYAN, "EA");
def SellOpen = MS < MS[MS_Displace] and MS < MS_Overbought and close < MA;
def SellClose = MS > MS[MS_Displace] or close > MA;
AddOrder(OrderType.SELL_TO_OPEN, SellOpen and ShowStategyPositions, open[-1], 1, Color.RED, Color.RED,"EA");
AddOrder(OrderType.BUY_TO_CLOSE, SellClose and ShowStategyPositions, open[-1], 1, Color.RED, Color.RED,"EA");
#//FloatingProfitLoss Labels
#####################################################################################################
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;
}
def fplTargetWinLoss = .50;
def fplTargetWinRate = 1;
input HidePrices = no;
#Hint HidePrices: Useful to see posistions without the candles on the chart. Must have "show strategy positions" set to yes.
def fplHideFPL = yes;
def targetWinLoss = fplTargetWinLoss;
def targetWinRate = fplTargetWinRate;
def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
HidePricePlot(hidePrices);
def FPL = FPL();
# Entry Calculations. Note: Only parses on a Strategy Chart
def entry = EntryPrice();
def entryPrice = if !IsNaN(entry) then entry else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];
#is active trade
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);
#Win/Loss ratios
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;
#Counts
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 = if winCount == 0 and lossCount == 0 then 0 else if lossCount == 0 then 1 else winCount / lossCount;
def winLossRatio = winCount / entryCount;
def avgReturn = TotalSum(entryFPLAll) / entryCount;
def avgWin = TotalSum(entryFPLWins) / winCount;
def avgLoss = TotalSum(entryFPLLosses) / lossCount;
#Floating P/L labels
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"Total Trades: " + entryCount + " ", Color.Cyan);
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"WinCount: " + winCount + " | LossCount: " + lossCount , color = if winRate >= targetWinRate then Color.Green else Color.Red);
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"W/L: " + AsPercent(winLossRatio) + " ", color = if winLossRatio > targetWinLoss then Color.Green else Color.Red);
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"AvgWin: "+AsDollars(avgWin)+" | AvgLoss: "+AsDollars(avgLoss),color = if TotalSum(entryFPLAll) >= 0 then color.Green else Color.Red);
AddLabel(ShowFloating_pl_Labels and ShowStategyPositions,"Total Profit: " + AsDollars(TotalSum(entryFPLAll)), color = if TotalSum(entryFPLAll) > 0 then Color.Green else Color.Red);
Attachments
Last edited by a moderator: