Dynamic Adjustment of Trading Lot Size Based on Win Rate

21 Mar 2017
0 comment
0 attachment

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:

  • Initialization: Sets initial parameters such as the number of trades, initial lot size, and counters for all trades and winning trades.
  • Trade Detection: Identifies new trades based on market entry and exit conditions. It uses boolean flags to detect trade transitions.
  • Win Rate Calculation: Calculates the percentage of winning trades and adjusts the lot size if the win rate exceeds 80% over the last 10 trades.
  • Trading Conditions: Defines conditions for entering long and short positions based on the moving average of the closing prices over a specified period.
  • Order Execution: Executes buy or sell orders based on the defined conditions, using the dynamically adjusted lot size.
  • Risk Management: Sets predefined profit targets and stop-loss limits to manage risk.

This example is useful for understanding how to implement dynamic adjustments in trading strategies based on performance metrics, utilizing ProBuilder’s programming capabilities.

What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
robertogozzi Master
Roberto https://www.ots-onlinetradingsoftware.com
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...