- ago
Happy Sunday fellow traders.

How can I set a valid number of bars for a Limit Order Condition? Example: I want to place a Limit Order at the Highest High (10), but I want that this order is valid only for the next 3 bars.

Thank you in advance!
0
317
Solved
1 Replies

Reply

Bookmark

Sort
- ago
#1
Hola Diego,
You could code it like this:
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript4 { public class MyStrategy : UserStrategyBase {       int ExpireAfter = 3, signalBar = -1;       bool expired = false, setup = false;       Highest h;       Random random = new Random();       public MyStrategy()       {          AddParameter("Expire after", ParameterType.Int32, 3, 1, 20, 1);                }              //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars) {          h = Highest.Series(bars.High, 10);          PlotIndicatorLine(h, WLColor.Blue, 1);          ExpireAfter = Parameters[0].AsInt;          StartIndex = Math.Max(ExpireAfter, 11);          } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Short)) {             if (!setup)             {                setup = bars.High[idx] > h[idx -1];                signalBar = idx;                SetBackgroundColor(bars, idx, WLColor.FromArgb(10, WLColor.Red));             }             // reset if setup has timed out             if ((idx + 1 - signalBar) > ExpireAfter)                expired = setup = false;             if(!expired && setup)             {                //how many bars since setup triggered?                DrawBarAnnotation((idx - signalBar).ToString(), idx, true, WLColor.Chocolate, 10);                //artificial delay of random bars for testing purposes                var rnd = random.Next(1, 5);                DrawBarAnnotation(rnd.ToString(), idx, false, WLColor.DarkCyan, 10);                if (idx >= signalBar + rnd)                   PlaceTrade(bars, TransactionType.Short, OrderType.Limit, Highest.Series(bars.High, 10)[idx]);                               } } else {             PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, Lowest.Series(bars.Low, 10)[idx]); } } } }
1
Best Answer

Reply

Bookmark

Sort