Build a Bitcoin Day Trading BOT in Python: Just 1 Hr [2021]ūüĒī

MIC test MIC Test 
[ūüėá Follow Captions to learn more ūüėá] hello everyone I'm Ritvik Dashora and I'm¬†
back with a new video and some new learnings   in this video i'm gonna show you the key to 
creating a profitable bitcoin trading bot and   i bet you you are going to love this key concept 
that i'm going to show in this video. Bitcoin, you   can see although it's not a big difference the 
spread or the alpha is just 0.08 percent but still   it has some alpha but something of my more 
interest is that the sharp ratio of our bot is   more than the sharp ratio of the bitcoin buy and 
hold strategy we have created something great here   in this video, we will make a bitcoin paper 
trading bot using binance WebSocket in python   also being a finance professional I will keep my 
focus more towards the implementation of a trading   strategy in the bitcoin trading bot then just 
like making a very hard to understand algorithm   and guys we don't need to open an account in the 
binance platform to make this trading bot we   are not spending any money today because we 
are we will create a paper trading simulator to   make this bot if we are success successful 
then yes we can trade in binance as well   like we can synchronize it with the binance 
api as well so before we get into that if you   are new around here hey I'm Ritvik and on this 
channel I turn you into a successful financial   programmer, here I upload videos on trading bots 
highly valuable finance APIs implementation of ai   in finance etc etc so that's something of your 
interest then smash the subscribe button and   hit on the bell icon also watch this video till 
the end and there is an amazing gift for all my   lovely subscribers also shout out to part-time 
larry as I got the motivation to make a video on   bitcoin trading board from him let's make the 
strategy and a prototype of the model this is   going to be a long video so sit back relaxed grab 
a coffee and enjoy this video I bet you this is   gonna be one of your best time investment of your 
life for our trading bot we will use a very simple   safe and cost-effective strategy which is known 
as core-satellite approach and this is basically a   strategy which is used a lot by portfolio managers 
and the concept behind this is that we invest a   significant amount of money into a benchmark or I 
would say in passive investment and then remaining   amount in actively managed investment so what we 
do is we follow a particular benchmark through   passive management and then we try to 
grab the alpha or I would say the extra return   additional return through the actively managed 
portfolio and actually I have dropped a link   of a cfa blog which explains what is the 
core-satellite methodology if you want   to know more then you can just click on that 
and read it so what is the core in our strategy   what we will be doing is we will invest 80 
percent of our portfolio into bitcoin directly   and then we will play with the remaining 
20 % in order to reduce the volatility   of our portfolio now what I am focusing is into 
reducing the risk as much as possible because   we all know that cryptocurrencies are 
highly volatile and I'm more interested to get   a higher risk-adjusted return, not just a higher 
absolute return so this is my main focus behind   making the trading strategy so again 80 directly 
in bitcoin and 20 will be actively managed   in this slide you can see that if we have a 
positive outlook if the bot has a positive   outlook then we will have the overall exposure 
of 80 percent which is passively managed   and 20 which is actively managed so the 100 of our 
portfolio will be into bitcoin if the bot predicts   that we will have a negative outlook then 80 
percent is still in passive management and 20   is short so we have negative minus actually 20 
percent which comes up to 60 exposure into bitcoin   so all in all the trading exposure on BTC which 
is bitcoin will be ranging from 60 to 100.

