Implementing a Trading Cooldown Period in ProBuilder

01 Jun 2023
0 comment
0 attachment

This code snippet demonstrates how to implement a cooldown period in trading using the ProBuilder language. The purpose is to prevent entering new trades for a specified number of bars after exiting a trade. This can be useful in strategies where a cooling-off period is required to avoid overtrading or to comply with trading rules.


ONCE BarCount = 0
ONCE TradeON = 1 //1=trading allowed, 0=trading disabled

IF LongOnMarket AND Not LongOnMarket[1] THEN
    TradeON = 0 //disable trading
    BarCount = 0 //reset counter
ENDIF

IF Not LongOnMarket THEN
    //Increase counter when not on market
    BarCount = BarCount + 1
ENDIF

IF BarCount > 10 THEN
    TradeON = 1
ENDIF

Explanation of the Code:

  • Initialization: Two variables are initialized at the start. BarCount keeps track of the number of bars since the last trade, and TradeON is a flag indicating whether trading is allowed (1) or disabled (0).
  • Trade Exit Detection: The code checks if the market status changed from being in a trade (LongOnMarket) in the previous bar to not being in a trade in the current bar. If this condition is true, trading is disabled and the bar counter is reset.
  • Incrementing the Bar Counter: If not currently in a trade (Not LongOnMarket), the bar counter (BarCount) is incremented by one for each bar that passes.
  • Re-enabling Trading: Trading is re-enabled (TradeON set to 1) once the bar counter exceeds 10. This means that trading will only be allowed after at least 10 bars have passed since the last trade.

This snippet is a practical example of how to manage trading activity programmatically, ensuring that trades are not executed too frequently, which can be a crucial aspect of certain trading strategies.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/how-not-to-enter-if-i-just-took-a-trade/#post-82316

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