Simulating a real-time ticker

Posted 7 months ago
1970 Views
|
12 Replies
|
6 Total Likes
|
 Hello all, It seems that my previous inquiry was removed by the moderators as they wanted to see me invest time and effort in my problem. I have invested time and effort in the more "algorithmic" parts of my project, but now I need a bit of help in controlling the charting of stock data.Here is my situation. For the past 20 years, as an Electrical Engineer with an interest in the recognition of stock trading patterns, I have worked with Technical Analysis. I have worked for startups and written software that can recognize trendlines, technical patterns, and Elliott Waves. I have done all of my previous work in C# and absolutely love the topic. It gives me something to do besides my day job as a university professor in the Department of Computer Science. I have been enamoured with Mathematica since the mid 2000's. However, for some reason, I have found it difficult to learn, compared to all the previous (traditional and not so traditional) languages I have mastered (Basic, Forth, C, C++, C#). One of my problems is that I was always impatient to get the work done and I could do that with something like C++ with my favorite, C#. It could take me a couple of 100's of lines, but the process was rather simple. But with Mathematica, it is a different mental approach. What I love with Mathematica, is that I can take those 100's of C# lines and compress them into 10 lines (or less) of Mathematica. The problem is that, while I can write those 100's of C# lines in an hour or so, it takes me a week for me to get my head around all the functions, tables, lists, etc,,, I use in Mathematica. The final result in Mathematica is like a work of art. It is beautiful, it speaks to my mind. The work in C# produces a beautiful application, but I don't find the program itself that fascinating. It is a bit like pointillism. The pixels (the programs or lines of code), in themselves are not that interesting, but the overall effect (the application) can be quite stunning. In Mathematica, you don't have pixels, but swaths of paint of different colors (List, Map, BlockMap, etc...) and combinations of a few swaths (a single line of Mathematica can be simply mesmerizing). However, it can take hours to design that swath or understand someone else's swath. I thus decided to delve into Mathematica and I wanted an interesting project to do so. I chose to rewrite my Technical Analysis Suite in Mathematica. My C# version, displays stock data, indicators, patterns (like channels, triangles, pennants, flags, etc...) and now can analyze all the potential Elliott wave patterns present in a stock move from one time (minute, day, week or month) to another. I plan on taking a large number of stocks over a certain period and generate all of the possible Elliott wave patterns that were created for each stock during that period. Then I will feed to patterns (and their subsequent result) into an AI algorithm to create an Algorithm that can then be used to determine if a particular stock should be bought or sold. I have never been convinced that a single individual (Ralph Nelson Elliott) could, before the advent of computers, figure out how markets work. But, I figure that his theory of Elliott Waves could be tested. The problem, from what I understand, is that, if you put five Elliotticians (as they are called) into a room and show them a stock chart, you will get 5 different interpretations, all of them acceptable! My view is to take away the human element, find any and all potential waves, then feed them into an AI machine and see if our powerful modern-day algorithms can figure out which patterns have strong predictive powers. As such, I have put in a lot of work in converting my C# code to Mathematica. I can't tell you how amazed I am in the power of Mathematica, taking thousands of lines of C# code and reducing them to about a dozen lines of Mathematica code! Given a stock over a period of time, my code will determine ALL the potential Elliott Wave patterns that occurred for that stock during that period of time. At some point that information could have been used to make trading decisions. However, my interest is in deciding, once and for all for me, whether or not the different patterns mentioned in the literature (things like the bat, the butterfly, the crab, the shark, etc... from "The Harmonic Trader" do occur and do have predictive power. I have found that they do occur, and often. As for the predictive power, I see anecdotal evidence, but would like to take the decisive step of simply feeding mathematically provable patterns (meaning that the patterns I feed are ALL the possible wave patterns in the chart) into an AI program and generating an algorithm (e.g. Neural Network) that could then tell us what its predictive power is. We could also see if the Support/Resistance prices generated by the Elliott Waves are also validated. In order to do so, I wanted to have the ability to display the stock on a chart. I have used Mathematica[s InteractiveTradingChart which offers a great way to display stock data. My problem is that InteractiveTradingChart shows the chart in one short. What I want to do is to show the chart incrementally. What I mean by that is that I want to show the first bar, and then, after a short period of time, show the next bar, and so on. I might want to show each new bar after a delay of 5 seconds (or any other time period) in order to simulate a ticker. Also, my algorithm works in an incremental fashion. You feed it a new bar and it figures out what new information or new Elliott Wave may have emerged from the new bar. All I've been able to do so far is ask the chart to redraw itself, with the new bar, But I do this by reloading the FinancialData (which takes a while) after having updated the last day of interest. This is not satisfying. I would like to read all the data in one short (reading weekly bars for instance, from Jan 1, 2020 to the current day) and then feeding the bars one at a time to the chart once every 5 seconds. I don't know how to programatically control the display of the chart. There is a little window on the bottom which can be slid left or right or stretched to show more or less of the stock. One approach I an thinking of is to start the window on the first day of the stock data I have and every 5 seconds, change the change the right edge of the window to the next day. In this way, it would look like the chart is updating itself every 5 seconds. Can anyone help with my problems?
