- ago
There's an error in the logic of this code which I have been trying ages to resolve but I just cant see it.
Sorry for the noob question but please can someone help me understand what I am doing wrong.
The strategy as it stands places one short order right at the beginning but doesn't place any further to that even though the two indicator lines are crossing
Thanks in advance for your help.

0
427
Solved
8 Replies

Reply

Bookmark

Sort
- ago
#1
Topic title edited.

WAS: Coding error
IS: Strategy fails to submit next orders after first

Hope it looks more explicative now. Now can you copy and paste code to the forum so that we don't have to type it in?
0
- ago
#2
thanks Eugene, here's the (slightly updated) code.

CODE:
public override void Initialize(BarHistory bars) {          indicator1 = new MACD(bars.Close,Parameters[0].AsInt,Parameters[1].AsInt);          PlotIndicator(indicator1,new WLColor(0,0,0));          indicator1.MassageColors = true;          indicator2 = new WMA(new MACD(bars.Close,Parameters[0].AsInt,Parameters[1].AsInt),50);          PlotIndicator(indicator2,new WLColor(0,0,255));          indicator2.MassageColors = true;          indicator3 = new RSI(bars.Close, 14);          PlotIndicator(indicator3, new WLColor(0, 0,0));          indicator3.MassageColors = true;          indicator4 = new SMA(new RSI(bars.Close, 20), 50);          PlotIndicator(indicator4, new WLColor(0, 0,255));             indicator4.MassageColors = true;          indicator12 = new MACD(bars.Close,Parameters[0].AsInt,Parameters[1].AsInt);          indicator22 = new WMA(new MACD(bars.Close,Parameters[0].AsInt,Parameters[1].AsInt),50);          StartIndex = 50; }       public override void Execute(BarHistory bars, int idx)       {          int index = idx;          if (FindOpenPosition(PositionType.Long) == null && (indicator1.CrossesOver(indicator2, index)))             _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");          else          if (FindOpenPosition(PositionType.Short) == null && (indicator12.CrossesUnder(indicator22, index)))             _transaction = PlaceTrade(bars, TransactionType.Short, OrderType.Market, 0, 0, "Short At Market (2)");       }
0
- ago
#3
Thanks. Your strategy is not programmed to sell (or cover - if short comes first) any positions. It will open just one position and hold it forever. You have to add some exits to it.
0
Best Answer
- ago
#4
Did you intend to create a stop-and-reverse strategy instead? If so, you'll find some helpful code samples in these topics:

https://www.wealth-lab.com/Discussion/Reverse-into-short-position-at-market-close-6914
https://www.wealth-lab.com/Discussion/Stop-and-Reverse-not-working-as-expected-6950
https://www.wealth-lab.com/Discussion/Buy-Short-same-bar-with-single-position-7102
https://www.wealth-lab.com/Discussion/Stop-and-Reverse-from-EasyLanguage-to-WL7-7698
0
Cone8
 ( 24.57% )
- ago
#5
Here's your SAR freebie - note that the way it's programmed, the backtest will always start with a Long Position first. So, if a crossunder occurs before a crossover at the beginning of the test period, that will be ignored until a long position is created first.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 { public class SAR7891 : UserStrategyBase {       public SAR7891()       {          AddParameter("MACD Period1", ParameterType.Int32, 12, 10, 18, 2);          AddParameter("MACD Period2", ParameterType.Int32, 26, 20, 30, 2);       }       public override void Initialize(BarHistory bars)       {          _macd = new MACD(bars.Close, Parameters[0].AsInt, Parameters[1].AsInt);          PlotIndicator(_macd, new WLColor(0, 0, 0));          _macd.MassageColors = true;          _wmaMacd = new WMA(new MACD(bars.Close, Parameters[0].AsInt, Parameters[1].AsInt), 50);          PlotIndicator(_wmaMacd, new WLColor(0, 0, 255));          _wmaMacd.MassageColors = true;          _rsi = new RSI(bars.Close, 14);          PlotIndicator(_rsi, new WLColor(0, 0, 0));          _rsi.MassageColors = true;          _smarsi = new SMA(new RSI(bars.Close, 20), 50);          PlotIndicator(_smarsi, new WLColor(0, 0, 255));          _smarsi.MassageColors = true;          StartIndex = 50;       }       public override void Execute(BarHistory bars, int idx)       {                   if (LastPosition?.PositionType == PositionType.Short)          {             if (_macd.CrossesUnder(_wmaMacd, idx))             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                PlaceTrade(bars, TransactionType.Cover, OrderType.Market);             }          }          else                   {             if (_macd.CrossesOver(_wmaMacd, idx))             {                PlaceTrade(bars, TransactionType.Sell, OrderType.Market);                PlaceTrade(bars, TransactionType.Short, OrderType.Market);             }          }                }       MACD _macd;       WMA _wmaMacd;       RSI _rsi;       SMA _smarsi;       Transaction _transaction; } }
1
- ago
#6
Updated code still doesn't work and I still cant see why... Can you help me please? With this code the first trade is short, followed by a cover and buy however it is still missing signals imbetween.

CODE:
public override void Initialize(BarHistory bars) {          macd1 = new MACD(bars.Close,Parameters[0].AsInt,Parameters[1].AsInt);          PlotIndicator(macd1,new WLColor(0,0,0));          macd1.MassageColors = true;          macd_trigger = new WMA(new MACD(bars.Close,Parameters[0].AsInt,Parameters[1].AsInt),50);          PlotIndicator(macd_trigger,new WLColor(0,0,255));          macd_trigger.MassageColors = true;          rsi1 = new RSI(bars.Close, 14);          PlotIndicator(rsi1, new WLColor(0, 0,0));          rsi1.MassageColors = true;          rsi_trigger = new SMA(new RSI(bars.Close, 20), 50);          PlotIndicator(rsi_trigger, new WLColor(0, 0,255));             rsi_trigger.MassageColors = true;          macd12 = new MACD(bars.Close,Parameters[0].AsInt,Parameters[1].AsInt);          macd_trigger2 = new WMA(new MACD(bars.Close,Parameters[0].AsInt,Parameters[1].AsInt),50);          StartIndex = 50; }       public override void Execute(BarHistory bars, int idx)       {          int index = idx;          //first time through          if (FindOpenPosition(PositionType.Long) == null && (macd1.CrossesOver(macd_trigger, index)))             PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          else          if (FindOpenPosition(PositionType.Short) == null && (macd12.CrossesUnder(macd_trigger2, index)))             PlaceTrade(bars, TransactionType.Short, OrderType.Market);          //subsequent times after the first trade          else          //if (LastPosition?.PositionType == PositionType.Short)          if (FindOpenPosition(PositionType.Short) != null)          {             if (macd1.CrossesOver(macd_trigger, index))             {                PlaceTrade(bars, TransactionType.Cover, OrderType.Market);                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);             }          }          else          //if (LastPosition?.PositionType == PositionType.Long)          if (FindOpenPosition(PositionType.Long) != null)          {             if (macd12.CrossesUnder(macd_trigger2, index))             {                PlaceTrade(bars, TransactionType.Sell, OrderType.Market);                PlaceTrade(bars, TransactionType.Short, OrderType.Market);             }          }       }


0
- ago
#7
But @Cone's code works and does just what's required?
0
- ago
#8
thanks @Eugene and @Cone, its working now.
1

Reply

Bookmark

Sort