Market Sentiment Strategy For ThinkOrSwim

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.

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

  • META 5Min.jpg
    META 5Min.jpg
    219.8 KB · Views: 285
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
466 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top