Implementing Multi-Variable Entry Criteria in Trading Strategies

29 Mar 2023
0 comment
0 attachment

This code snippet demonstrates how to implement a trading strategy in ProBuilder that evaluates multiple variations of a single variable to decide on trade entries and position sizing. The strategy checks a range of settings for an indicator and places trades based on the cumulative signals from these settings.


a = (start of range to check)
b = (end of range to check)
PosSizeIncrease = 0 // Set to 1 to turn on increased position sizing
PositionSize = 1
Flag = 0
For Period = a to b
    Indicator1 = (indicator)[Period]
    Indicator2 = (indicator)[Period]
    IF (long entry conditions) THEN
        Flag = Flag + 1
    ENDIF
    IF (short entry conditions) THEN
        Flag = Flag - 1
    ENDIF
NEXT
IF PosSizeIncrease = 1 THEN
    PositionSize = ABS(Flag)
ENDIF
IF Flag > 0 THEN
    BUY PositionSize Contracts at Market
ENDIF
IF Flag < 0 THEN
    SELLSHORT PositionSize Contracts at Market
ENDIF
IF LongOnMarket and (long exit conditions) THEN
    SELL at market
ENDIF
IF ShortOnMarket and (short exit conditions) THEN
    EXITSHORT at market
ENDIF

The code snippet above is structured to handle multiple conditions for entering and exiting trades based on a range of indicator periods. Here's a step-by-step breakdown:

  • Variable Initialization: Variables a and b define the start and end of the range for the indicator periods to check. PosSizeIncrease controls whether position sizing increases with the number of signals, and PositionSize starts as 1.
  • Loop Through Indicator Periods: A loop runs from a to b, calculating the indicator values for each period. It adjusts the Flag variable up or down based on whether the conditions for a long or short entry are met.
  • Adjust Position Size: If PosSizeIncrease is enabled, the absolute value of Flag (number of net positive or negative signals) determines the position size.
  • Execute Trades: Trades are executed based on the value of Flag: buying if positive (indicating more long signals) and selling short if negative (more short signals).
  • Exit Conditions: The strategy also includes conditions for exiting trades, both for long and short positions.

This approach allows a strategy to be more flexible and potentially more robust by considering multiple scenarios for entry rather than relying on a single parameter setting.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/multiple-strategies-in-one-strategy/#post-68931

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