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

Question:
Quantiacs On Linux Lesson 3?

In Lesson 1 we learned that Quantiacs is a startup which helps me blend DIY Data Science with individual investing.

In Lesson 2 we learned how to 'back-test' a strategy called: 'Sell in May'.

Like lesson 1 and 2, I started this lesson, lesson 3, by installing VirtualBox on my laptop:

https://www.virtualbox.org/wiki/Downloads

Inside of VirtualBox I installed Ubuntu 14:

http://releases.ubuntu.com/14.04/ubuntu-14.04.3-desktop-amd64.iso

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
wget https://3230d63b5fc54e62148e-c95ac804525aac4b6dba79b00b39d1d3.ssl.cf1.rackcdn.com/Anaconda-2.3.0-Linux-x86_64.sh
Then, I installed Anaconda Python:
cd   ~qq/Downloads
bash Anaconda-2.3.0-Linux-x86_64.sh
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: http://continuum.io/thanks and https://binstar.org
>>> 
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:
# ~/q15.py
import quantiacsToolbox
returnDict = quantiacsToolbox.runts('/home/qq/q15q.py')
# end
The above script calls this script:
# q15q.py

import numpy as np
import pdb

def myTradingSystem(DATE, OPEN, HIGH, LOW, CLOSE, VOL, exposure, equity, settings):
    # Date of most recent observation before 'todays' trade:
    myd                   = DATE[-1]
    last_close_price      = CLOSE[-1][1]
    yest_last_close_price = CLOSE[-2][1]
    # Most recent gain may be predictive:
    lastg  = 100.0*(last_close_price-yest_last_close_price)/yest_last_close_price
    if str(myd) > '20150923':
      # Demo of pdb:
      pdb.set_trace()
    mymnth = str(myd)[4:6]
    xpct       = -1.0
    bullweight = np.array([[0.0, 1.0]])
    bearweight = np.array([[2.0, -1.0]])
    if lastg < xpct:
      # If today down more than x%, I should buy F_ES because of mean reversion.
      weights = bullweight
    elif (mymnth=='05') or (mymnth=='06') or (mymnth=='07') or (mymnth=='08') or (mymnth=='09'):
      # Else,
      # Sell in May, Go Away.
      weights = bearweight
      # Above, I want to be -100% weighted in F_ES.
      # The cash I get from that short-sale forces me to be
      # 200% weighted in CASH.
    else:
      # Return in Oct
      weights = bullweight
      # Above, I want to be 100% weighted in F_ES.
      # So, I should be 0% weighted in CASH.
    return weights, settings


def mySettings():
    settings= {}
    # Futures Contracts
    settings['markets']  = ['CASH','F_ES']
    settings['lookback']= 3
    settings['budget']= 10**6
    settings['slippage']= 0.05
    return settings
The above script implements this English logic.
  • Today, If SP 500 falls by more than 1%, I want to go-long at next Open.
    Meaning I want to be +100% weighted in F_ES.
  • Else, look at the calendar.
    During May, June, July, Aug, Sept,
    short-sell F_ES such that I am -100% weighted in F_ES.
  • Else, go-long at next Open so I am +100% weighted in F_ES.
The behavior I expect to see is that most of the time during the summer, I will be -100% weighted in F_ES.
The summer will have some down days which fall more than 1%.
The day after the down day, I want to go-long at next Open.
Then the day after that I will probably return to -100% weighted in F_ES.
Then, when October returns I will become +100% weighted in F_ES until the next May.
I ran a simple shell command to run my script:
python q15.py
I saw this syntax in my shell:
qq@nia110 ~ $ 
qq@nia110 ~ $ python q15.py
Loading Data...
Done! 
Evaluating Trading System
> /home/qq/q15q.py(16)myTradingSystem()
-> mymnth = str(myd)[4:6]
(Pdb) myd
20150924
(Pdb) last_close_price
95938.0
(Pdb) yest_last_close_price
96425.0
(Pdb) lastg
-0.50505574280528909
(Pdb) c
> /home/qq/q15q.py(16)myTradingSystem()
-> mymnth = str(myd)[4:6]
(Pdb) c
qq@nia110 ~ $ 
qq@nia110 ~ $ 
The above screen-dump shows me interacting with the Python debugger.

The debugger (AKA 'pdb') is easy to operate.

I use it to display values of variables while the code is in the middle of a calculation or loop.

My most common mistake with pdb is to display a variable which has the same name as a pdb command.

For example, pdb has a command called, 'c'.

If I try to display a variable named 'c', pdb will assume that I want to call the 'c' command.

The work-around is easy; if I have a variable named 'c' I change the name to something like 'myc'.

After I used pdb to look at some values, I allowed it to continue.

Eventually, a window appeared:


According to that chart, this hybrid strategy had similar behavior to the 'Sell in May' strategy:


Based on the charts, which strategy do you prefer?




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