Implementing a Weekly Profit Limit in Trading Strategies

27 Jun 2021
0 comment
0 attachment

This code snippet demonstrates how to set and enforce a weekly profit limit in a trading strategy using the ProBuilder language. The strategy resets the profit calculation at the beginning of each week and disables trading once the profit exceeds a predefined limit.


ONCE MyProfit = 0 //Use this variable to tell when your profit exceeds X
ONCE TradeON = 1 //1=trading enabled, 0=trading disabled
ONCE X = 500 //500 € max weekly profit
IF OpenDayOfWeek = 1 AND OpenDayOfWeek[1] <> 1 THEN //at week start
    MyProfit = StrategyProfit //save the total profit at the start of the week
    TradeON = 1 //re-enable trading
ENDIF
IF (StrategyProfit - MyProfit) >= X THEN
    TradeON = 0 //disable trading after X € gained
ENDIF
IF MyLongConditions AND Not OnMarket ANd TradeON THEN //add TradeON as an additional condition to enter a trade
    BUY 1 Contract AT Market
ENDIF

The code snippet above is structured to manage trading based on weekly profit limits:

  • Initialization: Three variables are initialized using the ONCE keyword. MyProfit tracks the profit at the start of the week, TradeON is a flag to enable or disable trading, and X sets the maximum weekly profit limit.
  • Weekly Reset: At the start of each week (when OpenDayOfWeek equals 1 and the previous day’s OpenDayOfWeek was not 1), the script resets MyProfit to the current StrategyProfit and re-enables trading by setting TradeON to 1.
  • Profit Check: During the week, the script checks if the current profit (StrategyProfit - MyProfit) has reached or exceeded the limit X. If it has, trading is disabled by setting TradeON to 0.
  • Trade Execution: A trade is executed only if the conditions defined in MyLongConditions are met, the strategy is not currently in the market (Not OnMarket), and trading is enabled (TradeON).

This approach helps in managing risk and ensuring that the trading strategy adheres to specific financial goals or constraints.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/how-to-set-a-weekly-profit/#post-141701

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.
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...