Systematic Intraday Index Trading Experiment

sridhga

Well-Known Member
#21
Sorry, I am not familiar with Amibroker, I have developed my own python backtesting engine as it gives me the flexibility to make customized frameworks and add new features as per my needs.

Zerodha provides a very nice python kite connect api package which can be easily used with any python backtester and it streams live data and we can place trades in real time. Also they provide historical data for backtesting.

Try Using a python open sourced packages as it will make our life easy, for this system I have used the python reinforcement learning framework, RL is a supervised machine learning algorithm which can be trained and later it will learn by itself. This algorithm is usually used in training computers to play chess, crawl web, sentiment analysis etc

You can check this article for an overview:
https://neptune.ai/blog/best-reinforcement-learning-tutorials-examples-projects-and-courses
Example trading scenario:
https://towardsdatascience.com/deep-reinforcement-learning-for-automated-stock-trading-f1dad0126a02

I will just give a hint on how I have built this system, first just look at charts and analyze how you would need to trade it manually:

Preprocessing steps:
1. Select the time frame to trade say 5 min.
2. Identify all the candle pivots (3 candles, with lower highs or higher lows on either side)
3. Mark the structure for all the identified pivots.
4. If needed you can also use some technical signals.

Analysis rules and training the RL model:
1. After completion of each candle, mark its location in the overall structure and take trades (both Buy & Sell)
2. Close the trades at the end of next candle.
3. Define the rewards i.e positive value if its a winner and negative value if its a loss.
4. Now let the RL model iterate over the entire data and learn by itself which are the optimal candles to buy and sell so that its reward points are maximized
5. Manually this is not possible but with computer after a million odd iterations the model will get trained to identify the best candles which will give us higher points.

That's all...enjoy...:)

You indicated that you would use 5 min time frame.
For a candle pivot, you needed 3 candles.
You are looking for a million odd iterations.
There are just 75 number of 5 minute candles in a day on the index. So, the number of pivots are far lesser than that.
Assuming 21 working days in a month, you get 252 working days in a year.
How many years of data does one need for this process to complete 1 million iterations?
Have you completed the run for such a process?
 

ncube

Well-Known Member
#22
You indicated that you would use 5 min time frame.
For a candle pivot, you needed 3 candles.
You are looking for a million odd iterations.
There are just 75 number of 5 minute candles in a day on the index. So, the number of pivots are far lesser than that.
Assuming 21 working days in a month, you get 252 working days in a year.
How many years of data does one need for this process to complete 1 million iterations?
Have you completed the run for such a process?
@sridhga sir, yes that's correct, the test index data is from 2000-2021, as you rightly said, the total number of 5 min candles that can form is 75*250*20 = 375000. But running this whole data will be just one iteration. Now how the ML algorithms work is by running multiple iterations over the same data with new offsets and error factors introduced in each iterations and that permutation & combinations can easily be in billions.

For example to simplify in our case, the 2nd run would combine candle 1 and 2 after the buy/sell signal to close the trade and check the rewards, this will continue in each iteration till the opposite trade signal is generated. Again the iterations will keep continuing until optimization of max rewards happen at multiple levels and there cannot be further visible improvements possible even if we run more iterations which in this case would be many multiples of million runs, apart from this we also need to run generalization iterations across multiple time frames.. so that the model is not overfitting to test data alone and work in any time frames. However I was satisfied with the performance and hence stopped it after about 2million iterations.

But understand that even ML AI models make mistakes, its not that all trades will end in profits, the performance of this model is in the range of about 60-70% win ratio and profit factor of about 2.5-3.0, however future is always unknown and we cannot predict anything with surety, but it will help us to trade systematically without the stress of manual analysis.

I know its slightly complex to grasp if you are not from data analytics background, but its not difficult to build such models as there are readymade ML frameworks available and these days we can easily get cheap online cloud server resources for processing. You may start with a basic ML course to get familiar and understand it better.
 
Last edited:

ncube

Well-Known Member
#23
The sample size is low for it to be statistically significant, but its a good way to visualize the historical behavior of Nifty movements on budget days. The trend has been that until 11:30am there is no major change, 11:30-1:30 there is lot of noise, 1:30-3:30 a real trend emerges.

 

ncube

Well-Known Member
#24
Good Budget, As I had mentioned elsewhere in these situations one can buy/add to the positions when the market breaks its previous day high and not be left out. The trend became clear around 12:00pm. Have added to my existing positions and bought few new stocks today. One can later book profits once the momentum slows down and repeat the process. Just for fun also took a small intraday options trade...as it was too tempting to ignore...:)

All the best...Cheers!!!

1612172311512.png
 
Last edited:

ncube

Well-Known Member
#25
Good Budget, As I had mentioned elsewhere in these situations one can buy/add to the positions when the market breaks its previous day high and not be left out. The trend became clear around 12:00pm. Have added to my existing positions and bought few new stocks today. One can later book profits once the momentum slows down and repeat the process. Just for fun also took a small intraday options trade...as it was too tempting to ignore...:)

All the best...Cheers!!!

View attachment 45427
Returns from this list purchased on budget day gave an average return of 25%+ in 15 days....Mr Market is kind...feeling invincible....??? Is it time to wait for Mr Market to reveal his next plans...before exploring new opportunities?? Only time will tell...:)
 
Last edited:

Raj232

Well-Known Member
#26
Returns from this list purchased on budget day gave an average return of 25%+ in 15 days....Mr Market is kind...feeling invincible....??? Is it time to wait for Mr Market to reveal his next plans...before exploring new opportunities?? Only time will tell...:)
Did you stop the algo or improving on it .. please update :)
 

ncube

Well-Known Member
#27
Market fall today is not very convincing as broader markets have not participated so far in sync. Now one can start looking for good opportunities in coming days gradually with focus on stocks that have stayed strong in this fall. I will start buying again from next week if the broader market remains strong.
 
Last edited:

ncube

Well-Known Member
#28
Did you stop the algo or improving on it .. please update :)
As mentioned in another thread, I have taken this model live to hedge my Index positions, currently hedging only the NIFTYBEES+GOLDBEES positions, based on the effectiveness will also include my equity portfolio positions. Current status is as follows:

1614323861475.png


Base is the total positions in 3 accounts which will shift dynamically based on Niftybees and Goldbees prices, it is currently -1.34% due to Nifty/Gold prices falling. But the hedge based on the model is performing well, overall the returns is at +9.44% over last 2 months. (Live hedge only from Feb)
 
#29
As mentioned in another thread, I have taken this model live to hedge my Index positions, currently hedging only the NIFTYBEES+GOLDBEES positions, based on the effectiveness will also include my equity portfolio positions. Current status is as follows:

View attachment 45576

Base is the total positions in 3 accounts which will shift dynamically based on Niftybees and Goldbees prices, it is currently -1.34% due to Nifty/Gold prices falling. But the hedge based on the model is performing well, overall the returns is at +9.44% over last 2 months. (Live hedge only from Feb)
Hey Ncube

Thank you for Positing such informed content as a thread.

But me coming from a non- programming background wasn't able to understand much from this thread.

Just wanted to know do you have proper entry and exit rules for this setup and if yes have you backtested this and what was the result of the backtest
 

Similar threads