Implementing Fixed and Dynamic Stop Loss Strategies Using SMA in ProBuilder

06 Jul 2017
0 comment
0 attachment

This code snippet demonstrates how to implement different stop loss strategies in trading using the ProBuilder language. The strategies include a fixed stop loss, a fixed percentage below the Simple Moving Average (SMA), and a dynamic stop loss that updates based on the SMA.


// Fixed Stop Loss below SMA50
Sma50 = average[50,0](close)
MyLongConditions = close CROSSES OVER average[10,0](close)
IF MyLongConditions AND Not OnMarket THEN
    BUY 1 Contract at Market
    Sl = close - Sma50
    Tp = Sl * 2
    SET TARGET PROFIT Tp
    SET STOP LOSS Sl
ENDIF

// Fixed Stop Loss at 1% below SMA50
Sma50 = average[50,0](close)
MyLongConditions = close CROSSES OVER average[10,0](close)
IF MyLongConditions AND Not OnMarket THEN
    BUY 1 Contract at Market
    Sl = close - (Sma50 * 0.99)
    Tp = Sl * 2
    SET TARGET PROFIT Tp
    SET STOP LOSS Sl
ENDIF

// Dynamic Stop Loss at 1% below SMA50
Sma50 = average[50,0](close)
IF LongOnMarket THEN
    Sl = TradePrice - (Sma50 * 0.99)
    SET STOP LOSS Sl
ENDIF
MyLongConditions = close CROSSES OVER average[10,0](close)
IF MyLongConditions AND Not OnMarket THEN
    BUY 1 Contract at Market
    Sl = close - (Sma50 * 0.99)
    Tp = Sl * 2
    SET TARGET PROFIT Tp
    SET STOP LOSS Sl
ENDIF

The code is structured into three main parts, each implementing a different type of stop loss strategy:

  • Fixed Stop Loss below SMA50: This section sets a stop loss exactly at the difference between the current close price and the SMA50. The target profit is set to twice the stop loss value.
  • Fixed Stop Loss at 1% below SMA50: Similar to the first, but the stop loss is set 1% below the SMA50. This provides a cushion against market volatility.
  • Dynamic Stop Loss at 1% below SMA50: This part updates the stop loss dynamically to always be 1% below the SMA50 for ongoing trades. It adjusts the stop loss as the SMA50 changes, which can be beneficial in trending markets.

Each section checks for a long entry condition based on the crossover of the close price over a shorter SMA (SMA10), and if the condition is met and there is no open position, it executes a buy order. The stop loss and target profit are then set according to the respective strategy.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/stop-loss-debajo-de-la-mm50-al-momento-de-la-compra/#post-175764

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
134
strategy
171

Recent Snippets

How to Track Up/Down Cumulative Volume Inside Each Candle (Tick Volume Delta)
indicator
This snippet separates the real-time, intra-candle volume into up-volume and down-volume based on whether the last [...]
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 [...]
Logo Logo
Loading...