- ago
How do I know if a stop order has been triggered/executed for

1) Backtesting
2) Real trading?

and how to use it correctly?

Do I understand correctly that a stop order should be placed in the strategy at the same time as opening a position?
0
1,184
8 Replies

Reply

Bookmark

Sort
Cone8
 ( 7.72% )
- ago
#1
You don't have to place a stop on the entry bar, but if you do you should be pretty sure that the entry is above (long) the stop price so it doesn't immediately take you out of the position.

Here's an example. On an intraday basis, you can be pretty sure that the market entry after the crossover will be above the stop price that's 1% below the moving average. The PlotStopsAndLimits() call shows you dots where the stops (and limits, if any) are active.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          StartIndex = 20;          _ma = SMA.Series(bars.Close, StartIndex);          PlotIndicatorLine(_ma);          PlotStopsAndLimits(); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) {             if (bars.Close.CrossesOver(_ma, idx))             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, _ma[idx] * _pct, "entryBar");             } } else {             double stop = _ma[idx] * 0.98;             PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, _ma[idx] * _pct); } }       //declare private variables below       SMA _ma;       double _pct = 0.99; } }
0
- ago
#2
Hi. I have another issue with `OrderType.Stop` that I don't understand. I have an entry condition:

CODE:
public override void Execute(BarHistory bars, int idx) { var hasLongOpenPosition = HasOpenPosition(bars, PositionType.Long); if (!hasLongOpenPosition && dt.Hour is >= 8 and < 13 && bars. High[idx] > _upperChannel[idx]) { var trade = PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, _upperChannel[idx]); }


WL enters sometimes. I don't understand when doesn't. Consider a bar:


`High` hits upper channel value, no opened positions, time 9am. I expect it to enter at price of upper channel. (I need WL to enter not the next bar, but the same bar at 9am, since I'm 100% sure I'll enter at almost same price at live-mode). Why WL doesn't enter the position?
0
- ago
#3
This is an example when WL8 enters after second `PlaceTrade`:

0
- ago
#4
Orders are entered for the next bar in general. A stop will be placed on the next bar. Note that bars.High[idx] is unknown until the bar completes.
0
Cone8
 ( 7.72% )
- ago
#5
Tip:
To see when stop and limit orders are active, add this statement to your strategy in the Initialize() section. It will add a dot on the bar a stop and/or limit order is active.
0
- ago
#6
QUOTE:

Note that bars. High[idx] is unknown until the bar completes.


Sorry, not following. Is `bars. Hight[idx]` is null, NaN when `this. Execute` is called?

QUOTE:

Orders are entered for the next bar in general. A stop will be placed on the next bar.


Is it same with block strategies too? I think I saw that blocks are entering same day with signal, or am I wrong?
0
- ago
#7
https://www.wealth-lab.com/blog/anatomy-of-a-wl7-strategy
0
Cone8
 ( 7.72% )
- ago
#8
QUOTE:
Is it same with block strategies too?
Yes.

QUOTE:
I think I saw that blocks are entering same day with signal, or am I wrong?
Yes, you're wrong.
There is, however, "Same bar exits" that can be enabled. If you open the Help (F1) and search "same bar", you'll see a few topics that discuss how that works. To code it, see AutoProfitTargetPrice and AutoStopLossPrice In the QuickRef,
2

Reply

Bookmark

Sort