Accessing position data for other symbols in the dataset
Author: RedPepper
Creation Date: 8/15/2018 10:38 AM
profile picture

RedPepper

#1
I am trying to, in each bar, rank the symbols in my dataset by stock performance and by strategy performance.

By using SetContext I can get stock performance data for the other symbols, by it seems like the Position object only has the data for the original symbol that we are in.
Is there a way I can access position data for the other symbols? - specifically ProfitPerBar ?

The way I have been trying to do this is like this:

CODE:
Please log in to see this code.


This works fine for the StockPerformance variable, but for the AvgProfitPerBar variable it only accesses the positions for the original symbol that this gets called for.

Is there a way to do what I am trying to do here?
profile picture

Eugene

#2
Welcome aboard.

QUOTE:
I am trying to, in each bar, rank the symbols in my dataset by stock performance and by strategy performance.

Let's start by asking what is your objective with this? A lot of code and design patterns have been accumulated during the last decade of Wealth-Lab .NET so if you could clarify your goal there may a ready solution?

P.S.
QUOTE:
specifically ProfitPerBar ?

According to the QuickRef (F11 key), In portfolio simulation mode the ProfitPerBar property is always based on 1 share while the Strategy is executing and returns the correct number if running in a Performance Visualizer i.e. after the position sizing has been applied. To save me some typing here's penultimate forum post explaining this concept:

All open positions in a table?
profile picture

RedPepper

#3
I want to rank the symbols in my dataset according to performance, because I want to use the rank to assign a probability to each symbol in each bar that a signal will actually get executed, and I want to determine a maximum allocation out of the total portfolio to this particular symbol, again based on past performance.

The system I am working with trades many small positions in each symbol, but should allocate more money to symbols that are doing well, along the lines described above.

With respect to the ProfitPerBar property - I do not necessarily see it as a problem that it is based on 1 share - I could still work with that, but it still only accesses the positions that belong to the original symbol, not the ones that belong to the symbol in the context loop.
profile picture

Eugene

#4
QUOTE:
but it still only accesses the positions that belong to the original symbol, not the ones that belong to the symbol in the context loop.

Not sure what you mean. Code above accesses the positions in the symbol being looped by DataSetSymbols.

P.S.
Note that depending on what you're after in the omitted Strategy code, you may need to rewrite it for looping through all the DataSet's components in Single Symbol Mode as exemplified by:

WealthScript Techniques | Portfolio-wide conditions

Another (advanced) example:

Limit Monthly Drawdown / Portfolio Equity Tracker, posts #12 and #15
profile picture

RedPepper

#5
Thanks Eugene.

I think my problem is that if I execute for one symbol, then it will not have any positions for any of the other symbols in the dataset, since they are not being traded.

If I execute it as a "Backtest on all symbols" in the dataset, I presume it does this by doing it one symbol at a time, and the same problem will occur. When it runs through the first symbol, it will not yet know anything about the positions created from the next symbols.

This is because I am looping through the symbols within the bar loop. I am only trying to calculate ranks within the symbol loop, and I am only executing trades within the bar loop, outside the symbol loop, if that makes any sense.

I think I will have to rethink how I do this :)
profile picture

Eugene

#6
I take it that the ranking by performance happens on each bar (as per your post #1) in order to influence trading signals. If my assumption is correct, your system has feedback then. And in this case, it makes little sense to calculate the ranks outside the main (trading) loop by bars. Consequently, you might have to rewrite your script to run in single symbol mode so that it loops through DataSetSymbols and operates across the entire portfolio.

As the ProfitPerBar property isn't intended for use in WealthScript Strategies, in the trading loop you might want to query the NetProfitAsOfBar property instead and do necessary calculations. Note that the value it returns is based on 1 share.
profile picture

RedPepper

#7
Yes, I agree that I should be using NetProfitAsOfBar instead.

I also think you are right, I should rewrite this to single symbol mode, I also want to manage allocations to each symbol on a portfolio level, and I think that will require this as well.

So - just to make sure I am on the same page - when running the "Backtest on all symbols", all of the code in the Execute() will run for the first symbol, and then when it is finished, it will initialize again and run for the second symbol and so forth?

profile picture

Eugene

#8
QUOTE:
when running the "Backtest on all symbols", all of the code in the Execute() will run for the first symbol, and then when it is finished, it will initialize again and run for the second symbol and so forth?

Yes, your understanding is correct. Position sizing kicks in after the last symbol is processed (with 1 share). This is when the 'impossible' trades are weeded out.
profile picture

RedPepper

#9
I have now rewritten this to single symbol mode, and that seems to work very well. Thanks a lot for your help.

Am I understanding it correctly that when I am running this in single symbol mode on a portfolio, I am not able to access portfolio equity or cash dynamically, because everything is based on 1 share during the execution, so EquityCurve[] and CashCurve[] will not work?

If I want to use position values along with portfolio equity and cash, dynamically in the above situation, would the solution then be along the lines of the PortfolioTracker class that you linked to above?
profile picture

Eugene

#10
Glad to help.

QUOTE:
Am I understanding it correctly that when I am running this in single symbol mode on a portfolio, I am not able to access portfolio equity or cash dynamically, because everything is based on 1 share during the execution, so EquityCurve[] and CashCurve[] will not work?

The EquityCurve / CashCurve isn't accessible in WealthScript Strategy. Technically, these are available to Performance Visualizers. There is an extension-powered solution (not available during evaluation) that works sufficiently good with most scripts (not without certain exceptions): Interacting Dynamically with Portfolio Level Equity.

QUOTE:
If I want to use position values along with portfolio equity and cash, dynamically in the above situation, would the solution then be along the lines of the PortfolioTracker class that you linked to above?

This approach should also work (even in free trial mode).
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).