mrsic8
 ( 16.79% )
- ago
Hello,

I can't see the error. Maybe anybody can help me. I am getting no positions!
Thanks in advance.
CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace StrategiesCollection {    public class Template : UserStrategyBase    {       public Template() : base()       {          AddParameter("PeriodFast", ParameterTypes.Int32, 25, 0, 100, 5);          AddParameter("PeriodSlow", ParameterTypes.Int32, 100, 0, 200, 5);       }       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {          _emaFast = EMA.Series(bars.Close, Parameters[0].AsInt);          PlotIndicator(_emaFast, Color.Blue, PlotStyles.DashedLine, true, "emaFast");          _emaSlow = EMA.Series(bars.Close, Parameters[1].AsInt);          PlotIndicator(_emaSlow, Color.Crimson, PlotStyles.DottedLine, true, "emaSlow");          StartIndex = 50;       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {          _enterLong = _emaFast.CrossesOver(_emaSlow, idx);          _enterShort = _emaSlow.CrossesUnder(_emaFast, idx);          _exitLong = bars.Close[idx] < _emaFast[idx];          _exitShort = bars.Close[idx] > _emaSlow[idx];          if (!HasOpenPosition(bars, PositionType.Long))          {             if (_enterLong)             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             }             if (!HasOpenPosition(bars, PositionType.Short))                if (_enterShort)                {                   PlaceTrade(bars, TransactionType.Short, OrderType.Market);                }                else                {                   var isLastPositionActive = HasOpenPosition(bars, PositionType.Long) || HasOpenPosition(bars, PositionType.Short);                   if (isLastPositionActive)                   {                      Position p = LastPosition;                      if (p.PositionType == PositionType.Long)                      {                         if (_exitLong)                         {                            ClosePosition(p, OrderType.Market);                         }                         else                         {                            if (p.PositionType == PositionType.Short)                            {                               if (_exitShort)                               {                                  ClosePosition(p, OrderType.Market);                               }                            }                         }                      }                   }                }          }       }       //declare private variables below       private bool _enterLong;       private bool _enterShort;       private bool _exitShort;       private bool _exitLong;       private IndicatorBase _emaFast;       private IndicatorBase _emaSlow;    } }


Kind regard
0
628
Solved
2 Replies

Closed

Bookmark

Sort
- ago
#1
Hello,

I'd refactor the trading logic like this (untested):
CODE:
... var isLastPositionActive = HasOpenPosition(bars, PositionType.Long) || HasOpenPosition(bars, PositionType.Short); if (isLastPositionActive) { Position p = LastPosition; if( (_exitLong && p.PositionType == PositionType.Long) || (_exitShort && p.PositionType == PositionType.Short) ) ClosePosition(p, OrderType.Market); } else {    if (!HasOpenPosition(bars, PositionType.Long))       if (_enterLong)          PlaceTrade(bars, TransactionType.Buy, OrderType.Market);    if (!HasOpenPosition(bars, PositionType.Short))       if (_enterShort)          PlaceTrade(bars, TransactionType.Short, OrderType.Market); }
0
Best Answer
mrsic8
 ( 16.79% )
- ago
#2
Thanks Eugene.
The code is now a little bit shorter.
0

Closed

Bookmark

Sort