60 if   the bot predicts that the bitcoin will go down 100 
percent if the bot predicts that the bitcoin price   will go up to keep this video easy to understand 
I will use a very famous technical indicator AROON   in order to predict the outlook of bitcoin 
price now what is aroon how to use it I will   explain each and everything in this video now 
in order to do the technical analysis, we will   install this library called talib this library is 
specifically made for technical analysis of any   particular security very soon I'll be uploading 
a video just focusing on talib there are many   implementation and there are like many ways to 
install and to use this particular library that's   amazing library I will cover a lot of things 
in that video as well now let's see what is   aroon technical indicator so the word aroon is 
Sanskrit for dawn's early light the arron indicator   attempts to show when a new trend is dawning the 
indicator consists of two lines up and down that   measure how long it has been since the highest 
high lowest low has occurred within an n period   range and this is the formula that is used for 
calculating the aroon up and aroon down what we   will be doing is we will be using its oscillator 
let me just write it here aroon oscillator   it's here by the way the link of this website 
will be in the description box so you can   read the description of this technical 
indicator and there are many more as well so you   can refer this at this link for the description 
so aroon oscillator this this is the indicator   that we'll be using the aroon oscillator is 
calculated by subtracting the aroon down   from aroon up now this is a very important 
line which is the resultant number will   oscillate between 100 and minus 100 so it 
is range-bound between 100 and minus 100 aroon oscillator will be high when the aroon up 
is high and the aroon down is low indicating   a strong upward trend the aroon oscillator will 
be low when the aroon down is high and aroon   up is low indicating a strong downward 
trend so it means that if we have the   aroon oscillator which is close to 100 then there 
is a very high possibility of an upward trend   if the number is close to minus 100 then the 
very then there is a very high possibility of   a strong downward trend this is the 
concept that we will be using for this   trading strategy and this is the concept that 
we'll be implementing in our trading   the automated trades so the final thing to see 
before writing the code the parameters the trade   interval in our case will be just one minute so 
you're gonna see a lot of trades you can put 15   or 30 minutes or even one day in your case 
but in this trading bot, it will be just one   minute our aroon time period is 14 intervals if you 
remember I showed you the description of aroon  technical indicators it requires a particular 
number of time intervals in this case it's 14   amount that we are going to use for our trading 
purpose is one thousand dollars this is paper   trading simulation amount so you can make it a 
million dollar a billion dollar it all depends   on you in my case it's just one thousand dollars 
the last thing is that there is no transaction   cost although we can update the code easily if you 
want to consider the transaction cost as well but   in this case, there is no transaction cost so let's 
start writing the code these are the steps that   we will follow in the code we will install the 
WebSocket client first then we will install talib   then we will be providing all the required 
parameters that we discussed in the last slide   then we will create an amazing 
paper trading simulation platform   it will be an amazing thing I'm telling you then 
after that, we'll be using WebSocket and binance to   extract real-time crypto data in the end we will 
be making and implementing our trading strategy   and then we will run the code and then we will do 
our final and I would say result analysis that is   analysis of sharp ratio alpha etc to see the 
adjusted I would say, risk-adjusted return guys   I'm not moving forward if you're not liking 
this video just take one minute one second of   your time not even one minute to like this video 
down below and then keep on watching this video   I'm I guarantee you you will love this but i 
just deserve at least one like from you so like   this video and also subscribe if you don't want 
to miss out on my future videos and just write in   the comment box like nice video or keep it up 
or something like this it gives me motivation   and also helps a lot with the algorithm the 
youtube algorithm so please please please comment   something like and subscribe please and then let's 
watch this video it's amazing thing is coming   so rub your hands and let's start writing the 
code guys please come to google collab because   installing talib in google cool lab is fairly 
easy I'll tell you how to do it so I would highly   recommend you to come to google collab and then 
write code with me here the first thing we need to   do here is pip install WebSocket-client now what 
is websocket websocket is a two-way communication   platform where we not only extract data but also 
but can also push our desired data into any   particular server now for people who are like me 
who don't understand these tech buzz words then   basically we will be using this library in order 
to extract the real-time bitcoin pricing data from   binance platform for detailed information 
about WebSocket, I will very soon upload   a video just specifically focusing on websocket 
secondly we need to install talib guys for some   reason pip installed talib does not work and there 
is a different way to install talib in windows   and mac and google collab so that's why for this 
reason now I requested you guys to use google   collab for this particular code in order to you in 
order to install Talib in google collab there is a   very good answer by someone on stack overflow 
we need to use these lines and then paste it   here and then just run this code run this 
cell this will automatically install talib   in google collab i have posted the link of this 
this website in the description box you can   just click over there and copy and paste it 
here it will take I would say one or one and   a half minutes to install but yeah eventually 
it will install talib in google collab you can   see that i just ran this cell and it is installing 
talib it will take some time so let me just pause   this video for a moment and then come back it 
took me some time to install this talib library   so let me import all these important libraries 
that I'll be using here import websocket import   talib import numpy as np what else 
import json will be used here json   will be used here so these are the parameters 
that I provided as I already mentioned in the   ppt slides our aroon time period is going to be 
14 in this case the amount that I'll be using   for trading purposes one thousand dollars core 
trade amount which is the amount that will be   used in passive investment in my case in actually 
this trading bot's case we'll be using 80 directly   into bitcoin investment and then the remaining 
20 percent will be used for active management   and I have provided trade amount is equal to 
20 of the amount which is actively managed   so you have installed web socket we installed talib providing 
all the required parameters now we will create a   paper trading simulation platform perfect so guys 
we'll have to make two different functions   for buying and selling in this paper trading 
simulation let me just write it here paper trading   simulation functions commented here so we would 
require two different um functions def buy and   let me just provide two inputs which are actually 
the amount of money that we are buying say like   800 dollars of bitcoin to buy something like 
this so amount so let me just write allocated   money and then the price that will be used 
to buy that that much amount of bitcoin so price   so the quantity will be allocated amount 
allocated money divided by price right because if   for example, if I'm buying bitcoin worth of 800 
and say the price is 100 it's not 100 but yes   if the price is 100 then the quantity is 8.

so yeah the quantity is equal to allocated price   divided by allocated money divided by price and 
this much money that is used to buy bitcoin   should be subtracted from the account which is 
this 1000 so let me just write it here money   end is equal to amount for now and 
then I'll have to subtract this   much amount from the money end so 
money end it will be subtracted   it will be quantity multiplied by price 
also I need to I should have mentioned   portfolio here because we will be spending money 
and there will be there should be a portfolio   which should represent the number of bitcoin that 
we own so initially it's say zero and in this case   when we buy some amount of money then this 
portfolio should be increased should be plus is   equal to and then by quantity now in order to 
track all the trades what I should do here is   let me think yeah I should provide an empty 
investment empty list here and then I should   append all the trades in that in this particular 
list such that I can refer it in future   to see all the trades that have been executed 
like either it's buy or sell so for buy I'll have   to put investment dot up actually you know one 
thing I'll have to see if whether this list is   empty or not any in the starting if if investment 
is equal to empty list then investment dot append   and then just append this much which is 
quantity now actually I can do it allocated money   else again investment dot append allocated 
money but one more thing is that let's add   all the new investment in like let's add all 
the new investment with the last one so that we   will have an idea of what is the total amount 
that is being that that has currently been invested   so this list will not track all the transactions 
but it will track all the invested amount   right so what I'm saying is like in investment 
and then minus 1 plus is equal to investment   minus 2.

