- ago
Hello!

We can’t seem to get the stop-and-reverse logic in our strategy to function the way we want. We use Opus (claude.ai) for help with the C# coding, but he hasn’t been able to figure it out.

Is it possible in WL8 to exit and enter a position on the same bar? We’d really appreciate any guidance you can provide. We weren’t able to find a post about this, so please feel free to point us to one if it has already been discussed.

Thank you!
0
47
6 Replies

Reply

Bookmark

Sort
- ago
#1
Yes, I can provide an example when I’m back but in short you just have to issue two PlaceTrades, one Sell and one Short for example, in the same code block.
0
Cone8
- ago
#3
Here's a template for a simple SAR strategy.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript6 { public class MyStrategy : UserStrategyBase { public override void Initialize(BarHistory bars) {          _ma1 = SMA.Series(bars.Close, 20);          _ma2 = SMA.Series(bars.Close, 50);          PlotIndicator(_ma1, WLColor.Blue);          PlotIndicator(_ma2, WLColor.Black);       } public override void Execute(BarHistory bars, int idx) {          double trigger = bars.Close[idx] - 0.35 * (bars.High[idx] - bars.Low[idx]);          bool enterlong = _ma1.CrossesOver(_ma2, idx);          bool exitlong = _ma1.CrossesUnder(_ma2, idx);           // Alternatively          //         bool enterlong = _ma1[idx] > _ma2[idx]; //         bool exitlong = _ma1[idx] < _ma2[idx];          SAR(bars, enterlong, exitlong, OrderType.Stop, trigger); }       //copy and paste this "as is"       public void SAR(BarHistory bars, bool entryLong, bool exitLong, OrderType orderType, double price = 0)       {          //start a position          if (OpenPositions.Count == 0)          {             if (entryLong)             {                if (price == 0)                                  PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                               else                   PlaceTrade(bars, TransactionType.Buy, orderType, price);             }             else if (exitLong)             {                if (price == 0)                   PlaceTrade(bars, TransactionType.Short, OrderType.Market);                else                   PlaceTrade(bars, TransactionType.Short, orderType, price);             }          }          else if (HasOpenPosition(bars, PositionType.Long))          {             if (exitLong)             {                if (price == 0)                {                   PlaceTrade(bars, TransactionType.Sell, OrderType.Market);                   PlaceTrade(bars, TransactionType.Short, OrderType.Market);                }                else                {                   PlaceTrade(bars, TransactionType.Sell, orderType, price);                   PlaceTrade(bars, TransactionType.Short, orderType, price);                }             }          }          else          {             if (entryLong)             {                if (price == 0)                {                   PlaceTrade(bars, TransactionType.Cover, OrderType.Market);                   PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                }                else                {                   PlaceTrade(bars, TransactionType.Cover, orderType, price);                   PlaceTrade(bars, TransactionType.Buy, orderType, price);                }             }          }       }       SMA _ma1;       SMA _ma2; } }
1
- ago
#4
Great — that did the trick! Thank you for your help.
0
- ago
#5
QUOTE:
Is it possible in WL8 to exit and enter a position on the same bar?

It's possible, but with great confusion. You need to peek ahead at the "future partial bar" to understand what's going on presently, and "peeking" kind of violates the WL paradigm. I would avoid doing that.

What you can do is employ a finer time scale. So instead of using Daily bars, use 30 minute bars instead. And your resulting code will likely be better behaved.
2
Cone8
- ago
#6
We're discussing SAR enter/exit - Stop And Reverse positions here.
No peeking required. Just see the template above.
0

Reply

Bookmark

Sort