- ago
Close greater than Open not working

There should be no entry here, since the Open is greater than the Close





0
347
Solved
4 Replies

Reply

Bookmark

Sort
Cone8
 ( -0.70% )
- ago
#2
Sorry about this.
I found a bug in the code "N bars ago". Until it's fixed you need to put any rule that uses it last. Just switch the order of your conditions - put Current candle is a Bullish Hammer (1 bar ago) after the Close is greater than Open.
1
- ago
#3
I just validated this as a problem. The code below, generated from the building block strategy, is commented with the issue. I simplified the sell blocks, though...

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.Candlesticks; namespace WealthScript6 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() { } public override void Initialize(BarHistory bars) {          cp = CandleGeneDecoder.FindPattern("Bullish Hammer");          indicator1 = bars.Close;          indicator2 = bars.Open;          PlotStopsAndLimits(3);          trailing = false;          PlotStopsAndLimits(3);          StartIndex = 0; } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                // Save the index - this is currently the incoming idx value (current bar)                savedIndex = index;                flag = false;                                // Decrement the index so we can check for bullish hamsmer from one bar ago                index = index - 1;                if (index >= 0)                {                   if (CandleGeneDecoder.DetectPattern(bars, index, cp))                      flag = true;                }                                // Restore idx to the saved index which is unnecessary here.                // Or, perhaps index should have been set to savedIndex instead                // of setting idx to savedIndex (in which case you'd have to use                // idx instead of index in the bar offset references inside                // the if (flag)).                idx = savedIndex;                if (flag)                {                   // Check for close greater then the open.                   // This should be for the current bar.                   // But, index is being used which is still one bar back...                   if (index - 0 >= 0 && indicator1[index] > indicator2[index - 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)             {                Backtester.CancelationCode = 141;                value = (1.00 / 100.0) + 1.0;                ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * value, "Sell at 1% profit target");             }             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                Backtester.CancelationCode = 141;                value2 = 1.0 - (1.00 / 100.0);                ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice * value2, "Sell at 1% stop loss");             }          } } public override void NewWFOInterval(BarHistory bars) {          indicator1 = bars.Close;          indicator2 = bars.Open; }       private CandleGenePattern cp;       private int savedIndex;       private bool flag;       private TimeSeries indicator1;       private TimeSeries indicator2;       private double value;       private double value2;       private bool trailing;       private TimeSeries stops;       private Transaction _transaction; } }
0
Cone8
 ( -0.70% )
- ago
#4
Precisely. In short, the statement:
idx = savedIndex;

should be (and should have been):
index = savedIndex;

Already fixed for Build 18.
0
Best Answer

Reply

Bookmark

Sort