Stop/Limit Orders

Modified on 2015/10/24 21:33 by Eugene — Categorized as: Community Components

Syntax

public static Position BuyAtStopLimit(this WealthScript ws, int bar, double price) public static Position BuyAtStopLimit(this WealthScript ws, int bar, double price, string signalName) public static Position ShortAtStopLimit(this WealthScript ws, int bar, double price) public static Position ShortAtStopLimit(this WealthScript ws, int bar, double price, string signalName) public static bool SellAtStopLimit(this WealthScript ws, int bar, Position p, double price) public static bool SellAtStopLimit(this WealthScript ws, int bar, Position p, double price, string signalName) public static bool CoverAtStopLimit(this WealthScript ws, int bar, Position p, double price) public static bool CoverAtStopLimit(this WealthScript ws, int bar, Position p, double price, string signalName)

public Position BuyAtStopLimit(int bar, double price) public Position BuyAtStopLimit(int bar, double price, string signalName) public Position ShortAtStopLimit(int bar, double price) public Position ShortAtStopLimit(int bar, double price, string signalName) public bool SellAtStopLimit(int bar, Position p, double price) public bool SellAtStopLimit(int bar, Position p, double price, string signalName) public bool CoverAtStopLimit(int bar, Position p, double price) public bool CoverAtStopLimit(int bar, Position p, double price, string signalName)


Parameter Description

barBar
priceLimit price
signalName(optional) Entry/exit signal name

Description

This is a set of four overloaded functions that implement Stop/Limit order types. Original WL4 code created by Dion Kurczek.

Warning: all the methods can be used for backtesting only. They are N/A for real trading and will trigger an index out of bounds exception when attempted to use for Alert generation.

Example

Example using C# extension methods:

using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;

namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { for(int bar = GetTradingLoopStartBar(40); bar < Bars.Count; bar++) { if( bar < Bars.Count-1 ) // for backtesting only { if (IsLastPositionActive) { if( !this.SellAtStopLimit( bar+1, LastPosition, Lowest.Series(Low,40)bar ) ) this.CoverAtStopLimit( bar+1, LastPosition, Highest.Series(High,40)bar ); } else { if( this.BuyAtStopLimit( bar+1, Highest.Series(High,40)bar ) == null ) this.ShortAtStopLimit( bar+1, Lowest.Series(Low,40)bar ); } } } } } }


Legacy syntax example:

using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components; // Stop/Limit orders here /*** Requires installation of Community.Components Extension from www.wealth-lab.com > Extensions ***/

namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { PositionHelper ps = new PositionHelper( this ); // Pass WealthScript for(int bar = GetTradingLoopStartBar(40); bar < Bars.Count; bar++) { if( bar < Bars.Count-1 ) // for backtesting only { if (IsLastPositionActive) { if( !ps.SellAtStopLimit( bar+1, LastPosition, Lowest.Series(Low,40)bar ) ) ps.CoverAtStopLimit( bar+1, LastPosition, Highest.Series(High,40)bar ); } else { if( ps.BuyAtStopLimit( bar+1, Highest.Series(High,40)bar ) == null ) ps.ShortAtStopLimit( bar+1, Lowest.Series(Low,40)bar ); } } } } } }