Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Quantiacs On Linux Lesson 4?

In Lessons 1, 2, and 3 we learned some simple ideas behind the Quantiacs API.

Here, in Lesson 4, I demo a blend of Quantiacs and some simple Machine Learning technology.

The Machine Learning technology here is sklearn which is supported by hosts running in the Quantiacs cloud.

You can learn about sklearn at the URL listed below:

This lesson, Lesson 4, uses Logistic Regression which is discussed at the URL listed below:

To walk through Lesson 4, I downloaded VirtualBox and installed it on my laptop:

Inside of VirtualBox I installed Ubuntu 14:

I logged into Ubuntu and installed some packages:
apt-get install zip openssh-server emacs wget curl
Next I created an account named 'qq' which is the account I use when I work with Quantiacs:
useradd -m -s /bin/bash qq
passwd qq
Then, I logged into the qq account:
ssh -YA qq@localhost
Next, I downloaded Anaconda which contains Python:
mkdir -p ~qq/Downloads
cd       ~qq/Downloads
Then, I installed Anaconda Python:
cd   ~qq/Downloads
After the install I adjusted ~qq/.bashrc:
echo export PATH=${HOME}/anaconda/bin:{$PATH} >> ~qq/.bashrc
Also I did a bug-work-around in Anaconda:
mv ${HOME}/anaconda/bin/curl ${HOME}/anaconda/bin/curl_anaconda
And, I verified:
qq@nia110 ~ $ 
qq@nia110 ~ $ 
qq@nia110 ~ $ python
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 17:02:03) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: and
qq@nia110 ~ $ 
qq@nia110 ~ $ 
qq@nia110 ~ $ 
I noticed that I installed Python 2.7.x.
Quantiacs wants me to run Python 2.7.x., not Python 3.x.y.

Next, I installed some Quantiacs software:
pip install quantiacsToolbox
I wrote a tiny two-line Python script:
# ~/
import quantiacsToolbox
returnDict = quantiacsToolbox.runts('/home/qq/')
# end
The above script calls this script:
import numpy as np
from sklearn import linear_model

def get2nd_opinion(myCLOSE):
    # I should use 2-feature Logistic Regression to get 2nd opinion:
    cp       = [ row[1] for row in myCLOSE ]
    if np.isnan(cp[0]):
      cp[0]  = cp[1]
    cplead_l = cp + [cp[-1]]
    cplag1_l = [cp[0]] +           cp
    cplag2_l = [cp[0]] + [cp[0]] + cp
    # I should snip off end-val.
    cplead_l = cplead_l[1:]
    cplag1_l = cplag1_l[:-1]
    cplag2_l = cplag2_l[:-2]
    # They should be same length
    len(cp) == len(cplead_l)
    len(cp) == len(cplag1_l)
    len(cp) == len(cplag2_l)
    # I should convert them to np.arrays to enable arithmetic:
    cp_a      = np.array(cp)
    cplead_a  = np.array(cplead_l)
    cplag1_a  = np.array(cplag1_l)
    cplag2_a  = np.array(cplag2_l)
    # I should calculate pct-deltas:
    pctlead_a = 100.0 * (cplead_a - cp_a)/cp_a
    pctlag1_a = 100.0 * (cp_a - cplag1_a)/cplag1_a
    pctlag2_a = 100.0 * (cp_a - cplag2_a)/cplag2_a
    x_a       = np.column_stack((pctlag1_a, pctlag2_a))
    y_train   = pctlead_a[:-1]
    yc_train  = y_train > 0.0
    x_train   = x_a[:-1]
    # For API info, Google: sklearn LogisticRegression
    lrmodel   = linear_model.LogisticRegression(), yc_train)
    x_oos = x_a[-1]
    aprediction = lrmodel.predict_proba(x_oos.astype(float))[0,1]
    return aprediction

def myTradingSystem(DATE, OPEN, HIGH, LOW, CLOSE, VOL, exposure, equity, settings):
    # Date of most recent observation before 'todays' trade:
    myd        = DATE[-1]
    mymnth     = str(myd)[4:6]
    bullweight = np.array([[0.0, 1.0]])
    bearweight = np.array([[2.0, -1.0]])
    # I am bullish by default:
    weights  = bullweight
    opinion2 = get2nd_opinion(CLOSE)
    if opinion2 < 0.45:
      # I am bearish:
      weights = bearweight
    if (mymnth=='05') or (mymnth=='06') or (mymnth=='07') or (mymnth=='08') or (mymnth=='09'):
      # Else,
      # Sell in May, Go Away.
      weights = bearweight
      if opinion2 > 0.55:
        # Sometimes I am bullish in summer:
        weights = bullweight
    return weights, settings

def mySettings():
    settings= {}
    # Futures Contracts
    settings['markets']  = ['CASH','F_ES']
    settings['lookback']= 1010
    settings['budget']= 10**6
    settings['slippage']= 0.05
    return settings
The above script implements this English logic.
  • By default I am bullish.
  • If Logistic Regression (LR) is less than 45% confident that tomorrow will go up,
    then I become bearish.
  • Next I look at the calendar.
  • Between May and Oct, I am bearish.
    If LR is more than 55% confident that tomorrow will go up,
    then I become bullish.
I ran a simple shell command to run my script:
I saw this syntax in my shell:
qq@nia110 ~ $ 
qq@nia110 ~ $ python
Loading Data...
Evaluating Trading System
qq@nia110 ~ $ 
qq@nia110 ~ $ 
Eventually, a window appeared:

According to that chart, this LR strategy did poorly compared to the 'Sell in May' strategy:

Machine Learning, however, is powerful technology.

Most people who learn how to implement Machine Learning, start with Logistic Regression.

So the main topic of Lesson 4 is: blend Quantiacs API with sklearn Logistic Regression.

You have questions?

E-me: Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me