Skip to content

Instantly share code, notes, and snippets.

View robcarver17's full-sized avatar

Robert Carver robcarver17

View GitHub Profile
@robcarver17
robcarver17 / temp.py
Last active January 2, 2024 06:02
Get IB historical data native python API updated for bar class
# 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:
import numpy as np
from systems.provided.futures_chapter15.basesystem import *
import numpy as np
from syscore.algos import robust_vol_calc
## test kurtosis AND skew with trading rules
# 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:
# 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:
# 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:
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)
@robcarver17
robcarver17 / temp.py
Last active September 17, 2022 13:55
# 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:
"""
Implement the handcrafting method
This is 'self contained code' which requires wrapping before using in pysystemtrade
"""
## CAVEATS:
## Uses weekly returns (resample needed first)
## Doesn't deal with missing assets
### 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):
"""
The starter system has the following features:
- single market
- binary forecast from simple MAV
- exit from trailing stop loss
- fixed positions once in trade
"""