YouTube2Text

Day 5.1 - Coding Risk Management — Transcript

by Moon Dev · 2,653 words · 348 segments · language en · Watch on YouTube

Full transcript

  1. 0:01all right all right so let's get back to
  2. 0:03coding and let's code some risk
  3. 0:06management strategies so these are
  4. 0:08actually going to be functions that I
  5. 0:10used for risk management and the first
  6. 0:14thing we'll need to do is we'll need to
  7. 0:15connect to the actual exchange so I'm
  8. 0:19just going to copy in all this
  9. 0:20information
  10. 0:22here copy it
  11. 0:28and if you saw that I'll try to give you
  12. 0:31some tricks here as well just throughout
  13. 0:34command B will open up my sidebar
  14. 0:36command B uh and if I hold down if I'm
  15. 0:40up here selected and I hold down shift
  16. 0:43and then select here it will select all
  17. 0:44of them and then if I ever need to
  18. 0:47indent something and I do that select
  19. 0:50and then I shift and I select all of it
  20. 0:52and then I hit command bracket left
  21. 0:55bracket moves It To the Left Right
  22. 0:57bracket moves it to the right so I
  23. 0:59always appreciate uh these little tricks
  24. 1:01as well so I'll try to as I do them I'll
  25. 1:04I'll remember to remind you but remember
  26. 1:07risk is everything so you want to make
  27. 1:09sure to handle your risk accordingly and
  28. 1:13I use just like a tiny percentage of my
  29. 1:15trading portfolio in my accounts when
  30. 1:17I'm testing and I don't scale that up
  31. 1:20until you know it's it makes sense and
  32. 1:24it's been forward tested for a long time
  33. 1:26so I want to go ahead and show you a
  34. 1:29couple functions here that are going to
  35. 1:32go ahead and help you manage your risk
  36. 1:35so the first thing is our kill switch
  37. 1:37the kill switch is something that you
  38. 1:39can call at any moment and it will take
  39. 1:42you out of the
  40. 1:43position so for example if you were in a
  41. 1:46position
  42. 1:48where you you had like a 10 contracts of
  43. 1:52whatever you ethereum or whatever and
  44. 1:57for whatever reason your Bot says hey
  45. 2:01you know you need to get out of this
  46. 2:03position maybe it's because your profit
  47. 2:04and loss has been hit well the kill
  48. 2:06switch is going to get you out of that
  49. 2:08position now you can adjust this to be
  50. 2:10your own uh this is just how I do it and
  51. 2:13I'm going to bring in the code and from
  52. 2:16there we're going to fix it up and you
  53. 2:19can use it if you want but like I said
  54. 2:21you know it's probably better that you
  55. 2:24learn from it and then kind
  56. 2:27of Do Your Own Thing
  57. 2:31because it's not perfect I mean for me
  58. 2:33it's perfect but hey it might not be
  59. 2:35perfect for you so here here it is um
  60. 2:38this is all the code let's go through it
  61. 2:40I'm going to make this a little smaller
  62. 2:42so you can read it I hope you can still
  63. 2:45read
  64. 2:46this I think you can um you can see it
  65. 2:50it depends on a lot of stuff like open
  66. 2:52positions so we're going to have to go
  67. 2:53over quite a bit of stuff here in this
  68. 2:55in this video but I'm just going to copy
  69. 2:57paste everything and then I'm going to
  70. 2:59put all of the code below as well so
  71. 3:01we'll go line through line but I want to
  72. 3:04get rid of all these errors first so
  73. 3:06what I'm going to do is first off let's
  74. 3:09make this screen so you can see the
  75. 3:11whole all the code okay so you can see
  76. 3:13the whole code and before I go through
  77. 3:16all this I want to get rid of the errors
  78. 3:17so I'm going to do that let's import
  79. 3:20pandas this is one of the packages that
  80. 3:23I highly recommend you study and master
  81. 3:26we want to get our symbols in there
  82. 3:30so let's just bring in all of this
  83. 3:36information okay probably don't need to
  84. 3:39fetch my balance right
  85. 3:41now and so we have all the information
  86. 3:43we need you can see these errors are
  87. 3:46popping up here because the squiggly
  88. 3:48lines and that's because this is calling
  89. 3:50a function now this
  90. 3:54function we have to create so let's go
  91. 3:57ahead and create this function so you're
  92. 3:58going to get a lot of good code here
  93. 4:00from this this training um so buckle in
  94. 4:03for sure because this is depend a lot of
  95. 4:06this kill switch is dependent off of
  96. 4:07other code which is dependent off of
  97. 4:09other code which is dependent off of
  98. 4:11other code right so let's go ahead and
  99. 4:14bring in our open positions this is
  100. 4:16another function that is super useful
  101. 4:18because it shows you all of your open
  102. 4:20positions so in order to the kill switch
  103. 4:23we need the open positions so open
  104. 4:27positions and I'll put all this code
  105. 4:29below so don't worry um you can see that
  106. 4:33it you need to pass in a symbol if you
  107. 4:35don't pass in a symbol it's just going
  108. 4:36to use the default symbol these things
  109. 4:39will need to be changed and remember
  110. 4:42that based on your exchange this part
  111. 4:45might be different because your symbol
  112. 4:48will be in different index positions so
  113. 4:50you'll have to play around with
  114. 4:53that you could actually just mark this
  115. 4:55out too because this is kind of a custom
  116. 4:58thing that I built
  117. 5:00you can just put the index position up
  118. 5:03here
  119. 5:05index position equals whatever the index
  120. 5:08position
  121. 5:10is you can do it that way and just Mark
  122. 5:13all of this out like that and by the way
  123. 5:17to do that I hit command
  124. 5:20backslash so select it all then command
  125. 5:23black
  126. 5:24backslash now this essentially this is
  127. 5:27super important this is going to pull
  128. 5:28all of your balance es all your balance
  129. 5:30information for fex it was a little
  130. 5:33different because I had to get the
  131. 5:35balance information for USD because I'm
  132. 5:37using the contract so it doesn't matter
  133. 5:40if I'm trading like Solana or ethereum
  134. 5:42or whatever it's actually trading USD
  135. 5:45for those so this is going to be a
  136. 5:46little different for each exchange but
  137. 5:48essentially I fetch balance here and
  138. 5:51then within the balance it's a
  139. 5:53dictionary and I can get my open
  140. 5:55positions by saying hey tap into that
  141. 5:59femix balance and go to the info then
  142. 6:01the data and then the position
  143. 6:04section and then you can see this code
  144. 6:07does something similar but it gets the
  145. 6:10index position so that's what I said you
  146. 6:13could hardcode up there but essentially
  147. 6:15my index position for BTC is four so a
  148. 6:19lot of my code actually just shows
  149. 6:20instead of index position it will just
  150. 6:22be like four like that so you can do it
  151. 6:25either way and then it Taps into that
  152. 6:28dictionary and looks at the side and
  153. 6:31size dictionaries are super important to
  154. 6:33get good
  155. 6:35at if you're if you have like a list of
  156. 6:37things you want to jot down to really
  157. 6:39study dictionaries is definitely one of
  158. 6:41them now this tells me if it if it's
  159. 6:44long or short and if there's an open
  160. 6:46position pretty much this is a if
  161. 6:48statement and that's another really
  162. 6:50great thing to be great at if
  163. 6:53statements so drop that down and study
  164. 6:55that for sure if you're not already
  165. 6:57great at them um it's a little little
  166. 6:59difficult right because a lot of people
  167. 7:01already are Masters at coding and a lot
  168. 7:03of people are better at coding than I am
  169. 7:06and then there's some people watching
  170. 7:08this that don't know how to code at all
  171. 7:11so my my suggestion is just like
  172. 7:13anything I say just study it and then
  173. 7:16get different opinions remember my rule
  174. 7:18of three three articles or three videos
  175. 7:20and if you do that you're going to learn
  176. 7:21your answer number one and then number
  177. 7:24two through a million is you're going to
  178. 7:26learn so much new stuff so here you can
  179. 7:28see the open position positions it turns
  180. 7:30the open position to true or false if it
  181. 7:32shows buy or sell that means there's an
  182. 7:34open position and then if it shows buy
  183. 7:38the long equals true and long equals and
  184. 7:41if it shows sell then long equals false
  185. 7:43and then if it shows neither of them
  186. 7:45then I just say none and false and then
  187. 7:48this is just for my knowledge I print
  188. 7:50out all this information so every time
  189. 7:53it runs through I can see like what are
  190. 7:55we in open position what's the index
  191. 7:57position and then it returns all this
  192. 8:00information and if I'm giving you
  193. 8:02something to study how returning
  194. 8:10Works in Python because essentially you
  195. 8:13return all this information and then you
  196. 8:14can use it so I'm returning open
  197. 8:16positions open position bu open position
  198. 8:19size long and index position and now I
  199. 8:22can use my kill switch you can see my
  200. 8:25kill switch takes in symbol as well and
  201. 8:28if you don't
  202. 8:30if you don't bring in a symbol then it's
  203. 8:32just going to use the symbol that you
  204. 8:35had set and then all of these I push put
  205. 8:38in the symbol here to open position it
  206. 8:40grabs my open
  207. 8:42position then it tells me if I'm long or
  208. 8:45not because remember I returned all that
  209. 8:46information up here and then it shows me
  210. 8:49the kill size I have to pass in the
  211. 8:52symbol to all of
  212. 8:55these and this is kind of set up
  213. 8:58Advanced like you could could do it
  214. 8:59without this you would just have to use
  215. 9:01the symbol that you set up up here
  216. 9:06right symbol equals ubtc
  217. 9:09USD so my kill switch grabs all the
  218. 9:12information it needs and then while open
  219. 9:14position equals true meaning we have an
  220. 9:18open position it's just going to Loop
  221. 9:20and essentially Loop until it's finished
  222. 9:23you don't need this data frame if you
  223. 9:25don't want it but I just I use it
  224. 9:28because you might need it in the future
  225. 9:29but you definitely don't need it um and
  226. 9:32then it cancels all open positions so
  227. 9:35when I'm cancel when I'm trying to get
  228. 9:38out of all positions I want to make sure
  229. 9:39all orders are closed like I don't want
  230. 9:41any open orders and then it it sees the
  231. 9:44open position gets all the information
  232. 9:46again gets the size it needs to kill
  233. 9:48meaning you know when I first got it
  234. 9:50started like I I actually wasted a lot
  235. 9:52of money with this so I'm glad we're
  236. 9:53going over this uh the kill size what I
  237. 9:57used to do is say like okay I'm going to
  238. 9:59open positions with like let's say 10
  239. 10:02contracts of whatever and I would open
  240. 10:05that position and then my kill function
  241. 10:09would close 10 contracts as well but
  242. 10:12what happened for the longest time
  243. 10:15was sometimes when I open the position
  244. 10:18maybe only eight of the 10 contracts
  245. 10:20would fill so now I have a open position
  246. 10:23of 10 or eight instead of 10 but my code
  247. 10:27thinks it's 10 so for the long time I
  248. 10:29just said okay in my kill switch let's
  249. 10:32close the size that I opened so since my
  250. 10:36code says 10 it tries to close 10 but
  251. 10:39remember it only filled eight out of the
  252. 10:4210 so I actually have an open position
  253. 10:44of eight and it's closing 10 meaning I'm
  254. 10:47going short too so this is going to save
  255. 10:50you a lot a lot just to make sure to set
  256. 10:54the kill size to the actual open size
  257. 10:56and that's why I wanted to show you all
  258. 10:57this information now the ask and the bid
  259. 11:00to get out of the position I actually
  260. 11:02want to get out as soon as possible but
  261. 11:05I don't want to pay I don't want to be a
  262. 11:07taker meaning I don't want to like with
  263. 11:11fex or the exchanges I use like you have
  264. 11:13to pay more in order to just take the
  265. 11:16liquidity off the market meaning like I
  266. 11:18don't want to wait for my my order to
  267. 11:21fill I want to get it now well if you
  268. 11:25want to do it that way
  269. 11:27then then you're going to pay more so in
  270. 11:31my case I always get the ask and the bid
  271. 11:33so I can set the orders at the ask or
  272. 11:35the bid and I get paid on fex to do that
  273. 11:40opposed to having to pay so you can see
  274. 11:42this calls another function so we're
  275. 11:44actually going to have to bring in
  276. 11:45another function up here and this
  277. 11:47function is super helpful so you know
  278. 11:51that's what I'm trying to do in this in
  279. 11:52this training is just cut out the noise
  280. 11:55and give you everything that's just that
  281. 11:57I use in my dayto day so you can just
  282. 12:01start from a place of of uh you know
  283. 12:06experience so this is a function that
  284. 12:08you'll probably need ask bid symbol
  285. 12:11equals symbol if you don't add a symbol
  286. 12:14uh in here it's just going to use your
  287. 12:16one you set up top and then this it gets
  288. 12:18the order book and then the bid it goes
  289. 12:21through the order book and and reads the
  290. 12:23dictionary and the bid is in this
  291. 12:26placement and the ask is in this
  292. 12:27placement and then I just print this out
  293. 12:29to show what my ask and bid are or at
  294. 12:33least my ask is and then F literal is
  295. 12:36something that is super important so F
  296. 12:41literal it allows you to put different
  297. 12:45variables inside of sentences so I would
  298. 12:48definitely go ahead and you know if
  299. 12:49you're not really good with f literal go
  300. 12:51learn about that a bit super simple to
  301. 12:53use but it's like one of my favorite
  302. 12:55things and then I again return the ask
  303. 12:58and the bid and don't worry I'll put all
  304. 13:00this code below um what I like to do
  305. 13:02sometimes is I like to copy this or I
  306. 13:05like to write here saying ask bid if I
  307. 13:08if I ask for index zero it's actually
  308. 13:11getting the ask if I ask for index one
  309. 13:14it's getting the bid but now that we
  310. 13:16have this we have our full kill switch
  311. 13:19and it should work so it gets the ask in
  312. 13:21the bid and then it says hey if the long
  313. 13:23if we're in a long position then we
  314. 13:26would want to get out of it
  315. 13:29well I'm sorry this says long equals
  316. 13:31false so that means we're in a short
  317. 13:32position in order to get out of it we
  318. 13:34need to create a limit by order right
  319. 13:37because we're in a short position so we
  320. 13:38have to buy it back and then you pass in
  321. 13:40all the information I showed you in the
  322. 13:42last video then I do a couple print
  323. 13:44statements just for myself hey I just
  324. 13:46made a buy to close now I'm going to
  325. 13:48sleep for 30 seconds but else if I'm in
  326. 13:52a long position equals true that means
  327. 13:55I'm actually in a long position so I
  328. 13:56need to get out of it with a sell order
  329. 14:00and this is super important code
  330. 14:02because your risk managing your risk is
  331. 14:05everything and then I have an else
  332. 14:06statement here that something that I
  333. 14:09didn't expect happened in the kill
  334. 14:10switch function and it will print that
  335. 14:12out and let me know so yeah that's the
  336. 14:15uh that's the kill switch for you and
  337. 14:18that's going to be super super helpful I
  338. 14:20think this video is getting a little
  339. 14:22long I'll make sure to get the code down
  340. 14:24down below and I will make a second
  341. 14:26video for uh these next two things which
  342. 14:29are super super important as well uh
  343. 14:32they they kind of build off of the kill
  344. 14:34switch that's why I had to show you the
  345. 14:35kill switch first I'll make sure the
  346. 14:37codes below let me know if you have any
  347. 14:38questions and I I'll see you in the next
  348. 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.