Implementing Z-Score Based Trading Decisions and Trailing Stops in ProBuilder

05 May 2023
0 comment
0 attachment

This code snippet demonstrates how to implement a trading strategy in ProBuilder that utilizes Z-Score for decision making and includes a trailing stop mechanism for managing open positions. The strategy skips trades based on the Z-Score and manages exits with dynamic stop-loss adjustments.


//*********************************************************************************
// Z-Score management
ONCE SkipOneTrade = 0
ONCE SkipFlag = 0
ONCE MinTrades = 51 //30 or 51
DirectionSwitch = (ShortOnMarket AND LongOnMarket[1]) OR (ShortOnMarket[1] AND LongOnMarket)
IF Zpos THEN
    IF (CurTrade = 1) AND SkipFlag = 0 AND ((Not Onmarket) OR DirectionSwitch) THEN
        IF N >= MinTrades THEN
            SkipOneTrade = 1
            SkipFlag = 1
        ENDIF
    ENDIF
ELSIF Zneg THEN
    IF (CurTrade = -1) AND SkipFlag = 0 AND ((Not Onmarket) OR DirectionSwitch) THEN
        IF N >= MinTrades THEN
            SkipOneTrade = 1
            SkipFlag = 1
        ENDIF
    ENDIF
ENDIF
//*********************************************************************************
ONCE MA = 100 //100
ONCE T = 1 //1=ema
IF close CROSSES OVER average[MA,T] AND Not OnMarket THEN
    IF SkipOneTrade THEN
        SkipOneTrade = 0
    ELSE
        BUY AT Market
        SkipFlag = 0
    ENDIF
ELSIF close CROSSES UNDER average[MA,T] AND Not OnMarket THEN
    IF SkipOneTrade THEN
        SkipOneTrade = 0
    ELSE
        SELLSHORT AT Market
        SkipFlag = 0
    ENDIF
ENDIF
set stop ploss 500 //500
set target pprofit 5000 //5000
//
//*********************************************************************************
//trailing stop function
trailingstart = 50 //50
trailingstep = 5 //5
//reset the stoploss value
IF NOT ONMARKET THEN
    newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
        newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
        newSL = newSL+trailingstep*pipsize
    ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
        newSL = tradeprice(1)-trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
        newSL = newSL-trailingstep*pipsize
    ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
    SELL AT newSL STOP
    EXITSHORT AT newSL STOP
ENDIF
//*********************************************************************************
// graph Zscore coloured(255,0,0,255)
graph TotalWin
graph TotalLose

Explanation of the Code:

  • Z-Score Management: The strategy begins by setting up conditions to skip trades based on the Z-Score. Trades are skipped if the Z-Score is positive or negative under certain conditions, helping to avoid entering the market during unfavorable times.
  • Trade Execution: Trades are executed based on the moving average crossover strategy. The strategy buys or sells short when the price crosses over or under a specified moving average, respectively, unless a trade is set to be skipped.
  • Stop Loss and Profit Targets: Fixed stop loss and profit targets are defined to manage risk and potential returns.
  • Trailing Stop Function: A trailing stop is implemented to protect profits as a trade becomes profitable. The stop loss is adjusted in steps as the price moves favorably, securing a part of the gains.

This snippet provides a practical example of combining various trading strategy components like Z-Score filtering, entry/exit rules, and dynamic risk management in the ProBuilder language.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/z-score-to-improve-strategies/#post-174413

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