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:
DEFPARAM CUMULATEORDERS to false, ensuring that orders do not accumulate over multiple bars.longonmarket and shortonmarket over three consecutive bars.Long1 and Short1, count the number of long and short trades that open and close within the same bar, respectively.TotalT counts the total number of trades considered for the calculation.This snippet is useful for traders who want to analyze the frequency of quick trades and possibly adjust their strategy based on these insights.