Backtesting Script Success Rate

G

Gildes

New member
VIP
Hi guys,

I need your help, I am looking for a base script I could use to test any strategy based on my criteria for Long or short who could
label the Success Rate.

Thank you
 
Last edited by a moderator:
DeftMove

DeftMove

New member
I have something for you, it does require some tweaking though. It also has been the best workaround since TOS does not give WR, only P/L.

The idea is to isolate winning trades as a gain of 1 and losing trades as a loss of 1, regardless of whether it was a big or small loss.

https://tos.mx/SWDW3kQ
Code:
##Template for counting strategies
#Updated: 4.25.2020 by DeftMove - gkc
#Negative and positive winrate represented by DOLLAR AMOUNT P/L in report above/below zero

#PLACE STUDIES HERE


input tradeLot = 1; #hint tradeLot: Number of shares per order.
input analyzeWR = yes; #hint analyzeWR = if YES, dollar amounts P/L in report above/below zero are considered win/loss discrepancy. if NO, dollar amounts P/L in report are considered P/L
input runBuyAnalysis = yes; #hint runBuyAnalysis: if YES, run upward plays, if NO, prevent upward plays
def entryConditionBuy = ; #edit for each strategy, UP ENTRY CONDITIONS
def exitConditionBuy  = ; #edit for each strategy, UP EXIT  CONDITIONS
def exitGoodBuy = open[-1] > EntryPrice();
def exitBadBuy  = open[-1] < EntryPrice();

AddOrder(OrderType.BUY_TO_OPEN, condition = entryConditionBuy and runBuyAnalysis, price = open[-1], tradeLot, name = "BuyOpen " + open[-1]);
AddOrder(OrderType.SELL_TO_CLOSE, runBuyAnalysis and exitConditionBuy and exitGoodBuy, price = if analyzeWR then EntryPrice() + 1 else open[-1], tradeLot, name = "SellClose " + open[-1] + "P/L: " +  (open[-1] - EntryPrice()));
AddOrder(OrderType.SELL_TO_CLOSE, runBuyAnalysis and exitConditionBuy and exitBadBuy, price = if analyzeWR then EntryPrice() - 1 else open[-1], tradeLot, name = "SellClose " + open[-1] + "P/L: " +  (open[-1] - EntryPrice()));

input runSellAnalysis = yes; #hint runSellAnalysis: if YES, run downward plays, if NO, prevent downward plays
def entryConditionSell = ; #edit for each strategy, DOWN ENTRY CONDITIONS
def exitConditionSell = ; #edit for each strategy, DOWN EXIT  CONDITIONS
def exitGoodSell = open[-1] < EntryPrice();
def exitBadSell  = open[-1] > EntryPrice();

AddOrder(OrderType.SELL_TO_OPEN, condition = entryConditionSell and runSellAnalysis, price = open[-1], tradeLot, name = "SellOpen " + open[-1]);
AddOrder(OrderType.BUY_TO_CLOSE, runSellAnalysis and exitConditionSell and exitGoodSell, price = if analyzeWR then EntryPrice() - 1 else open[-1], tradeLot, name = "BuyClose " + open[-1] + "P/L: " + (EntryPrice() - open[-1]));
AddOrder(OrderType.BUY_TO_CLOSE, runSellAnalysis and exitConditionSell and exitBadSell, price = if analyzeWR then EntryPrice() + 1 else open[-1], tradeLot, name = "BuyClose " + open[-1] + "P/L: " + (EntryPrice() - open[-1]));
Your strategy/studies need to generate entry and exit conditions for upward and downward trades for this to work. Place them in the conditions indicated.

Once you click the reports, the default settings will give you a P/L in dollar amounts. Positive amounts mean your strategy has a >50% win rate, negative means it is <50% WR.

Calculate WR by this equation:
[ (#TotalTrades - P/L) / 2 + P/L ] /#TotalTrades
There are toggles for upward or downward trades as well as to switch off WR and show P/L instead. Lastly, if you want more lots traded per order, there is an input for that as well.

Also, please let me know if there could be any improvements. I just started working on this a two days ago after I learned that strategies exist and I doubt my code is streamlined (not a code writer IRL)
 

Similar threads

Top