Hello everyone,
This afternoon I’ve been trying to calculate the win percentage of a strategy, with the idea being that when performance is poor a smaller position size can be taken. My graph is showing 0 or 10 rather than the expected 0-100 range. Any comments or ideas greatly appreciated!
Leo
DEFPARAM CumulateOrders=False
REM Buy
indicator1 = close
indicator2 = SuperTrend[7,67]
c1 = (indicator1 >= indicator2)
IF c1 THEN
BUY 1 SHARES AT MARKET
ENDIF
REM Sell
indicator3 = close
indicator4 = SuperTrend[7,67]
c2 = (indicator3 <= indicator4)
IF c2 THEN
SELLSHORT AT MARKET
ENDIF
// Initialize variables
wins = 0
totalTrades = 10
// Loop through the last 10 trades
for value1 = 1 to totalTrades
next
If strategyprofit[value1] > strategyprofit[value1 + 1] then
wins = wins + 1
endif
// Calculate the win percentage
value2 = ((wins / totalTrades) * 100)
// Output the win percentage
GRAPH value2 as "Win %"
You shoukd move NEXT from line 31 to line 36, to make the code in between to be part of the iteration.
Do not use REM as it may conflict with the keyword REMark. Use double slashes (“//”), instead.
You are not summing up the last 10 trades, but the trades in the last 10 bars. If you are using a 1-hour TF, then you will only scan what happened in the last 10 hours.
Arrays are needed to do what you want:
DEFPARAM CumulateOrders=False
// initialize the array on the very first bar
ONCE Elements = 10
IF BarIndex = 0 THEN
FOR i = 1 TO Elements
$WinningTrade[i] = 0
NEXT
ENDIF
/*
UPDATE the array everytime a trade is a winner
Shift Elements-1 elemnts to the left, to drop the leftmost (oldest) trade and
make room for the current trade to be stored in element 1 (the rightmost element)
*/
IF StrategyProfit <> StrategyProfit[1] THEN
FOR i = Elements DOWNTO 2
$WinningTrade[i] = $WinningTrade[i - 1]
NEXT
$WinningTrade[1] = (StrategyProfit > StrategyProfit[1]) //assign 1 for a winning
// trade, 0 otherwise
// Initialize variables
wins = 0
// Loop through the last 10 trades
for i = 1 to Elements
wins = wins + $WinningTrade[i]
next
// Calculate the win percentage
value2 = ((wins / Elements) * 100)
ENDIF
// Buy
indicator1 = close
indicator2 = SuperTrend[7,67]
c1 = (indicator1 >= indicator2)
IF c1 THEN
BUY 1 SHARES AT MARKET
ENDIF
// Sell
indicator3 = close
indicator4 = SuperTrend[7,67]
c2 = (indicator3 <= indicator4)
IF c2 THEN
SELLSHORT AT MARKET
ENDIF
// Output the win percentage
GRAPH value2 as "Win %"
ALL HAIL ROBERTO
You legend! Thank you so much, I would never have got that.
Leo