Python Forum
Backtesting trading strategy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Backtesting trading strategy (/thread-25084.html)



Backtesting trading strategy - Finpyth - Mar-18-2020

Hallo

This is my last question for my thesis.

I’m about to backtest my strategy, but ran into the problem that i have some price data from Eikon that i have to use. I tried with backtesting.py but ran into 2 problems. 1 The dataset must include open, high, low and close. In my dataset i only have the adjusted close price. 2 Is that i can only find examples for a single stocks and not an index/portfolio.

Then i thought that maybe the best solution was to loop over my dataset with my strategy, but i’m open for other solutions too if you have a better idea.

The strategy i have to test is a long/short strategy and i have all the historical holding positions in 2 pandas dataframes called long and short, with an index={1,2,3..} and all the holding positions in the cells.
My test set is the stock names in the columns a Date index={1-1-2000,2-1-2000} and and prices in the cells.

I think i must convert the long and short to have the same index as my test set, but i can't override the index. The test set and long and short have the same lenght and is for the same period.
I tried to set the index like this:
short.reindex(test_set)
and get this error:
Error:
ValueError: Index data must be 1-dimensional
I tried to reset the index of my test_set too with ".reset_index", but the index is not reset Think

An examples of my long, short and test_set:
long = (['Index', '0', '1'],
        ['0','stock_a','stock_b'],
        ['1','stock_a','Stock_c']

short = (['Index', '0', '1'],
         ['0','stock_c','stock_d'],
         ['1','stock_b','Stock_d']
So for the long strategy, in day 0 i buy stock_a and stock_b. For day 1 i keep stock_a, sell stock_b and buy stock_c

And an example of my test_set
test_set=(['Index','   stock_a','stock_b','stock_c','stock_d','stock_e'],
          ['1-1-2000',  100,      200,      300,      200,      250],
          ['2-1-2000',  101,      203,      299,      205,      251])
So after i have set the index, how do i loop over it and backtest it, and is it possible to include transaction cost if there is a trade?

best regards Big Grin


RE: Backtesting trading strategy - Finpyth - Mar-20-2020

I now did solve the first problem about set the index to be the same.
The solution is:
test_set.index = pd.to_numeric(long.index)
My only problem now is to use the value of the cells(stock names) in long and short to pick the right values from the columns in test_set.

Anybody who knows it?