Skip to content

Instantly share code, notes, and snippets.

Avatar

Robert Carver robcarver17

View GitHub Profile
@robcarver17
robcarver17 / temp.py
Last active Oct 12, 2020
Get IB historical data native python API updated for bar class
View temp.py
# Gist example of IB wrapper ...
#
# Download API from http://interactivebrokers.github.io/#
#
# Install python API code /IBJts/source/pythonclient $ python3 setup.py install
#
# Note: The test cases, and the documentation refer to a python package called IBApi,
# but the actual package is called ibapi. Go figure.
#
# Get the latest version of the gateway:
View temp.py
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as stats
import numpy as np
from systems.provided.futures_chapter15.estimatedsystem import *
system = futures_system()
del(system.config.instruments) # so we can get results for everything
View pythonIBAPIexample3.py
# Gist example of IB wrapper ...
#
# Download API from http://interactivebrokers.github.io/#
#
# Install python API code /IBJts/source/pythonclient $ python3 setup.py install
#
# Note: The test cases, and the documentation refer to a python package called IBApi,
# but the actual package is called ibapi. Go figure.
#
# Get the latest version of the gateway:
View heuristic correlations.py
### Following code is boilerplate for optimising
import matplotlib
matplotlib.use("TkAgg")
import pandas as pd
from scipy.optimize import minimize
import numpy as np
from scipy.stats import norm
from collections import namedtuple
def optimise_for_corr_matrix(corr_matrix):
View get_expected_risk.py
import pandas as pd
import numpy as np
def get_expected_risk_for_system(system):
value_of_positions_proportion_capital = get_positions_as_proportion_of_capital(system)
instrument_returns = get_instrument_returns(system)
instrument_returns = instrument_returns.ffill().reindex(value_of_positions_proportion_capital.index)
View coinflips.py
import random
from copy import copy
def ishead():
result=random.uniform(0,1)
if result>=0.5:
return 1
else:
return 0
View temp.py
from copy import copy
seq_length = 20
def decimalToBinary(n):
# return padded binary number as list
binary_number_as_str=bin(n)
binary_number_as_str = binary_number_as_str[2:]
binary_number_as_str = binary_number_as_str.zfill(seq_length)
binary_number_as_list = [int(x) for x in binary_number_as_str]
View kings_college.py
#import matplotlib
#matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from scipy.optimize import minimize
import numpy as np
### Following is the optimisation code:
### First the main function.
def optimise_with_sigma(sigma, mean_list):
View scratch_16.py
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
def plot_results_for_bin_size(size, pd_result, centre_on_mean = False):
bins = get_bins_for_size(size, pd_result, centre_on_mean = centre_on_mean)
@robcarver17
robcarver17 / scratch_16.py
Last active Nov 3, 2020
get system risk
View scratch_16.py
import pandas as pd
import numpy as np
def get_positions_as_proportion_of_capital(system):
list_of_instruments = system.get_instrument_list()
positions = [system.portfolio.get_actual_position(instrument_code) for instrument_code in list_of_instruments]
positions = pd.concat(positions, axis=1)
positions.columns = list_of_instruments
positions[positions.isna()] = 0.0
You can’t perform that action at this time.