- ago
i m testing a strategy but i dont know how to set the backtest to enter at close of the day, it always enter at the open of the next day, how i set up the block strategy to open at close?
0
521
5 Answers

Reply

Bookmark

Sort
- ago
#1
It’s possible by using OrderType.MarketClose in a code based strategy. But like all other order types the order is placed for the bar following the signal bar. If you want to place an at close order based on the current bar’s value, it’s possible by looking ahead, but this is dangerous because you really would not know the indicator values for the current bar until the closing price is established. And by that point it’s too late to place an order 🤷🏼‍♂️

Fir this reason we don’t support that in a building block strategy. We don’t want to lead people down a dangerous path by introducing subtle look ahead issues.
0
- ago
#2
Hi Glitch, how are you?

I understand the risks you mentioned, but I would still like to do a test with close order based on the current bar’s value... How can I do this in the code?
1
- ago
#3
Using a pattern like this, where you "peek ahead" at tomorrow's data (assuming daily scale) and place MarketClose orders.

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars) {          smaShort = SMA.Series(bars.Close, 20);          smaLong = SMA.Series(bars.Close, 50);          PlotIndicator(smaShort);          PlotIndicator(smaLong, Color.Red); } //Execute public override void Execute(BarHistory bars, int idx) {          //because we are looking at tomorrow's bar     if (idx == bars.Count - 1)             return;              //look at tomorrow's crossovers to place orders at close on that bar     if (smaShort.CrossesOver(smaLong, idx + 1))             PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose);     else if (smaShort.CrossesUnder(smaLong, idx + 1))             PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); }       //private members       private SMA smaShort;       private SMA smaLong; } }
0
- ago
#4
I understood but didnt understand how I do this here..

Here, can you help me? This is the original, buy if RSI<25. I want buy at the close day..

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript2 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() { } public override void Initialize(BarHistory bars) {       indicator = new RSI(bars.Close,2);       Plot(indicator,Color.FromArgb(255,0,0,0));       source = new Highest(bars.High,2);       Plot(source,Color.FromArgb(255,0,0,255));       pct = 0.00;       pct = (100.0 + pct) / 100.0;       multSource = source * pct;          StartIndex = 2; } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                if (indicator[index] < 25.00)                {                   condition0 = true;                }             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0);             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                if (idx - foundPosition0.EntryBar + 1 >= 7)                {                   ClosePosition(foundPosition0, OrderType.Market);                }             }             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                val = multSource[idx];                ClosePosition(foundPosition0, OrderType.Limit, + val);             }          } }    private IndicatorBase indicator;    private double pct;    private double val;    private IndicatorBase source;    private TimeSeries multSource;       private Transaction _transaction; } }
0
- ago
#5
CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript2 {    public class MyStrategy : UserStrategyBase    {       //Initialize       public override void Initialize(BarHistory bars)       {          indicator = RSI.Series(bars.Close, 2);          source = Highest.Series(bars.High, 2);          PlotTimeSeries(indicator, "RSI", "rsi", Color.FromArgb(255, 0, 0, 0));          PlotTimeSeries(source, "HH", "Price", Color.FromArgb(255, 0, 0, 255));          //to stabilize the 2-period RSI need StartIndex to be bar #6-8          StartIndex = 8;       }       //Execute       public override void Execute(BarHistory bars, int idx)       {          //because we are looking at tomorrow's bar          if (idx == bars.Count - 1)             return;                    if(LastOpenPosition != null)          {             ClosePosition(LastOpenPosition, OrderType.Limit, source[idx], "Limit");             if (idx + 1 - LastOpenPosition.EntryBar + 1 >= 7)                ClosePosition(LastOpenPosition, OrderType.MarketClose, 0, "Timed");          }          else          {             //look at tomorrow's data to place orders at close on that bar             if (indicator[idx + 1] < 25.00)                PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose);          }                }       //private members       private TimeSeries indicator, source;    } }
1

Reply

Bookmark

Sort