Calculating Percentage of Trades Closing Within the Same Bar

05 Apr 2024
0 comment
0 attachment

This code snippet is designed to help visualize and quickly identify the percentage of trades that open and close within the same trading bar. This can be particularly useful for traders looking to refine their strategies, especially when considering the adjustment of exit orders.


DEFPARAM CUMULATEORDERS = false // simple buy/short strategy
MM1 = ExponentialAverage[20](close) // Long
IF MM1 crosses over MM1[1] THEN
    BUY 1 LOT AT MARKET
ENDIF
// Short
IF MM1 crosses under MM1[1] THEN
    SELLSHORT 1 LOT AT MARKET
ENDIF
////// closed SL & TP to find easily right cases
set stop ploss 5
set target pprofit 5
///////////////////
// Snippet if longonmarket[2]=0 and longonmarket[1]>0 and longonmarket=0 then
    Long1=Long1+1
elsif shortonmarket[2]=0 and shortonmarket[1]>0 and shortonmarket=0 then
    Short1=Short1+1
endif
// Graph variables
// Long1 = count #longs open/close in the same candle
// Short1 = count #longs open/close in the same candle
//graph Long1 as "Long1"
//graph -Short1 as "Short1"
// if (onmarket and barindex=tradeindex) or(barindex=tradeindex and barindex=tradeindex(2)) then
    TotalT=TotalT+1
endif
// due to the lag of 1 bar calculation for Long1 & Short1, results have to been considered 1 bar after close, best to see final result
graph 100*(Long1+Short1)/TotalT

Explanation of the Code:

  • Initialization: The code starts by setting DEFPARAM CUMULATEORDERS to false, ensuring that orders do not accumulate over multiple bars.
  • Strategy Definition: A simple moving average crossover strategy is defined using an exponential moving average (EMA) of the close prices over 20 periods. Trades are initiated based on the direction of the crossover.
  • Stop Loss and Take Profit: Both stop loss and take profit are set at 5 points.
  • Trade Count Logic: The snippet checks if a long or short position was opened and closed within the same bar. This is determined by checking the status of longonmarket and shortonmarket over three consecutive bars.
  • Graph Variables: Two variables, Long1 and Short1, count the number of long and short trades that open and close within the same bar, respectively.
  • Total Trades: TotalT counts the total number of trades considered for the calculation.
  • Result Graph: Finally, the percentage of trades that open and close within the same bar is graphed as a percentage of total trades considered.

This snippet is useful for traders who want to analyze the frequency of quick trades and possibly adjust their strategy based on these insights.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/identify-the-of-trades-which-open-and-close-in-the-same-bar/#post-72577

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Pepsmile Veteran
Code artist, my biography is a blank page waiting to be scripted. Imagine a bio so awesome it hasn't been coded yet.
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...