- ago
I created a simple moving average crossover. buy on 50 sma over 200 sma
Sell on 50 sma under 200 in Building Blocks.

I then dragged a building block "sell at stop loss" at 10%. I am looking to exit if the sma 50 under 200 OR stop loss is 10%. Can I do this in a building block? Meaning can I have an "OR" in the sell at market building block?

I see some signals in the position window exit at stop loss. But then I see alerts that exit and it does not look right.

Here is the screen shot of my Building Blocks.



Here is the code produced in the Open as C# Coded strategy.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript4 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() {          AddParameter("Period", ParameterType.Int32, 50, 5, 75, 1);          AddParameter("Period1", ParameterType.Int32, 200, 20, 300, 5); StartIndex = 200; } public override void Initialize(BarHistory bars) {          indicator = new SMA(bars.Close,Parameters[0].AsInt);          PlotIndicator(indicator,new WLColor(0,0,0));          smoother = new SMA(indicator,Parameters[1].AsInt);          smoother.ParentIndicator = indicator;          PlotIndicator(smoother,new WLColor(0,0,255));          indicator2 = new SMA(bars.Close,Parameters[0].AsInt);          smoother2 = new SMA(indicator2,Parameters[1].AsInt);          smoother2.ParentIndicator = indicator2;          trailing = false;          PlotStopsAndLimits(3); } public override void Execute(BarHistory bars, int idx) {          int index = idx;          bool condition0;             condition0 = false;             {                if (indicator.CrossesOver(smoother, index))                {                   condition0 = true;                }             }             if (condition0)             {                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");             }             condition0 = false;             {                if (indicator2.CrossesUnder(smoother2, index))                {                   condition0 = true;                }             }             if (condition0)             {             foreach(Position foundPosition0 in OpenPositions)             {                if (foundPosition0.PositionTag == 0)                {                   Backtester.CancelationCode = 227;                   ClosePosition(foundPosition0, OrderType.Market, 0, "Sell At Market (1)");                }             }             }             condition0 = false;             {                condition0 = true;             }             if (condition0)             {             foreach(Position foundPosition0 in OpenPositions)             {                if (foundPosition0.PositionTag == 0)                {                   Backtester.CancelationCode = 227;                   value = 1.0 - (10.00 / 100.0);                   ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice * value, "Sell at 10% stop loss");                }             }             } } public override void NewWFOInterval(BarHistory bars) {          indicator = new SMA(bars.Close,Parameters[0].AsInt);          smoother = new SMA(indicator,Parameters[1].AsInt);          smoother.ParentIndicator = indicator;          indicator2 = new SMA(bars.Close,Parameters[0].AsInt);          smoother2 = new SMA(indicator2,Parameters[1].AsInt);          smoother2.ParentIndicator = indicator2; }       private IndicatorBase indicator;       private IndicatorBase smoother;       private IndicatorBase indicator2;       private IndicatorBase smoother2;       private double value;       private bool trailing;       private Transaction _transaction; } }


Thank you,
Larry
0
198
Solved
6 Replies

Reply

Bookmark

Sort
Cone8
 ( 26.65% )
- ago
#1
It's correct. Both Sell conditions apply to the entry directly above. It's an implied "OR".
0
- ago
#2
Cone,

Here is my example:

QQQ entry signaled on 3/13/23 with entry price of 286.29. Profit % says 10.59%. Very nice. However, I get a signal to stop with a 10% stop loss. Why is that. I am not losing money?

Thank you,
Larry



Here is Open Positions showing QQQ with profit of 10.59%.



Here is signal with a stop on QQQ showing Order Price of 257.66 "Sell at 10% stop loss"













0
Glitch8
 ( 8.38% )
- ago
#3
The Signal is telling you that the Strategy has issued a Stop order SIGNAL that you need to place for the next bar for the currently open position.

The name of the Signal is "Stop Loss" because that's the name of the Building Block that produced the Signal.

It does not mean the position is losing money. However, if the price next bar declines and hits the stop loss level, then it would lose money.
0
Best Answer
- ago
#4
Glitch,

Got it.

Thank you,
Larry
0
Cone8
 ( 26.65% )
- ago
#5
Instead of using a "touch stop", i.e., a stop order that is active during the trading day (or bar interval), you could add another Sell At Market block with a Condition to sell after a Close with -10% loss (or more). Like this -

0
- ago
#6
Cone,
Thank you for the additional info.

I have not used stop loss before. However, my new strategy looks good for this. This seems to be a great way to easy some stress...

Also, placing with my broker a stop loss, is a nice bit of automation on this. So looks like I can reduce risk by a lot. However, I do understand, markets can gap down and also have sudden drops that may hit them.
0

Reply

Bookmark

Sort