- ago
I'm coding the follow backtesting:

Buy strategy:
When the Price of 2 days ago closed under of UpperBB and the Price of yesterday closed over of UpperBB and the volume of yesterday Closed over of SMA21[volume] then, buy when the Price of today cross over of the High Price of yesterday

Sell strategy
Trailling ATR 10

I didn't understand why the strategy doesn't works in the example attached. Probably I'm missing something.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; using WealthLab.TASC;    namespace WealthScript9 {    public class MyStrategy : UserStrategyBase    {       public override void Initialize(BarHistory bars)       {                    BBUpper = BBUpper.Series(bars.Close,21,2.00);          PlotIndicator(BBUpper,Color.Red);                    smaVolume = SMA.Series(bars.Volume, 21);          PlotIndicator(smaVolume, Color.Blue);          stopATRHigh = ATRTrail.Series(bars, 20, 2.00);          PlotTimeSeries(stopATRHigh, "ATR10", "Price", Color.Black);                 }              public override void Execute(BarHistory bars, int idx)       {          if (idx == bars.Count - 1)             return;          if (idx < 21) return;                    if ((bars.Close[idx - 1] < BBUpper[idx - 1]) &&              (bars.Close[idx] > BBUpper[idx])             && LastOpenPosition == null                && (bars.Volume[idx] > smaVolume[idx])                   && (bars.Open[idx + 1] > bars.High[idx]))             PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          else if (bars.Close.CrossesUnder(stopATRHigh, idx +1) && LastOpenPosition != null)             PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);       }       private TimeSeries stopATRHigh;       private BBUpper BBUpper;       private SMA smaVolume;    } }


0
815
5 Replies

Reply

Bookmark

Sort
Glitch8
 ( 10.62% )
- ago
#1
It's because of the rule you have where the next day's open has to be above the current day's high. That's not the case in the bar you pointed out.
0
- ago
#2
Also note that your strategy cannot generate alerts because of these idx+1 checks.
0
- ago
#3
Tks guys!

I had changed my code as below but I still missing something.
I'm trying to buy at the point detached on the picture below.

CODE:
public override void Execute(BarHistory bars, int idx)       {          if (idx == bars.Count - 1)             return;          if (idx < 21) return;          if (LastOpenPosition == null             && (bars.Close[idx - 1] < BBUpper[idx - 1])              && (bars.Close[idx] > BBUpper[idx])                   && (bars.Volume[idx] > smaVolume[idx])                      && (bars.High[idx + 1] > bars.Close[idx]))             PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.Close[idx]);          else if (bars.Close.CrossesUnder(stopATRHigh, idx +1) && LastOpenPosition != null)             PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);       }


0
- ago
#4
This implies having a stop order at yesterday's High rather the limit at Close.

CODE:
PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, bars.High[idx]);
0
Glitch8
 ( 10.62% )
- ago
#5
I think we're getting mixed up with the syntax. Let's do a reset, and just state the rules you'd like to use in plain English, I can then code up an example.

Your code here is problematic ...

CODE:
&& (bars.High[idx + 1] > bars.Close[idx]))


what this does is check the high price of TOMORROW and then uses this information to determine whether to place a trade TODAY. So, it's peeking into the future.
1

Reply

Bookmark

Sort