So what I'm doing here is say I have 
invested 800 dollars in bitcoin so what and   then there's one more trade that buy 100 dollars of 
bitcoin again in that case what I'll be what this   function will be doing is that it will add 100 
with 800 that is already been executed so the new   investment amount the last element of this list 
will be 800 plus 100 which is 900 right so this   is what I'm making it will be more clearer when I 
will be executing the trade so now let's write the   because I have mentioned money end it will be it 
will be getting subtracted by this much and then   portfolio will be increase and then investment 
this list will be made according to the   number of transaction that we are having now 
let's write a similar sell function which is   def sell again the same thing allocated 
money comma price so for selling also I'll   be required to mention the amount that is being 
sold so similar to this one to buy I'll have to   mention quantity is equal to allocated money 
divided by price now money and in this case, will   be added because I am selling so it will be added 
quantity multiplied by price and then portfolio   will be subtracted minus is equal to quantity now 
in investment what we need to do here is you know investment dot append and then we'll have 
to append this allocated money now because we are   selling this much amount of bitcoin will 
have to subtract yeah we'll have to subtract this   allocated money and then just add investment minus 
1 plus is equal to investment minus two like as we   did in in the case of buying so what I'm doing 
here is I have let me just check it again I have   calculated the quantity which is easy 
allocated money divided by price money end   is increased by price into quantity portfolio is 
decreased by quantity because I'm selling then   append minus allocated money so for example if I'm 
selling 100 dollars of bitcoin so it will be -100   will be the last element and then I'm adding 
the second last element with this one so minus   hundred and whatever the last element so exactly 
so it will be the remaining investment after   selling that particular amount of bitcoin perfect 
even there is one more thing actually I should   write it here because I am changing investment and 
no actually I am because I am changing money to   I'm sorry money end and portfolio I should provide 
here global because these are global parameters   these are not local parameters so global money and 
portfolio and then in the investment similarly for this one global money and portfolio 
and investment perfect let me run this code   yeah it's running so I think everything is right I 
hope we have not made any mistakes here so let me   check these functions if the price is equal to 100 and 
if I run this function which is allocated money   and then price so if the allocated money is 300 
and price is price but running it I should have   investment of 300 perfect because I have 
bought 300 worth of the security portfolio is 3 which is 300 divided by 100 
which is perfect and then what else money end money and is 700 perfect because 
we have used 300 of our 1000 amount   of portfolio perfect so this is this is 
the thing now if price is equal to   say 70 and then I am buying 500 of the security 
let's see what happens investment is 300 and   then why it's 800 yeah it's it should be 800 
only perfect because we bought 500 here we   have already bought 300 so the net investment 
should be 800 which is perfect and the portfolio   should be 3 plus something which is 500 divided 
by 70 if I write 500 divided by 70 let's see 500   divided by 70 then it's 7 point something so 
it should be 10 plus 7.14 which is 10.7.14   this is also perfect and then money end 
should be let me just think it before   running this so it was 700 before 
it should be 200 now so money end it's 200 perfect it's it's 
working perfectly now let's see   sell we already have 800 of our investment 
and suppose the price is now gone up to 150   so we are gonna make profit and we are selling 400 
of this on this price so in this case let's see   what should be the investment investment should be 
300 800 minus 400 should be there's there should   be one more element which should be 400 here which 
is exactly as I thought it's 400 now portfolio should be 10 and then because we are actually 
selling let me just write it here 400 divided by   150 this much amount of uh the security 
is being sold so it should be around eight   point something like seven point something 
so let's see the remaining portfolio is seven point one four seven exactly money 
end should be let me just think it before   running it it was 200 before and I and 
now I sold 400 so it should be 600 now   it's 600 so yeah it's working perfectly perfectly 
like the way I thought it's exactly the same so   now what we'll be doing here is we will 
be having different prices on the basis of   the real-time prices that we are extracting 
from WebSocket, you'll get to know about that   and on the basis of those prices, we will 
we will execute these paper trades and like   have a track record of investment 
list and portfolio and everything   perfect so yeah this is exactly what I wanted 
the next step is to use the WebSocket in the   binance platform to extract real-time crypto 
data perfect just three steps remaining   for the trading bot so guys this is the WebSocket 
documentation so let's start making it I'll   be using this ctrl c and then let me add some 
lines here perfect so yeah I'll be requiring this I need on open actually I don't 
need it on_open as well because   it's not necessary our message is important I'll 
tell you each and everything what all these things   mean so we need on message and 
then on close is equal to close   then we would require a socket here so let 
me just start writing it here like this I should provide it here socket   is equal to there would be a link here right 
which I will refer it here socket and then what link to use it here in socket because in this 
case if you can see that echo dot web socket is   used in our case what we will be requiring is 
we will be using binance link so this is the   binance documentation again the link will be in 
the description for you to refer you can see that   the web this the base endpoint for binance is this 
so we will require exactly like this copy it paste   it here and socket then after that we need slash 
ws slash stream name let me write it like this   slash ws and then slash stream actually 
stream should be the exchange let me just go   a bit down I'm referring to this stream 
name of k line which represents actually   to the candlesticks and it is actually this 
one let me copy it here and then paste it here   here each symbol should be BTCUSD yeah it 
should be btc usd for trading purpose okay perfect   so let me just write it here btc usd t and then 
interval should be in in this case in our case   it's one minute you can make your own interval as 
well so one minute and let me just make it   more user friendly so it will be cc is equal to 
btc usd and then interval is equal to one minute   and let's convert it into f string cc 
and this one to interval perfect I think that's it so this will be the socket 
that will be used here ws websocket and then   all these things so if I if i write 
it here like def on_close and then ws   just print a message say close and then again 
def on message if it's on message and then this   print message if I run this and then ws dot 
run forever I should be receiving messages this   message message message and then if I close it i 
should get the message close perfect now what is   what are these messages these are actually the 
messages these are actually the prices all these   things that we have seen here these things are 
here so if I print message here it will be   giving me all the messages let me just run it 
here so yeah so these are the candlesticks of   btc usd this is the real-time data that I 
am getting it's like second wise second data   although I mentioned one-minute interval 
it is showing me it is providing me second   per second data but the thing here is that if I 
go here in the end and all this x there will be   a true here you can see there's a true here so 
this true represents the the closing of one minute   so at this moment, the this candle was closed 
this one minute interval was close so we will be   using this particular candle in order to make our 
strategies and all this other data is not useful   let me just stop this code for now so in this 
output you can see that there's a dictionary   and then there's one more dictionary 
inside that dictionary which is k   and then inside k we have all 
the information such as interval   the close open price bitcoin is trading at 
around 57 right now 57 000 high low volume   etc and this x is important here so we need to do 
some data engineering here so let me just you know what I'll what I have to what i should 
do here is I should make it in the json format   so json message json message is equal to json dot 
loads and then message now if there's no need   of this now the candlestick which is cs um it is 
actually json message and then this one which is k   so k perfect now another thing is that uh that 
we are interested in is this x thing if this   x is where is it if this x is false uh then 
the candle is not closed if the x is true   then on that particular moment that that 
interval is closed and at this moment this candle   actually got closer you can see that after just 
after this candle all the highs and the lows are   changed because there's a different candle 
now after that so we are just focused on this   record which is this one so uh x is very important 
we'll have to check whether it's true or false so   let me just write a variable which is 
candle closed is equal to and then inside cs   I am interested in x right now let me take 
out all this close high and lows as well   because these are also important volume 
is not important in our case because   our aroon technical indicator it incorporates 
only high low and close so I can write here   as well so closes and close high and then 
low these are cs close as let me just check c high is cs and then inside this h and low is 
cs and then inside this low perfect now let's do one thing let's just print everything so 
print cs i don't need cs right now because cs k   i am just interested to print candle 
closed candle closed print close print high print low let's see what do we have so 
yeah the first is not closed and then this   is close this is high this is this low and 
then on every second it is taking the data   right let me just wait for the close sorry the the 
true data so yeah after some time i got this true   so this is this one is actually 
very important let me just pause   stop this cell and let me just go to this 
one true where is it yeah so true so this   data is the most important data that i'll be 
required that i'll be using for making the trading   strategy and the closing price is this one the 
high is this one and the low is this one for this   entire one minute interval so this is what i'm 
interested in i am not concerned about all these   data perfect let me go back to this code again 
so what we need to do here is now is we will make   a list of all these things close high low 
as we get the new data right so similar to   the investment thing like we are we were 
appending the the transaction the amount   the amount of transaction that we did in 
this investment list which was empty before   and similarly, we will be making 
many empty sorry many lists for closes   highs and lows so let me just go here and 
let me provide here close, highs and lows   one two and three and if I let me run this 
cell and come back here so so if candle   if candle closed is equal to true then in that 
case I would append closes dot append what will   happen uh I'll append this close this close and 
let me the float of the close similarly   for highs and lows, I'll be making this 
list of highs and this one is lows and here   high in here no so only if the candle is closed 
then in that case I'll start making this   candle sorry this list which is close, highs and 
lows so we comment all these things and then print   all these things so close this trend highs print 
lows let's see what do we get I'll have to wait   a bit in order to get the result let's see here 
um i after some time the first output has come   if the candle is closed in that case uh 
the the closing price is here the highs   is here and the low is here whenever the candle 
is close at all all those times it will append you   can see that the second one is here so the second 
minute ended i would say output is the first one   is the first close is this one the second close 
is this one the first high is this one the second   high is this one the first low is this one second 
low is this one and so on so this is how with   all the minutes it will be making this particular 
all these lists uh for closes highs and lows   perfect now if you remember in our strategy 
we were investing the entire like 80 of   the entire money into bitcoin whenever we run 
the code so we'll have to implement this first   and the remaining 20 percent will be used for the 
active management the core and satellite strategy   so let me just delete all these lines also i 
don't need to print these two things and it's   always good to print the closes and provide 
let me write some string as well so closes   yes so yeah this is how we will have 
all the closes now let's make the   and the condition that if the core trade the 
the first trade which is the core trade is   not yet executed then we'll have to buy 
the 80 of our portfolio into bitcoin right   and if it is already executed then in that case 
we'll have to skip that step right so if   the trade is already executed I'll have to 
provide one more variable there let me just think   yeah I'll have to provide one more value variable 
and then I will call it like this yeah so   just go here and provide here core to trade 
if the core is required like it's not yet   traded if core to trade is true then in that 
case I'll have to run this cell as well so if core to trade if it's true then 
i will buy 80 in bitcoin right so i am 80   actually, I have already provided this also by 
this much amount by this much amount so the   price on which we'll be buying the 80 of this 
would be the price of the last one which is the closest   and the last one actually I should provide it 
here as well the last price is equal to sorry   like this and then minus one perfect so if core 
go to trade is true then buy this much amount   and then how much quantity will it buy
so core quantity let me just do one more thing   let me provide a core quantity is equal 
to zero actually i forgot one thing here   i should make all these parameters global so in 
this message sorry on on message global closes   highs lows core to trade core quantity we are using 
this function here and this function we already   made these two parameters global and let me just 
write it again so money end and then portfolio   and investment right so cool yeah I'll come back 
to this one core quantity is equal to actually i   should add it because it's it was zero before core 
quantity i'll add it which is core trade amount   divided by closes and then minus one this will be 
the quantity that will be added to that parameter   which is called quantity and then in the end 
it will make this core to trade equal to false so   so the next time it will run these lines so core 
to trade was true in the starting you can see it   here and then in the end this core to trade will 
become false so these lines will not get executed   after the first execution so in the first place 
only we will buy 80 of our amount in bitcoin   and then we'll be remaining with the twenty 
percent of this amounts amount I'll have to   check it so let me just run this code and
let's run this code as well where the first the first can the first candle is 
closed already and I've got this   one let me just close it if i see investment perfect so I have bought 800 of bitcoin and the 
like using these lines and if I see the portfolio   it should be there portfolio is already this one 
so this much bitcoin that's point zero one bitcoin   has been bought using this 800 dollars and 
money end should be equal to 200 now this gets   perfect so the remaining 200 will be used for our 
trading purpose this is great now let's move on to   the next part of this code so guys our bot has 
already implemented this 80 of our portfolio   in bitcoin now the most important part which 
is this 20 of this actively managed portfolio   just to remind you again this 20 will be used 
to reduce the volatility as much as possible   such that we have the
overall volatility of our portfolio   lower than the actual volatility of bitcoin and 
I'm only interested in risk-adjusted return I'm   only interested in sharpe ratio, not interested 
in the absolute return because bitcoin as everyone   knows it is very very volatile so yeah we have 
already implemented this 80 percent of our trade   20 is remaining we will do the analysis of aroon 
and then we will make our like some lines to make   the trade so let's go on now I just added 
this line which is a print core investment   we bought dollars and then the amount which is 
actually 800 if you remember we've already seen   it like this worth of bitcoin yeah there's this 
one more thing actually i forgot i should provide   these as well in the global parameters because 
these are the parameters that we are changing   to so sorry not this one core quantity so core to 
trade and code quantity in the global parameters   now let's jump to the to the aroon analysis 
so aroon is equal to talib dot yeah so it's   aroon oscillator this one and then inside this i 
need to provide high low and then time period so   we have highs aroon like this and let me 
just run this code as well I'll have to wait   for a minute because let me pause this video 
for a second as well when we run this cell   it takes some time to make the candles because 
whenever the candle closes you can see like this   it provides that it at that time because if the 
candle x the value of x is equal to x is equal to   true then only it will make the candle and 
then print it so closes there's the first close   and then the core investment we bought 800 worth 
of bitcoin and then print aroon it was nan   why it's nan because the aroon time period is 
14 and if you remember because it's 14 it will be nan for 13 periods and then 
from the 14th one it will have some value   which will be if you remember it will be between 
100 and -100 let me pause let me stop this cell   let's check again what is investment should be 
800 portfolio should be something similar to that   so this much amount of bitcoin has been bought 
the money and should be 200 again perfect now   what we need to do here is we will do some 
further analysis first of all I'm only   interested in the last aroon because whenever 
I will be placing the trade at that time I will   be considering the last aroon value not any 
value which is before the last one so the last   aroon and then the last one right it's
it's better to round it in two decimal points so   like this so let's do one thing print 
and then last sorry last aroon and   in order to make it easy to understand let me 
just provide this 3 that is that one period   and the cell and this one as well and then print 
last one so I'll have to wait for three minutes   in order to see what is the first aroon 
value so let me run this code again pause   this video for a moment and let's see what 
happens so we can see that after some time like after 3 intervals it 
has provided a value of hundred   so as per aroon indicator having just three 
periods three time period three interval of time   period it is predicting that hundred percent 
of the exposure should be in bitcoin because   it predicts that the next enter and the next I 
would say close will be higher right this is what   it means and then the next one was actually a bit 
higher you can see it it is a bit higher but yeah so at this moment again it is recommending us 
to have hundred percent exposure because it   still predicts that it will be higher but in this 
case it actually went down a bit and similarly   this the bot also changed the exposure to 66.67 
and then after that only zero percent exposure   it can go negative as well yeah you can see that 
the next one is negative so the bot is predicting   that the next not the bot actually the aroon 
indicator technical indicator as predicting that   the next candle will be a downward trend and it 
is predicting a 30 minus 33.33 percent exposure in   that by saying exposure actually it's not exposure 
it's a number between 100 and -100 and what I am   assuming here what I'm considering here is that 
if the number is 60 66.67 percent then I'll have   66.67 percent exposure into 
bitcoin so what I'm doing is that twenty percent   of investment remaining investment is just two 
hundred dollars so of that two hundred dollars   sixty six point six seven percent should be in 
long position or 33.33 percent in short position   position and so on so this is how I'll be making 
this bot now why all these numbers are so   straightforward like 100 166.670 and minus 33.33 
and minus the 33.33 because the time period   I've provided is just three just to have all these 
numbers because I'll have otherwise I would have   been required to wait for 15 minutes to 
check all this solution so that's why i'll in the   end i'll change this to 14 only but for now this 
is just three so this is the interpretation what   again repeating what I'm what I'll be doing 
here is I'll be taking the last aroon and then on   the basis of the last aroon value I'll be getting 
the exposure of that remaining 20 percent of   the portfolio this is our uh strategy for the 
satellite I would say the active management   let me pause let me stop this cell for now 
so let's come back to the code let me delete this line again and the next 
one would be amount so the amount is equal to   last aroon and then and last multiplied 
by trade amount divide by 100 so trade amount   divided by hundred so what I'm doing here is if 
the last one is for example in this case 66.67   so it will be multiplying 66.67 to the trade 
amount which is 200 if you remember 200 is for   the active management because 800 is always 
already invested in bitcoin divided by 100   in order to get the exact amount that is required 
I would say that the recommended exposure   and then let's see the portfolio value if it 
is already executed if some of the trades are   already executed using this aroon methodology 
i would i would say our own analysis then   i should have a port value as well now this 
port value is not the entire portfolio value   this port value uh is referring to only that 200 
dollars so I would say 20 of the portfolios   value so this is this should be 
portfolio multiplied by the last price   what was that yeah it was last price only the 
last price minus and then what was the quantity   for the core investment which is code quantity 
because here we have increased the core quantity   and then so minus core quantity multiplied by 
the last price so this is the net investment   or I would say this is the net portfolio 
value of that active investment so that's why   I'm reducing I'm subtracting this one hence so 
in order to check the actual trade amount and   let me just write trade amt because trade amount 
is already taken it should be amount minus port   value so by saying it like this I mean that this 
is the amount that will be required to trade to   buy or to sell let me explain it again so if in 
this case 66.7 or actually, in this case, 100   was the aroon value right so this this the amount 
will be 100 multiplied by trade amount which   is 200 divided by 100 so the entire 200 of
exposure this is the recommended exposure this amt   is recommended exposure using the aroon technical 
indicator so the entire 200 will be in the buy   side so we will buy it how to do it I will put 
this buy function here after some time and then   the next one is 66.67 so in this case the 
last aroon is 66.67 multiplied by trade   amount which is 200 divided by 100 so this 
will be different value and the recommended   exposure has decreased because of this change 
the portfolio value will be in this case i put   the entire portfolio value multiply the last price 
minus the core one and then the remaining the   the trade amount will be this the recommended 
exposure minus the actual portfolio value of   the active management part which is the 20 of the 
portfolio so this will be the amount so if   this is negative then we'll have to sell if this 
is positive then we'll have to buy more bitcoin   I think like uh writing all this buy and sell 
functions will make you like more clearer so   let me write remaining lines which are actually 
i should put this line as well which is print last   aroon is last aroon and then recommended exposure 
is empty as i already mentioned amt is recommended   exposure trade amount is the amount that will be 
required to trade whether it's buy or sell in this   case if you remember we should have sold some 
amount some like 33.33 percent of the actively   managed which is 33.33 of 200 dollars in this case 
so the recommended exposure and then our last   aroon is this one and actually, I should write
the real-time portfolio value as well because this   portfolio value is just off that the actively 
managed portfolio value this which is the 200 not   200 actually 20% of the portfolio's value I should 
have the entire portfolio value as well right   so real-time portfolio value is equal to what will 
be that it will be port value and then plus the   the core value which is the core quantity 
multiplied by last price and then also the   money end also similar to and I think similar 
to the closes highs lows and investment list   we should have I would say a list for the 
real-time portfolio value as well as that we track   the portfolio value for with all the 
trades or with all the intervals so i   should provide a real-time portfolio list 
here real-time portfolio value is equal to   and then an empty list and similar to this 
this closes highs and lows I will   append this value sorry real-time portfolio value 
dot append then let me take just the float of   rt portfolio value and then in the end let's 
just print this as well so f and then inside that   and then inside that the real-time portfolio 
value is equal to dollars and then real-time   portfolio value which is this one and then 
this let me print the trade amount which is   trade amt let me print port value as well 
print port value I have I'm already printing   this one and then real-time portfolio value 
is also here so I can do it like this print   and then real-time portfolio value let's see 
let's see what happens so if i run this code   I don't need this one so I'll delete one this 
one and then let's run this code as well again   I'll have to pause this video for some time and 
then let's see what is the result so we've got   the first candle so the closest this one the core 
investment we bought 800 worth of bitcoin and   then nan what is this nan okay the uh the trade 
amount is nan and then it is because   amount is nan because last aroon is nan in this 
case also port value is equal to zero because   we have not invested this remaining 200 anywhere 
it's just 800 for now last aroon is nan the   recommended exposure is also nan so there's no 
problem with that the real-time portfolio value is   still 1 000 because uh the 800 is invested in this 
one and the remaining 200 is already present as   oh yeah it's actually present in the account 
only so yeah the second minute has gone and the   okay where is it yeah the first close 
is this one the second close is this one   again nan because I'll have to wait for three 
candles to pass for my first aroon value this   should be zero the recommended the last aroon is 
nan the recommended exposure is nan you   can see that there is some increase in the in the 
port in the real-time portfolio value why because   bitcoin value has increased you can see from 
this one 770 it has increased to 761.

