Implementing a Multi-Level Profit Taking Strategy in ProBuilder

08 May 2019
0 comment
0 attachment

This code snippet demonstrates how to implement a multi-level profit taking strategy using the ProBuilder programming language. The strategy is designed to scale out of positions at various profit levels, ranging from 0% to 5%. It also includes an emergency exit condition in case of a significant market drop.

defparam cumulateorders = false
if (your long entry condition) and om = 0 then
    buy 1 contract at market
    pp = close //stored position price of our 1 position
    om = 6
endif

//store more accurate position price after first bar on market
if positionprice <> positionprice[1] then
    pp = positionprice
endif

//exit all trades if exit condition true
if (your short exit condition) then
    sell at market
    om = 0 //not on market
endif

//exit with any profit strategy
if om = 6 and close > pp * 1.0 then
    //sell 1 contract at market
    om = om-1 //one position closed
endif

//exit with 1% profit strategy
if om = 5 and close > pp * 1.01 then
    //sell 1 contract at market
    om = om-1 //one position closed
endif

//exit with 2% profit strategy
if om = 4 and close > pp * 1.02 then
    sell 1 contract at market
    om = om-1 //one position closed
endif

//exit with 3% profit strategy
if om = 3 and close > pp * 1.03 then
    //sell 1 contract at market
    om = om-1 //one position closed
endif

//exit with 4% profit strategy
if om = 2 and close > pp * 1.04 then
    //sell 1 contract at market
    om = om-1 //one position closed
endif

//exit with 5% profit strategy
if om = 1 and close > pp * 1.05 then
    //sell 1 contract at market
    om = om-1 //one position closed
endif

//Emergency exit at 10% drop from position price
if om <> 0 and close < pp * 0.90 then
    sell at market
    om = 0 //not on market
endif

graph om

This code snippet is structured to manage multiple exit strategies for a single trading position, allowing partial exits at predefined profit levels. Here's a step-by-step explanation:

  • Initialization: Disables accumulation of orders with defparam cumulateorders = false.
  • Entry Condition: Checks for a user-defined entry condition and whether the market position variable om is 0, indicating no open positions. It then buys a contract and initializes variables for tracking.
  • Position Price Update: Updates the stored position price pp after the first bar on the market to ensure accuracy.
  • Exit Conditions: Multiple exit conditions are checked:
    • General exit based on a user-defined condition.
    • Incremental profit targets from 0% to 5%, each reducing the om count by 1 upon execution, indicating a partial close of the position.
    • An emergency exit that triggers if the price drops 10% from the position price, closing all positions.
  • Graph Output: The variable om is graphed to visually track the number of open market positions.

This example provides a framework for scaling out of positions at multiple profit levels, which can be customized with specific entry and exit conditions as per trading requirements.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/scaling-out-using-multiple-strategies/#post-145546

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