Implementing Time-Dependent Trailing Stops in ProBuilder

25 Aug 2023
0 comment
0 attachment

This code snippet demonstrates how to implement trailing stops with different percentage values based on specific time intervals during the trading day. Trailing stops are a type of stop-loss order that adjusts automatically to the price movements of a stock.


IF time >= 000000 AND time <= 130000 THEN
    percentagelong = 0.45
    percentageshort = 0.30
ELSIF time > 130000 AND time <= 230000 THEN
    percentagelong = 0.45
    percentageshort = 0.30
ENDIF

TGL = (close/100)*percentagelong
TGS = (close/100)*percentageshort

if not onmarket then
    MAXPRICE = 0
    MINPRICE = close
    PREZZOUSCITA = 0
ENDIF

if longonmarket then
    MAXPRICE = MAX(MAXPRICE,close)
    if MAXPRICE-tradeprice(1)>=TGL then
        PREZZOUSCITA = MAXPRICE-tgl
    ENDIF
ENDIF

if shortonmarket then
    MINPRICE = MIN(MINPRICE,close)
    if tradeprice(1)-MINPRICE>=TGS then
        PREZZOUSCITA = MINPRICE+tgs
    ENDIF
ENDIF

if onmarket and PREZZOUSCITA>0 then
    EXITSHORT AT PREZZOUSCITA STOP
    SELL AT PREZZOUSCITA STOP
ENDIF

The code is structured to adjust the trailing stop percentages based on the time of day. Here’s a breakdown of its functionality:

  • Time Check: The code first checks the current time and sets the trailing stop percentages for long and short positions accordingly. Two time intervals are defined: from midnight to 1:30 PM and from 1:30 PM to 11:00 PM.
  • Trailing Stop Calculation: It calculates the trailing stop values (TGL for long positions and TGS for short positions) based on the closing price and the defined percentages.
  • Market Entry: If not currently in the market, it initializes the maximum and minimum prices and sets an initial exit price.
  • Long Position Management: For a long position, it updates the maximum price and checks if the current price has moved sufficiently above the purchase price to adjust the exit price upwards.
  • Short Position Management: For a short position, it updates the minimum price and checks if the current price has moved sufficiently below the purchase price to adjust the exit price downwards.
  • Exit Execution: If in the market and an exit price has been set, it places a stop order to exit the position at the calculated price, either for a short sale or a sell order.

This example is useful for understanding how to implement dynamic trading strategies based on time-specific criteria in ProBuilder.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/trailing-stop-16/#post-84233

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