Implementing a Sequential Trade Outcome Tracker in ProBuilder

04 Dec 2017
0 comment
0 attachment

This ProBuilder script is designed to track the outcomes of the last three trades and change a flag based on a specific sequence of wins and losses. The code also includes conditions for entering trades based on a moving average crossover strategy, and visualizes the trade outcomes and the flag status on the chart.


ONCE MyProfit1 = 0
ONCE MyProfit2 = 0
ONCE MyProfit3 = 0
ONCE LWL = 0
IF LWL THEN MyProfit1 = 0 ENDIF
If StrategyProfit > StrategyProfit[1] then
    MyProfit1 = MyProfit2
    MyProfit2 = MyProfit3
    MyProfit3 = 1
ElsIf StrategyProfit < StrategyProfit[1] then
    MyProfit1 = MyProfit2
    MyProfit2 = MyProfit3
    MyProfit3 = -1
Endif
LWL = (MyProfit1 = -1) AND (MyProfit2 = 1) AND (MyProfit3 = -1)
IF close crosses over average[20] and Not LongOnMarket then
    buy at market
elsif close crosses under average[20] and Not ShortOnMarket then
    sellshort at market
endif
set stop ploss 100
set target pprofit 300
graph LWL coloured(255,0,0,255)
graph MyProfit1
graph MyProfit2
graph MyProfit3

Explanation of the Code:

  • Initialization: The variables MyProfit1, MyProfit2, MyProfit3, and LWL are initialized to zero using the ONCE keyword, ensuring they are set only once when the script starts.
  • Tracking Trade Outcomes: The script checks if the current strategy profit is greater or less than the previous period's strategy profit to determine if the trade was a win (1) or a loss (-1). These values are then shifted between MyProfit1, MyProfit2, and MyProfit3 to keep a record of the last three trades.
  • Setting the LWL Flag: The LWL flag is set to true (1) if the sequence of last three trades is Loss-Win-Loss (MyProfit1 = -1, MyProfit2 = 1, MyProfit3 = -1).
  • Trade Execution: The script executes a buy order if the close price crosses over a 20-period moving average and there is no existing long position, and a sell short order if the close price crosses under the moving average without an existing short position.
  • Trade Management: A stop loss and a profit target are set at 100 and 300 points, respectively.
  • Visualization: The LWL flag and the values of MyProfit1, MyProfit2, and MyProfit3 are graphed on the chart, with the LWL flag highlighted in red when true.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/change-a-variable-flag-after-a-lwl-streak-possible/#post-175070

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