Implementing Time-Limited Buy Limit Orders in ProBuilder

06 Apr 2017
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to set a buy limit order with a specific validity period measured in bars. The order is placed at the close price of a bar where a moving average crossover occurs and remains valid for a predefined number of bars. If the order is not executed within this period, it is automatically cancelled, and optionally, a market order can be placed instead.


// Definition of the validity length of the order
ONCE NbBarLimit = 10
MM20 = Average[20](close)
MM50 = Average[50](close)

// If MM20 crosses over MM50, we define 2 variables "MyLimitBuy" and "MyIndex" containing the close price at that time and the index of the bar of the cross.
IF MM20 CROSSES OVER MM50 THEN
    MyLimitBuy = close
    MyIndex = Barindex
ENDIF

IF BarIndex >= MyIndex + NbBarLimit THEN
    MyLimitBuy = 0
ENDIF

// Place an order at the price MyLimitBuy valid as long as this variable is greater than 0 and we are not in a long position.
// Remember: MyLimitBuy is greater than 0 for the 10 bars after the bar of the crossing.
IF MyLimitBuy > 0 AND NOT LongOnMarket THEN
    BUY 1 SHARES AT MyLimitBuy LIMIT
ENDIF

// In case the order was not executed, it is possible to replace the expired buy limit order with a buy at market price order.
IF MyIndex + NbBarLimit AND MyLimitBuy > 0 AND NOT LongOnMarket THEN
    BUY 1 SHARES AT MARKET
ENDIF

Explanation of the code:

  • Setting the Order Validity: The variable NbBarLimit is set to 10, defining the number of bars for which the limit order will remain valid.
  • Detecting the Crossover: Two moving averages (MM20 and MM50) are calculated. When MM20 crosses over MM50, the closing price and the bar index at the time of the crossover are stored in MyLimitBuy and MyIndex respectively.
  • Order Expiry: If the current bar index is greater than or equal to the sum of MyIndex and NbBarLimit, MyLimitBuy is set to 0, effectively cancelling the order if it has not been executed.
  • Placing the Limit Order: A buy limit order is placed if MyLimitBuy is greater than 0 and there is no existing long position. The order is placed at the price stored in MyLimitBuy.
  • Handling Unexecuted Orders: If the limit order has not been executed by the end of its validity period and there is still no long position, a market order is placed to buy 1 share.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/discussion-re-pure-renko-strategy/page/5/#post-122279

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.
paul New
Code artist, my biography is a blank page waiting to be scripted. Imagine a bio so awesome it hasn't been coded yet.
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...