- ago
Hello again.

I would like to sort the Backtestdata by symbol names. Because i am not a C# programmer i did a quick and dirty approach which looks like that.

This runs in the Initialize routine for the strategy at the moment. Certainly this can be done more elegant in a C# style. I would be happy to see the C# implementation or as Linq expression.

I thought by assgining the sorted list to Backtestdata, the symbols would be processed in this order. But they won't.

CODE:
// only order the symbols when entering the first symbol if (first == 0) { first = 1; // sot the Barhistory information by symbol name List<BarHistory> bh = BacktestData; BarHistory tmp; for (int i = 0; i < bh.Count - 1; i++) for (int j = i + 1; j < bh.Count; j++) { if (bh[i].Symbol.CompareTo(bh[j].Symbol) > 0) { tmp = bh[i]; bh[i] = bh[j]; bh[j] = tmp; } } // assign it to BacktestData for (int i = 0; i < BacktestData.Count; i++) BacktestData[i] = bh[i]; // reference the current bar with the first in the list bars = BacktestData[0]; //for (int i = 0; i < BacktestData.Count; i++) //{ // WriteToDebugLog(BacktestData[i].Symbol); //} } // output the current symbol WriteToDebugLog(bars.Symbol);


There are several things i do not know about it. Did i only exchange references or objects, deep or flat (C# related, maybe you can clarify it too)?

Although the symbols are sorted this way, they are processed in unsorted sequence.

Is there a way to process them in a freely choosen sequence ?
Is the problem how i assigned the data ?
Is BacktestData right object to control the sequence for processing?

Thanks for your help.

Edit: Maybe you should rename BacktestData into BacktestData-Object or sth. like that. thx.
0
121
Solved
7 Answers

Reply

Bookmark

Sort
Cone7
- ago
#1
Sorry to answer a question with a question, but...

Why are you doing this unnecessary operation / what's the goal here?
What requires you to control the "sequence for processing"? In which method are you attempting to do that?
1
MIH7
- ago
#2
Hello Cone.

One reason why i want to do it is because the sequence of the the symbols is different after each load. I want to check if with the same sequence of the symbols and the solution DrKoch provided in the other thread i would be able to get the same result for the backtest after reloading the data. (Nobody answered that question, so i want to try it out)

A couple of other reason, like simple to explore the framework and learn something about it's elements.

Currently i run the code in Initialize() of the strategy, as i wrote. If it turns out to be useful there might be a better place to implement it.
0
Cone7
- ago
#3
I'll go out on a limb and just say controlling how the backtester works internally isn't part of the framework and I can't predict what consequences altering the list order would have (if any) on a backtest.

fyi, BacktestData has the BarHistories of all the symbols in the backtest, but all the symbols may not be used for every bar of the backtest, which could start on a date before data is available or end after the data series ends, or because they're not in a Wealth-Data constituent list for the current Execute() date, or maybe even for some other reason.
0
Best Answer
MIH7
- ago
#4
Well, unfortunately, I don't understand what you're writing, aside from the fact that you think it might not help.

Maybe I'll describe it more formally and maybe you can answer if it's worth trying.
When I load the strategy the initialization for the data goes through like this.
(Initialization routine of MyStrategy : UserStrategyBase)

Load1: Initialization() of S1,S4,S3,S9,S7 ...
Load2: Initialization() of S1,S3,S4,S6,S9 ...

My thoughts were (I may be wrong), that the backtest processes the symbols (their data) sequentially.
It doesn't even have to be sequential, that just makes it more descriptive.

The idea now is, that by sorting the symbols (the data) the same state is generated, which we had in a previous loading process. However the backtester is now working, it starts the work from the same (data) status. From this point, we know how to make the strategy repeatable through the Transaction.Weight solution.

Load1: Initialization() of S1,S2,S3,S4,S5 ...
Load2: Initialization() of S1,S2,S3,S4,S5 ...

The idea can be checked, however my implementation attempt seems to be wrong.
At this point I have too little knowledge about the objects and the C# language context.

Maybe you can help me, or you can say with certainty that it will not work and explain it to me (again?!). Thank you in advance.

Edit:
I hope i did not confuse with the data load itself. The data is loaded as normal and i want to rearrange the data before the backtest starts.
0
MIH7
- ago
#5
By modifying the main idea i was able to find a solution.

I understand why you asked for the reason, so i provided the information.
Here are some points i like to summarize.

1. The inner workings of the Backtester were not relevant in detail. The question was if providing data in the same way would lead to the same output. As we know now, it does.

2. I was curious for the BacktestData because if wanted to know if it is the source, that the strategy retrieves the Barhistory from. At that point i thought that it would be pointless to operate on an object that is not involved. The modification of the solution made this knowledge obsolete, but of course is interesting to know in other situations.

3. In the end the idea behind the operation was not uncecessary at all.
0
- ago
#6
MIH,
with your goal to make a backtest reproducable you face two obstacles.

1. random positions if there is not enough capital to open all positions
2. multithreading, which calls the Execute() method in unpredictable sequence.

To make a backtest reproducible I suggest the following solution:

1. In the global init routine assign a "weight" to each symbol. Leave some space for several positions.
2. In Execute() you use this weight (depending on current symbol) and increment it for several positions in the same symbol.

I will provide an example implementation later, when I find the time...
0
MIH7
- ago
#7
Thanks, i already solved it. There is a solution that makes the process deterministic + persistent.

You can have a lookt at the following posts. (#32, #34)

https://www.wealth-lab.com/Discussion/How-to-configure-deterministic-backtests-8203
0

Reply

Bookmark

Sort