- 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
1,292
Solved
8 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
Best Answer
- 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;    } }
2
- ago
#6
Hi Eugene and Glitch,

I understand the risks of look ahead bias with using daily scale data and issuing buy at close orders based on peeking ahead at today's indicator values. However, I just want to use this approach to do some early strategy development. If I can get something decent working with daily scale data I'll switch to 60 or 15 minute bars to verify that my strategy actually works before implementation.

The idea I'd like to test is to buy at market on close if:
- today's low is 2% below yesterday's low
- and today's close is 2% below today's AveragePriceHL

Then sell at tomorrow's open

Here's an image to illustrate:


I attempted to modify my own code by using the example you provided above but I don't have any experience yet with coding in C# and I wasn't sure if I was making the correct changes. Do you have any time to help me adapt this code?

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript4 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() { } public override void Initialize(BarHistory bars) {          indicator1 = bars.Low;          indicator2 = bars.Low;          indicator12 = bars.Close;          indicator22 = bars.AveragePriceHL;          StartIndex = 0; } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                if (index - 1 >= 0 && (indicator1[index] < indicator2[index - 1] * (1.0 - (2.00 / 100.0))))                {                   if (index - 0 >= 0 && (indicator12[index] < indicator22[index - 0] * (1.0 - (2.00 / 100.0))))                   {                      condition0 = true;                   }                }             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");             }          } }       private TimeSeries indicator1;       private TimeSeries indicator2;       private TimeSeries indicator12;       private TimeSeries indicator22;       private Transaction _transaction; } }


Thanks
0
Cone7
- ago
#7
Remember, a script like this will not generate signals for the MarketClose order. To do it properly, you'd need to use 1-minute bars in case the low happened near the end of the day, which happens often!

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript5 { public class MOCtoOpen5504 : UserStrategyBase { public override void Initialize(BarHistory bars) {          StartIndex = 1; }       public override void Execute(BarHistory bars, int idx)       {          Position p = LastOpenPosition;          if (p != null)          {             // if a Position is open close it tomorrow at market             ClosePosition(p, OrderType.Market);          }                    // NO SIGNALING because we're peeking          if (idx >= bars.Count - 1)   return;          // if tomorrow's low is 2% less than today's low          if (bars.Low[idx + 1] < bars.Low[idx] * 0.98)          {             // AND tomorrow's close is 2% less than tomorrow's AvgHL             if (bars.Close[idx + 1] < bars.AveragePriceHL[idx + 1] * 0.98)             {                //buy at MarketClose tomorrow                PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose);             } } } } }
1
- ago
#8
Okay, understood. Thanks very much for your help.
0

Reply

Bookmark

Sort