Implementing a Trade Suspension Mechanism After Consecutive Losses

19 Feb 2018
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to implement a trading suspension mechanism that pauses trading activities after a specified number of consecutive losing trades. The system resumes trading after a certain time has elapsed.


Add TRADEOK to your entry conditions.
AllowedLosingTrades = 2
once tradeok = 1
if strategyprofit < strategyprofit[1] then
  losercount = losercount + 1
endif
if losercount = AllowedLosingTrades then
  tradeok = 0
  restarttime = time + 010000
  if restarttime >= 240000 then
    restarttime = time - 230000
  endif
endif
if not tradeok and time = restarttime then
  tradeok = 1
  losercount = 0
endif

The code snippet above is structured to control trading based on the recent performance of the strategy, specifically targeting losing trades. Here’s a step-by-step breakdown:

  • Initialization: The variable TRADEOK is added to the entry conditions to control whether new trades can be initiated. AllowedLosingTrades is set to 2, representing the threshold of consecutive losing trades allowed before pausing trading.
  • Tracking Losses: The system checks if the current strategy profit is less than the previous period’s profit. If true, it increments the losercount by 1.
  • Suspending Trading: If losercount equals AllowedLosingTrades, trading is paused (tradeok is set to 0), and a restarttime is calculated by adding 010000 to the current time. This addition represents the time delay (in the format HHMMSS) after which trading can resume.
  • Handling Overflow: There’s a check to ensure that restarttime does not exceed 240000 (the representation of time in HHMMSS format). If it does, it adjusts restarttime by subtracting 230000 from the current time to wrap around the clock correctly.
  • Resuming Trading: The system checks if trading is paused and if the current time matches restarttime. If both conditions are true, trading is resumed (tradeok is set back to 1) and losercount is reset to 0.

This mechanism is useful for strategies that need to pause and reassess after a series of losses, potentially avoiding further drawdowns during unfavorable market conditions.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/adding-a-countdown-timer-to-pause-system/#post-116557

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.
Vonasi Master
V-oyaging ON A S-mall I-ncome
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
33
indicator
132
strategy
171

Recent Snippets

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) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Logo Logo
Loading...