Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Optimize API - is it open source?

Can't find it on the Quantopian Github. Can anyone point me in the right direction?

3 responses

Not open source...they bandied it about a bit...yet bottom line is not yet...
and it's a few years now, so a prediction based on the past is "No".

Many thanks Alan. I suppose we all understand the basics of the various optimisation techniques so it's probably no big deal. It's more a question of completeness for me but there we go....

For the sake of completeness (or, perhaps, making myself look like a complete idiot), here's a rough draft of my own take on optimization. Note that I am still a novice and have only a cursory familiarity with Q's tutorials on the subject...

#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
Created on Sat Dec 28 13:35:23 2019

@author: christophercoffee  
"""

import pandas as pd  
import numpy as np  
np.random.seed(3)  
from collections import Counter  
from pprint import pprint

r = np.random.pareto(5,size=100)  
r /= r.sum() / len(r)  
r -= 0.5  
s = pd.Series(r)  
t0 = s.copy()



def adj1(x,lev):  
    x[x > 0] *= lev / x[x > 0].abs().sum()  
    x[x < 0] *= lev / x[x < 0].abs().sum()  
    return x / 2  
def adj2(x,floor):  
    x[x > 0] += floor  
    x[x < 0] -= floor  
    return x  
adj3 = lambda x,cap: np.sign(x) * np.minimum(x.abs(), cap)

def optimize(x,lev=0.95,lev_tol=0.15,floor=0.00001,cap=0.012,dollar_tol=0.001):  
    t = x.copy()  
    for i in range(1000):  
        if (abs(t.abs().sum()-lev) <= lev_tol) & \  
           (abs(t.sum()) <= dollar_tol) & \  
           (t.abs().max() <= cap) & \  
           (t[t != 0.0].abs().min() >= floor):  
            break  
        t = adj3(adj2(adj1(t,lev),floor),cap)  
#    pprint(Counter(np.array(t)))  
#    pprint(pd.DataFrame(np.array([s, t]).T))  
    print(t.abs().sum())  
    print(t.sum())  
    print(t[t != 0.0].abs().min())  
    print(t.abs().max())  
    print(i)

optimize(t0)  
optimize(t0,cap=0.011)  

I initially included a "lasso" (here I am blatantly appropriating the term from machine learning) adjustment (np.sign(x) * np.maximum(0, x - lambd)) in order to avoid infeasibly small trades. I since took it out since I felt like it just muddied the waters (and it appears no one charges or otherwise bothers with flat trading fees these days anyway).

I included two examples to demonstrate how slight changes to the max position size (cap) can drastically affect both results and runtime in my version.

I just thought it was interesting.