Thursday, September 29, 2016

Fourier transform based trading strategy

The Fourier Transform based Python code is based on the Basic Trading Strategy running on Quantopian framework.

Starting cash: $10,000, tested from 8/1/15 to 8/31/16

The algorithm creates a buy and a sell threshold. The thresholds are functions of past performance. The idea of using FT is that the high frequency noise can be filtered out and the trend can be obtained. The ratio is the mean of a few recent prices divided by the mean of more prices. In the special case if you use 1 price, the mean is itself.

from scipy.fftpack import fft
import numpy
from scipy.fftpack import ifft

def initialize(context):
    
    context.security = sid(43721) # SCTY
    N = 20
    prices = numpy.asarray(history(N, '1d', 'price')) 
    # Turn off high frequencies
    wn = 5
    y = fft(numpy.transpose(prices)[0])
    print(numpy.transpose(prices))
    y[wn:-wn] = 0
   
    x1rec = ifft(y).real
    current_rec = numpy.mean(x1rec[:15])
    average_rec = numpy.mean(x1rec) # average of N elements
    ratio = current_rec/average_rec
    
    buy_threshold = 0.99 # Ratio threshold at which to buy
    close_threshold = 1.0  # Ratio threshold at which to close buy position
   
    current_price = data[context.security].price    
    cash = context.portfolio.cash

    if ratio < buy_threshold and cash > current_price:
        
        # Need to know how many shares we can buy
        number_of_shares = int(cash/current_price)
        
        # Place the buy order (positive means buy, negative means sell)
        order(context.security, +number_of_shares)
        
    elif ratio > close_threshold:
        
        # Sell all of our shares by setting the target position to zero
        order_target(context.security, 0)
    
    # Plot the stock's price
    record(stock_price=data[context.security].price)

Job submission on HPC using SLURM

Load spark and other needed modules
module load zlib/1.2.8 openssl/1.0.1e java/1.8.0_31 protobuf/2.5.0 myhadoop/Sep012015 spark/1.5.1

First need to assign the environment variable:
export HADOOP_OLD_DIR=/scratch/scratch3/${USER}_hadoop.$SLURM_JOB_ID

Then
sbatch run.sh

View your pending jobs (xxxx is your ID)
squeue -u xxxxxx -t pending

View your running jobs (xxxx is your ID)
squeue -u xxxxxx -t running

View all running jobs
squeue -t running 

Cancel a job (12345 is the job ID)
scancel 12345

Thursday, September 22, 2016

Making requests with SOAP using Python SUDS


You may need to install SUDS first by typing
pip install suds
in the command line mode (Windows)

from suds.client import Client

url="https://example.edu/xxx/service?wsdl"

client=Client(url)


method_list = [method for method in client.wsdl.services[0].ports[0].methods]

print method_list


orgchart = client.service.getOrgHierachary(deptNo="1000")

print orgchart

Wednesday, September 21, 2016

Python script to connect to LDAP and write a list of users to a text file

Download python ldap 64bit here
http://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap
and install using pip

The code helps write a list of users to a text file. 3 nested loops for affiliation, last name, and first name help arrange the user list in the corresponding order and at the same time reduce the size of the return results from the query.

The content in result_data looks like the following (some attributes were already removed)

[('uid=xxxxxxxx,ou=people,dc=examples,dc=edu', {'departmentNumber': ['12345'], 'displayName': ['Doe, John'], 'cn': ['John Doe'], , 'l': ['Orlando'], 'telephoneNumber': ['+1 800 000 0000'], 'eduPersonAffiliation': ['employee', 'staff'], 'street': ['Ocean Avenue'], 'sn': ['Doe'], 'mail': ['john.doe@examples.edu'], 'postalAddress': ['Ocean Avenue'], 'givenName': ['John']})]

import ldap

try:

    l = ldap.open("ldap.examples.edu")

    l.protocol_version = ldap.VERSION3

except ldap.LDAPError, e:

    print e


baseDN = "dc=examples,dc=edu"

searchScope = ldap.SCOPE_SUBTREE

## retrieve all attributes

retrieveAttributes = None



text_file = open("userlist.txt", "w")

affiliation = ['faculty','staff']

try:

    for affl in affiliation:

        for ln in range(ord('A'), ord('Z') + 1):

            for fn in range(ord('A'), ord('Z') + 1):

                searchFilter = "(&(eduPersonAffiliation=" + affl + ")(sn=" + chr(ln) + "*)(givenName=" + chr(fn) + "*))"

              

                ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)

                result_set = []

                while 1:

                    result_type, result_data = l.result(ldap_result_id, 0)

                    if (result_data == []):

                        break

                    else:

                        uid = result_data[0][0].split(',')[0].split('=')[1]

                        if 'displayName' in result_data[0][1]:

                            displayName = result_data[0][1]['displayName'][0]

                        else:

                            displayName = ""

                        if 'departmentNumber' in result_data[0][1]:

                            departmentNumber = result_data[0][1]['departmentNumber'][0]

                        else:

                            departmentNumber = ""           

                        if 'mail' in result_data[0][1]:

                            mail = result_data[0][1]['mail'][0]

                        else:

                            mail = ""

                        if 'telephoneNumber' in result_data[0][1]:

                            telephoneNumber = result_data[0][1]['telephoneNumber'][0].replace("+1","").replace(" ","")

                            telephoneNumber = "(" + telephoneNumber[:3] + ")" + " " + telephoneNumber[3:6] + "-" + telephoneNumber[6:]

                        else:

                            telephoneNumber = ""

                      

                        text_file.write(uid.ljust(16) + displayName.ljust(30) + departmentNumber.ljust(9) + mail.ljust(200) + telephoneNumber.ljust(14) + "\n")

      

except ldap.LDAPError, e:

    print e

  

text_file.close()