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:
Check out this related content for more information:
https://www.prorealcode.com/topic/discussion-re-pure-renko-strategy/page/5/#post-122279
Visit Link