Code a custom NinjaTrader 8 indicator using ChatGPT (part 2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is part two of our coding a custom ninjatrader 8 indicator using chat GPT when we left off last time we had just used the prompts to generate the uh what we hope is a functioning piece of code but I already know since I've tried to run this that it is not the case but let's see what we can do to make it uh functioning so if you'll remember from last time we we have a couple wmas welders moving average and an EMA that we want to um have in our custom indicator just increase the font size a little bit there and you know with a couple color changes as properties that the user can set as conditions on what color to color to paint the candles alrighty so let me show you since I already hinted that I had tried it what it ends up looking like or what I was able to get it to look like this isn't quite what we started out with as intention but we do end up getting some colors on the candles I'm gonna go ahead and go back to the code and I'm gonna delete that and then start from scratch from the chat GPT generated content so let me go ahead and go to the ninjatrader version um we called ours color candle with wmas but I'm going to go ahead and remove everything that is actually functioning right now I'm gonna take that out and leave a section of code that I don't think I could increase the font on this any further so hopefully you can see this clearly I have not recorded ninjatrader interface before alrighty okay so let's go back to the prompt and we're gonna we're gonna copy the last generated file here I know it's missing a couple things like right here because it was trying to we had asked it to do some updates so I'm gonna have to remember to get that I'm gonna copy there uh one of the things you'll notice is that we had to tell it to continue in order to generate additional code it gets that way sometimes when I don't quite know it it's certain strings that cause it in this code editor um embedded you know code display so when you call continue it'll you know print out the rest of what it's supposed to print but it's not within a this nightly nicely formatted display so we're gonna have to do a couple copy and paste to get back to where we were all right so that's just the first portion and I'm going to copy this and the way you do it is you just paste it at the end of wherever that is so in this case it's after the SQL sign I'm gonna paste it in and then I have to reformat it tab it a couple times tab this and as we know right at this point our class and our namespace isn't fully enclosed I'm just going to enclose that for now even though I'll be overwriting it in a second just so that ninja script editor doesn't complain um so I've copied this now I need to get the rest of the and it happens to have in closing class but not name space so I'll remember to paste that much in so I'll just delete that and paste the rest in alrighty I think that's everything we'll get it nice and neat here okay no syntax errors thus far Let's see we can actually compile all the other things that I um when I created a new indicator with the same name so that's how I started I should I should step back I just click the plus button here go new indicator click next and then I put in the name of the indicator I want in this case it was candle color ring with wmas and that's what I put in and it generated the file but I don't want to regenerate or recreate that file and I didn't want to really delete it so hopefully you can follow along that much I'm going to remove the Imports that the um that the the chat GPT generated and just take whatever was already available to me I'll do I could clean up some more but I'll just leave it at that okay all right we'll compile this and we already got our first um set of Errors first of all there's no such built-in indicators such as or called WMA luckily I created one called Wilder's ma already when I was doing my other project this again I'll just go straight go to it is a indicator it looks like the EMA just uses a different constant as the denominator compared to the the EMA code that ninjatrader provides you so this constant um you know you can see the math is basically where the the code flow is basically the same except I have our different constant value and the value in the onbar update is also a little bit different not much you know so between that and that okay so that's our that's our the first thing we'll have to do is we'll have to reference the uh the one that I created instead of WMA because that actually doesn't exist let's compile that and see what other errors we get all right okay so this is the the parameters or the properties that we had talked about that I'll need to copy from a previous generate call which is right here and this too got cut off so I'm going to have to do it in twos which is this portion here so that is an annoying thing and this is where the properties actually go and it tells you that and we'll run into this when you generate files with I don't know if it's a size issue like number of code lines or characters that it generates or what type of content it generates like if there's a special character that is throwing it off or what okay I think that should be what we have okay now we have to finish off what we started before since there's no Wilders we'll just have to reference the one that does exist and here we get our first real code challenge of sorts the the indicator while there's ma doesn't take a single value it takes a series so what we'll need to do is pass it in a series and I have some of that code off already so I'm going to go ahead and copy them into this let me find that so um just do this and I'll actually change the name of it to uh close uh series and um because of the complications that I actually had I'm gonna just use a single series The wmas there'll be two of them in the code but for the Simplicity of uh in the the length of this recording I'll just have one series but if I wanted a separate series for each of the wmas I would have to you know do like close series and then call it something else like price diff series and then what I would use in my WMA here I would just call it um closed series and this could be a price diff series hopefully you get the picture there for the built-in EMA one close by itself without a um trailing you know bracket that close means it's already a series a data series I don't know if I could Link in in Visual Studio you should be able to follow through and see oh I guess Mouse over you can see that in the tool tip it says up type I series double and that's what we really had to pass into the other ones to I series or series double so that error should should get us past that we have other errors down below here where we are it's setting candle color some built-in indicator property name candle color that chat GPT thinks exists but it actually doesn't instead what it needs to be is that we need to set our brush and we'll just call it a candle color all right let's just call it Kendall brush to be correct and then we will use this candle brush uh to set the bar brush to equal to Candle brush and and bar brush is the actual property and along with a candle outline brush we're gonna we're gonna color both to be the same color the the outline and also the bar itself so that should get us past that compilation and we'll come up with a new one and in this particular one I um I'm not a you know versed enough in ninjatrader to know exactly why but we can't have solid color brush as our property type or I guess one way I fixed it was just reference brush instead of solid color brush so that's what I'm gonna do here for the neutral the down and the up color the other thing I'll also have to do is where it sets like say up color brush it's it's using this convention the other way you could do it is actually not reference you know this this means basically the same thing as what was before except we don't have a private variable like in down color we have a private variable called down color that we're setting the past and values to or you can just have a public property called up color I'll keep the convention that ninjatrade or chat GPT had had created or generated but what I'll need to do is also change the type of this variable too so we'll go up to the top you can see they're also called a color brush so I'm going to just change them to brush as well so that we are correct that the values are being set and looks like we got past all of our compilation issues which means um well did I compile it yes I compiled it I'm going to go to my chart I've already loaded it but let me go and remove it first and then we'll be at it together just to what I have um right now if you didn't catch that was two two plots the Wilder's ma that we're also using in the in this new aggregated indicator and also an EMA both with a period of 14. the the first one the Wilder's one we're gonna change that to um let's change this color to B I don't know if Coral is a good color for display but you can see the Wilder's one is the coral the dotted or dashed yellow is the EMA and so if we go back to adding our indicator that we just created the color nope that's not it that's candle coloring with wmas we'll add that in our up color is green so when our condition says Hey use up color we'll use green I'm going to change that to Dodger blue because why not and the down color will change to Yellow neutral will be gray our period for our wacb 14 our EMAs will also be 14 just so that it'll match what's currently displayed and it's not that's pretty good you know we the colors changed except it looks like we just have neutral colors showing on the up bar so we're gonna need to change our our logic that actually picks which color to paint the candles but um it's not bad so we'll go back to our code and figure out what we need to do so there's actually two things one is that our WMA because we're referencing a local data series we're not actually updating those values on on bar update we could change this all we want but right now the WMA value is actually not being generated you know with each each run of the onbar update so we're going to have to decide what we want to do there and what we'll do is say we'll set the closed series at the current bars value and what we'll do is we'll just use the close price so the EMA and the WMA will be using the same thing now the other alternative if you really you know want to be technical is you could just use the close right there as well instead of a custom one but I'm going to show the custom one because in reality what we would do is you know do like close minus open that was the intent of the original design but I think it gets a little too complex for what I'm trying to show here so I'm just going to take the closing value by itself um to do the closed series and while I'm at it I'm just going to do the price diff series to be the same again this isn't what you would do you'd put your own logic here to use against the um to run it to run that data into the WMA and or EMA and so that that so you would probably want to do this here if you use the closed series or something and run the EMA on that but for simplicity's sake we'll do that and I don't know if this needs to be done if setting this value there actually runs this but I'm going to keep it in here because I don't think it hurts I think in here there's a check if it's recently been run then it doesn't run it again so it's kind of like a if you're familiar like garbage collection calls in Java or c-sharp you can call it all day long but it'll only do it if it needs to be done kind of thing all right so that should now generate based off of the closed series we now have to modify how we want to do our logic and what I'm going to do is simplify it and only use the wma1 in the ma value to figure out when to color the candle and what I'll do is say if the if the wma1 is greater than the EMA let's call that our up color and on the but if it's um if it's less than EMA we'll go ahead and call that the down color otherwise if it's equal we'll just make it the neutral doji color and if I recompile that oops again oops series I forgot my bracket here and uh just as a pause at this point what we'll say is or you know talk about this what we're saying is at the close using the closed series at the current bar set it to the the closing price at that bar as well before our error was this where we're setting the series itself the whole series not just the the bar value or the value at a particular index or position and so you're you're just missing or mismatching types now if we were to do this this would actually be valid but I don't know if that's a efficient use of the code especially if we're adding our own logic at some point so you're just saying you know for the current price our current location in the series set it to also the current uh closing price let me compile that and that should get rid of it now what we have to do here is refresh reload our ninja script and here we go that is not correct our bar is not the yellow and green of of what we have here so we're gonna have to figure out why it's not coloring it to be those two things and I actually don't know why so I'm going to go ahead and um I'm gonna remove the indicator first and then give it a try again and then it's still not working I might need to re-reload the chart restart ninja Trader uh actually one of the things that this code didn't provide is to be able to um there's you know depending on the thread that it's running on if you're updating these colors and accessing this you may not be able to actually set the bar color in the on the onbar update because of Mitch mismatching um threads that they're running on and I'm not 100 familiar with it but I do know that when I was working on this particular indicator for someone I had to add this section of code so I'm going to copy that and what I'll do is we'll this will will create a new method called um let's go private Lloyd set bar brush oops and in there we'll uh we'll take two values oops we'll go um we'll copy this right here and we'll take in the EMA uh you may value and the WMA value and then I'll actually move all of this logic in here and EMA value so we'll use that instead of vma0 instead of referencing that just and this needs to be a double this is a problem with working in JavaScript for too long you forget to specify types till the compiler tells you and in here we'll go ema0 that's the current value wma1 so now we're and this is actually a better design anyways to remove some of your UI logic in this case the painting logic away from the actual calculation logic all we're doing say is on bar update do the calculation and for the painting just call some other method to do the the painting logic in such a small piece of code it really doesn't matter and I don't know if this will actually work to fix the indicator issue I didn't run into it previously and it still looks like it did not work okay well I actually don't know what's going on so what I'm gonna do is I'm gonna go ahead and restart ninjatrader and see if that clears out some of the caching of the the files because I've compiled it a couple times and changed the contents of it I don't know if that is causing the problem so I'm going to go ahead and restart that and hopefully we'll get lucky fingers crossed and I'm gonna go ahead and restart that while that's booting up my analysis so far of chat GPT is that unless you are fully confident in doing what you're trying to get its help on I think it's probably more harm or more time wasting than to just you know find some other source like an existing indicator that you can copy and just you know change a few things here and there just because of the training model that it uses is can be old you know for for chat GPT version 4 or the GPT model version four it's using I think data from 2021 of course ninjatrader hasn't changed all that much so it should be fine it's probably one of the better candidates compared to other trading platforms for like tradivate or Quant Tower because those are fairly new and their apis have changed a little bit but you know it's good to know what you're doing and just using chat GPT as a assistant versus the source of Truth or the primary source of Truth and I don't quite see why it's not working right now so I'm gonna go and remove it again and go back to the code and I will cheat this time um but what I'll cheat with is the file that I actually created before I started recording it's not too much different than what we have here but I do know that it works so I'm hoping that doing that will work some of most of the changes are what we've already talked about except for the last one which was painting the color in a separate environment um I'm gonna go ahead and compile all right compilation succeeded and I'll choose the and I'll change that to Dodger blue and yellow those are my two favorite colors to do things like this where you have you know up color crossovers things like that blue for me means um you know same as green and yellow same as red it's just not green or red all right so it works again I don't quite know why the other one didn't work I'm still new to ninjatrader I'm sure I could figure out over time but you can see here you know our logic is working if you'll remember our Coral is the WMA and our dotted yellow is the EMA and in our logic right here anytime the uh actually this one might be reversed yes if the EMA which is the dotted yellow is over the coral because the EMA is actually faster if if you have the same period while there's is slower than EMA EMA uses the current most recent price more has a higher weight to it versus the so this this does make sense you'd have to increase your your Demi WMA period or decrease it to a bigger number like oh not a smaller number or increase the EMA to a bigger number to to treat the ma as as uh slower in this case because they have the same 14 period the EMA is actually faster so that's why it's on top and you know the dotted yellow is the one that's above in that case we'll call it a the fast moving average versus the slow moving average if the fast is greater than the slow we'll paint the bars blue and if the opposite is true where the fast the the dotted yellow or Dash yellow is below the slow of coral will painted red um and that's it all right I hope you enjoyed this chat GPT um using chat cheap PT to to write ninjatrader code I I don't quite recommend it just yet there are conveniences that it has provided but I think there's probably more work for the simple ones than it's worth at this point especially if you're starting out new you're probably just going to confuse yourself and try to make whatever it generated work versus starting from scratch copying an existing one like I did when I created the Wilder I copied the ma I changed the places where it needed to be changed and that was a whole lot more straightforward because I knew I was working with a code that actually compiled and works versus something chat GPT kind of assumed or it thinks would work so again like And subscribe I'll do a couple more of these videos hopefully as soon as I can with trade evade and and other platforms and maybe ninjatrader again thanks for watching
Info
Channel: Latter-day Trader - Codes
Views: 2,202
Rating: undefined out of 5
Keywords: chatgpt, generative ai, ninjatrader, nt8, indicator, wilders moving average, wma, custom indicator
Id: GaklZbNqDcU
Channel Id: undefined
Length: 28min 56sec (1736 seconds)
Published: Sun Mar 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.