Wednesday, April 25, 2018

Using Interactive Brokers Python API

Some months ago Interactive Brokers released API software for the Python programmming language. Accessing the API with Python was already available with 3rd party software, but now it's part of the official API bundle.
I will show here a very basic example of how to get a basic application to connect with IB using the API. The example prints live quotes from stock tickers passed as parameters.

The API connects to the standard Trader Workstation (TWS) software, or it's also possible to use the specific IB Gateway software, which doesn't have the overhead of all the UX, so it might be convenient if you plan to leave the application working for a long time and want to save some PC resources.
You need to prepare your TWS or IB gateway before attempting to connect with the API, please check: http://interactivebrokers.github.io/tws-api/initial_setup.html#tws.

First of all, the MOST IMPORTANT THING TO NOTICE is that the API works in an asynchronous way, meaning that the results of your method calls are not returned right away, but your method call is queued in the system, and once the information is ready, it is returned in a callback. This is not completely clear in the API documentation, which makes it hard for someone new to start with it. After learning about this, using the API becomes trivial with the documentation in hand. The documentation can be found here: http://interactivebrokers.github.io/tws-api

There are 2 basic classes: EClient (which is in the client module) and EWrapper (which is in the wrapper module). EClient has all the necessary methods to initiate actions, like requesting market data or placing an order. EWrapper has all the callback methods that will be called by the API with the requested information, or with information regarding the outcome of your initiated actions.
The usual workflow is to override methods from EWrapper to handle the requests made with methods from EClient. Don't worry if it is not too clear now because with the attached example, this becomes more clear.
Check the client.py (which has the EClient class definition) and the wrapper.py (which has the EWrapper class definition) files from the API to see the available methods, the methods are properly documented with comments in the source code.

The example can be seen here: https://github.com/bsampietro/ib_example
If you don't use git (although I seriously recommend it), don't worry there is a link in github to download the code as a zip file.

Wednesday, October 4, 2017

Install a custom Android OS

In this post I am going to try to explain some concepts that I think are not really well explained out there regarding installing a custom Android OS version. And showing along the commands to do it from scratch.

It’s important to say that I am not an Android expert at all, I am just posting what I learned in the way while installing a custom Android version on my phone, so this is going to be just a very high level overview. If you are going to do this on your phone, please first do a thorough research on how to do it specifically for your phone.

Rooting
Rooting is the process of getting admin rights on your Android OS, the same as being admin in Linux or Windows. Manufacturers usually don’t give this type of access so you don’t mess your phone’s software.
Is important to note that this has NOTHING to do with installing a new Android version on your phone. To have root access is just being able to do admin stuff on your current Android version.

Unlocked bootloader
The bootloader on your phone is like the basic program that is in charge of booting the Android operating system. It would be the same as the BIOS on a PC computer. It is the first program that is run when the phone is switched on, searches for an operating system and boots it.
Again, manufacturers usually lock the bootloader of your phone, so that you don’t try to do fancy things on your phone.
So to be able to install a new Android version, the first thing you need to do is unlock the bootloader. Some manufacturers provide a way to do it, usually you register on their web page and they send you a code and the instructions to do it. Not without previously warning you that you will loss the warranty. Just google “unlock bootloader [phone brand and model]” for specific instructions for your phone.
Some other manufacturers directly don’t allow to unlock it, in this case there is usually developers around the world that have already posted a patch to be able to do it anyways .

Phone modes, fastboot and first steps
To be able to install a new Android OS to your phone, you will need to connect your phone to your PC and use a couple of command line programs called fastboot and adb. These programs come with the Android SDK, the individual Android SDK package is called “platform-tools”. There is no need to download the whole android SDK which is about 2 GB, you can just download this package, which is around 10 MB. It can be found here: https://developer.android.com/studio/releases/platform-tools.html, yes it is on the releases part of the android site! Certainly it looks that these people don’t want to make things the easy way.
So you need to do 2 things before attempting to install a new Android OS:
First: unlock your bootloader (explained above)
Second: boot your phone in bootloader mode (some people call it “fastboot mode”, because is the mode that you need to be in to be able to use the fastboot program) to be able to use the fastboot program with your phone. There are certain special button combinations that you need to do depending on your phone, usually a combination of power and volume up/down, sometimes also connecting the USB cable of your computer while pressing a certain button. Google the specific one for your phone.
Or you could also just plug your phone (enable usb debugging from your Android settings) and do:

>adb reboot bootloader

This should always work.
You can tests that the phone is ready to accept fastboot commands with the command:

>fastboot devices

and a description of your phone model will appear.
Then you can execute:

>fastboot -h

and a help page is displayed, with all the commands and options that are availabe for fastboot. Besides “fastboot oem unlock” and “fastboot oem lock” which are the commands (with some variations) that you need to use to unlock and lock your phone. The real star here is this option:

flash [ ]

So the “flash” word appear here, flashing seems to be a very sophisticated word but is more or less the same as copy, meaning that you will copy a filename on your computer to a partition in your phone.

Android partitions
Before proceeding, let’s go to what are your Android phone partitions, which are very similar to standard Linux partitions:

/boot – is the partition where the kernel and the ramdisk resides, this is the partition needed to boot Android. You don’t need to know what a kernel and a ramdisk are, this is just for information purposes.

/system – is the partition where the android system is actually installed.

/recovery – is the partition where the recovery program resides. If you boot into “recovery mode” the bootloader gives control to this partition. More on this later.

/data – is the partition where everything related to the user is stored, like installed apps, messages, etc.

/cache – is the partition used for the caching of frequently accessed data and app components.

Flashing
So basically what the flash command of the fastboot program does, is copying some file on your computer to one of this phone partitions. This file is usually an img file, which is similar to an iso file, meaning that it is a file intended to be copied bit by bit to a media device, like the iso file is intended to be copied bit by bit into a CD. This way the partition will become an exact image of the img file. Is not that the img file is copied to a partition with a filesystem, is that the entire partition will become a copy of the img file. This kind of copying in the android world is called flashing. In the Linux world to do this kind of copying one uses the “dd” command.

Now that we know what flashing is, we need to know what to flash.

The ROM files
The Android OS is developed by Google and it is open source. As phones are not yet very standarized and the Android project is not mature enough yet, this core version of the Android OS is not ready to install on every phone, like Windows or Linux distros are ready to install (more or less) on every PC computer. There is some work needed to be done on top of the OS to make it compatible to a specific device. CynagogenMod was an open source project intended to do this, customize the base Android OS for a vast array of phone models, but they discontinued the project last year. Luckily another project called Lineage OS, got the source code of CynagogenMod, and now they are doing this work. Their web page is: lineageos.org
Go to downloads and see if they are supporting an OS image for your phone, if not, you will have to google where you can get one, a great place for support is: www.xda-developers.com
These customized Android OS versions are called ROMS.
Once you find the correct ROM file containing the customized Android OS version for your phone(it is usually a zip file), then it is recommended that you also download the Google Apps package (another zip file), this file contains Google Play and other commonly used Google applications. It can be downloaded from the open gapps site: opengapps.org. Check your ROM developer instructions to know which version you should download.
So you will first flash your ROM zip file and then apply on top the gapps zip file.
If you have an external card, it is advisable that you copy these 2 files to it, before going into the next step.

Custom recovery tool
If you have an external card, the standard way of installing a new Android OS (in the Android world is called flashing a ROM), is first to download a custom recovery tool that allows you to do lots of fancy stuff, including flashing ROMS.
There are 2 main custom recovery tools: ClockworkMod (CWM) and Team Win Recovery Project (TWRP). TWRP is touch based and has more features, so if it is available for your phone is recommended.
You can google them and find custom image files to install them on your device.
To install (also called flash, because the image is being copied to a partition on your phone) any of these custom recovery tools on your device, you have to use fastboot. From your PC launch a terminal, go to where your fastboot is installed and:

>fastboot flash recovery either_twrp_or_cwm_file.img

Writing in the recovery partition of your phone, the image file for the specific recovery tool that you have just downloaded.

Then you have to reboot into custom recovery mode, which means rebooting from the /recovery partition from your phone that you have just flash. You can do so by pressing a certain button combination or by going to bootloader mode and then to recovery mode, or by using the adb tool.
Once in the custom recovery tool, you can flash the Android OS ROM and gapps zip files. 

Direct install (not needed if using custom recovery tool)
If you don’t have an external card or don't want to install the custom recovery tool, then you have to install the ROM directly with fastboot from your computer by first erasing the userdata and cache partitions with:

>fastboot -w

flashing the ROM file:

>fastboot update downlodade_rom_file.zip

and installing google apps:

>fastboot update gaaps_file.zip

(Note: I haven’t tested these last 2 commands, I installed the new Android version via the custom recovery tool)

Summing up
1. Download the ROM zip file and google apps (gapps) zip file from your computer to your harddrive
2. Connect your phone to your PC and copy these 2 files to the sd card.
3. Reboot the phone in bootloader mode, and use fastboot to unlock your bootloader
4. Install a custom recovery tool also with fastboot. These can be: TWRP or CWM.
5. Reboot your phone into the custom recovery tool and flash the Android OS ROM and gapps zip files.


After doing this and adjusting some steps based on what the ROM publisher recommends, you will have a brand new Android version on your phone. The difference at least in my case was HUGE, is much more faster, and it works fantastic, much better than high end new phones, even though my phone is at least 4 years old, and it was not one of the more expensive ones back then.