Answer
12 Replies
Sort By:
Posted 5 months ago
 HenrickI would be very interested in working with you on your book on Finance Patterns. I agree with you that such patterns do exist but they change throughout the day, on different days and over different time periods bound by major economic events, such as the 2008 stock market crash. I am a senior Wolfram technology consultant and WRI's consultant expert on finance and economics. I have a PhD in Maths, was a maths professor, then a finance professor and professor of computational finance at the Graduate School of Business at IIT, Chicago. I also designed trading strategies for traders at the CBOT before joining WRI in 2010. My email address is mkelly@wolfram.com.Regards Michael
Answer
Posted 6 months ago
 Hello Mike, Collaboration is always good in my book. Pease realize that though proficient in C#, I am not so with Mathematica. I started with Mathematica about 6 years ago and it has been in fits and starts. I would get discouraged by how different Mathematica was from your other languages and would go back to C# where I felt I was more productive and really knew what I was doing. I even had a period where I used NetLink to have Mathematica call libraries I had written in C#. I also understand that I could have worked the other way around, using Mathematica from C#. But this time, I decided to dedicate myself to rewriting my whole application in Mathematica. My day job is as an instructor in the department of Computer Science at the University Of South Florida, in Tampa. Though I have taught in the Computer Science Department for the past 15 years, my Ph,D, is in Electrical Engineering with a specialization in Signal Processing and Pattern Recognition. I have worked in handwriting recognition and image processing at IBM's TJ Watson Research Lab, face recognition, stock pattern recognition. Because of those areas of interest, I have spent most of my time coding solutions to problems in the aforementioned fields. I have been interested in the stock market since I worked for a startup that wanted to recognize stock patterns to give advice about buying, selling or holding stocks. I have been working on my hobby for the last 20 years and my ideas on how to solve the problem of finding patterns in stock charts have evolved, particularly in terms of implementing algorithms to detect patterns and more importantly, see if there are any patterns, and if so, if they have any predictive capability. Knowing one way or another would be important. Perhaps you can tell me about yourself, your interests, why this interests you, your experience in this area, etc...Regards, Henrick Jeanty
Answer
Posted 7 months ago
 Henrick:I'd be interested in collaborating with you on this sort of thing. But, I won't have any availability for a couple of weeks.If you want a collaborator, just let me know.
Answer
Posted 7 months ago
 Rohit, I've been looking at your solution a bit more in order to try to understand what it does. You get the data through FinancialData[...] You then make a table of individual frames, with one frame for each new bar. The frame goes contains as many bars as the index of the frame (i.e. Frame # 20 would show 20 bars) and the defaultDuration is the number of frames/second. What I really want to do is draw my own graphics (mostly lines) on individual frames. Is that even possible to do ?
Answer
Posted 7 months ago
 Hello Rohit. Yes, it is true that once I've loaded the stock data, it is all there and I can compute all the patterns, waves, etc... However, my algorithm works incrementally, meaning that having analyzed the last N bars, it updates new patterns, waves, etc,,, when the new bar comes in. So, for example, say I am looking at 1 minute bars and started doing so when the market opened on a particular day. After 30 minutes I would have 30 bars and, presumably, information about patterns, waves, etc... up to that point. Then, at minute 31 I receive bar #31. At that point, I don't want to have to recompute everything with 31 bars. Instead, my algorithm uses the newest bar to update its patterns, waves, etc... So, I want to simulate a ticker that updates, let's say, every minute with new 1 minute bar ( Could have the update be every second to go faster) and on every new bar, I want to display on the chart (that is another question I have) extension of trendlines, appearance of patterns, waves, etc... I could even display BUY or SELL signals or display the appearance of support or resistance lines.Now, perhaps you can answer this question. Can one draw lines on individual frame, and if so, how?
