Syntax
public enum AutoStopMode { AsPercent, AsPoint };
public virtual void ApplyAutoStops(int Bar)
public virtual void SetAutoStopMode(AutoStopMode mode)
public virtual void InstallTimeBasedExit(int Bars)
public virtual void InstallStopLoss(double StopLevel)
public virtual void InstallTrailingStop(double Trigger, double StopLevel)
public virtual void InstallReverseBreakEvenStop(double LossLevel)
public virtual void InstallBreakEvenStop(double Trigger)
public virtual void InstallProfitTarget(double TargetLevel)
public virtual void OnProcessingComplete()
Parameter Description
mode | Select AsPercent (by default) or AsPoint |
Bar | Bar number to apply AutoStops |
Parameters like "Bars", "Trigger" etc. are typical and should be used like you do with Wealth-Lab's built-in exits. See "Using Automated Exits (AutoStops) in WealthScript" below for more information.
Description
Automated stops aka AutoStops had always existed in legacy Wealth-Lab versions, but were purposefully removed from Version 6 due to possible peaking when incorrectly mixing them with manual stops. These methods can help you program various types of automated exits in your trading systems and reduce the size of your code.
The "AutoStops" for Version 6 were created by Dave Aronow.
Note: you must call
PositionHelper.OnProcessingComplete() at the end of your Strategy as shown on examples below. Without it, running several Strategies that employ AutoStops with different settings will result in totally wrong trades. Update Community Components to v2017.05 or higher.
Using Automated Exits (AutoStops) in WealthScript
The WealthScript language contains a number of functions that can help you program various types of automated exits in your trading systems. All of these exits could also be programmed manually, however the automated exits can be used as a way to reduce the size of your code. If you wish to implement exit rules that go beyond what the automated exits offer, you can code these manually using WealthScript Trading System functions.
Stop LossUse the InstallStopLoss function to install a stop loss in your system. You specify the stop loss value as a percentage. For example, InstallStopLoss( 5 ) will install a 5% automated stop loss. The Stop Loss is a simulated stop order. If prices hit your stop loss level during the trading day your position is closed at the stop loss price. If prices open below the stop loss level, your position is closed at market open.
Profit Target
Use the InstallProfitTarget function to install a profit target exit. The target level is expressed as a percentage. For example, InstallProfitTarget( 13.5 ) will install a 13.5% profit target into your system. If prices hit your target level during the trading day your position is closed at the limit price. If prices open above the target price, the position is closed at market open.
Breakeven StopUse the InstallBreakevenStop function to implement a breakeven stop in your system. A breakeven stop works by establishing a profit trigger value. As soon as the position closes at or above the trigger profit, the breakeven stop is put into place. The stop order is placed at the position entry price. For example, InstallBreakevenStop( 2 ) will cause the breakeven stop order to be placed as soon as the trade closes with at least 2 percent profit. If prices hit the entry price during the trading day the position is closed at the breakeven level (minus Commission and Slippage, if any). If prices open below the entry price, the position is closed at market order.
Reverse Breakeven Stop
Use the InstallReverseBreakevenStop function to install a reverse breakeven stop in your system. You specify a percentage loss level that will trigger the breakeven stop. If the position closes at or below the specified loss level, the breakeven exit (implemented as a limit order) is put into place. For example, InstallReverseBreakevenStop( 3.3 ) will cause the breakeven stop to be placed as soon as the trade closes with a loss of 3.3% or more. If prices rebound back to the entry level, the positions will be closed at the breakeven level. If prices open above the entry level, the position is closed at market open.
Trailing StopUse the InstallTrailingStop function to install an automated trailing stop into your system. This function takes two parameters. The first specifies the percentage profit required to first trigger the trailing stop. The second parameter specifies how much of the current profit you wish to protect by the stop (expressed as a percentage 1 to 100). For example, InstallTrailingStop( 10, 40 ) will trigger a trailing stop once the position closes with a percentage profit of 10% or higher. The trailing stop is initially placed to protect 40% of the maximum profit of the trade. If the highest profit so far is $4.00, the stop order is initially placed at $1.60 below the high of the bar. As prices increase, the stop order is automatically raised to protect 40% of the highest profit to date. The trailing stop is never lowered, even if prices move in the opposite direction.
Time-Based ExitsUse InstallTimeBasedExit if you want your system to exit positions after a certain number of bars, regardless of other conditions. For example, InstallTimeBasedExit( 10 ) will cause a position to be closed 10 bars (trading days) after entry.
Applying the Automated StopsYou should install the various automated exits at the beginning of your script, outside of the main trading system loop. In order to execute the stops you must call the ApplyAutoStops function within the trading system loop. ApplyAutoStops takes as a parameter the bar number for which the stops should be processed.
Note: you must call
PositionHelper.OnProcessingComplete() at the end of your Strategy as shown on examples below. Without it, running several Strategies that employ AutoStops with different settings will result in totally wrong trades. Update Community Components to v2017.05 or higher.
The example script below uses the various automated exits to exit positions. Use this script to help visualize how the stops behave.
Automated Stops and Bar of EntryWealth-Lab assumes you are running scans at the end of the trading day for orders that need to be placed the following day. Because of this, automated stops do not execute on the bar for which a position was entered, even if you call ApplyAutoStops on that bar. Another reason that automated stops do not execute on the entry bar is because it cannot always be determined by the open, high, low, and close values if the stop would have executed before or after the position entry.
Order of Automated Stop ProcessingAutomated exits are processed in a
pessimistic order. Time-based exits are processed first, followed by stop losses, breakeven stops, and lastly profit targets. (For more details, see Programming Trading Strategies > Peeking >
Order of Trading Signals in the WealthScript Programming Guide (Help menu).
Example
The following is a sample strategy implementing
AutoStops in C# similar to the AutoStops functionality in WL4. This includes the six "
Installxxx" methods as well as the
ApplyAutoStops,
SetAutoStopMode and
OnProcessingComplete methods.
Example using C# extension methods:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community.Components; // < AutoStops here in PositionEx static class
/*** Requires installation of Community.Components Extension from www.wealth-lab.com > Extensions ***/
namespace WealthLab.Strategies
{
public class AutoStopsDemo : WealthScript
{
protected override void Execute()
{
// Select AutoStop mode: AsPercent or AsPoint
AutoStopMode AsPercent = AutoStopMode.AsPercent;
//sample calls to auto stop methods
PositionEx.SetAutoStopMode(AsPercent); // choose AutoStopMode
PositionEx.InstallTimeBasedExit(30);
PositionEx.InstallStopLoss(5);
PositionEx.InstallProfitTarget(10);
PositionEx.InstallBreakEvenStop(5);
PositionEx.InstallReverseBreakEvenStop(3);
PositionEx.InstallTrailingStop(6, (100 - 70));
for(int bar = 20; bar < Bars.Count; bar++)
{
// Apply auto stops
this.ApplyAutoStops(bar);
if (!IsLastPositionActive)
{
BuyAtMarket(bar + 1);
}
}
PositionEx.OnProcessingComplete();
}
}
}
Legacy syntax example:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using Community.Components; // < AutoStops here
/*** Requires installation of Community.Components Extension from www.wealth-lab.com > Extensions ***/
namespace WealthLab.Strategies
{
public class AutoStopsDemo : WealthScript
{
protected override void Execute()
{
// Create an instance of PositionHelper (class containing AutoStops),
// passing a WealthScript object as "this"
PositionHelper a = new PositionHelper(this);
// Select AutoStop mode: AsPercent or AsPoint
PositionHelper.AutoStopMode AsPercent = Community.Components.
PositionHelper.AutoStopMode.AsPercent;
//sample calls to auto stop methods
a.SetAutoStopMode(AsPercent); // choose AutoStopMode
a.InstallTimeBasedExit(30);
a.InstallStopLoss(5);
a.InstallProfitTarget(10);
a.InstallBreakEvenStop(5);
a.InstallReverseBreakEvenStop(3);
a.InstallTrailingStop(6, (100 - 70));
for(int Bar = 20; Bar < Bars.Count; Bar++)
{
// Apply auto stops
a.ApplyAutoStops(Bar);
if (!IsLastPositionActive)
{
BuyAtMarket(Bar + 1);
}
}
a.OnProcessingComplete();
}
}
}