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