Page 1 of 1

Issue with the backtester Matthew created

Posted: Sat Oct 09, 2021 4:07 pm
by Chuti
Hi guys, hope you are doing great :cool:

Some days ago I found out Matthew uploaded a video that explained how to create a backtester. I added it to my script and it seemed to be working fine, the problem is I encountered an error with the returns. The strategy is supposed to TP at 1% move in his favor and SL if price goes 8% against (yeah the R:R is awful but its done like that for a reason and it works). It called my attention that with an 89.33% WR + $1 Starting balance, I had $67.93 after 75 trades. Thats why I calculated using open interest the result of having 75 trades, having a 100% WR and making 1% per trade. Well that gave $2.1 with a starting balance of $1, in other words a x2.1. The conclusion is that obviously there is something wrong with the code. Let me clarify something before you insult me lol. By the code I mean my code, since even though I copied the backtester section of the indicator Matthew shared, I slightly modified it. I'll leave the modification I did below.

Code: Select all

tpPoints = input(title = "Target Profit (%)", step = 0.1, group = "User Settings", minval = 0.0, defval = 1)
slPoints = input(title = "Stop Loss (%)", step = 0.1, group = "User Settings", minval = 0.0, defval = 8.5)
percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)

//
var t_entry = 0.0
var t_stop = slPoints
var t_target = tpPoints
var t_direction = 0

// Exit Trade Settings
strategy.exit(id = "Long Exit", from_entry="Long", profit = percentAsPoints(tpPoints), loss = percentAsPoints(slPoints))
strategy.exit(id = "Short Exit", from_entry="Short", profit = percentAsPoints(tpPoints), loss = percentAsPoints(slPoints))
---

Code: Select all

var bgcolor = color.new(color.gray, 0)
if draw_backtester
    if barstate.islastconfirmedhistory
        dollarReturn = balance - start_balance
        f_fillCell(testTable, 0, 0, "Total Trades:", tostring(strategy.closedtrades), bgcolor, color.white)
        f_fillCell(testTable, 0, 1, "Win Rate:", tostring(truncate((strategy.wintrades/strategy.closedtrades)*100,2)) + "%", bgcolor, color.white)
        f_fillCell(testTable, 1, 0, "Starting:", "$" + tostring(start_balance), bgcolor, color.white)
        f_fillCell(testTable, 1, 1, "Ending:", "$" + tostring(truncate(balance,2)), bgcolor, color.white)
        f_fillCell(testTable, 2, 0, "Return:", "$" + tostring(truncate(dollarReturn,2)), dollarReturn > 0 ? color.green : color.red, color.white)
        f_fillCell(testTable, 3, 0, "Return:", (dollarReturn > 0 ? "+" : "") + tostring(truncate((dollarReturn / start_balance)*100,2)) + "%", dollarReturn > 0 ? color.green : color.red, color.white)
        f_fillCell(testTable, 3, 1, "Max DD:", tostring(truncate(maxDrawdown*100,2)) + "%", color.red, color.white)
Help is much appreciated :happy1: