Wednesday, October 19, 2011

Scala Stock Charts - part 6 some GUI things

I got a bit caught up in all the programming and finished up a few diagrams, then some refactoring and was able to remove a couple of hundred lines of code. The program now looks like this:

As you can see a lot have happened since the last pictures of the application. So here is some things.

Main part

The applications is build up around a main frame that contains the bar chart and some overlays. As you can see there is also some text fields and buttons. The upper part is to search stocks/indexes and also handle the overlays in the bar char. These fields and buttons are:

  • Search field to enter a search criteria to filter the instruments to search for.
  • Search button, to apply the search
  • A combo box, to show the filtered search result, and also choose the stock to view. This combo box listen to changes and updates the bar chart with the selected stock
  • Next there is a box to limit the range of the data. I have not introduced recalculation for splits and emissions so to have a possibility to handle to not show before these events, I added a box to filter away prices before this date part.
  • Next there is the intervals for the moving averages in the chart, default they are set to 5,20,50 but can be changed but have as most 3 different, but also as few as one. Default they are Simple Moving Average (SMA),
  • to change to Exponential Moving Average (EMA) there is a checkbox
  • to update changed settings for Moving averages there is a button "Update averages"
  • finally on the top there is a check box to switch on/off viewing of "Bollinger Bands"
In the bottom part there is buttons for additional chart types. these are the ones I picked as most interesting for me and they show some different aspect of the prices.
  • Stocastics (show how a stocks price is doing in relation to past movements)
  • RSI (shows how strong a stock is moving in the current direction)
  • Aroon (shows if a stock is trending or oscillating)
  • MACD (and also MACD-histogram) (a momentum oscillator)

As I mentioned the main part is a Bar chart diagram. It contains some overlays like volume, moving average and optional Bollinger bands. These help to get a basic overview of a stocks movement and trend. It is made up of a specialized panel for the bar chart to handle the drawing of the chart.

The main application implements a method named top that is used in wrapped Swing features used in Scala. I have an object representing my starting point of the application, the first lines looks like:
As you can see I provide a title and also here is the search field where to put a search string. The most important thing to note here is the inheritance of SimpleSwingApplication which makes this object a Swing application

The next part looks like this
Here you can see some on the fly declaration and reactions to events on the components.  The following code shows how to add these to the contents of other panels. you can also see the creation at the click of the button, creation of a generic diagram for "stochastics" mixed in with a DrawListener to make it drawable. Also how the series of stochastics is added to a generic diagram can be seen here.
This last part of the code adds the actual panels containing the fields, bar chart and buttons to the Swing application.

Additional panels

To add some extra check to the main part there is some diagrams that first was their own specialized panel, but after some refactoring they now all except MACD uses a generalized panel called GenericDiagram and here is a part of that code:

And here is addition of some of the buttons into menu panel (top panel) followed by some example of the bottom panel with addition of button and reactions to its event. You see here that here is where the panel frame is set up inside the reaction to show the GenericDiagram with "stochastic" data.
Not so much to mention really except that as you see I extend with a trait with basic chart properties. this is intended to have common properties for this and other diagrams. The var's is to be able to add series and date series to the diagram. So this panel is only responsible to draw the things that are sent to it.

Ability to draw

No bar chart is complete if it is not possible to draw some help lines. This is managed by adding a trait for this, that can be mixed in wherever I want it. The code for that trais is:
An example of this functionality is to draw help lines in a MACD diagram. (or any really)
This can be mixed in like in this case:
And then it is possible to draw on that panel.


  1. Hi there! Have you published your code somewhere like GitHub or something? I must say that your application looks very promissing, it would be great to look into it bit deeper. Cheers!

  2. Hi, not as it is now. I was thinking of doing that. But I put in a lot of work in this and I am not sure that all statistical stuff is correct yet. I was planning to do some partial release there with just a few of the diagrams.

    Which parts would be interesting?

  3. In which part i'm interested in is hard to say. I would say: an uncomplicated (comparing to eclipsetrader or something like that) library for handling stock prices and a bit of visualisations and some simple analysis built-in. I was also think of teaching myself Scala and that's way your work interested me so much. Does your library/program can deal with different time scales (day, week, etc)? I'm thinking about playing around with some indicators and as long as there isn't AmiBroker for Linux, I'm looking for other solutions:) Of course I'm aware that its hard to achieve even 1% of AmiBroker functionality. But it seems like lots of fun for me:) Have you consider using ta-lib, which is free technical analysis library written in Java? Cheers!

  4. Ok, I will ponder it for a while. Probably I will not do anything before Chrismas anyway. Lots of other things right now.

    I Think I stumbled upon TA-lib when I was doing Parabolics, but I found it somewhat unclear in naming of classes and so.
    My purpose of doing this is anyway to join 2 of my interests, TA and learning new programming languages.

    Anyway, it would be an interesting thing to have an open source project for TA in Scala, but also that would require some extra time, but why not. Could be fun, and also improve what I already made, by modularize it and make it easy to add diagrams etc.

  5. what is the Ironic ?
    i am listning this first time.

  6. Thanks. Well not all posts are ironic :)