Reputation: 1
I would like to find a way on how to track the number of times that the below strategy has taken maxProfit. The idea is to stop the strategy to run once, we hit certain number of maxProfit.
strategy.exit(id="XL STP", profit = maxProfit, loss = nRes*100)
I do not know a way to track this number while the strategy is being executed.
Upvotes: 0
Views: 144
Reputation: 1
I did Dave method above but still not working as expected.
//reset profit reached to zero if a new day
if(ta.change(time('1D')))
numberMaxProfitReached := 0
MaxReached := na
if(numberMaxProfitReached < maxNumberProfit)
MaxReached := strategy.position_size > 0 ? math.max(nz(MaxReached, high), high) : strategy.position_size < 0 ? math.min(nz(MaxReached, low), low) : na
if(math.abs(strategy.position_avg_price - MaxReached)*100 >= maxProfit)
numberMaxProfitReached = numberMaxProfitReached + 1
if(shortCond)
strategy.entry("Sell", strategy.short, 1)
else if(longCond)
strategy.entry("Buy", strategy.long, 1)
if (strategy.position_size < 0)
strategy.exit(id="XS STP", profit = maxProfit, loss = nRes*100)
if(buySignal)
MaxReached := strategy.position_size > 0 ? high : low
strategy.close("Sell")
else if(strategy.position_size > 0)
strategy.exit(id="XL STP", profit = maxProfit, loss = nRes*100)
if(sellSignal)
MaxReached := strategy.position_size > 0 ? high : low
strategy.close("Buy")
The idea is to increment maxProfitReached by 1 every time we have hit our maxProfit. However, when I backtested it, it still doesn't increment the maxProfitReached number although conditions are met.
Upvotes: 0
Reputation: 865
Use an integer counter variable that you increment once per trade at most only if the current profit is above your maxProfit variable
The strategy.position_avg_price
gives you the entry price
So the difference for example for a long between the high
and strategy.position_avg_price
should give you the current PnL of your trade
You can track the higher/lower points of your trades like this
var MaxReached = 0.0 // Max high/low reached since beginning of trade.
// resetting min/max at each signal
if signal_candle[1]
MaxReached := strategy.position_size > 0 ? high : low
MaxReached := strategy.position_size > 0
? math.max(nz(MaxReached, high), high)
: strategy.position_size < 0 ? math.min(nz(MaxReached,Rlow), low) : na
Upvotes: 0