How to do Backtesting in Trading *for FREE* with Python 2021 [PART-3]馃敶 - YouTube

Channel: Financial Programming with Ritvik

[0]
hello hello MIC Test
[1]
馃憠[FOLLOW CAPTIONS TO LEARN MORE]馃憟
[9]
hello everyone I鈥檓 Ritvik Dashora and I鈥檓聽 back with a new video and some new learnings聽聽
[14]
this is the third video of our playlist on back聽 testing in the first video if you remember we聽聽
[19]
have done actually we have created the data frame聽 of five minute interval bitcoin prices for free聽聽
[26]
using 12 data although 12 data does not provide聽 that much I would say information in just one聽聽
[33]
run we have created a an amazing for loop to聽 make that entire data frame for free and the聽聽
[40]
second video we talked about the engulfing using聽 TA-Lib if you want me to cover playlists like this聽聽
[48]
one and which you don't have to go at any places聽 everything will be in just one playlist and like聽聽
[53]
this video if it crosses 200 likes I鈥檒l be taking聽 this thing very seriously and I鈥檒l be covering all聽聽
[58]
the important topics in one playlist so please聽 like this video right now we also created a聽聽
[64]
column called engulfing in the end using the聽 TA-Lib library so you can see that this is the聽聽
[70]
data frame that we created in the first video聽 so this is like this all this data is from the聽聽
[76]
12 data library and the engulfing information is聽 through the tele library if you want to like know聽聽
[83]
more about 12 data and engulfing I would聽 say TA-Lib library then I have created some聽聽
[88]
what's a separate videos on TA-Lib and 12 data聽 you should watch that those videos to know to聽聽
[94]
learn more about these two amazing libraries and I聽 would say there's a lot of applications that that聽聽
[100]
is possible through these two amazing things yeah聽 after that the second video was focused to make聽聽
[106]
the the back testing and for that we have actually聽 referred to our code which is this one bitcoin聽聽
[114]
trading bot code that we actually created in our聽 video on the engulfing strategy of a bitcoin day聽聽
[120]
trading board you can click on the I button聽 to watch that video it was around 45 minutes聽聽
[125]
long and it was an amazing video with a lot of聽 information I have skimmed a lot of things about聽聽
[131]
that big video in my last video previous video聽 you can go through the skimmed part as well if聽聽
[137]
you want otherwise I would highly recommend you to聽 watch that video on how to create that the bitcoin聽聽
[143]
day trading bot so I used these parameters from聽 that the port after that I have returned these聽聽
[150]
two functions on buying and selling using the聽 paper trading simulator again I have made a very聽聽
[156]
detailed video on paper trading simulation you can聽 again click on this I button to watch that video聽聽
[162]
all these videos will be in the description box as聽 well so you can refer to all of them so and then聽聽
[167]
after that we actually made this entire strategy聽 on core strategy and the alpha strategy which is聽聽
[174]
the satellite strategy to make this column in聽 the end of the final final df data frame and in聽聽
[182]
the end we saw that our trading strategy was not聽 that great as far as the prices are concerned we聽聽
[190]
started with one thousand dollars of money and聽 then we ended at 435 dollars after two months聽聽
[196]
so we have lost more than 50 percent of the money聽 using this strategy there's a basic graph that we聽聽
[201]
made although the bitcoin has also lost a lot of聽 money but our strategy has lost more money so yeah聽聽
[208]
maybe our trading strategy actually sucked anyways聽 but yeah before saying this thing I will have to聽聽
[215]
go through a lot of analysis and I鈥檒l be referring聽 to this one so we'll talk about the return聽聽
[220]
analysis the sharp ratio analysis which actually聽 represents the risk adjusted return then we'll do聽聽
[226]
some risk analysis which is actually volatility聽 and the value at risk we'll see what are excess聽聽
[231]
returns and then we'll make a confidence interval聽 for one candle such that we get to know that in聽聽
[237]
any particular candle what is I would say the聽 what is the possibility of that candle to be聽聽
[243]
in any particular range with 95.5 percent聽 confidence and then in the end we'll see聽聽
[248]
how to do the maximum drawdown actually I鈥檒l聽 cover this maximum drawdown in another video聽聽
[253]
and there will be some homework for you guys for聽 maximum drawdown as well so I鈥檒l come back to this聽聽
[261]
code so we have actually plotted a very basic聽 graph using matplotlib and we can see that聽聽
[267]
there's a lot of spread in the end of bitcoin and聽 our trading strategy now let's see like analyze聽聽
[274]
all these things one by one so let's see what聽 is the bot return so bottle 10 is equal to I鈥檓聽聽
[281]
actually referring to the mean return of all the聽 candles so np dot mean and then we'll have to take聽聽
[286]
the I would say returns that the return of each聽 candle so for example in this case this divided by聽聽
[294]
this but the logarithmic return is better than the聽 the simple return so I would say np dot log then聽聽
[300]
final df rd port value divided by this and then聽 dot shift 1. similarly for the bitcoin return I聽聽
[308]
would say BTC by saying bitcoin I mean the buy and聽 hold strategy for this it is np dot this and then聽聽
[316]
bit coin actually I can provide close as well聽 here and then here will be close so our bot has聽聽
[324]
made an average return of this amount and the聽 bitcoin would have an average candle return of聽聽
[331]
this one actually I鈥檓 not that interested in聽 the absolute return actually I should focus on聽聽
[336]
the risk adjusted return which is also known as聽 the sharp ratio and for that I would need the聽聽
[341]
standard deviation also because if you remember聽 the formula of sharp ratio is portfolio written聽聽
[346]
minus risk rate divided by standard deviation of聽 the portfolio considering this freely to be zero聽聽
[351]
right so it will be the portfolio return divided聽 by the standard deviation of the portfolio聽聽
[356]
so the bot standard deviation is np dot聽 s3d and I鈥檒l just copy this thing here聽聽
[363]
similarly for the bitcoin standard deviation聽 is close and then here it should be closed聽聽
[370]
so the sharp ratio of the bot which is sharp聽 of part is equal to the bot return divided by聽聽
[377]
the bot standard deviation right so let's聽 see what is it it's negative for sure because聽聽
[382]
the the portfolio return is negative significantly聽 negative actually and similarly the sharp ratio of聽聽
[389]
bitcoin would be this and then this and then聽 sharp ratio of this now after this after clicking聽聽
[396]
this I would actually see that my strategy is good聽 or bad because if the sharp ratio is also I would聽聽
[403]
say less than the bitcoin shop ratio then it means聽 that my strategy is actually really really bad and聽聽
[409]
I should work on making more strategies and it's聽 more yeah no no okay my strategy has actually the聽聽
[415]
sharp ratio is also lesser than the sharpness聽 of bitcoin so strange so yeah so the strategy聽聽
[421]
that I made it has a lesser return than a lesser聽 absolute return than the bitcoin and the lesser聽聽
[427]
risk adjusted return than the bitcoin it means聽 that it was really bad strategy no worries our聽聽
[432]
main focus is to do bad testing right so that's聽 why we do back testing so that we avoid these聽聽
[438]
bad strategies in our real life so daily if if you聽 talk about the average daily return right how for聽聽
[446]
board so let me just average bot I鈥檓 sorry f daily聽 bot average return will have to compound it for聽聽
[454]
one day right so the return is one plus bot return聽 and then it should be compounded by some number so聽聽
[463]
see what are like how many five minute intervals聽 are there in a day so in a day we have 24 hours聽聽
[470]
and in one hour there are 60 minutes it means聽 that five sorry 12 5 minute intervals right聽聽
[476]
so basically we have 288 so we'll have to do it聽 like this and then minus one so the daily bought聽聽
[483]
average return is minus one point actually let's聽 make it in percentages so I鈥檒l have to multiply it聽聽
[490]
with 100 so it's one point three eight six聽 one or six two percent which is a negative聽聽
[497]
similarly for if we talk about the daily聽 bitcoin average return in this time period it is聽聽
[504]
minus point seven seven percent so in one聽 day on an average bitcoin has lost point聽聽
[510]
seven seven percent in last two months that's the聽 that's really horrible because of this crash of聽聽
[517]
bitcoin from around 60 000 to around 30 000聽 level which is like 50 depreciation in the聽聽
[523]
value of bitcoin in the last two months and our聽 bot has actually made more losses so it's minus聽聽
[530]
1.388 percent daily average return of our bot聽 interesting so the excess return if I talk about聽聽
[539]
in daily sense so excess daily average return聽 is basically this minus this it means that if聽聽
[550]
I had invested directly in bitcoin without any聽 strategy I would have made 60 sorry 0.61 percent聽聽
[558]
more than the like more than having the bitcoin聽 trading strategy actually I should not say it聽聽
[564]
like more than this one I would have lost聽 this much amount of money less than the聽聽
[569]
amount that I lost while making the strategy聽 that was the thing so in daily average sense also聽聽
[576]
we have a significant difference of 0.61 percent聽 that's a lot daily average in last two months聽聽
[584]
so not only like we'll have to work on increasing聽 our return when the strategy will have to work on聽聽
[589]
the and decrease decreasing the standard deviation聽 as well which is risk as well because I am very聽聽
[595]
much interested in the sharp ratio and our shop聽 ratio is actually less than the sharp ratio of the聽聽
[599]
bitcoin buy and hold strategy which is a point聽 of worry because in this case we'll have to make聽聽
[605]
another strategy or we'll have to like play with聽 this engulfing strategy in order to decrease the聽聽
[609]
standard deviation as much as possible such聽 that we have a more a higher sharp ratio of聽聽
[615]
our strategy and similarly we'll try to like the聽 first thing is to increase to have a higher sharp聽聽
[620]
ratio and then second I would say aim would聽 be to increase the absolute return as well聽聽
[625]
such that we have some excess daily average聽 return of our bot with respect to the the buy聽聽
[631]
and hold bitcoin so that's the idea this number聽 the I would say having a negative access a daily聽聽
[635]
average return is not that threatening because the聽 absolute return does not make a very good picture聽聽
[640]
of the trading strategy the better understanding聽 would be to to analyze the risk as well聽聽
[646]
now how to analyze the risk the first thing is聽 to do the sharp ratio analysis and the standard聽聽
[650]
deviation analysis that we have already done in聽 order to see the risk adjusted return the second聽聽
[654]
thing is actually the value at risk model now I聽 have done I have made more videos on value at risk聽聽
[662]
previously you can again like I鈥檒l put in that's聽 in this I button and in the description box so聽聽
[668]
I鈥檝e made videos on value at risk using parametric聽 method and value risk using monte carlo simulation聽聽
[674]
those videos were specifically made for the聽 value address analysis and in this like video聽聽
[680]
I will be referring to the codes that actually聽 made in that video so basically we first made a聽聽
[687]
normal distribution function and then then we took聽 the I would say five percent from the left to get聽聽
[694]
calculate the value at risk so import matplotlib聽 dot pi plot as plt I think I鈥檝e already imported聽聽
[700]
it anyway so I鈥檝e i've done it again now I will聽 be using from scipy dot stats import no nom is聽聽
[708]
the library that is used to make the normal聽 distribution function I鈥檒l run this now I鈥檒l聽聽
[713]
have to make an x axis and then on that x-axis聽 on the basis of the standard deviation and the聽聽
[719]
the the mean return we will be making the normal聽 distribution so x-axis will be the evenly spaced聽聽
[727]
numbers from any particular number to聽 another number so I鈥檓 just like making an聽聽
[731]
evenly spaced x-axis here so that can be done聽 using np dot a-range let me do it from minus聽聽
[740]
0.5 to 0.0 0.5 and then like this so if I run聽 it like this so I have the array from minus 0.5聽聽
[749]
to 0.5 ah with an interval of 0.01 now the norm聽 dist is equal to norm dot pdf which is probability聽聽
[757]
distribution function I鈥檒l take x and then what聽 return is the average return of the pot which is聽聽
[763]
actually water tonight I bought return and bought聽 standard deviation it's like this now plt dot plot聽聽
[768]
x and then norm dist let's take color to be red聽 plt dot show perfect so we have got the normal聽聽
[774]
distribution we'll have to work on the the x axis聽 to make it look I would say good let's do it like聽聽
[781]
this point 0.05 to this one and let's increase聽 it by this we'll have to work a little bit more聽聽
[787]
should be like point this one to this one that's聽 perfect that's perfect a normal distribution聽聽
[793]
and now I鈥檒l be referring to this portion a聽 straight line from this portion such that we聽聽
[799]
have the value at risk of this much I would say聽 portion of the normal distribution so I will be聽聽
[805]
let's see the confidence interval to be point聽 zero five which is five percent the value at聽聽
[812]
risk is basically norm dot ppf and then we'll聽 we'll have to take this confidence interval聽聽
[818]
this is actually the parametric value address聽 that I鈥檓 talking about we can do the same thing聽聽
[821]
with my with monte carlo simulation as well聽 again like I鈥檝e made two different videos聽聽
[827]
covering these two different topics you can聽 refer to those videos to know more about it聽聽
[832]
so this is the parametric normal distribution聽 sorry parametric value address calculation so聽聽
[837]
norm dot ppf and then confidence interval and聽 then bot return and then port std okay so there's聽聽
[843]
this is not ppt this is ppf so yeah the value at聽 risk is this much if I multiply it with 1000 sorry聽聽
[852]
100 you can see that in one interval yeah because聽 like we have taken this spot return and this for聽聽
[859]
one enter which is five minute interval so yeah聽 in one interval our bot will not make a loss聽聽
[866]
of more than point six seven percent with a聽 confidence interval with sorry with the confidence聽聽
[872]
of 95 percent so I鈥檓 95 percent confident that聽 my bot will not make a loss of point six seven聽聽
[881]
percent in just one candle that's the main聽 idea of value at risk now and you'll say that聽聽
[887]
like why do I care by just saying one candle聽 like you'd be more interested to see what聽聽
[893]
will be the value at risk of one day right聽 so in that case we can directly do it by聽聽
[897]
doing it like this 288 so var of a day would be聽 var of one candle multiplied by np dot sqrt which聽聽
[908]
is square root of n so you can say that yeah so聽 we'll have to do it multiply by 100. so you can聽聽
[914]
say that in one day I am 95 confident that my聽 bot will not make a loss of more than 11.364聽聽
[925]
which is also a big number very big number but we聽 are talking about the bitcoin we are not talking聽聽
[929]
about stocks like apple right or Coca-Cola this聽 is a very volatile security and that's why the聽聽
[935]
value at risk is saying that in one day I鈥檓 95聽 confident that my what will not make a loss of聽聽
[941]
more than 11.36 percent right this is the main聽 concept of failure risk you can do the same聽聽
[948]
calculation for one month or one week as well you聽 just need to like multiply it with the square root聽聽
[954]
of this number now two things are remaining聽 one is confidence interval another thing is聽聽
[959]
drawdown I鈥檒l be talking about drawdown in my next聽 video so let's make our confidence interval here聽聽
[963]
of one candle right so yeah the count is basically聽 the number of observation in that which is a聽聽
[970]
final df if you remember final df dot shape and聽 then the first one which is this one as we see聽聽
[977]
it was around 17 000 yeah so this is the count we聽 need count to calculate the confidence interval so聽聽
[982]
the lower is basically yeah considering 95.5聽 percent confidence interval I鈥檒l be taking聽聽
[987]
the value of z to be equal to two so the lower聽 confidence interval is what return minus two I鈥檓聽聽
[994]
considering 95.5 percent confidence interval聽 that's why this is the value of z is equal to聽聽
[999]
2 and then bought std divided by np dot sqrt聽 and then count lastly the upper is equal to this聽聽
[1008]
plus this so you can see that my I don't know聽 like how will you will use this information but聽聽
[1015]
yeah it's better to make confidence interval of聽 one candle and even one day so what's the range聽聽
[1022]
of returns of your of your bot I would say聽 of your trading strategy in one day or one聽聽
[1029]
candle or one week or so such that you have聽 an idea that this you like you can say that聽聽
[1034]
you're 95.5 percent confident that it will聽 go up to this much or down to that much so聽聽
[1039]
you know what is the range of your investment聽 so I was a very very important parameter for聽聽
[1044]
back testing as well again for back testing聽 in my opinion the most important thing is to聽聽
[1049]
see the the sharp ratio because that reflects聽 the risk adjusted return and all these other聽聽
[1057]
parameters also very important which should聽 be considered to do the entire back testing聽聽
[1061]
analysis and you just don't refer to the the I聽 would say the absolute return because absolute聽聽
[1068]
return will give a very biased view set viewpoint聽 of any particular trading strategy yeah in my next聽聽
[1074]
video we'll be talking about the drawdown method聽 which is actually a different video and I鈥檒l be聽聽
[1080]
referring to some different model as well in that聽 video but that there will be an application of聽聽
[1086]
that model in this bot in order to have a better聽 back testing analysis and that would be an amazing聽聽
[1092]
video and you should definitely follow that video聽 as well let me know if I missed some important聽聽
[1098]
thing here for back testing analysis mention in聽 the comment section I鈥檒l be making more videos on聽聽
[1102]
back testing and maybe like from my next videos聽 I鈥檒l be covering those things as well so just聽聽
[1107]
write it in the comment section and that would聽 be really really helpful and if you don't know if聽聽
[1113]
something is missing just mention in the comment聽 section that you like this playlist because this聽聽
[1118]
would be really really helpful with the algorithm聽 YouTube algorithm and my videos will get a very聽聽
[1123]
good I would say recommendation to people so聽 just please support me like this video and聽聽
[1129]
comment something great about my video and if you聽 like my work you can tip me by buying me a coffee聽聽
[1135]
and use dollars or euros or Indian rupees the聽 links of all this I would say buy me a coffee聽聽
[1141]
is in the description box and after watching these聽 three videos you at least like can buy one coffee聽聽
[1147]
to me if you want of course yeah also as I already聽 mentioned in my last video as well the gift to all聽聽
[1154]
my subscribers is that I share my google drive聽 access and I鈥檝e shared all the information of聽聽
[1159]
how you can get a free access of my google drive聽 in my last video and in my next video I鈥檒l be聽聽
[1163]
talking about the drawdown analysis of our trading聽 bot strategy thank you so much for watching this聽聽
[1168]
video till the end you can click here to subscribe聽 to my YouTube channel and over there you can see聽聽
[1172]
the drawdown analysis if you can see that it means聽 that I have already uploaded it if you cannot see聽聽
[1177]
it just wait for one more day it will be there聽 and over there you can see how I created this聽聽
[1182]
bitcoin day trading bot to do all this analysis聽 and see you in my next video thank you so much