Creating a trading system from scratch

How many lines of code you are comfortable with


  • Total voters
    61

UberMachine

Well-Known Member
For the backtest I just used nifty50 stocks as universe, the backtest report I had posted in earlier post does not consider the slippage/stop-loss. The daily stock recommendation is just the top 5 stocks which have gained the most the previous day. I have calculated the daily return as just the difference between close & open price.

Todays recommendation was : ['AXISBANK', 'WIPRO', 'ITC', 'HDFCBANK', 'HDFC']

I am really sorry I will not be able to share my complete code base in an open forum. Hope you can understand. Though I am using the backtrader concept, over the months I have fully revamped it as per my needs, mainly support for pandas and have introduced many machine learning algorithms based on my research and proprietary libraries. Also it will not run directly on the backtrader current version. However I will try to share code snippets wherever possible.
Got it :up:.
I am glad that your backtesting results are on par with mine (In fact, mine are much more conservative)
 

ncube

Well-Known Member
Got it :up:.
I am glad that your backtesting results are on par with mine (In fact, mine are much more conservative)
I dont day trade, as its a long weekend for me. so just wanted to run the backtest on your strategy to understand how it performs. The result I have posted is ideal case scenario [results are very optimistic..:)]. If we introduce few filter rules as you have mentioned it can perform well. Another screnario if you are interested can explore shorting the top 5 losers along with shorting the top 5 gainers. There will be some nasty drawdowns but with filter it can be improved. Wanted to see the effect on long-short portfolio. However I observed that going long on the top 5 losers did not give good results. however if we short the losers also the returns are better.

1538387692986.png
 
Last edited:

UberMachine

Well-Known Member
Does it generate regular returns tear sheet?
Got the entire hack
Python:
import pandas
pandas.core.common.is_list_like = pd.api.types.is_list_like
import pyfolio as pf
pf.plotting.plot_rolling_fama_french = pf.plot_returns
This would help you get and running with pyfolio
You must create a benchmark to run full tear sheet. NIFTY 50 is preferable but you can even bunch a set of random numbers
Python:
# Daily returns in percentage
# calculated for my strategy
daily_rets = (result.query('open!=high').groupby(
    'timestamp').net_profit.sum()/100000)
daily_rets.index = daily_rets.index.tz_localize('UTC')

# Generating benchmark returns
import numpy as np
bench = pd.Series(np.random.normal(0.001, 0.002, daily_rets.shape[0])*0.01,
                  index=daily_rets.index)
 

UberMachine

Well-Known Member
I dont day trade, as its a long weekend for me. so just wanted to run the backtest on your strategy to understand how it performs. The result I have posted is ideal case scenario [results are very optimistic..:)]. If we introduce few filter rules as you have mentioned it can perform well. Another screnario if you are interested can explore shorting the top 5 losers along with shorting the top 5 gainers. There will be some nasty drawdowns but with filter it can be improved. Wanted to see the effect on long-short portfolio. However I observed that going long on the top 5 losers did not give good results. however if we short the losers also the returns are better.

View attachment 28971
Yes I do all of what you have said, included some more filters, expanded the stock universe in live trades
BUY doesn't work; its a surprise but its true (working out on an alternative LONG SHORT strategy) to smooth out returns.
I don't compound often and I work on just 5 leverage; so drawdown is under control.
I am presently trying to improve the system based on zscore and some memory (right now it just has previous day memory)
 

UberMachine

Well-Known Member
I dont day trade, as its a long weekend for me. so just wanted to run the backtest on your strategy to understand how it performs. The result I have posted is ideal case scenario [results are very optimistic..:)]. If we introduce few filter rules as you have mentioned it can perform well. Another screnario if you are interested can explore shorting the top 5 losers along with shorting the top 5 gainers. There will be some nasty drawdowns but with filter it can be improved. Wanted to see the effect on long-short portfolio. However I observed that going long on the top 5 losers did not give good results. however if we short the losers also the returns are better.

View attachment 28971
Thank @UberMachine for this tutorial and the codes.

Can you clarify how to automate it with Zerodha.

I am on Manjaro and use only Kite.

Thanks.
If you know jinja2, create a html page as shown here.
We just need to create a variable with the json basket in python, something similar to what I have done in NEST.
 

VJAY

Well-Known Member
Got the entire hack
Python:
import pandas
pandas.core.common.is_list_like = pd.api.types.is_list_like
import pyfolio as pf
pf.plotting.plot_rolling_fama_french = pf.plot_returns
This would help you get and running with pyfolio
You must create a benchmark to run full tear sheet. NIFTY 50 is preferable but you can even bunch a set of random numbers
Python:
# Daily returns in percentage
# calculated for my strategy
daily_rets = (result.query('open!=high').groupby(
    'timestamp').net_profit.sum()/100000)
daily_rets.index = daily_rets.index.tz_localize('UTC')

# Generating benchmark returns
import numpy as np
bench = pd.Series(np.random.normal(0.001, 0.002, daily_rets.shape[0])*0.01,
                  index=daily_rets.index)
1538391273802.png
 

VJAY

Well-Known Member
If you know jinja2, create a html page as shown here.
We just need to create a variable with the json basket in python, something similar to what I have done in NEST.
Dear UB,
Can you share the code for kite also with us....some times NEST login problems at morning...
 

VJAY

Well-Known Member
Thanks ncube....will check soon :)
Got the entire hack
Python:
import pandas
pandas.core.common.is_list_like = pd.api.types.is_list_like
import pyfolio as pf
pf.plotting.plot_rolling_fama_french = pf.plot_returns
This would help you get and running with pyfolio
You must create a benchmark to run full tear sheet. NIFTY 50 is preferable but you can even bunch a set of random numbers
Python:
# Daily returns in percentage
# calculated for my strategy
daily_rets = (result.query('open!=high').groupby(
    'timestamp').net_profit.sum()/100000)
daily_rets.index = daily_rets.index.tz_localize('UTC')

# Generating benchmark returns
import numpy as np
bench = pd.Series(np.random.normal(0.001, 0.002, daily_rets.shape[0])*0.01,
                  index=daily_rets.index)
1538391781980.png

Is this error solved ?
 
Really great now we have great system the only problem i have is going from huge profits to losss..... today lt&fh was down 4.50% by 12....it recover in afternoon session n closed at 1.08%....:(:(:( if these kinda drawdowns are avoided it would be even a greater system
 

Similar threads