To visualize trades on the chart, something like "PositionHelper.DrawTradeLines(this,false);"
would be desirable, as it was in WL6 Community Components
would be desirable, as it was in WL6 Community Components
Rename
Here you go. Paste the DrawTradeLines routine to your Strategy code or a compiled DLL (which targets .NET Core) and call from BacktestComplete(). Team will discuss if this feature is worth including in our open source WealthLab.Community library.
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using WealthLab.ChartWPF; using System.Drawing; using System.Collections.Generic; namespace WealthScript4 { 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) { hh = new Highest(bars.High, 33); ll = new Lowest(bars.Low, 33); PlotIndicator(hh, Color.Green); PlotIndicator(ll, Color.Red); } //a basic stop and reverse system public override void Execute(BarHistory bars, int idx) { PlaceTrade(bars, TransactionType.Cover, OrderType.Stop, hh[idx]); PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, ll[idx]); if (!HasOpenPosition(bars, PositionType.Long)) PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, hh[idx]); if (!HasOpenPosition(bars, PositionType.Short)) PlaceTrade(bars, TransactionType.Short, OrderType.Stop, ll[idx]); } //run after Execute has finished public override void BacktestComplete() { DrawTradeLines(GetPositions(), false); } public void DrawTradeLines(List<Position> lst, bool showSignal = false) { for (int iPos = 0; iPos < lst.Count; iPos++) { Position position = lst[iPos]; int positionExitBar; double positionExitPrice; if (position.IsOpen) { positionExitBar = position.Bars.Count - 1; positionExitPrice = position.Bars.Close[positionExitBar]; } else { positionExitBar = position.ExitBar; positionExitPrice = position.ExitPrice; } Color col; if (position.PositionType == PositionType.Long) col = (positionExitPrice - position.EntryPrice) > 0 ? Color.Green : Color.Red; else col = (positionExitPrice - position.EntryPrice) > 0 ? Color.Red : Color.Green; DrawLine(position.EntryBar, position.EntryPrice, positionExitBar, positionExitPrice, col); if (showSignal) { DrawBarAnnotation(position.EntrySignalName, position.EntryBar, position.PositionType == PositionType.Long, Color.Black, 12); DrawBarAnnotation(position.ExitSignalName, position.ExitBar, position.PositionType == PositionType.Short, Color.Black, 12); } } } //declare private variables below IndicatorBase hh; IndicatorBase ll; } }
Thank you so much!
This works in the case of “Single Symbol”, but not with “Portfolio Backtest”.
For single symbols and for portfolios call it from Cleanup() like this...
CODE:
public override void Cleanup(BarHistory bars) { DrawTradeLines(GetPositions(), false); }
Your Response
Post
Edit Post
Login is required