Implementing a Manual Stop Loss in ProBuilder

17 Oct 2022
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to manually implement a stop loss mechanism in a trading strategy, which exits a trade based on the closing price of a bar rather than an intraday price hit. This approach can potentially avoid premature exits due to temporary price spikes.


MyStop = 20 * pipsize //20 pips SL
IF Not OnMarket AND MyConditions THEN
    BUY 1 CONTRACT AT MARKET
    EntryPrice = close
    StopLoss = EntryPrice - MyStop
    SET TARGET pPROFIT 50
ENDIF

IF LongOnMarket THEN
    IF close <= StopLoss THEN
        SELL AT MARKET
    ENDIF
ENDIF

Explanation of the Code:

  • Setting the Stop Loss: The variable MyStop is defined as 20 times the pipsize, which sets the stop loss to 20 pips below the entry price.
  • Entry Condition: The IF statement checks if the market position is not already open and other custom conditions (MyConditions) are met. If true, it executes a market order to buy 1 contract.
  • Defining Entry and Stop Loss Prices: Upon entering the market, the entry price (EntryPrice) is set to the closing price of the current bar. The stop loss (StopLoss) is then set as the entry price minus MyStop.
  • Setting a Profit Target: A profit target is set to 50 pips above the entry price using SET TARGET pPROFIT 50.
  • Exit Condition: Another IF statement checks if the position is long and if the closing price of the current bar is less than or equal to the stop loss price. If this condition is met, it triggers a market order to sell, thereby closing the position.

This method ensures that the trade is only exited after a bar closes at or below the stop loss level, potentially allowing for recovery in the price within the duration of the bar and avoiding a stop-out from a brief price dip.

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