Creating a trading system from scratch

How many lines of code you are comfortable with


  • Total voters
    61

VJAY

Well-Known Member
I saw in one of the earlier posts the strategy results are stored in dataframe. If this is available one can easily create the pyfolio tearsheet as follows:

1. Install pyfolio using the command: pip install pyfolio

Then in your note book import it as follows:
Code:
import pyfolio as pf
%matplotlib inline
# silence warnings
import warnings
warnings.filterwarnings('ignore')

pf.create_full_tear_sheet(strategy_returns, benchmark_rets=nifty50_retruns)

#where strategy_returns is a series of daily return indexed by date
# nifty50_returns is a series of daily return of your benchmark in this case nifty50.
#benchmark_rets can be skipped if not required.
However I got a slightly different result than my stats, have not debugged it.

View attachment 28935

View attachment 28936
Dear ncube/UB,
I think we need to get one result file for this code to work.....Please help in this regard....

1538361583659.png
 

VJAY

Well-Known Member
Dear UB,
Today I want to start trade with FO file....but when at 9.10 tryed to create order file with system this error showed!!!!!!!!then I again switch back to Nifty50 :(

1538366016822.png
 

UberMachine

Well-Known Member
Exactly when i download files like this i get error but anyways no issue one question i think in backtest there is no code for commision
because when i change the commision from 0.5 to 1 or any other value the profits remain the same....!!!
Exactly when i download files like this i get error but anyways no issue one question i think in backtest there is no code for commision
because when i change the commision from 0.5 to 1 or any other value the profits remain the same....!!!
Its the correct link.
Select bhavcopy in reports not bhavcopy(PR.zip)
If you still encounter errors, post it :)
 

UberMachine

Well-Known Member
Hi @UberMachine , finally got some time yesterday to simulate and backtest your strategy discussed. Looks very promising but is very sensitive to execution price. If the price slippage at open is controlled then this can be a robust strategy.

The backtest results from 2016-01-01 is as follows:

View attachment 28930

1. Benchmark with Nifty:
View attachment 28931
2. Max Drawdown: -24%
View attachment 28932

3. Monthly Returns Heatmap:
View attachment 28933

4. Monte Carlo simulation: 1000 iterations, 25% probability for 25% draw-down:
View attachment 28934
Excellent observation regarding price sensitivity. :up: How did you discovered it?
Its a corner case and I have dealt with it by excluding the specific cases from the results so that the returns are much more rational. I have not included it in my original notebook but have described it in threads on how to make these adjustments.
Also, how did you generated these charts (what program do you use)
 

UberMachine

Well-Known Member
@VJAY , I am not using the @UberMachine code for backtesting, I have coded this strategy in my backtesting platform which store the strategy results in different data structures for calculating these statistics. Hence these stats code cannot be directly applied here.

However few of the code snippets are as follows but would require some adaptation to use it in your code:
Code:
sharpe = np.sqrt(252) * (np.mean(returns)) / np.std(returns)
recovery_factor = total_returns / abs(max_dd)
cagr = ((end_value / start_value) ** (1 / years)) - 1
If possible, could you please share the data structure. I just wanted to debug why these platforms show different results.
 

Similar threads