Tired of Manipulating Arrays – Try a Vector and a Queue
An array like structure but are dynamic and have a plethora of tools at your disposal. Arrays are cool and can be multi-dimensional and can be easily manipulated. But they require a lot of forethought as to how much size to reserve for their implementation. Now don’t think this is going to be an advanced EasyLanguage tutorial, because it’s really not. Most of us TRS-80, Ti-99/4A, Vic-20 and Commodore 64 trained programmers of the early ’80s have not welcomed objects with open arms and that is really a mistake. In this sense we are like cavemen – we have all of the rudimentary tools at our disposal and can create some really cool stuff and we can really understand what we are doing. With time and effort, we can get to the same place as object-oriented programmers. We just don’t like the concept of using other’s tools as much as we like using ours. So if you aren’t classically trained in programming you may have an advantage when tieing into the objects of a programming language. This little tutorial is a very brief glimpse into a whole different world of programming. The beauty is you can combine “old school” programming with objects – even if you don’t understand how the objects are truly constructed. I want to introduce the concept of the Vector and the Queue- truly cool Swiss Army knives. First the vector. Let’s just jump into some of the code – it really is simple.
Object Instantiation – a long word for declaring variable:
You have to tell EasyLanguage you want to use some of the tools in the elsystem.collections. You do this by simply tell it you are Using elsystem.collections. The word collections is a catch-all for a bunch of different types of data structures. Remember data structures are just programming constructs used to hold data – like an array. All the variables that you declare in EasyLanguage are arrays – you just aren’t really aware of it. When you index into them to get prior values then you become slightly aware of it. In this portion of code, I create five vectors and one queue and assign them the Null or an empty value. I just finished a programming gig where I had to build dynamically sized bars from the base data. Kind of like creating 15, 30, 60-minute bars from a 5-minute bar chart or stream. I did this using arrays because I wanted to be able to index into them to go back in time and I didn’t how far I wanted to go back. So I declared some arrays with large dimensions to be safe. This really takes a bite out of your resources which costs space and time. I had played with Vector like objects in Python, so I thought I would post about them here and show how cool they are. Remember this is a rudimentary program and could be streamlined and cleaned up. Each vector will store their respective time, open, high, low and close values of the combined bar. In a later post, I would like to do this with a Dictionary. So the opVector will hold the open price, the hiVector will hold the high price and so on.
Build a Queue – why the extra ue?
I want to build 15-minute bars from 5-minute bars so I need to know when to sample the data to properly collect the corresponding data. If I start at 9:30 then I want to sample the data at 9:45 and look back three bars to get the open and the highest high and the lowest low. The close will simply be the close of the 9:45 bar. I want to do this at 9:45, 10:00. 10:15 and so on. I could manipulate the time and use the modulus function to see if the minutes are multiples of 15 and I tried this but it didn’t work too well. So I thought since I was already in the collections why not build a list or a queue with all the timestamps I would need. This is how I did it.
I simply use the CalcTime function to add 15-minute intervals to the start time and then I add them to the queue: timeStampQue.Enqueue(Value1); You access the methods or tools to a class by using the dot (” . “) notation. Once I instantiated or created the timeStampQue I gained access to all the tools that belong to that object. The Enqueue method simply appends the list the value that you pass it. I would have preferred the method to be labeled simply add. How did I figure out the right method name you ask? I accessed the Dictionary from the View menu in the TDE. Here is a picture to help:
I use the keyword Once to just execute the code one time. You could have said if BarNumber = 1, but why not use the tools at your disposal, I figured out the combBarTimeInterval by using the 5-minute bar multiplier (3). I then looped from startTime to endTime in 15-minute intervals and stored the timeStamps in the queue. So every time stamp I need is in the timeStampQue. All I need now is to compare the time of the 5-minute bar to the time stamps inside the queue. This is where using object really come in handy.
Old school would have looped through all of the elements in the list and compared them to the value I was seeking and if found it would return true. In the object world, I can simply ask the object itself to see if the value is in it:
condition1 = timeStampQue.Contains(t);
Cool! If condition1 is true then I know I am sitting on the 5-minute bar that shares the same timestamp as a 15-minute bar. If the time stamps are the same then I can start building the large timeframe from the lower timeframe. You add elements to a vector by using the insert method. I simply looked it up in the dictionary. I had to specify where to insert the value in the vector. I simply inserted each value into the  location. Remember we are inserting so everything else in the vector is moved down.
I only need to keep track of the last 10 15-minute bars, so once the vector count exceeded 10, I simply popped off the value at the back end – pop_back(). I figured this out by looking at Martin Whittaker’s awesome website – www.markplex.com.
To check my work I printed the 15-minute bars on each 5-minute bar to make sure the bars were being built properly. These data structures expect an object to be inserted, added, popped so when you print out one of their values you have to tell the print statement what the object should be translated as. Here the keyword asType comes into play. Take a look at my code, and you will see what I mean. I hope this gets you excited about objects because the collections class can save you a ton of time and is really cool. Use it and you can brag that you are an OOP programmer at your next cocktail party.
Please follow and like us: