Implementing Monthly Trading Limits Based on Capital Gain in ProBuilder

27 Jul 2017
0 comment
0 attachment

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:

  • Initialization: The defparam cumulateorders=false line ensures that orders are not accumulated across different bars, resetting the trading conditions for each new calculation.
  • Setting Initial Capital: StartCapital is set to 10000, representing the initial amount of capital at the start of the month.
  • Monthly Profit Calculation: The 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 Authorization: The 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.
  • Trading Strategy: Within the 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.
  • Graphical Output: The 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.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/limite-de-gain-mensuelle/#post-160897

Visit Link
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.
Nicolas Master
I created ProRealCode because I believe in the power of shared knowledge. I spend my time coding new tools and helping members solve complex problems. If you are stuck on a code or need a fresh perspective on a strategy, I am always willing to help. Welcome to the community!
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...