Answer
Posted 7 months ago
 Henrick,Is there a reason for wanting to couple the analysis with the animation? All of the data that is going to be animated is already available in data so you can pre-compute. Here is a trivial example that precomputes the change in volume between samples and adds it to the chart as a label during the frame generation. analyzeData[data_] := data[[All, 2, 5]] // Differences volumeChange = analyzeData[data]; frames = ParallelTable[TradingChart[data[[1 ;; n]], PlotLabel -> "Volume Change: " <> ToString[DecimalForm[volumeChange[[n - 1]], DigitBlock -> 3]], AspectRatio -> 1/4, PerformanceGoal -> "Speed", ImageSize -> 800], {n, 2, Length@data}]; Export["tcanim.gif", frames, "DisplayDurations" -> 1] 
Answer
Posted 7 months ago
 Hello Rohit, This is much better. I will try it. Thanks.
Answer
Posted 7 months ago
 Hello Vitaly, Wow! This looks great. Let me first say this. This Wolfram Community is absolutely wonderful. You ask a question and very quickly you get replies from knowledgeable people. I love it !However, I have another problem which has to do with my previous way (meaning C# in Windows) to do things. I really love how you even provide code AND the resulting animation. Thank you so much. I would have an event that would say that I have a new candlestick. Upon receiving the event, I would then have code execute to update the effect of the new candlestick on the data we had so far. My question becomes, how can I use the new animation to call my code (basically, analyzeNewBar[newBar] where I would pass the new bar to my algorithm. Perhaps you can send me some code where you could show me where I would: 1) Know what the latest candle (newCandle) is. 2) call analyzeNewBar[newCandle]I have a new question, but I will be starting a new discussion with it.
Answer
Posted 7 months ago
 To add a variant of @Rohit's nice example I'd mention possibility of creating a GIF where you can define time each frame duration with option "DisplayDurations" in seconds (in the images above it is 0.1 and o.5 seconds to see the difference). You also can speed up and control appearance with some options (ParallelTable, PerformanceGoal, etc). It would go something like this: data = FinancialData["GE", "OHLCV", {{2020, 01, 01}, Today, "Week"}]["DatePath"]; frames = ParallelTable[ TradingChart[data[[1 ;; n]], AspectRatio->1/4,PerformanceGoal->"Speed",ImageSize->800], {n, 2, Length@data}]; Export["itcanim.gif",frames,"DisplayDurations"->.5] 
Answer
Posted 7 months ago
 Henrick,I also noticed that the animation is sluggish / erratic when it starts, but tends to smooth out if it is left running for a full cycle. Did not see any error display in the expression though. One way to improve performance is to rasterize the frames. Takes longer up-front but the animation works better. rasterFrames = Rasterize[#, "Image"] & /@ frames; ListAnimate[rasterFrames, DefaultDuration -> Length@data] 
Answer
Posted 7 months ago
 Thank you Rohit. Your solution is a start. I put it in and it does animate the chart. However, I see that as the chart is updated, it might have 4 or 3 or 1 new candlestick added to the previous ones. I really would like to have it update with only 1 new candlestick Interestingly enough, while your example was running, the cell had the Orange bar on the right with the + sign at the top that indicates that there is an error. So, I aborted the evaluation and looked at the frames variable. It was very informative. Thank you very much. I am not marking your reply as a solution yet, as I would like to see what others might contribute. However, I could use what you sent, with some changes. Again, thank you very much.
Answer
Posted 7 months ago
 Hi Henrick,I could not find a way to programmatically change the date range in InteractiveTradingChart. You could try something like this. data = FinancialData["GE", "OHLCV", {{2020, 01, 01}, Today, "Week"}]["DatePath"]; frames = Table[TradingChart[data[[1 ;; n]]], {n, 2, Length@data}]; (* Update every second, scale DefaultDuration to reduce update frequency *) ListAnimate[frames, DefaultDuration -> Length@data] 
Answer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments