- ago
I'm trying to do the follow backtesting strategy:

- Buy at market when de low price cross under the lowest of 2 lows bars shit 1 and discounted 0.988 of the low price.
- Sell at the end of day. (close market)

I'm using the daily chart and the strategy consist to open and close de trade in the same day. (daytrade strategy using the daily chart).

how can i buy and sell on the close market at the same day?

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthLab.Strategies { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() { }        public override void Initialize(BarHistory bars) {          indicator1 = bars.Low;                   indicator2 = new Lowest( ((bars.Low >> 1) * 0.988), 2 );          PlotIndicator(indicator2,Color.FromArgb(255,0,0,0));          StartIndex = 2; } public override void Execute(BarHistory bars, int idx) {          //because we are looking at tomorrow's bar          if (idx == bars.Count - 1)             return;                    int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;       if (foundPosition0 == null)          {             condition0 = false;             {                if (indicator1.CrossesUnder(indicator2, index + 1))                {                   condition0 = true;                }             }             if (condition0)             {                                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                value = 1.0 - (4.00 / 100.0);                ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice * value, "Sell at 4% stop loss");             }                 }          {             lastBar = bars.IsLastBarOfDay(idx) == true;             if (lastBar)             {                condition0 = true;             }          }          if (condition0)          {             ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");          } }       private TimeSeries indicator1;       private IndicatorBase indicator2;       private double value;       private bool trailing;       private TimeSeries stops;       private bool firstBar;       private bool lastBar;       private Transaction _transaction; } }
0
1,817
Solved
11 Replies

Closed

Bookmark

Sort
- ago
#1
While we're not sure how to make the FAQ more visible (clicking "Support" already defaults to it), please find your question answered there with a link to code sample:

How can my strategy from Blocks enter or exit at close?
https://www.wealth-lab.com/Support/Faq
1
- ago
#2
The strategy example that you suggested to close EOD positions works in daily charts?
0
- ago
#3
my position only close at the next bar

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthLab.Strategies {    public class MyStrategy : UserStrategyBase {     public override void Initialize(BarHistory bars) {          indicator = bars.Low;                   source = new Lowest( ((bars.Low >> 1) * 0.988), 2 );          stop = source * (1.0 - (4.00 / 100.0));          PlotTimeSeries(source, "Lowest", "Price", Color.Blue);          PlotTimeSeries(stop, "Lowest Stop", "Price", Color.Red);          //StartIndex = 8; } 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.Stop, LastOpenPosition.EntryPrice * (1.0 - (4.00 / 100.0)), "Sell at 4% stop loss");             //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.CrossesUnder(source, idx + 1)) {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                }          }       }       //private members       private TimeSeries indicator, source, stop; } }


0
Glitch8
 ( 10.62% )
- ago
#4
Hi rafael,

Here's how you can code a Strategy to exit at market close of the same bar as entry. You just have to issue the sell on the same bar where you issue the buy.

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript1 { public class MyStrategy : UserStrategyBase {       public override void Initialize(BarHistory bars)       {          indicator = bars.Low;          source = new Lowest(((bars.Low >> 1) * 0.988), 2);          stop = source * (1.0 - (4.00 / 100.0));          PlotTimeSeries(source, "Lowest", "Price", Color.Blue);          PlotTimeSeries(stop, "Lowest Stop", "Price", Color.Red);          //StartIndex = 8;       }       public override void Execute(BarHistory bars, int idx)       {          PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, source[idx]);          PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);       }       //private members       private TimeSeries indicator, source, stop; } }
0
Glitch8
 ( 10.62% )
- ago
#5
0
- ago
#6
tks so much Glitch!!!!
0
- ago
#7
Glitch, in your change the order duplicate adding another trade at the next day after the first one. Do you know how can I fix it?
0
Glitch8
 ( 10.62% )
- ago
#8
I don’t think there’s anything to fix. It added another trade because the price hit the limit for that bar.
0
- ago
#9
A buy order is actived when the low price cross under of the blue line. As you can see in the follow picture the second trade doesn't cross the blue line, only the first one.

0
Glitch8
 ( 10.62% )
- ago
#10
It's a common mistake. Each bar, you are placing a trade for the FOLLOWING bar, based on data that ocurred on THIS bar.

So, it you want the chart to visually show you the indicator from the PREVIOUS bar, you need to offset it one bar like this. Here you can see that indeed the price did cross below on both bars.

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript1 {    public class MyStrategy : UserStrategyBase    {       public override void Initialize(BarHistory bars)       {          indicator = bars.Low;          source = new Lowest(((bars.Low >> 1) * 0.988), 2);          stop = source * (1.0 - (4.00 / 100.0));          TimeSeries offset = source >> 1;          PlotTimeSeries(offset, "Lowest", "Price", Color.Blue);          PlotTimeSeries(stop, "Lowest Stop", "Price", Color.Red);          //StartIndex = 8;       }       public override void Execute(BarHistory bars, int idx)       {          PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, source[idx]);          PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);       }       //private members       private TimeSeries indicator, source, stop;    } }


1
Best Answer
- ago
#11
Tks so much for your support!!
1

Closed

Bookmark

Sort