- ago
I want to have a single position and if a sell or cover is generated than I want to be able to buy or short in the same bar if a signal is generated. The problem I find is the a buy or short must be the first activity in the Execute routine so I don't see how I can doit. Any suggestions.
0
673
4 Replies

Closed

Bookmark

Sort
- ago
#1
This is called Stop & Reverse (SAR) as far as I understand. Check out these topics for code samples:

https://www.wealth-lab.com/Discussion/Stop-and-Reverse-not-working-as-expected-6950
https://www.wealth-lab.com/Discussion/Reverse-into-short-position-at-market-close-6914

Hope this helps.
0
Glitch8
 ( 10.41% )
- ago
#2
Here is a minimal working example:

CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars) {          channelLow = EnvelopeLower.Series(bars.Low, 5, 0, EnvelopeMAType.EMA);          channelHigh = EnvelopeUpper.Series(bars.High, 5, 0, EnvelopeMAType.EMA);          PlotTimeSeriesLine(channelLow >> 1, "Channel Low", "Price", Color.Green, 1);          PlotTimeSeriesLine(channelHigh >> 1, "Channel High", "Price", Color.Red, 1); } //Execute public override void Execute(BarHistory bars, int idx) {          if (LastPosition?.PositionType == PositionType.Long)          {             PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, channelLow[idx]);             PlaceTrade(bars, TransactionType.Short, OrderType.Stop, channelLow[idx]);          }          else          {             PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, channelHigh[idx]);             PlaceTrade(bars, TransactionType.Cover, OrderType.Stop, channelHigh[idx]);          } }       //private members       private IndicatorBase channelLow;       private IndicatorBase channelHigh; } }


0
- ago
#3
Thanks for the examples. My criteria for cover is completely different than buy and I have like more than 20 different triggers for each. Maybe the restriction to have sell and cover routines execute first can be removed in the future
0
Glitch8
 ( 10.41% )
- ago
#4
There is no such restriction.
0

Closed

Bookmark

Sort