PYTHON TRADINGBOT SIMULATION TUTORIAL (with code)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome in this video/ potentially video serious I'm going over building a trading bot simulation in Python the idea is to extract the live price feed for the Bitcoin price extract relevant information out of it and apply certain logic to the data feed so that you can design a trading strategy out of it later on to be more specific I will design a life pric Bitcoin feed and calculate the price development over a 30second period This Time Horizon is just made up to make the concept clear here you can do whatever time Horizon you're interested in I picked the very short one obviously just to be able to Showcase it in the video so the challenge here is to get the price as of now set it into relation to the price within his 30second window meaning after 30 seconds you will need an updated price Anchor Point and the calculation starts again with that you can design a certain strategy like if the price was rising or dropping by a certain amount within a certain period of time you want to end enter into the position besides the buying condition you can also use that to implement the take profit stop loss or trading combination out of those very important to mention we keep the price development window fixed meaning after 30 seconds have passed you reset the price development calculation so you start from scratch again meaning from the price after the 30 seconds have passed so your price development at that point is zero so you start your k calculation from that point on again the more dedicated approach here is to have a rolling calculation meaning you always consider the price 30 seconds before the current price without re-anchoring so you keep the look back window rolling we will do that in a potential follow-up video here we are keeping things as simple as possible for now let's get started step by step with the fixed look back window calculation first of all we need need some libraries websocket to establish a connection to the binance servers to receive live Bitcoin prices Jason to transform a Jason object from the binance server to a python dictionary and panas for data handling that's it Library wise then I'm going to Define two variables here init price and init timestamp both are set to none so what are those variables the price is containing the reference price and the time stamp is containing the reference timestamp meaning these variables are going to update as soon as we are getting data flowing in so whenever we are getting data flowing in the first time this is being set so the first price flowing in with our data feed is going to be our initial price but as we didn't establish any data feed yet I'm setting those variables to none and as soon as we receive the First Data tick these are being set and these variables are going to be updated every 30 seconds as these are the reference price and time stamps so initially we don't have any price and time stamp information so I'm just setting them to none next we are going to establish the live price data feed as I've worked with websocket and setting up web socket data feeds or websocket uh price feeds I'm just jumping over it so what I'm doing is simply copy pasting from the web socket documentation so I'm defining a function on message and that function so Dev on message is just organizing what to do when you receive a message from a socket or web soet server this needs a WS which we will Define in some seconds and and a message which is the actual message from the server now within this function we have to and that is very important get those two variables in the global variable space as this function is being called every time when we receive data from the server so potentially every 30 seconds these variables are going to change so we need to be able to change those variables within this function so I'm just getting them in the global variable Space by defining them as globals so Global inet price Global inet timestamp here with that we can manipulate them and get them in the global variable space now what else do in this function for now I'm going to Define an dictionary here I'm just calling the dict underscore and this is simply using the Json library and loads the server message what this is going to do is simply taking the message from the server and transforming that to a python dictionary so this is just a dictionary and before moving on I'm just going to print that out to show you what we are getting on each server message because then it's easier to understand what I'm doing in the next steps here so I said this just getting the uh reference price and reference Tim stamp into the global variable space so we can make changes within this function to those two variables and then it's just printing out whatever the server delivers us we still have to Define this ws and this is again just copy paste from the library just copy pasting that here actually so get out of this function then Define WS web socket web socket app then the socket then on message on message and then run it forever so we still have to Define this socket and that is very easy to find this is just in the binance web socket API documentation so I'm just copy pasting again I will reference that in the video description so we need to Define socket as this one here so as said this is just copy paste I'm just taking the Kline one minute stream here for my live price feed and Define that as socket so I've gone over that in more detail in some of my previous videos I will try to link it in the video description but I said mostly copy paste from two documentations first web soer documentation second binance web soet API documentation okay so this is going to print out the message from the binance server on this web socket Stream So Bitcoin usdt on the one minute Kline stream and it's just printing it out every time you receive data from the server so if I'm running this you will see that I'm getting the following you see that I'm just getting uh live tick here every second so let's take a look at that so what do we got here let's stop that so it's not that uh annoyingly updating all the time so you see we are getting a dictionary with a ton of information which we don't need as a whole so I'm just doing some filterings here so what do we need we need the Tim stamp and that is this event time stamp here so that is a so-called Unix Tim stamp we will transform that to a human readable time stamp in some seconds but this one is telling us when exactly was this happening and here you you have some information but for us relevant is the close timestamp here so that is the live price of the Bitcoin currently so we just need to extract from this dictionary the event timestamp and the price that's all we need for now so let's actually do that and we have to do that within this on message function so what we are going to do is just defining some additional variables here which is the current price and the current price is this dictionary then you uh access the data dictionary again so you just take dict then index for data then you're getting to this level of dictionary and then you get to K which is containing the close which you need so this the next level of dictionary so you need to index 4K and and then you have you you're in this level of dictionary and then you can simply extract see the close price so this is just your current price and as you see this is currently string formatted you want to have it as a numerical value so you are just doing a type cast here to a floating value that's it so with that you are getting the current price you also need the current time stamp this one here so just defining TS here for Tim stamp then take again the dictionary then what do we need we need again to access the data wues data and then directly in this level of dictionary you just access capital E all right and as you would just get a Unix timestamp value I'm using a very handy panas function to just transform that to human readable time stamp which is just pend us to daytime then provide this Unix timestamp and Define the unit as Ms so let's print out what we are getting so we are printing out current price or let's take time stamp and current price and with that we should get this in a more dense format here so let's run this again important whenever you run this again and you made changes to the inet price and inet time stamp you have to execute that again otherwise you're getting new referenced uh uh um prices and time stamps you will see that in some seconds so executing that and executing that again and you now see very conveniently you're getting a current Tim stamp so this one here and the current price updated every second here as simple as that so we have now Danse information we have relevant information and we can move on to what we discussed in the beginning of this video implementing some logic into that so let's stop that for now so what is the next step here next step would be to Define our initial time stamp on and our initial price variable and how can we do that we need to Define if this initial time stamp and price variable is actually a non value which is initially true so because they are non values before this uh stream is running you just Define a condition here if in it timestamp is none then you want to assign your inner timestamp to the first Tim stamp value so in the First Data tick let's say this would be our first data tick you will get the time stamp as this one here right and in the second data tick this wouldn't update because it wouldn't simply update because the inner Tim stamp is not nonone anymore because it was assigned to a value so this will only be triggered in very first data tick if and I just mentioned that if you reset those variables before running the stream which is critical now you can also Define if inet price is nonone but it's redundant if you have a time stamp you also have a price value so I'm just defining in it price as the current price same argumentation as before avoiding some redundant code here but if you feel more I don't know if you feel better assigning that to also if in it price is none you can also do that but you don't need to so you have an initial price which is let's assume again this is our first data tick this is our initial Tim stamp this is our initial price and this is only going to be updated once that is when starting the stream so we have an inner time stamp defined and an inprise defined as soon as we are running the stream now we want to know how far this current Tim stamp and this current price is away from the reference one so in other words in easier words what is the difference between this Tim stamp and this one assuming that is our initial Tim stamp same for here what is the price development so the price here as you see was dropping by one one cent you want to you want to know what is the difference between the current price and the initial price and then same logic for the for the subsequent values what is the difference between this one and the initial one this one and the initial one right and so on and so forth so how can you do that you first Define a Time Delta just calling that time Delta and that is just the time difference between the current value and the initial value so you can use Panda's time Dela function for that you can also work with the daytime module that is on you so this is calculating a Time Delta and you want to have the time data of the current time stamp TS so that would be in the second data tick this one and the initial time stamp that would be this one so minus init TS so with that you're getting the time difference of those two now this I want to have in seconds so it's easier to calculate with that so with that I'm getting this difference in seconds all right so with that I have my time Delta and now what do I want to do with my time Delta as I said in the beginning if my time Delta is above 30 so if 30 seconds have passed then I want to reset my initial time stamp so let's take a very simplified example here so let's say I Take 5 seconds or or whatever that is in the first five rows here then it's it's uh taking the time Delta here so time Delta between this one time Delta between uh uh this one time data between this one time data between this one until 5 Seconds have passed and then this is my new reference or my new anchor point so this is my new time St my new inner Tim stamp or my new reference Tim stamp and this is my new reference price okay so this is then calculating the difference or the price development between this and this value so in this case I see that the Bitcoin was slightly Rising within 5 seconds and this also telling me what is my time pass from here until here and then the calculation starts again then this is my reference and then I'm I have this next one but now it's benchmarking against this one same as this one and that is happening every 30 seconds so how you can Implement all that talk into code you take a condition if my time data which is in seconds is larger you can also take larger or equal to 30 then you want to reset your init TS and your init TS now becomes your TS so after 30 seconds it's going to be reset then you also reset your price as it just showed you so you set your inner price to the current price and then it's going to start from zero again so you also want to get your price Delta here and your price Delta is simply your current price minus the initial price so you are just getting but I showed you this difference you're getting so your difference between your current price and the last reference point all right so you're just taking uh not print sorry price Delta is my current price minus the initial price so let's put that all together and uh work with some print statements so it's more clear what is happening here so let's get rid of this print statement for now and we start with we are printing the current price so the current price is just my current price variable and then we're doing that with all what we defined so the picture is getting more more clear what we what we were doing here so current timestamp is the TS we want our initial price initial price is our init price and want our initial time stamp is our in init TS and we want our price Delta price Delta so we're printing out price Delta and now let me go over that with you again so that that is more clear so we are very important resetting the inet price and the inet time stamp here again and then we are running this whole structure here now let's take a look at the the output so first tick here it's jumping down now wait a second so first tick you have a current price and a current time stamp that is the very first data tick and you see that the initial price is becoming this first price you're getting also the initial time stamp is getting this first time stamp you're getting so this is the initialization part the price Dela obviously is simply zero because the current price minus the initial price is the exact same so these are the the exact same values then the N next sorry data tick is flowing in and you have a current price of this one here and you have a new updated current Tim stamp the initial price is still the same as before because 30 seconds haven't passed yet and now you have a price Delta now the current price is below the initial price and you have a price Dela of of 1 cent here all right and then this is going going further further further further until 30 seconds have passed and then it's starting over again that's it for this video thank you very much for watching and I'm looking forward to see you in the upcoming videos bye-bye
Info
Channel: Algovibes
Views: 3,646
Rating: undefined out of 5
Keywords: algo trading, algorithmic trading, backtest results, backtesting, financial assessment, financial modeling, financial quantification, portfolio assessment, portfolio management, quantitative analysis, quantitative assessment, quantitative finance, risk analytics, risk assessment, risk calculation, risk management, risk measurement, risk mitigation, risk modeling, risk quant, risk quantification, trading bot, trading strategy
Id: v4S2rgU5Rs0
Channel Id: undefined
Length: 20min 47sec (1247 seconds)
Published: Sun Feb 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.