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?
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?
Rename
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.
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; } }
Your Response
Post
Edit Post
Login is required