So this 800   worth of investment in bitcoin has increased a bit 
which is like 0.572 and this is the one and then   real-time portfolio value is also a list which 
is which we are making which we are appending   with uh new values so and the first one is one 
thousand second one is one thousand point five   seven to something let's wait for the third 
one yeah so the third one has come 1 000   nan this one yeah so uh 5000 sorry 57 
000 something then this one and then   the bitcoin value is again increased again 
our aroon value is nan it's zero the aroon last   value is nan recommended exposure is nan for the 
remaining actively managed portfolio uh real-time   portfolio value is increased to one zero one point 
of uh one zero one thousand one point nine one one   eight four so we are making money now because we 
have invested 800 dollars in bitcoin and the value   of bitcoin is increasing with all these intervals 
we can see that the real-time portfolio   list is also increasing like we are adding
different elements with different candles let's   wait for another candle and see what happens 
so yeah the next candle has come and yes this   one the first candle is this second third and then 
this one so the bitcoin value has decreased a bit   this is 200 why this is 200 because uh you 
can see what is this 200 uh yeah trade amount   trade amount is 200 and then portfolio value is 
zero because we have not yet invested anything   the last aroon is 100 and then recommended exposure 
is 200.

You can see that our bot is predicting   that the next candle will be high like the 
the price of the bitcoin will increase in   the next candle that's why it is recommending us 
to have the hundred percent exposure for that so   what is the 100 exposure which is 200 because 800 
is already invested in our core investment so   we have bought we should buy this much amount 
of money and then real portfolio value is again   this one again in this case actually here I should 
have written a buy function just to buy this much   amount of money so let me just pause this sorry 
stop this cell here in this case there has to be   there should be a trade of 200 dollars of bitcoin 
right and in this case we should sell some part   of this actively managed portfolio because the 
last aroon is 66.71 it is recommending a 66.71   which means a recommended exposure of 133.34 
so the remaining amount should have been sold   in this candle so let's that's absolutely fine 
let's just put some of the values here let me just   delete like I have checked this thing 
so I don't need to print this again   let me just delete this as well so in the last 
one if you can see that if yeah this thing   which what is this thing this was trade amount if 
you remember so if the trade amount is positive   right then we will buy this much amount of 
money like we should we should have bought   the amount of money which is trade
amount if it's negative then we will sell   this much amount of money very simple so if 
trade amount is greater than zero then buy   which is this one buy how much uh trade amount 
amt and what should be the price price is equal to   the last price and similarly if the trade amount 
is less than zero let me just    fine trade amount is less than zero in this 
case we will have to sell this much amount the   absolute value of the trade amount which is 
sell and then abs because it's a negative i'll   have to provide minus trade amount and then 
comma price is equal to last i just wrote all   this thing the print we bought dollar then trade 
amount this much amount of money worth of bitcoin   these are just two empty lines and then similarly 
for this one we sold a minus trade amount worth of   bitcoin I think that's it for this one it should 
run now so let me try from the starting   run the cell run the cell and then this one 
and then let's run this and let's wait for   a couple of candles like four or five 
candles to come and then we'll see that it is   executing the orders or not the first one has come 
you can see the closes uh this is the last closing   price the core uh investment we bought 800 worth 
of bitcoin that's fine and last our own is n a n   why it's nan because that time period we provided 
for our aroon analysis is three we'll have to   wait for three candles recommended exposure is nan 
the real-time portfolio value is 1 000 because we   just invested 800 and bitcoin the remaining 200 
is already present in our bank account so   yes after some time we can see that the first one 
was we bought 800 worth of bitcoin and the second   one because there was no recommended exposure 
because the aroon's value uh was zero sorry it was   nan at that time we don't have any trades after 
three candles the fourth one the last aroon value   was minus hundred the recommended exposure is 
minus 200 because the entire 100 percent is   short in this recommendation and then you can see 
that the the real-time portfolio value is actually   decreasing because the value of bitcoin is going 
down in this case and this eight hundred dollars   which we invested is going down ah so yeah in this 
case we sold two hundred dollars worth of bitcoin   so now that uh now the exposure is the overall 
portfolio exposure is just 600 because we had   800 and now it's only 600 of this one actually 
it's a bit less than 600 because the value of   bitcoin actually went down a bit anyways so and 
then the next candle as for the next candle the   last aroon's value is six minus 66.67 uh it was 
minus 100 before so we'll have to buy something   yes we bought something the recommended exposure 
has increased to minus 133 percent from minus   200 percent and the value of portfolio real time portfolio
is this one we are in loss right now uh we bought   66.88 in this case and then in the next candle 
it is minus thirty three percent again we'll have   to buy something and so on again the next candle 
has come which is which is zero so we'll buy sixty   six point six three uh dollars of this one so 
yeah let's see what happens uh if we run the   investment variable so we can see that we can 
easily track all these things so investment was   800 in the first case in the second case it was 
let me just wait for this thing so yeah it sold   200 of this one so it went down to 600 and 
then it wanted us to buy this one this much   amount of value now this is not the real-time 
portfolio value this is the invested amount so   that's why uh this is exactly 600 dollars now we 
bought 66 so it is six six is six point something   then we bought 66 again we bought 66 again so it 
has come up back to around 800 dollars level again   the portfolio should be yeah it is zero point 
so right now after all these things we own point   zero one three eight seven seven uh bitcoin 
in our portfolio the money end should be   around 200 yeah because it's this one this much 
here and uh 1 000 minus this much is this one   now there's a problem here because you can see 
that in investment list uh in the end   even after uh like stopping this cell we 
have 800 worth of investment in bitcoin now   what I need here is that whenever I stop this 
bot uh by clicking here I should sell all the like all the investments that i have made 
in bitcoin so in order to do that thing   we need to write some lines in in this def on 
close function okay so let's uh write it like   this portfolio value is equal to portfolio 
cross closes and then -1 if if portfolio   value is greater than because it can be greater 
than zero or less than zero then in that case sell   everything like which is portfolio value and in 
this case price is equal to closes and then minus   1 else buy if the portfolio value is negative 
then in that case in that case buy negative of   portfolio value and then price is equal to closes 
minus one and in the end what we need to do here   is we'll have to add it back to the money end 
which is the value in the account after settling   all the trades so money and plus is equal 
to investment and then minus one that's it now   let's just change it to all trades settled i think 
that's it uh now let's uh run this code and let's   see what is the outcome guys we have made an 
amazing trading bot i'm telling you so i ran   this code and i waited for a long time and then 
you can see that this has made a lot of trades   you can see the different exposures here right the 
portfolio value is zero in the end the money is uh   actually we started with one thousand dollars of 
portfolio value if you remember it's actually one   zero zero four we made some profit in this much 
amount of time and if i just check the bitcoin   uh return the benchmark return it's 0.35 or bot 
has given us an extra return which is actually   0.43 percent and this small period of time you can 
see that we have generated some alpha here so yes   we have made something great but as i promised 
you i'm not interested in the absolute return   i'm interested in the risk adjusted return 
yeah so you can see that the bitcoin   risk is standard deviation of logarithmic return 
of the of the benchmark which is the bitcoin and   the standard deviation of our portfolio return 
is the bot risk now the best way to check it is   through sharp ratio uh i'm considering here the 
risk-free rate is equal to zero because of this   very low-interest rate environment the risk 
rate is close to zero percent only so if you   remember the shop ratio is actually portfolio 
return minus the risk for rate divided by the   standard deviation if risk rate is equal to 
zero then it the sharp ratio will be equal   to the portfolio return divided by the standard 
deviation of that portfolio and then if you see   the sharp ratio in this case is 2.445 and if you 
calculate the sharp ratio of the bot then the   sharp ratio part is similar to this one which is 
bought return divided by bot risk and then this is   the sharp ratio you can see that the sharp ratio 
of our bot is more than the sharp ratio of of the   of uh benchmark which is uh like buy and hold 
bitcoin so we have made some alpha we have we have   a higher sharp ratio because of the lower risk why 
we have the lower risk because you can see that we   didn't have the entire hundred uh or the entire 
1000 dollars of exposure into the bitcoin all the   time we had varying uh exposure and you can see 
that because of this varying exposure in bitcoin   of course it has decreased the volatility 
but somehow it has generated more returns   than the buying and holding bitcoin you can 
see although it's not a big difference the   spread or the alpha is just point zero eight 
percent but still it has some alpha but uh uh   something of my more interest is that 
the sharp ratio of our bot is more   than the sharp ratio of the bitcoin buy and hold 
strategy we have created something great here   i guess you have learned a lot with this trading 
bot guys my next few videos will be on trading   bots only and i'll be implementing many 
strategies uh using the trading bots as i already   mentioned you I'm a finance guy I'm not a 
tech guy I just learned all this technology with   time but my mindset is always finance oriented 
so i will be implementing very uh core finance   strategies using this uh trading bots this 
is just a basic strategy using aroon technical   indicators there can be a lot of things like uh 
using fundamental analysis using sentiment trading   and a lot more um and then there can be 
some macroeconomic parameters as well like   PMI etc so there's there can be a lot of things 
which can be incorporated in this trading bot   which I'll be using in my future videos so stay 
tuned subscribe and also don't forget to follow   me on all these social media handles I started 
managing all these social media handles currently   so please please go and follow me everywhere i 
love you guys and let me tell you the gift for all   my subscribers this amazing million dollar code 
will be saved on in my google drive and my already   existing subscribers they don't they know that i 
share my google drive with my subscribers so this   is the gift for my new viewers if you want to uh 
get a free access of my google drive then you can   just go to my channel subscribe and then click 
on access to my google drive button and then fill   some basic details and i'll be using this email 
address in order to give the free access of my   google drive there are a lot of things saved over 
there and everything is absolutely for free and   there's no copyright issue if you want to reap 
some additional benefits and want to support me   to keep you motivated to post these types of 
educational videos then you should consider   becoming a patreon member on patreon you 
can see that there are several levels   and then with different levels i provide different 
services things like patreon only post and goal   category things like tip of the day early access 
to all my codes and etc for platinum the whatsapp   group access google hangout for legend i shared 
the trading strategy of the month some premium   fintech posts code of the week etc and in the 
god level you can see all these benefits and   you can see that it is fairly affordable and 
this all all your money will be used for very   good purposes only so please consider 
becoming a patreon member and support me   the patreon link is in the description box you can 
also buy me a coffee by clicking on the link which   is buy me a coffee which is in the description 
box and then select one three or five number of   coffees this is a very small amount that is that 
will be used for very good purpose and this is how   you will keep you motivated to post more videos if 
you like this video then click on the like button   also share it with your friends and i guarantee 
you that your friends will love this video you can   support me by clicking here down below on patreon 
click here to subscribe to my youtube channel   if you don't want to miss out my future videos 
and there you can find some recommended videos   very soon i'll be posting videos on talib 
and other things like WebSocket and making   a paper trading platform etc so stay tuned 
there's a lot of things uh there that are yet   to come here thank you so much for watching this 
video till the end i'll see you in my next video

You May Also Like