# Entry & Exit Analysis

#### maverickmitch

##### Member
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);``````

@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

@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);``````

@justAnotherTrader Got it - this makes sense. Is there a way to compound the value of the function LongExit and match it to the prior LongEntry, and then if that LongEntry > the next LongExit, it would be classified as a winning trade?

87k+ Posts
373 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?