Simulating a Trading Strategy with Equity Curve Calculation in ProBuilder

25 Mar 2024
0 comment
0 attachment

This code snippet demonstrates how to simulate a trading strategy in ProBuilder, which is particularly useful for testing strategies that involve pending orders. The example provided calculates an equity curve, allowing for performance analysis over time.


p = 300
upper = open + average[p](high[1]-open[1])
lower = average[p](close[2]-low[1])
if (not flag) and (low < (close[1] - lower[1])) and (low[1] > average[p,2](close[1])) then
    drawarrowup(barindex, low) coloured(0,128,0)
    entryprice = close[1] - lower[1]
    startequity = equity
    flag = 1
endif
if flag[1] then
    floatingequity = startequity + (close - entryprice)
endif
if (flag[1] = 1) and ((close > entryprice) or (high < average[p,2](close[1]))) and (entryprice <> 0) then
    equity = equity + (close - entryprice)
    flag = 0
    drawarrowdown(barindex, high) coloured(128,0,0)
endif
return floatingequity as "equity"

The code snippet above is structured to simulate entry and exit points of a trading strategy based on specific conditions, and to calculate the resulting equity curve. Here’s a step-by-step breakdown:

  • Initialization: The variable p is set to 300, which is used as a period for moving averages. The upper and lower boundaries are calculated based on historical price data.
  • Entry Condition: The strategy enters a trade (draws an upward arrow) if the current low is below the calculated lower boundary and other conditions are met. The entry price and starting equity are recorded, and a flag is set to indicate an open position.
  • Equity Calculation: While the flag is set (indicating an open position), the floating equity is updated based on the difference between the current close price and the entry price.
  • Exit Condition: The position is closed (draws a downward arrow) if the close price is above the entry price or other specified conditions are met. The equity is updated to reflect the profit or loss, and the flag is reset.
  • Output: The code returns the floating equity, allowing the user to track the equity curve over time.

This example is useful for understanding how to simulate trading strategies and visualize their performance through an equity curve in the ProBuilder language.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/strategy-as-indicator-to-overcome-limits-of-tick-by-tick/#post-106584

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.
Vonasi Master
V-oyaging ON A S-mall I-ncome
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...