Hello altogether,
for my strategy I want to reward the position size if the current winrate is above a certain value and punish otherwise.
I have found the code to implement it over all trades that have been done so far with this:
IF STRATEGYPROFIT> STRATEGYPROFIT[1] THEN
WinningTrades = WinningTrades + 1
ENDIF
However, there are some bad “times” in the strategy which go unheard due to the bigger amount of positive trades. This is why I wanted to implement the sliding window approach.
IF AllTrades > SlidingWindowWinRate THEN
FOR runner = 1 TO SlidingWindowWinRate DO
IF STRATEGYPROFIT[runner] < STRATEGYPROFIT[1 + runner] THEN
WinningTradesSlidingWindow = WinningTradesSlidingWindow + 1
ENDIF
NEXT
ENDIF
So far I have learned that looping over STRATEGYPROFIT does not work due to obvious reasons ;). Has someone done that so far?
Thanks in advance,
Daniel
For others who want to achieve the same :
IF MyProfitSoFar > MyProfitSoFar[1] THEN
WinningTrades = WinningTrades + 1
$SlidingWindowTrades[lastset($SlidingWindowTrades)+1] = 1
ENDIF
IF MyProfitSoFar < MyProfitSoFar[1] THEN
$SlidingWindowTrades[lastset($SlidingWindowTrades)+1] = 0
ENDIF
winningTradesVariable = 0
FOR i = LASTSET($SlidingWindowTrades) DOWNTO LASTSET($SlidingWindowTrades)-SlidingWindowWinRate do
IF $SlidingWindowTrades[i]THEN
winningTradesVariable = winningTradesVariable + 1
ENDIF
NEXT
looping over STRATEGYPROFIT does not work due to obvious reasons
You can loop over STRATEGYPROFIT values, just like a price consant or any other variables, but since it updates only when it has changes, the offset in brackets correspond to the bars offset from now, so there is no way to know how to get the last 10 changes just by making a loop “FOR I = 1 TO 10”
Turns out that with my solution that I posted above, I receive the following error if I want to run it in production/demo (not in backtest).
… exceeds the limit of 1,000,000 in an array
So I guess I start at square 1.
How to either fix the broken array?
How to calculate the sliding window winrate otherwise?
Regards
JSParticipant
Senior
Hi,
You can try to start the code with resetting the array…
UnSet($SlidingWindowTrades)