Optimize TradingView Strategies in Seconds (Free & Open-Source) — Transcript
Full transcript
- 0:00If you've ever tried tuning a
- 0:01TradingView strategy by hand one setting
- 0:03[music] at a time like this, you know
- 0:05that it's not exactly a fun process.
- 0:07Very manual, extremely [music] tedious.
- 0:10But check this out.
- 0:12Every dot you see on this screen is a
- 0:14complete backtest. [music] In this case,
- 0:16literally thousands of them running in
- 0:18just a span of seconds.
- 0:20The entire [music] search is guided by a
- 0:22machine learning algorithm that's
- 0:23intelligently exploring all the
- 0:25different settings combinations [music]
- 0:26in your strategy. And the coolest part
- 0:28is it's all running in your browser and
- 0:31your laptop.
- 0:33I've made the technology behind it
- 0:34completely [music] open source and
- 0:35because it runs on your hardware,
- 0:37it's free.
- 0:38In this video, I'm going to do a deep
- 0:41dive on how it all [music] works and
- 0:42show you how it makes your TradingView
- 0:44strategies that much more powerful.
- 0:49Hey guys, this is Justin here and today
- 0:50I'm really excited to go over with you a
- 0:52new open source project that I've been
- 0:54working on as well as strategy
- 0:56optimization platform that is now freely
- 0:58available for everyone. It has been a
- 1:01couple years in the making. A lot of you
- 1:03actually on Discord and on Patreon have
- 1:05helped me with this in various ways and
- 1:07beta tested it over the past couple of
- 1:09years and I'm really grateful for your
- 1:11help on that. And today I wanted to talk
- 1:13specifically about a piece of technology
- 1:15that I'm also open sourcing that makes
- 1:17it all possible. And that is the Pine
- 1:19Wisom compiler. So it's very different
- 1:21than others projects out there that
- 1:22transform Pine Script into other
- 1:24language is because what this does is it
- 1:26takes Pine Script and directly targets
- 1:27WebAssembly, which gets you really high
- 1:30throughput near native
- 1:32performance for essentially testing
- 1:34thousands of different settings for your
- 1:36indicator
- 1:37and doing that in a matter of seconds.
- 1:40So right here, if you scroll down on
- 1:42this new website that I've built out,
- 1:44you'll see that right away the first
- 1:46section is all about how one back test
- 1:48is not enough. And it's interesting
- 1:49because when you think about back
- 1:51testing especially on TradingView,
- 1:53it tends to assume that you know what
- 1:55the best settings are to begin with. And
- 1:57that's not always true. A lot of the
- 1:58time you to guess with your own
- 2:00intuition and your own playing around.
- 2:03And you can see as I'm changing this for
- 2:05each setting, I'm arriving at different
- 2:07parts in this overall sort of geography,
- 2:10this topography down here that you see.
- 2:12And what's neat is you can actually
- 2:14change this. This is a full-fledged
- 2:16compiler running directly on your
- 2:17browser. And if I change the EMA to an
- 2:20SMA, for example, it is near
- 2:22instantaneous. It will re-backtest all
- 2:25225 of these configurations and show you
- 2:28precisely what that topography looks
- 2:30like. So with that said, I'm going to go
- 2:33through a couple of slides here to show
- 2:35you just how this is possible. So under
- 2:37the hood, I'm calling this Pine Wasm. It
- 2:39is going to be completely open source
- 2:41for you guys to take a look at. This is
- 2:43a preview of the GitHub page. It's 100%
- 2:46in Rust. And it's actually really cool
- 2:49how it works. So under the hood, what's
- 2:51happening? Again, this is 100
- 2:53milliseconds flat, 225 backtests to give
- 2:56you a sense for how this scales. It's
- 2:58actually scales really nicely. A lot of
- 3:00the time it finishes before I'm even
- 3:03able to render it to the screen. But you
- 3:05can see this was less than a second. You
- 3:07cranked out a couple thousand backtests
- 3:09for such a simple strategy. And it's
- 3:12really neat because it gives you a few
- 3:13go back here, you can see how it
- 3:15definitely adds in this level of
- 3:17granularity to the overall topography
- 3:19and it gives you a sense for what
- 3:21settings are really working well and
- 3:23which areas you have to watch out for.
- 3:25And this will definitely change as you
- 3:27go through time. Markets are definitely
- 3:30not static. They are constantly
- 3:31evolving. So it's really important to be
- 3:33able to optimize at scale very
- 3:36frequently. And I wanted to build a tool
- 3:37to help you guys do that. If you look at
- 3:40a lot of the other projects out there,
- 3:41Pine Script is famously closed source.
- 3:43It only works on TradingView servers,
- 3:45but the language itself and the spec for
- 3:47it is very open. You can read about it,
- 3:50understand at a surface level what
- 3:52everything does. That's called a clean
- 3:54room implementation.
- 3:56And really everything that you see out
- 3:58there, this is just a sampling of the
- 4:00top three, but there's a lot of them.
- 4:02Pine Forge, Pine TS, Pine Core, these
- 4:04are all basically acting as transpilars.
- 4:07So taking PineScript as a scripting
- 4:09language and transpiling that into
- 4:12another general purpose coding language.
- 4:15And a lot of what makes PineScript so
- 4:17elegant, so readable, so efficient
- 4:20is really lost whenever you go from
- 4:23PineScript into another totally
- 4:25different language.
- 4:26This is just an example of JavaScript.
- 4:29So what you would have to do so you
- 4:30would take PineScript, use the abstract
- 4:32syntax tree to parse out all of that
- 4:34semantic meaning
- 4:35and then of course target JavaScript. So
- 4:38it's quite involved, whereas this is so
- 4:41much faster. You can see that
- 4:43intermediate language is what
- 4:46allows me to very easily go into
- 4:48something like WebAssembly for instance.
- 4:50And the process of going from an IO into
- 4:52WebAssembly is very well understood.
- 4:54That's something that a lot of big
- 4:56companies have invested a lot of
- 4:57resources into doing. So there was a lot
- 5:00of precedent for this type of thing. And
- 5:02of course the V8 engine itself has had
- 5:04billions of dollars worth of R&D and
- 5:06it's very good at optimizing the
- 5:07WebAssembly thereafter. So that's part
- 5:09of the reason why I'm able to get such
- 5:11incredible speeds.
- 5:13So what's really cool is I'm just like
- 5:15the Pine TS project and others like it,
- 5:17it runs in your browser except this time
- 5:19it will compile every strategy into a
- 5:21standalone zero import WebAssembly
- 5:25module. And I've done videos on this in
- 5:27the past where I discussed Lean Four,
- 5:29but I've been absolutely obsessed with
- 5:31it for the past year and a half and
- 5:33trying it out in various projects and
- 5:34this being one of them.
- 5:36And it's incredibly powerful because
- 5:38just like how it does in math proofs, it
- 5:40tests the underlying logic of your
- 5:42system. So in this case the underlying
- 5:44logic of my compiler. Because you could
- 5:47quickly see how this is not exactly
- 5:49human readable. When you take PineScript
- 5:51he translated down into that bytecode
- 5:53representation.
- 5:54This is just a very small example,
- 5:56right? Imagine tens of millions of these
- 5:58essentially unrolled. Sure maybe an
- 5:59agent could go through and understand
- 6:01it, but it's actually quite difficult to
- 6:03prove that it's doing what you believe
- 6:05it's doing.
- 6:06And that's where formal verification
- 6:07comes in.
- 6:08So, with formal verification, again, you
- 6:11end up with this nice web assembly
- 6:13module, zero imports, so it can run at
- 6:16scale. You can spin it off into
- 6:17different V8 isolates all in
- 6:19simultaneously.
- 6:21So, it's really powerful. You compile it
- 6:23once and any changes you do, it just
- 6:25recompiles extremely quickly because,
- 6:28again, the entire pipeline is written in
- 6:29Rust.
- 6:30So, you can see this is really why it
- 6:33all works. I mentioned I've been
- 6:34thinking about this for a couple of
- 6:36years in the sense of how I would design
- 6:37all of the opcodes, but what really made
- 6:39it feasible was this formal verification
- 6:42workflow.
- 6:43As you can see, I have 38,000 plus
- 6:45theorems and lemmas at this point that
- 6:47kind of targets certain functionality
- 6:49and it does a lot of checks on the logic
- 6:52of the compiler to make sure it's
- 6:53working as spec.
- 6:55And I know of at least 10 plus really
- 6:58like major bugs that it found after the
- 6:59initial implementation of the compiler,
- 7:01probably dozens. But for the most part,
- 7:04after the first few thousand theorems
- 7:05and lemmas, it did catch the vast
- 7:07majority of them. Nowadays, it becomes
- 7:09very difficult to even find anything
- 7:11wrong with it, so it's actually quite
- 7:13strong. And that's part of the reason
- 7:14why I'm so excited to open source it. I
- 7:16will open source all of these theorems
- 7:17and lemmas as well because this was
- 7:20definitely not cheap to generate. I'd
- 7:21say I was calculating the other day how
- 7:23much this actually cost and it was
- 7:25equivalent to around $40,000 of API
- 7:28credits in terms of just the sheer
- 7:30amount of volume and inference required
- 7:34to actually produce the amount of
- 7:35theorems at this scale.
- 7:37So, I do want that to be open source in
- 7:38case anyone else wants to help
- 7:39contribute and prove various things
- 7:41about the compiler itself.
- 7:43But this is just a a fun example of the
- 7:45type of things that it's catching these
- 7:46days. So, there's no chart out there
- 7:48that's 292 million years in length. But
- 7:52assuming there was and the week of the
- 7:54year variable here that's in Pine
- 7:56Script, after 292 million years, it was
- 7:59returning this insane negative integer
- 8:01just due to the mechanics of the
- 8:03bytecode that was being generated. And I
- 8:05found that quite funny because there was
- 8:07a proof and it it and I was able to
- 8:09patch it very easily, of course, but it
- 8:11shows just how powerful formal
- 8:13verification is as a school of thought.
- 8:15It's in a lot of ways like the ultimate
- 8:17form of unit testing. So with that said,
- 8:19I did want to run through an example
- 8:21right now over on the optimizer main
- 8:24site. Okay, so here we are over at
- 8:27optimizer.aihedge.io/studio
- 8:30and I have again just very simple, a
- 8:33very mundane simple strategy. It's not
- 8:36meant to be anything serious. I have it
- 8:38loaded right now on 4-hour Bitcoin, I
- 8:41believe. Bitcoin it's almost 15,000 bars
- 8:44in total of 4-hour bars.
- 8:47And you can get this from anywhere. You
- 8:48can get it from Yahoo Finance. Even
- 8:50TradingView has their own export
- 8:51feature. And really it's quite easy to
- 8:55import it. Maybe in the future one day
- 8:57I'll have the ability to support live
- 8:59streams streaming it in. That would be
- 9:00really cool. But for now you do bring
- 9:02your own data and once that's in you can
- 9:04pretty much use it pretty seamlessly.
- 9:07So here you can see it supports version
- 9:086. I have this really cool tab that
- 9:10shows you the entire process of how it
- 9:12gets lowered. And it's interesting
- 9:14because most people stop after the AST
- 9:16and target a different language. But of
- 9:18course with us we have to go through the
- 9:20whole best practice process of lowering
- 9:22it down to an IR, an intermediate
- 9:24representation, and then of course the
- 9:26IL, which is pretty much as close to
- 9:28bytecode as you can possibly get, and
- 9:30then of course WebAssembly. But this is
- 9:32how the main flow would go. I have this
- 9:35exact same copy of this over here so you
- 9:38can see I have this little back testing
- 9:41area here that's very similar to how
- 9:43TradingView conceptually works.
- 9:46And you can see that overall the
- 9:48drawdown's quite terrible, but the P&L
- 9:51it's not bad. I do notice that the buy
- 9:53and hold for this very simplistic
- 9:54strategy is actually better than the
- 9:56strategy itself in a lot of cases. And
- 9:58of course the drawdown is not great. So
- 10:00the way I would do this is after you are
- 10:02happy with the initial settings you can
- 10:04simply click optimize and this will
- 10:06bundle it into a WebAssembly module and
- 10:09bring you to this
- 10:11And here you can see that it's written
- 10:13all as one sentence. You can change the
- 10:15data set that you're operating on, but
- 10:17in general just take whatever that you
- 10:19were just on over there.
- 10:21you can specify the amount of trials
- 10:23that you want to run, of course in the
- 10:24browser or if you're on one of the plans
- 10:27you can run it in the cloud where you
- 10:28can spin up a lot of different
- 10:29concurrent instances. So the run
- 10:32settings itself, I want to say like a
- 10:35minimum trades of 10 tends to make
- 10:37sense. This is a very important one. If
- 10:40you ever want to do this, sometimes it's
- 10:42useful also to throw in drawdown as a
- 10:45concurrent objective function.
- 10:47So that will make sure that you're not
- 10:49only optimizing for profit, but also
- 10:51staying reasonable in the drawdown. And
- 10:54there's a lot of different settings for
- 10:55that. I can get into that in a different
- 10:57video. But that's pretty much it. Once
- 10:59you compile it into that web assembly
- 11:01module, you can just start running this
- 11:04in the browser. And it's pretty neat.
- 11:06You'll see it loading up and it's very
- 11:09fast. It'll map out the entire
- 11:11topography for you. You can see it's
- 11:13trying every possible slow length and
- 11:15fast length combination here.
- 11:17And what's cool is that you can actually
- 11:20see it. So this one has a third one with
- 11:22exit ATR buffer. And you can see that as
- 11:25well evolving concurrently. So it looks
- 11:27like it just finished here and it brings
- 11:31me to this screen. So you can see this
- 11:33is pretty much the frontier of the
- 11:35tradeoffs between smaller drawdown and
- 11:37higher profit factor.
- 11:39And looking through here, this is
- 11:40already much better I think than the
- 11:42default. Even up here it's actually not
- 11:44too bad. To a certain point you're just
- 11:46going to have to change the strategy if
- 11:47you want to see better ones than this.
- 11:50But it's interesting because if you look
- 11:52at this, you can see the topography is
- 11:54quite consistent here and this confirmed
- 11:56that it wasn't just a fluke, that there
- 11:58really is a sort of mountain range here
- 12:00where it leads to much more stable
- 12:02profit factor score and also drawdown.
- 12:06So anytime you want to examine a part of
- 12:08this little mountain range, for example
- 12:10that one, you click that and you can
- 12:12just look at these settings directly in
- 12:13Studio. So now we're going to compare it
- 12:16to the original reference that we have
- 12:17over here.
- 12:19So this is the original again for
- 12:20comparison. So 767,000
- 12:24PnL
- 12:25and it was 1.7 profit factor with a
- 12:28drawdown of 46.42%
- 12:31and just by changing the settings and
- 12:34nothing else, I didn't change any code,
- 12:36we were able to improve this to 1.5
- 12:39million of PnL
- 12:42and a 29% drawdown with a much higher
- 12:45profit factor as well. Increased there
- 12:48almost more than doubled it. So that is
- 12:51a good example of how the optimizer can
- 12:53be used to just improve your existing
- 12:56strategies and find for your market and
- 12:58your chart that you're trading
- 13:00over in TradingView what the best
- 13:02settings are. And again, I'm not trying
- 13:04to replace TradingView. If anything, I
- 13:05want to make it easier for people to
- 13:08optimize their TradingView indicators
- 13:10and to help you get more out of the
- 13:12strategies that you have over there.
- 13:14So hopefully this was useful for you
- 13:15guys. Feel free to go ahead and make a
- 13:18free account and try it out in your
- 13:19browser.
- 13:20There are lots of things you can do for
- 13:22free and that is by design. I wanted
- 13:24this to be a tool that can really help
- 13:26everyone regardless of what your means
- 13:29and circumstances are.
- 13:30So let me know what you think. Pine
- 13:32Wizem itself is open source. I'll be
- 13:34open sourcing those lean proofs as well
- 13:37and the benchmarks as well as this
- 13:40repository. It'll all be public once I
- 13:42finish cleaning it up for you guys.
- 13:44So if you have any [music] questions or
- 13:46comments, as always, feel free to leave
- 13:48those down below and subscribe for
- 13:49future updates.
About this transcript
This page contains the full transcript of Optimize TradingView Strategies in Seconds (Free & Open-Source) by Justin Dehorty, generated from the public captions YouTube serves with the video. The transcript has 2,650 words across 398 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 choose a caption track available on the video.
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, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.