- ago
Hello,

I'm testing an strategy using the C# Editor, however, since I'm not proficient in this language, could you guys help me find what's missing in the code?

My strategy basically looks for price closing above the SMA high to buy at market close and closing below the SMA low to sell short at market close.

At the same time, it needs to close the previous position. I tested using the building blocks but couldn't achieve this result.

Here's the code:

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript10 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          AddParameter("Period", ParameterTypes.Int32, 20, 3, 100, 1); } public override void Initialize(BarHistory bars) {          src = bars.Close;          smaHigh = new SMA(bars.High,Parameters[0].AsInt);          PlotIndicator(smaHigh,Color.FromArgb(255,255,0,0));          smaLow = new SMA(bars.Low,Parameters[0].AsInt);          PlotIndicator(smaLow,Color.FromArgb(255,0,128,0));          StartIndex = Parameters[0].AsInt; } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool buyCondition;                    if (foundPosition0 == null)          {             buyCondition = false;             {                if (src.CrossesOver(smaHigh, index))                {                   buyCondition = true;                }                            }             if (buyCondition)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose, 0, 0, "Buy At Market (1)");             }                   }          else          {             {                if (src.CrossesUnder(smaLow, index))                {                   ClosePosition(foundPosition0, OrderType.MarketClose, 0, "Sell At Market (1)");                }             }          }          Position foundPosition1 = FindOpenPosition(1);          bool sellCondition;          if (foundPosition1 == null)          {             sellCondition = false;             {                if (src.CrossesUnder(smaLow, index))                {                   sellCondition = true;                }             }             if (sellCondition)             {                _transaction = PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose, 0, 1, "Sell Short At Market (2)");             }          }          else          {             {                if (src.CrossesOver(smaHigh, index))                {                   ClosePosition(foundPosition1, OrderType.MarketClose, 0, "Buy to Cover At Market (2)");                }             }          }    }       private TimeSeries src;       private IndicatorBase smaHigh;       private IndicatorBase smaLow;       private Transaction _transaction; } }

0
966
Solved
5 Replies

Reply

Bookmark

Sort
- ago
#1
Hello,

Next time could you make your topic title summarize the real question e.g. "Entry/Exit Points" (was TOO generic) => "Reverse into short position at market close". Thanks.
0
- ago
#2
I need to reverse the position on all scenarios (bought or sold) when condition is met.
Thanks
0
- ago
#3
Here's a Stop and Reverse (SAR) strategy for MarketClose orders. Check if I understood your rules correctly and let me know if something goes wrong.

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript1 {    public class Reverse : UserStrategyBase    {       public Reverse() : base()       {          AddParameter("Period", ParameterTypes.Int32, 20, 3, 100, 1);       }              //Initialize       public override void Initialize(BarHistory bars)       {          src = bars.Close;          StartIndex = Parameters[0].AsInt;          smaHigh = SMA.Series(bars.High, StartIndex);          smaLow = SMA.Series(bars.Low, StartIndex);          PlotIndicator(smaHigh);          PlotIndicator(smaLow, 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;          //first trade          if (GetPositions().Count == 0)          {             if (FindOpenPosition(PositionType.Long) == null)             {                if (src.CrossesOver(smaHigh, idx + 1))                   PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose);             }             else             if (FindOpenPosition(PositionType.Short) == null)             {                if (src.CrossesUnder(smaLow, idx + 1))                   PlaceTrade(bars, TransactionType.Short, OrderType.MarketClose);             }          }          else          {             //look at tomorrow's crossovers to place orders at close on that bar             if (LastPosition != null)             {                if (FindOpenPosition(PositionType.Long) != null)                {                   if (src.CrossesUnder(smaLow, idx + 1))                   {                      PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose);                      PlaceTrade(bars, TransactionType.Short, OrderType.MarketClose);                   }                }                else                   if (FindOpenPosition(PositionType.Short) != null)                {                   if (src.CrossesOver(smaHigh, idx + 1))                   {                      PlaceTrade(bars, TransactionType.Cover, OrderType.MarketClose);                      PlaceTrade(bars, TransactionType.Buy, OrderType.MarketClose);                   }                }             }          }                            }       //private members       private TimeSeries src;       private SMA smaHigh;       private SMA smaLow;    } }
0
Best Answer
- ago
#4
Perfect! It works..

I just had to change the lines:

smaHigh = SMA.Series(bars.Low, StartIndex);
smaLow = SMA.Series(bars.High, StartIndex);

to

smaHigh = SMA.Series(bars.High, StartIndex);
smaLow = SMA.Series(bars.Low, StartIndex);

In case you wanna edit it for future reference.

Thanks Eugene!
1
- ago
#5
You're welcome Sergio. Nice catch. Edited the code.
0

Reply

Bookmark

Sort