Entry & Exit Analysis

Hi Everyone -

I am on a search to optimize my strategy, specifically to look back at a strategy and identify entry bar stats to refine entries.

i.e. if LongEntry fires, I want to see what the distance is from the SMA50 and I want to see what the average distance from SMA50 is for all LongEntry bars.

Is this possible to store this bar data and average it with the next LongEntry data?
 
Solution
Just set your LongEntry condition to whatever you want. For the following code I set the LongEntry condition to be when the 3 period SMA crosses above the 5 period SMA

Code:
# Define input parameters
input length = 50;
input lengthShort = 3;
input lengthMid = 5;


# Calculate SMAs
def SMA50 = Average(close, length);
def SMA3 = Average(close, lengthShort);
def SMA5 = Average(close, lengthMid);

# Define LongEntry condition
def LongEntry = SMA3 crosses above SMA5;

# Calculate distance from SMA50 at each LongEntry
def distance = if LongEntry then close - SMA50 else Double.NaN;

# Maintain a running total of distances and number of LongEntries
def totalDistance = CompoundValue(1, if LongEntry then totalDistance[1] + distance else...
Just set your LongEntry condition to whatever you want. For the following code I set the LongEntry condition to be when the 3 period SMA crosses above the 5 period SMA

Code:
# Define input parameters
input length = 50;
input lengthShort = 3;
input lengthMid = 5;


# Calculate SMAs
def SMA50 = Average(close, length);
def SMA3 = Average(close, lengthShort);
def SMA5 = Average(close, lengthMid);

# Define LongEntry condition
def LongEntry = SMA3 crosses above SMA5;

# Calculate distance from SMA50 at each LongEntry
def distance = if LongEntry then close - SMA50 else Double.NaN;

# Maintain a running total of distances and number of LongEntries
def totalDistance = CompoundValue(1, if LongEntry then totalDistance[1] + distance else totalDistance[1], 0);
def numEntries = CompoundValue(1, if LongEntry then numEntries[1] + 1 else numEntries[1], 0);

# Calculate average distance
def avgDistance = totalDistance / numEntries;

# Add a bubble at each LongEntry
AddChartBubble(LongEntry, low, "Avg Distance: " + avgDistance + "\nDistance: " +distance, color.green);
 
Solution
@justAnotherTrader thank you - what if I am looking to separate the averages for winning trades vs losing trades? i.e. avg distance from SMA50 for wins, avg distance from SMA50 for losses
It is possible, depending on how you consider a win is up to you. The following code defines a win as the next close being higher than the close at the long entry.

Interesting case study though, I will probably play around with it on my end to see how it works with other variables as well. Good luck

Code:
# Define input parameters
input length = 50;
input lengthShort = 3;
input lengthMid = 5;

# Calculate SMAs
def SMA50 = Average(close, length);
def SMA3 = Average(close, lengthShort);
def SMA5 = Average(close, lengthMid);

# Define LongEntry condition
def LongEntry = SMA3 crosses above SMA5;

# Calculate distance from SMA50 at each LongEntry
def distance = if LongEntry then close - SMA50 else Double.NaN;

# Define winning and losing trades
def WinTrade = close > close[1];
def LossTrade = close < close[1];

# Maintain a running total of distances and number of winning and losing trades
def totalDistanceWin = CompoundValue(1, if LongEntry and WinTrade then totalDistanceWin[1] + distance else totalDistanceWin[1], 0);
def numEntriesWin = CompoundValue(1, if LongEntry and WinTrade then numEntriesWin[1] + 1 else numEntriesWin[1], 0);

def totalDistanceLoss = CompoundValue(1, if LongEntry and LossTrade then totalDistanceLoss[1] + distance else totalDistanceLoss[1], 0);
def numEntriesLoss = CompoundValue(1, if LongEntry and LossTrade then numEntriesLoss[1] + 1 else numEntriesLoss[1], 0);

# Calculate average distance for winning and losing trades
def avgDistanceWin = totalDistanceWin / numEntriesWin;
def avgDistanceLoss = totalDistanceLoss / numEntriesLoss;

# Add a bubble at each LongEntry
AddChartBubble(LongEntry, low, "Avg Distance Win: " + avgDistanceWin + "\nAvg Distance Loss: " + avgDistanceLoss + "\nDistance: " +distance, color.green);
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
373 Online
Create Post

Similar threads

Similar threads

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