Hi,
For entry price:
Position pos in OpenPositions
pos.EntryPrice
For a position that sold there is no entry in OpenPositions
How can I find the exit price in a C# strategy?
Thanks!
For entry price:
Position pos in OpenPositions
pos.EntryPrice
For a position that sold there is no entry in OpenPositions
How can I find the exit price in a C# strategy?
Thanks!
Rename
CODE:You can use a Linq query to snoop the positions List for certain positions that have particular attributes.
public override void Initialize(BarHistory bars) { Position? pos = LastPosition; //include the '?' because pos must be nullable if (pos != null) WriteToDebugLog(pos.ExitPrice); System.Collections.Generic.List<Position> positions = GetPositions(); }
Like superticker said, you can use GetPositions to get the list of all historical positions in the backtest. Or just save the Position instance to a class level variable. Here's a contived example:
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript2 { 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) { } //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 (idx == bars.Count - 20) PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0); else if (idx == bars.Count - 10) PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0); else if (idx == bars.Count - 1) { List<Position> lst = GetPositions(); if (lst.Count > 0) { double exit = lst[lst.Count - 1].ExitPrice; string s = "Last Position had an exit price of " + exit.ToString("N2"); DrawHeaderText(s, WLColor.Cyan, 24); } } } //declare private variables below } }
Your Response
Post
Edit Post
Login is required