This ProBuilder code snippet demonstrates how to restrict trading activities based on the percentage gain of the capital within a month. It uses a boolean variable to control whether trading is allowed, based on the gain exceeding a predefined threshold.
defparam cumulateorders=false
StartCapital = 10000
if month<>month[1] then
startprofit=StartCapital+strategyprofit
endif
trading = (startcapital+strategyprofit)/startprofit < 1.04
if trading then
//trading is allowed
//put strategy here
if rsi[14] crosses over 50 then
buy 2 contracts at market
set target pprofit 100
set stop ploss 50
endif
endif
graph trading as "authorize trading"
Explanation of the Code:
defparam cumulateorders=false line ensures that orders are not accumulated across different bars, resetting the trading conditions for each new calculation.StartCapital is set to 10000, representing the initial amount of capital at the start of the month.if month<>month[1] condition checks if the current month has changed from the previous bar's month. If true, it updates startprofit to the sum of StartCapital and strategyprofit, which tracks the profit or loss from the strategy since the start of the month.trading boolean is calculated by comparing the current capital (initial plus profit/loss) to the startprofit. Trading is only allowed if the capital has not increased by more than 4% since the start of the month.if trading block, trading strategies are implemented. Here, a buy order is placed if the 14-period RSI crosses over 50, with a target profit of 100 and a stop loss of 50.graph trading as "authorize trading" line visually indicates on the chart when trading is authorized based on the defined conditions.This snippet is useful for implementing risk management by limiting trading activities when a certain percentage gain threshold is reached within a month, helping to preserve capital and manage risk effectively.
Check out this related content for more information:
https://www.prorealcode.com/topic/limite-de-gain-mensuelle/#post-160897
Visit Link