Day 5.1 - Coding Risk Management — Transcript
Full transcript
- 0:01all right all right so let's get back to
- 0:03coding and let's code some risk
- 0:06management strategies so these are
- 0:08actually going to be functions that I
- 0:10used for risk management and the first
- 0:14thing we'll need to do is we'll need to
- 0:15connect to the actual exchange so I'm
- 0:19just going to copy in all this
- 0:20information
- 0:22here copy it
- 0:28and if you saw that I'll try to give you
- 0:31some tricks here as well just throughout
- 0:34command B will open up my sidebar
- 0:36command B uh and if I hold down if I'm
- 0:40up here selected and I hold down shift
- 0:43and then select here it will select all
- 0:44of them and then if I ever need to
- 0:47indent something and I do that select
- 0:50and then I shift and I select all of it
- 0:52and then I hit command bracket left
- 0:55bracket moves It To the Left Right
- 0:57bracket moves it to the right so I
- 0:59always appreciate uh these little tricks
- 1:01as well so I'll try to as I do them I'll
- 1:04I'll remember to remind you but remember
- 1:07risk is everything so you want to make
- 1:09sure to handle your risk accordingly and
- 1:13I use just like a tiny percentage of my
- 1:15trading portfolio in my accounts when
- 1:17I'm testing and I don't scale that up
- 1:20until you know it's it makes sense and
- 1:24it's been forward tested for a long time
- 1:26so I want to go ahead and show you a
- 1:29couple functions here that are going to
- 1:32go ahead and help you manage your risk
- 1:35so the first thing is our kill switch
- 1:37the kill switch is something that you
- 1:39can call at any moment and it will take
- 1:42you out of the
- 1:43position so for example if you were in a
- 1:46position
- 1:48where you you had like a 10 contracts of
- 1:52whatever you ethereum or whatever and
- 1:57for whatever reason your Bot says hey
- 2:01you know you need to get out of this
- 2:03position maybe it's because your profit
- 2:04and loss has been hit well the kill
- 2:06switch is going to get you out of that
- 2:08position now you can adjust this to be
- 2:10your own uh this is just how I do it and
- 2:13I'm going to bring in the code and from
- 2:16there we're going to fix it up and you
- 2:19can use it if you want but like I said
- 2:21you know it's probably better that you
- 2:24learn from it and then kind
- 2:27of Do Your Own Thing
- 2:31because it's not perfect I mean for me
- 2:33it's perfect but hey it might not be
- 2:35perfect for you so here here it is um
- 2:38this is all the code let's go through it
- 2:40I'm going to make this a little smaller
- 2:42so you can read it I hope you can still
- 2:45read
- 2:46this I think you can um you can see it
- 2:50it depends on a lot of stuff like open
- 2:52positions so we're going to have to go
- 2:53over quite a bit of stuff here in this
- 2:55in this video but I'm just going to copy
- 2:57paste everything and then I'm going to
- 2:59put all of the code below as well so
- 3:01we'll go line through line but I want to
- 3:04get rid of all these errors first so
- 3:06what I'm going to do is first off let's
- 3:09make this screen so you can see the
- 3:11whole all the code okay so you can see
- 3:13the whole code and before I go through
- 3:16all this I want to get rid of the errors
- 3:17so I'm going to do that let's import
- 3:20pandas this is one of the packages that
- 3:23I highly recommend you study and master
- 3:26we want to get our symbols in there
- 3:30so let's just bring in all of this
- 3:36information okay probably don't need to
- 3:39fetch my balance right
- 3:41now and so we have all the information
- 3:43we need you can see these errors are
- 3:46popping up here because the squiggly
- 3:48lines and that's because this is calling
- 3:50a function now this
- 3:54function we have to create so let's go
- 3:57ahead and create this function so you're
- 3:58going to get a lot of good code here
- 4:00from this this training um so buckle in
- 4:03for sure because this is depend a lot of
- 4:06this kill switch is dependent off of
- 4:07other code which is dependent off of
- 4:09other code which is dependent off of
- 4:11other code right so let's go ahead and
- 4:14bring in our open positions this is
- 4:16another function that is super useful
- 4:18because it shows you all of your open
- 4:20positions so in order to the kill switch
- 4:23we need the open positions so open
- 4:27positions and I'll put all this code
- 4:29below so don't worry um you can see that
- 4:33it you need to pass in a symbol if you
- 4:35don't pass in a symbol it's just going
- 4:36to use the default symbol these things
- 4:39will need to be changed and remember
- 4:42that based on your exchange this part
- 4:45might be different because your symbol
- 4:48will be in different index positions so
- 4:50you'll have to play around with
- 4:53that you could actually just mark this
- 4:55out too because this is kind of a custom
- 4:58thing that I built
- 5:00you can just put the index position up
- 5:03here
- 5:05index position equals whatever the index
- 5:08position
- 5:10is you can do it that way and just Mark
- 5:13all of this out like that and by the way
- 5:17to do that I hit command
- 5:20backslash so select it all then command
- 5:23black
- 5:24backslash now this essentially this is
- 5:27super important this is going to pull
- 5:28all of your balance es all your balance
- 5:30information for fex it was a little
- 5:33different because I had to get the
- 5:35balance information for USD because I'm
- 5:37using the contract so it doesn't matter
- 5:40if I'm trading like Solana or ethereum
- 5:42or whatever it's actually trading USD
- 5:45for those so this is going to be a
- 5:46little different for each exchange but
- 5:48essentially I fetch balance here and
- 5:51then within the balance it's a
- 5:53dictionary and I can get my open
- 5:55positions by saying hey tap into that
- 5:59femix balance and go to the info then
- 6:01the data and then the position
- 6:04section and then you can see this code
- 6:07does something similar but it gets the
- 6:10index position so that's what I said you
- 6:13could hardcode up there but essentially
- 6:15my index position for BTC is four so a
- 6:19lot of my code actually just shows
- 6:20instead of index position it will just
- 6:22be like four like that so you can do it
- 6:25either way and then it Taps into that
- 6:28dictionary and looks at the side and
- 6:31size dictionaries are super important to
- 6:33get good
- 6:35at if you're if you have like a list of
- 6:37things you want to jot down to really
- 6:39study dictionaries is definitely one of
- 6:41them now this tells me if it if it's
- 6:44long or short and if there's an open
- 6:46position pretty much this is a if
- 6:48statement and that's another really
- 6:50great thing to be great at if
- 6:53statements so drop that down and study
- 6:55that for sure if you're not already
- 6:57great at them um it's a little little
- 6:59difficult right because a lot of people
- 7:01already are Masters at coding and a lot
- 7:03of people are better at coding than I am
- 7:06and then there's some people watching
- 7:08this that don't know how to code at all
- 7:11so my my suggestion is just like
- 7:13anything I say just study it and then
- 7:16get different opinions remember my rule
- 7:18of three three articles or three videos
- 7:20and if you do that you're going to learn
- 7:21your answer number one and then number
- 7:24two through a million is you're going to
- 7:26learn so much new stuff so here you can
- 7:28see the open position positions it turns
- 7:30the open position to true or false if it
- 7:32shows buy or sell that means there's an
- 7:34open position and then if it shows buy
- 7:38the long equals true and long equals and
- 7:41if it shows sell then long equals false
- 7:43and then if it shows neither of them
- 7:45then I just say none and false and then
- 7:48this is just for my knowledge I print
- 7:50out all this information so every time
- 7:53it runs through I can see like what are
- 7:55we in open position what's the index
- 7:57position and then it returns all this
- 8:00information and if I'm giving you
- 8:02something to study how returning
- 8:10Works in Python because essentially you
- 8:13return all this information and then you
- 8:14can use it so I'm returning open
- 8:16positions open position bu open position
- 8:19size long and index position and now I
- 8:22can use my kill switch you can see my
- 8:25kill switch takes in symbol as well and
- 8:28if you don't
- 8:30if you don't bring in a symbol then it's
- 8:32just going to use the symbol that you
- 8:35had set and then all of these I push put
- 8:38in the symbol here to open position it
- 8:40grabs my open
- 8:42position then it tells me if I'm long or
- 8:45not because remember I returned all that
- 8:46information up here and then it shows me
- 8:49the kill size I have to pass in the
- 8:52symbol to all of
- 8:55these and this is kind of set up
- 8:58Advanced like you could could do it
- 8:59without this you would just have to use
- 9:01the symbol that you set up up here
- 9:06right symbol equals ubtc
- 9:09USD so my kill switch grabs all the
- 9:12information it needs and then while open
- 9:14position equals true meaning we have an
- 9:18open position it's just going to Loop
- 9:20and essentially Loop until it's finished
- 9:23you don't need this data frame if you
- 9:25don't want it but I just I use it
- 9:28because you might need it in the future
- 9:29but you definitely don't need it um and
- 9:32then it cancels all open positions so
- 9:35when I'm cancel when I'm trying to get
- 9:38out of all positions I want to make sure
- 9:39all orders are closed like I don't want
- 9:41any open orders and then it it sees the
- 9:44open position gets all the information
- 9:46again gets the size it needs to kill
- 9:48meaning you know when I first got it
- 9:50started like I I actually wasted a lot
- 9:52of money with this so I'm glad we're
- 9:53going over this uh the kill size what I
- 9:57used to do is say like okay I'm going to
- 9:59open positions with like let's say 10
- 10:02contracts of whatever and I would open
- 10:05that position and then my kill function
- 10:09would close 10 contracts as well but
- 10:12what happened for the longest time
- 10:15was sometimes when I open the position
- 10:18maybe only eight of the 10 contracts
- 10:20would fill so now I have a open position
- 10:23of 10 or eight instead of 10 but my code
- 10:27thinks it's 10 so for the long time I
- 10:29just said okay in my kill switch let's
- 10:32close the size that I opened so since my
- 10:36code says 10 it tries to close 10 but
- 10:39remember it only filled eight out of the
- 10:4210 so I actually have an open position
- 10:44of eight and it's closing 10 meaning I'm
- 10:47going short too so this is going to save
- 10:50you a lot a lot just to make sure to set
- 10:54the kill size to the actual open size
- 10:56and that's why I wanted to show you all
- 10:57this information now the ask and the bid
- 11:00to get out of the position I actually
- 11:02want to get out as soon as possible but
- 11:05I don't want to pay I don't want to be a
- 11:07taker meaning I don't want to like with
- 11:11fex or the exchanges I use like you have
- 11:13to pay more in order to just take the
- 11:16liquidity off the market meaning like I
- 11:18don't want to wait for my my order to
- 11:21fill I want to get it now well if you
- 11:25want to do it that way
- 11:27then then you're going to pay more so in
- 11:31my case I always get the ask and the bid
- 11:33so I can set the orders at the ask or
- 11:35the bid and I get paid on fex to do that
- 11:40opposed to having to pay so you can see
- 11:42this calls another function so we're
- 11:44actually going to have to bring in
- 11:45another function up here and this
- 11:47function is super helpful so you know
- 11:51that's what I'm trying to do in this in
- 11:52this training is just cut out the noise
- 11:55and give you everything that's just that
- 11:57I use in my dayto day so you can just
- 12:01start from a place of of uh you know
- 12:06experience so this is a function that
- 12:08you'll probably need ask bid symbol
- 12:11equals symbol if you don't add a symbol
- 12:14uh in here it's just going to use your
- 12:16one you set up top and then this it gets
- 12:18the order book and then the bid it goes
- 12:21through the order book and and reads the
- 12:23dictionary and the bid is in this
- 12:26placement and the ask is in this
- 12:27placement and then I just print this out
- 12:29to show what my ask and bid are or at
- 12:33least my ask is and then F literal is
- 12:36something that is super important so F
- 12:41literal it allows you to put different
- 12:45variables inside of sentences so I would
- 12:48definitely go ahead and you know if
- 12:49you're not really good with f literal go
- 12:51learn about that a bit super simple to
- 12:53use but it's like one of my favorite
- 12:55things and then I again return the ask
- 12:58and the bid and don't worry I'll put all
- 13:00this code below um what I like to do
- 13:02sometimes is I like to copy this or I
- 13:05like to write here saying ask bid if I
- 13:08if I ask for index zero it's actually
- 13:11getting the ask if I ask for index one
- 13:14it's getting the bid but now that we
- 13:16have this we have our full kill switch
- 13:19and it should work so it gets the ask in
- 13:21the bid and then it says hey if the long
- 13:23if we're in a long position then we
- 13:26would want to get out of it
- 13:29well I'm sorry this says long equals
- 13:31false so that means we're in a short
- 13:32position in order to get out of it we
- 13:34need to create a limit by order right
- 13:37because we're in a short position so we
- 13:38have to buy it back and then you pass in
- 13:40all the information I showed you in the
- 13:42last video then I do a couple print
- 13:44statements just for myself hey I just
- 13:46made a buy to close now I'm going to
- 13:48sleep for 30 seconds but else if I'm in
- 13:52a long position equals true that means
- 13:55I'm actually in a long position so I
- 13:56need to get out of it with a sell order
- 14:00and this is super important code
- 14:02because your risk managing your risk is
- 14:05everything and then I have an else
- 14:06statement here that something that I
- 14:09didn't expect happened in the kill
- 14:10switch function and it will print that
- 14:12out and let me know so yeah that's the
- 14:15uh that's the kill switch for you and
- 14:18that's going to be super super helpful I
- 14:20think this video is getting a little
- 14:22long I'll make sure to get the code down
- 14:24down below and I will make a second
- 14:26video for uh these next two things which
- 14:29are super super important as well uh
- 14:32they they kind of build off of the kill
- 14:34switch that's why I had to show you the
- 14:35kill switch first I'll make sure the
- 14:37codes below let me know if you have any
- 14:38questions and I I'll see you in the next
- 14:40video
About this transcript
This page contains the full transcript of Day 5.1 - Coding Risk Management by Moon Dev, generated from the public captions YouTube serves with the video. The transcript has 2,653 words across 348 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.