I'd like to plot an intraday strategy's cumulative P&L (1-min bar scale) from market open to close (reset next day). Have checked prior discussions without success.
Any thoughts or coding examples anyone can share? Thank you
Any thoughts or coding examples anyone can share? Thank you
Rename
You mean how the unrealized P&L oscillates through the day every minute?
Here you go ...
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; 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) { rsi4 = RSI.Series(bars.Close, 4); dailyProfit = new TimeSeries(bars.DateTimes); PlotTimeSeriesMountain(dailyProfit, "Daily Profit", "DP", WLColor.Green, 2, 100, 50); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { //process profit for yesterday ProcessProfit(bars, idx); if (!HasOpenPosition(bars, PositionType.Long)) { if (rsi4[idx] < 40) PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { //code your sell conditions here if (rsi4[idx] > 60) PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } } //process final bar's profit public override void Cleanup(BarHistory bars) { ProcessProfit(bars, bars.Count - 1); } //process Profit for specified index private void ProcessProfit(BarHistory bars, int idx) { if (idx > 0) { if (bars.DateTimes[idx].Date != bars.DateTimes[idx - 1].Date) profitToday = 0; double profit = CurrentEquity - lastEquity; profitToday += profit; dailyProfit[idx] = profitToday; } lastEquity = CurrentEquity; } //declare private variables below private RSI rsi4; private TimeSeries dailyProfit; private double lastEquity = 0; private double profitToday = 0; } }
simply perfect, thank you Glitch & Team. good learning here :)
Your Response
Post
Edit Post
Login is required