Hi coders
I’ve been trying to use the graph instruction to display number of trades as in the statistic of a backtest. But when accumulating orders it not straight forward with a counter.
Does anyone have an idea on how to graph this?
cheers Kasper
An idea would be to fetch through all tradeindex within a loop to count them all.
Hi all,
was looking at this problem myself, and have come up with the following code. In a cumulating trades setting, it counts number of total trades, and number of long/number of short.
A word of caution though – it slips up on same bar entry/exits – it won’t count a trade if it opens and closes on the same bar, or if a trade netts off to no position in the same bar – an example picture is attached. This same bar close count problem might be overcome with an additional condition?
Graph tradenum, longcount and shortcount to see results.
If tradenum = 0 and not onmarket then
tradenum = 0
endif
If abs(countofposition-countofposition[1]) and tradenum=0 then
tradenum = 1
if shortonmarket then
shortcount = 1
else
if longonmarket then
longcount = 1
endif
endif
endif
if longonmarket[1] and shortonmarket then
tradenum = tradenum[1]+1
shortcount = shortcount[1]+1
endif
if shortonmarket[1] and longonmarket then
tradenum = tradenum[1]+1
longcount = longcount[1]+1
endif
if longonmarket[1] and longonmarket then
tradenum = tradenum[1]
longcount = longcount[1]
endif
if shortonmarket[1] and shortonmarket then
tradenum = tradenum[1]
shortcount = shortcount[1]
endif
If onmarket[1] and not onmarket then
tradenum = tradenum[1]
longcount = longcount[1]
shortcount = shortcount[1]
endif
If not onmarket[1] and longonmarket then
tradenum = tradenum[1]+1
longcount = longcount[1]+1
endif
If not onmarket[1] and shortonmarket then
tradenum = tradenum[1]+1
shortcount = shortcount[1]+1
endif
Regards,
Finning
You could do something like this to record mid candle trades. It does not allow for spread so you might want to factor that in in some way when comparing to the high and low of a candle.
This is the long only version. Not tested and written during first cup of coffee of the day!
if longlimitorderon and low < x then
longcount = longcount + 1
endif
longlimitorderon = 0
if longstoporderon and high > x then
longcount = longcount + 1
endif
longstoporderon = 0
//
//Your conditions and calculation of x price in here
//
//Limit order
if (your long entry conditions at a LOWER price) then
buy 1 contract at x limit
longlimitorderon = 1
endif
//Stop order
if (your long entry conditions at a HIGHER price) then
buy 1 contract at x stop
longstoporderon = 1
endif
//Market order
if (your long entry conditions at MARKET price) then
buy 1 contract at market
longcount = longcount + 1
endif
graph longcount