Saturday, November 1, 2014

how to 'soup'-up thinkorswim

thinkorswim (tos) is my trading platform of choice. it provides a full-featured charting platform with extensive quoting of many different markets (some of which you would pay extra for on other platforms just to see 'em,) and a robust user-programmable scripting language. however, at times, it is a love/hate relationship. this is because application performance is not always great.
the garbage about java...
i think the first thing to recognize is that tos is written in the java programming language. java is an object-oriented language that is based on a dynamic-memory technique called heap storage. heap storage enables programmers to concentrate on solutions and less on housekeeping. in particular, a java programmer doesn't have to figure out the the prickly details of when to deallocate memory. in java, memory deallocation is automated by a software subroutine called the garbage collector. the garbage collector sifts the heap looking for objects that have a zero reference-count and deallocates them. thus, heap storage vanquishes a whole raft of memory-leak considerations that are the bane of non-heap programs. garbage collection is done automatically and is triggered by low-levels of available heap (also explicitly in the tos help/system tab but something is wrong if you need to do that.) the trade-off with heap storage is a higher requirement for memory. the higher requirement comes from the need to have a high level of free memory in the heap so as keep the garbage-collector relatively inactive. several pointers that follow address the proper care and feeding of java's heap storage.
up your bits and bytes...
the bit-number refers to the number of bits used to compose the address of a particular byte in memory. a 32-bit machine can create a unique address for 2^32 (2 raised to the 32th power) bytes. which means that a 32-bit machine can only ever utilize 4 gigabytes of ram. now, if you are on a 64-bit machine then you probably don't know what i'm talking about. that is my first advice to soup-ing tos. upgrade to a 64 bit machine that has a boat-load of memory. a 64-bit machine can assign an address to 2^64 bytes (about 16-million terrabytes) which is to say you are only constrained by budget as to how much memory you can install on a 64-bit machine. at some point the amount of memory installed is overkill but applications are like furniture and expand to fill all available space. you should get at least 8 gigabytes which is twice the amount of memory that can be addressed by a 32-bit machine.
up your processors...
also, most pc's these days have multiple processors. this enables a computer to literally walk and chew-gum - genuinely parallel (not just concurrent) computing. java supports parallel processing by a programming technique called threading, and tos undoubtedly is a multi-threaded application. which means, that at a minimum say, it could be running the garbage collector in parallel while your chart is painting. (2/28/16 alas, the garbage collector suspends all threads and always runs single-threaded resulting in the infamous gc-pause, for this reason you might want to consider running two instances of tos) if you are puchasing a machine on which to run tos then more processors are better. quad-core is a buzzword i hear a lot, maybe there is an even greater multiple available. my old clunker is dual core.
a virtual fairy-tale...
your operating system is like a sly gigolo - it makes an application feel like it is the only application in the world. to a running application, the memory address space it sees starts at zero and ends at the architecture limit (2^32 or 2^64) and the app has not a care in the world about whether the memory it is accessing is actually loaded into memory or conflicts with another application's memory. the os's job is to translate this fairy-tale, single-application viewpoint into the real-world of the physical machine where the program starting address is definitely not zero, there is far less ram than the architecture limit and there are multiple applications vying for system resources. in order to accomplish this the os employs a memory trick called virtual memory in which the os divides memory into chunks, called pages, and backs them to the mass-storage device (i.e. disk.) which is all to say that despite having a boat-load of the fastest random-access memory that money can buy your system's performance still has a disk-speed component because of virtual memory. there are two points of soup'ing advice that address the proper care and feeding of virtual memory:
up your page-file...
the first advice is to make sure your computer has an adequately sized page-file. the page file is the backing-store for the memory of a running application. so, however much memory your running applications occupy, there must be as much space available (and then some) in the page file on disk. a general rule of thumb is that the page-file should be twice the size of the ram and it doesn't hurt to have more. under-utilization is the only downside to an overly large page-file, whereas, an under-sized page-file will cause your system to thrash (where the os gets into a vicious cycle of transferring the same pages between ram and the pagefile.)
solidify your experience...
the other bit of soup'ing advice that follows from virtual memory is to upgrade your disk to an ssd (solid-state disk.) ssd's eliminate the delay of having to wait for a physical read-head to move (called seek time) to the right spot above a spinning disk and greatly reduce the delay in reading data caused by the speed at which a physical disk passes beneath the read-head (called rotational latency time.) basically, ssd technology is something like 10x faster than hard disk technology, and much more if you are on a disk that is 4 or 5 years old. this is like crack for a memory hungry application like tos. i spent $120 at staples to buy a 100gb ssd. it took me 30min to copy the image from my clunker hard drive and the performance improvement was night and day, not least to mention my vista os reboots in under a minute now.
up your memory settings...
as for tos memory settings, i set mine to the 1280-1280 maximum available to my 32-bit machine. this means when tos starts up it allocates a heap of 1.2 gigabytes all at once. the trade-off is that other applications might be starved for memory when i have tos running. however, for me, when i am trading everything else is secondary.
throttle your quote-speed...
even with all these stops pulled-out, i still was experiencing some nasty performance problems that were only remedied by restarting tos. by monitoring the help/system display i could see that my system was exhibiting a behavior i call heap-thrashing. this is a vicious cycle whereby the free memory would deplete to 25k or so, then the garbage collector would free 125k or so, and then 100k would get immediately allocated. i believe that this behavior is a quote-speed problem because it only occurred when the cash-market was open and when i adjusted the quote-speed from real-time to max 10-second delay (on the settings/application-settings dialogue) i was able to almost completely eliminate this problem while preserving a substantially live-market experience. on the help/system tab you can monitor the number of quotes per minute being received by tos. when i was experiencing heap-thrashing i was receiving in excess of 12,000 quotes per minute. after throttling this back to max 10-second delay this number went to about 3,000 quotes per minute.
cable it...
additionally, i see intermittent performance problems. all the local platform metrics are 'nominal' but there's an intermittent delay drawing a chart or updating data in a custom watchlist column. when this happens there's probably a communication delay. i have found through trial and error that my thinkorswim platform experience is smoother when i cable directly to my router with an ethernet cable instead of using wi-fi. now, a lot of things can go wrong with radio communications. in my former career i was programming computer systems that communicated by radio to terminals and one such system was experiencing intermittent delays. the problem was diagnosed and corrected only after a month of full-time consultation with radio experts who used hi-tech monitoring equipment (side note: be careful what you say on a cell-phone because all that gets transmitted un-scrambled on public airwaves.)  it turned out that a similar system running on a different frequency that was only used between 10am and 11am in the morning was creating what the radio experts called 'harmonic distortion.' so with the density of wi-fi routers in some locations it doesn't surprise me that there is sometimes a hard-to identify problem with wi-fi communications. i never could diagnose my tos issue with wi-fi because everything else worked great- webpages loaded instantly, pings showed no packet loss -only tos was hampered. even so, i stay tethered to my router.
up your isp...
i have word that the thinkorswim servers are physically located in in dallas. you can check your communications delay to the dallas area using internet speed tests such as this. here's what i get for this test:

i am getting a relatively good tos experience with this level of communication service. upping the speed of isp communications will enable you to receive more quotes per minute over the same bandwidth but you also have to have a fast enough and big enough bucket on the other end to handle all those quotes. in retrospect, i think, perhaps, some of my tos performance issues might be related to my upgrading from dsl, where i was getting 1 mbs, to cable modem. 


here's the system config with which i am having a good tos experience:
  • 32 bit machine
  • dual-core
  • 2 gigabyte ram
  • solid-state disk.
  • 4 gigabyte pagefile
  • 1280-1280 tos memory settings
  • max 10-second delay quote-speed.
  • 10base-t cable to 20mb/s router
  • 20mb/s cable modem

but my next machine might be configured like:

  • 64 bit machine
  • quad-core machine
  • 8+ gigabyte ram
  • solid-state disk.
  • page-file 2 to 3x ram
  • maximum tos memory settings.
  • maybe real-time quote-speed.
  • 10base-t cable to 20mb/s router
  • 20mb/s cable modem







14 comments:

  1. Great post! I know it's a few years old now, are there any new things that you would change to soup up TOS in 2016? Thanks!

    ReplyDelete
    Replies
    1. I took the opportunity of your kudos to make a revision to this post based on what I learned about the garbage collector later on. Look for the strike-out's above.

      Delete
    2. Thanks for the great info. Are the system specs at the bottom still what you think would work well now in 2016?

      Delete
    3. I currently have my memory settings at 128/1280 and have not fussed with them in a while. The first number is the initial (and incremental) allocation while the second number is the limit. I believe I went to this configuration when I was running two instances of TOS and wanted to allow for lessor overall allocation in one of those instances. These days I typically only run one instance but have not felt the need to adjust memory settings. I believe this is because throttling the quote speed to max 10 second delay has been effective at reducing gc pauses.

      Delete
  2. Thanks for the write-up. I've been having some major pauses during the day and will try changing the settings and look for improvement. I'm using an iMac (Late 2013). 3.4 GHz Intel Core i5. 24 GB 1600 MHz DDR3. NVIDIA GeForce GTX 775M 2048 MB Is there anything different I should be concerned with when using a mac vs a PC to get the best performance? I too and her wired to my Airport extreme router. Thanks again for taking the time to create this analysis.

    ReplyDelete
    Replies
    1. Hi Brad, so the major clue is that this happens 'during the day' - not after hours when the market is mostly static. I suspect that you would benefit from throttling the quote speed. We all think we need 'real time' but would a 10 second delay really affect your trading? how about 30 seconds? Also, do you keep a large watch list open? Every one of those stocks is adding to the quote burden. Maybe trim some of those stocks from the list. Hope that helps.
      Best.
      -Allen

      Delete
    2. Thanks for the reply Allen. In the left sidebar I keep a watchlist of 25 stocks and was mostly using the flexible grid charing tab with 1 min 15 min 30 min 1 hr and daily chart open. Although it will freeze randomly during the day I noticed that it is very prone to freezing during the day when CNBC goes to commercial and witches from live TV to the graphic that its at commercial in trader TV. There was a time when I was displaying 32 stocks on charts and not using the watch list and noticed it freeze. But maybe I just never noticed. Time warner runs about 16 mbps DN and 1.5 Mps Up. I was thinking about doubling my speed which to fix which is what led me to finding your post. Well, Ive been trading some volatile stocks over short time frames so the 10 second delay scares me. I should probably stay away from some of those anyway.

      Delete
    3. Hi Brad, you can run multiple instances of tos on the same machine. Just launch TOS a second time and login again. So if the problem is CNBC you could launch a second TOS instance and run just CNBC from that second instance. That would give the CNBC instance its own heap and garbage collector. Pausing is usually due to the garbage collector which can only do its function running single-threaded. So, although you have a wonderful quadcore processer, when the garbage collector runs you effectively have a single core processor because all of the application threads will suspend until after the gc is done. You can see this happening if you have access to a CPU monitor program that breaks down the utilization by each core - 1 of the cores will pin-out while the other 3 cores are relatively idle.

      Delete
    4. I don't see how multiple instances of TOS can be opened. Did a google search but no luck. The icon nor the drop down menu give that option. I can however pop-out sections and put them on separate desktops if thats what you mean. But no obvious way to run two full separate instances.

      Delete
    5. That might be a limitation of iMac os. I don't know for sure, I'm not an Apple user. On my Dell PC I can just click on the desktop icon a second time and launch a second TOS instance. In any case you should be able to offload the CNBC video to an ipad, if you have one.

      Delete
  3. wow. This is really helpful stuff. Are you a programmer and trader? I agree on the 10 seconds. For futures trading that's 10 minutes. lol So what are your memory suggestions for running 2 instances? With 12 gigs of RAM? quad core 2mhz processor. One is order entry. Maybe 6gb. Other is a grid layout of 8 charts. 3gb?

    ReplyDelete
    Replies
    1. Yes. Programmer/trader guy here. I now have a 64bit, quad-core machine with 8gb. This version of tos will step up the memory allocation as incidents occur. I recommend going with this procedure. I suspect that just allocating 8gb straight-off will lead to performance problems in the garbage collector, which still must run single-threaded, only with 8gb it will have a much bigger heap to sift through.

      Delete
  4. BTW Windows says don't mess with page file. Get more RAM if you feel you have to mess with it. Just their point of view.

    ReplyDelete
    Replies
    1. Do think maybe Bill Gates owns some Intel stock? Seriously, the page file has to be at least as big as RAM just to provide backing-store. If your computer is thrashing are you going to wait for memory to arrive snail-mail or make a simple correction to the page-file size?

      Delete