This ProBuilder code snippet demonstrates how to dynamically adjust the trading lot size based on the win rate of executed trades. The code uses various ProBuilder-specific functions and conditions to manage trading decisions and lot sizes.
DEFPARAM CumulateOrders = false
ONCE AllTrades = 0
ONCE WinningTrades = 0
ONCE InitialLots = 2
ONCE LotSize = InitialLots
ONCE Trades = 10 //10 trades in a row over 80%
MyGain = StrategyProfit // tally each trade
t1 = OnMarket AND Not OnMarket[1] //it's a new trade
t2 = Not OnMarket AND Not OnMarket[1] AND MyGain <> MyGain[1] //detect any 1-bar trade
t3 = ShortOnMarket AND LongOnMarket[1] //it's a new trade (S & R)
t4 = ShortOnMarket[1] AND LongOnMarket //it's a new trade (S & R)
AllTrades = AllTrades + ((t1 OR t2 OR t3 OR t4) AND IsLastBarUpdate) // tally winning trades
IF MyGain > MyGain[1] THEN
WinningTrades = WinningTrades + 1
ENDIF
// calculate % of winning trades
IF AllTrades > Trades THEN
WinPerCent = WinningTrades * 100 / AllTrades
IF summation[Trades](WinPerCent > 80) = Trades THEN
LotSize = InitialLots //restore initial lotsize
ELSE
LotSize = InitialLots * 0.5 //halve lotsize
ENDIF
ENDIF
ONCE Periods = 10
MyLongConditions = close crosses over average[Periods] AND Not OnMarket
MyShortConditions = close crosses under average[Periods] AND Not OnMarket
IF MyLongConditions THEN
BUY LotSize CONTRACTS AT Market
ELSIF MyShortConditions THEN
SELLSHORT LotSize CONTRACTS AT Market
ENDIF
set target pprofit 200
set stop ploss 2000
The code snippet above is structured to perform the following operations:
This example is useful for understanding how to implement dynamic adjustments in trading strategies based on performance metrics, utilizing ProBuilder’s programming capabilities.