Using Strategy performance metrics to size positions
Author: innertrader
Creation Date: 5/25/2018 11:40 AM
profile picture

innertrader

#1
I would like to programatically use the type of data that is displayed on the Strategy Performance tab as part of a position sizing strategy while the strategy is running. The goal would be to access this data for a given time period (e.g. for the past 30 days or 500 bars) to inform and modify trading alerts in "real-time".

I read https://www.wealth-lab.com/Forum/Posts/PosSizer-request-Vary-active-positions-based-on-DataSeries-value-30626 but it seems like I have a more fundamental question which is how woujld you access or create such data so the strategy could use it (not just report it).

These are the two of the inputs I would like to code into in the trading strategy, for a given number of bars or a time period.

Winning percentage
Average Win Profit %


Not sure if I have to create a Dataseries to do this, or if this data is already available or how I would calculate these values on a rolling basis, updating at each bar.

BTW, I should mention that I plan to use this with an intraday trading strategy. It is acceptable to have the performance parameters calculated daily after market close, for use with the next day's intraday trading.
profile picture

Eugene

#2
Hi,

As this question has been raised many times before I'm going to save myself some typing. Please find the most closely matching topics below:

Trading by win rate and profit factor
Access to data/stats normally displayed in the performance tab during a backtest

Note that since this is unsupported, you're on your own with this technique.

Also, there's an example of another (simpler) technique in the QuickRef for ClearPositions but this is not supposed to work in multi-symbol backtest mode.
profile picture

innertrader

#3
Thanks, Eugene. Good stuff. Going though it now.
profile picture

innertrader

#4
From #15 in https://www.wealth-lab.com/Forum/Posts/Community-Components-library-28615:
QUOTE:
Long story short: it's impossible to access anything in any Tab from Strategy code. WL5 is not designed to access information generated by Visualizers, because they start to process backtest results after the strategy has already finished executing.

On the contrary, the article you're referring makes it possible to access -- among the rest -- the Net Profit number by utilizing a "hack" i.e. calling an instance of the internal TradingSystemExecutor class used to run strategies. Problem here is that the API documentation of the SystemResults and SystemPerformance classes is currently unavailable.


This is the closest thing I've found to what I'm trying to do but since this is so old, I was wondering if is still relevant.

I would like to programmatically export a Kelly statistic from a backtest. Currently I believe WL only generates Kelly for the use of the PosSizer, not as a performance statistic. I was not able to access the Wiki so I could be wrong. I can construct it myself if I can get the following data.

I would like to export (or construct) programmatically two performance metrics from the Backtest Performance Report (Performance Tab): Win Rate and Payoff ratio. I can also create the Payoff Ratio if I have the Average Profit % from Winning Trades and Average Loss % from Losing Trades. I want this data for each symbol in a dataset after running a strategy. Alternatively, if there is a way to copy / paste the data on the Performance tab into an Excel spreadsheet, that could work also.

I see that the Streamwriter function can be used to export to CSV Bar data, but I don't see reference to summarized performance data. Sounds like your reference to the hack above maybe comes close.

Currently, I am running backtests for individual symbols, and copy / pasting manually into Excel spreadsheet, to do additional processing on the data. As I mentioned above, I don't require all the data on the Performance tab, but I can extract what I need if there is a way to get it all programmatically.
profile picture

Eugene

#5
I've moved your post here from a different topic (the Community Components "blog thread"). It turns out that the new place is the perfect destination because my previous reply to you also answers your new question. It was a couple years ago, please see post #2 above. Magic! :)
profile picture

innertrader

#6
HI Eugene,

After reviewing the numerous threads related to the topic, here's what I came away with.

@sedelstein https://www.wealth-lab.com/Forum/Posts/Accessing-SystemResults-32290 #6 use of PerformanceEngine class seemed promising at first. I downloading the referenced MS123 source code to see if I could figure out what methods were available. I did not see Performance Ratio, so I assume I would have to construct it. However, when I try to run the @sedelstein code, I get errors such as, "Community.Components.Utility is obsolete" and "the type or namespace PerformanceEngine could not be found"

On the other hand, the link you provided above, https://www.wealth-lab.com/Forum/Posts/Trading-by-win-rate-and-profit-factor-32375/Page/1 has an example you created that I believe shows how I would have to get a Payoff Ratio statistic, In this sample code, there is a segment that calculates Win Rate, one of the three outputs I require. For the other two outputs I require to calculate Payoff Ratio I am assuming I would have to create my own Average Profit % for both winning and losing trades, .

However, it appears that this approach relies on documented and supported properties of the Position object, so there would be little risk in using this approach, as it doesn't come with the caveats that come of the other "jailbreaking" approaches do. Do you foresee any special challenges calculating average win and loss % for a dataset of positions by modifying this sample code?

Am I on the right track?

Robert

profile picture

Eugene

#7
Hi Robert,

Yes, there should be little risk in using this approach. I don't foresee much caveats in applying the averaging of winning/losing trades to this code sample in Trading by win rate and profit factor.


P.S. If you experience a difference in the numbers between the Wealth-Lab backtest and the "jailbreaking" approach, pay attention to this article, namely the section where it discusses getting different values on every run:

Interacting Dynamically with Portfolio Level Equity

Producing different results in each backtest run takes place when funds are insufficient (i.e. not enough money to take all trades). In other words, whenever there are skipped trade(s) and no Priority assigned, a difference is inevitable. To avoid this, your system must be either correctly assigning a value to the Position.Priority property and/or the option "Use worst trades in Portfolio Simulation" has to be activated in Wealth-Lab's Preferences dialog > "Backtest Settings".

Though less important but still a factor is the impact of custom Commission plan. The "jailbreaking" solution currently runs with its default values, ignoring any customized Commission settings applied.
profile picture

innertrader

#8
QUOTE:
P.S. If you experience a difference in the numbers between the Wealth-Lab backtest and the "jailbreaking" approach, pay attention to this article, namely the section where it discusses getting different values on every run:


Thanks, Eugene. Can't I avoid this issue by using Raw Profit mode? For this exercise I'm not concerned about portfolio effects or the absolute values for performance, but instead % and ratio for each symbol in a dataset.
profile picture

Eugene

#9
Probably.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).