Пример #1
0
    def testProcessTradingRuleSpec(self):

        ruleA = TradingRule(ewmac_forecast_with_defaults)
        ruleB = TradingRule(dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
                                 data=["rawdata.daily_prices",
                                       "rawdata.daily_returns_volatility"],
                                 other_args=dict(Lfast=50, Lslow=200)))

        trading_rules = dict(ruleA=ruleA, ruleB=ruleB)
        ans = process_trading_rules(trading_rules)
        assert "ruleA" in ans.keys()
        assert "ruleB" in ans.keys()

        trading_rules = [ruleA, ruleB]
        ans = process_trading_rules(trading_rules)
        assert "rule1" in ans.keys()

        ans = process_trading_rules(ruleA)
        assert ans['rule0'].function == ewmac_forecast_with_defaults

        ans = process_trading_rules(ewmac_forecast_with_defaults)
        assert ans['rule0'].function == ewmac_forecast_with_defaults

        ans = process_trading_rules([dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
                                          data=["rawdata.daily_prices",
                                                "rawdata.daily_returns_volatility"],
                                          other_args=dict(Lfast=50, Lslow=200))])
        assert ans['rule0'].other_args['Lfast'] == 50
Пример #2
0
    def testCarryRule(self):
        data = csvFuturesData("sysdata.tests")

        rawdata = FuturesRawData()
        rules = Rules()
        system = System([rawdata, rules], data)
        rule=TradingRule(carry, ["rawdata.daily_annualised_roll", "rawdata.daily_returns_volatility"], dict(smooth_days=90))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.iloc[-1][0], 0.411686026, 5)
Пример #3
0
    def testCallingTradingRule(self):

        # config=Config(dict(trading_rules=dict(ewmac=dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults"))))
        NOTUSEDrawdata, data, NOTUSEDconfig = get_test_object()

        rawdata = RawData()
        rules = Rules()
        system = System([rawdata, rules], data)

        # Call with default data and config
        rule = TradingRule(ewmac_forecast_with_defaults)
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], -3.280028, 5)

        # Change the data source
        rule = TradingRule((
            "systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
            ["rawdata.get_daily_prices",
             "rawdata.daily_returns_volatility"], dict()))

        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], -1.24349, 5)

        rule = TradingRule(
            dict(
                function=
                "systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
                data=[
                    "rawdata.get_daily_prices",
                    "rawdata.daily_returns_volatility"
                ],
                other_args=dict(Lfast=50, Lslow=200)))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], -3.025001057146)
Пример #4
0
    def testCallingTradingRule(self):

        # config=Config(dict(trading_rules=dict(ewmac=dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults"))))
        data = csvFuturesData("sysdata.tests")

        rawdata = RawData()
        rules = Rules()
        system = System([rawdata, rules], data)

        # Call with default data and config
        rule = TradingRule(ewmac_forecast_with_defaults)
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 2.1384223788141838, 5)

        # Change the data source
        rule = TradingRule((
            "systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
            ["rawdata.get_daily_prices",
             "rawdata.daily_returns_volatility"], dict()))

        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 0.029376, 5)

        rule = TradingRule(
            dict(
                function=
                "systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
                data=[
                    "rawdata.get_daily_prices",
                    "rawdata.daily_returns_volatility"
                ],
                other_args=dict(Lfast=50, Lslow=200)))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 3.84426755)
Пример #5
0
    def testCarryRule(self):
        data = csvFuturesData("sysdata.tests")

        rawdata = FuturesRawData()
        rules = Rules()
        system = System([rawdata, rules], data)
        rule = TradingRule(carry, [
            "rawdata.daily_annualised_roll", "rawdata.daily_returns_volatility"
        ], dict(smooth_days=90))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.iloc[-1][0], 0.411686026, 5)
Пример #6
0
    def testCarryRule(self):
        NOTUSEDrawdata, data, NOTUSEDconfig = get_test_object()

        rawdata = FuturesRawData()
        rules = Rules()
        system = System([rawdata, rules], data)
        rule = TradingRule(carry2, [
            "rawdata.daily_annualised_roll",
        ], dict(smooth_days=90))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 0.138302, 5)
    def testCarryRule(self):
        NOTUSEDrawdata, data, NOTUSEDconfig = get_test_object()

        rawdata = FuturesRawData()
        rules = Rules()
        system = System([rawdata, rules], data)
        rule = TradingRule(
            carry2, [
                "rawdata.daily_annualised_roll",
            ],
            dict(smooth_days=90))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 0.138302, 5)
Пример #8
0
    def testCarryRule(self):
        data = csvFuturesData("sysdata.tests")

        rawdata = FuturesRawData()
        rules = Rules()
        system = System([rawdata, rules], data)
        rule = TradingRule(
            carry2, [
                "rawdata.daily_annualised_roll",
            ],
            dict(smooth_days=90))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], 0.37666175, 5)
Пример #9
0
    def testCallingTradingRule(self):

        # config=Config(dict(trading_rules=dict(ewmac=dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults"))))
        data = csvFuturesData("sysdata.tests")

        rawdata = RawData()
        rules = Rules()
        system = System([rawdata, rules], data)

        # Call with default data and config
        rule = TradingRule(ewmac_forecast_with_defaults)
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.iloc[-1][0], 2.1384223788141838, 5)

        # Change the data source
        rule = TradingRule(("systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
                            ["rawdata.daily_prices", "rawdata.daily_returns_volatility"], dict()))

        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.iloc[-1][0], 0.029376, 5)

        rule = TradingRule(dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
                                data=["rawdata.daily_prices",
                                      "rawdata.daily_returns_volatility"],
                                other_args=dict(Lfast=50, Lslow=200)))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.iloc[-1][0], 3.84426755)
    def testCallingTradingRule(self):

        # config=Config(dict(trading_rules=dict(ewmac=dict(function="systems.provided.example.rules.ewmac_forecast_with_defaults"))))
        NOTUSEDrawdata, data, NOTUSEDconfig = get_test_object()

        rawdata = RawData()
        rules = Rules()
        system = System([rawdata, rules], data)

        # Call with default data and config
        rule = TradingRule(ewmac_forecast_with_defaults)
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], -3.280028, 5)

        # Change the data source
        rule = TradingRule((
            "systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
            ["rawdata.get_daily_prices",
             "rawdata.daily_returns_volatility"], dict()))

        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], -1.24349, 5)

        rule = TradingRule(
            dict(
                function=
                "systems.provided.example.rules.ewmac_forecast_with_defaults_no_vol",
                data=[
                    "rawdata.get_daily_prices",
                    "rawdata.daily_returns_volatility"
                ],
                other_args=dict(Lfast=50, Lslow=200)))
        ans = rule.call(system, "EDOLLAR")
        self.assertAlmostEqual(ans.tail(1).values[0], -3.025001057146)
Пример #11
0
def create_rules_for_random_system():
    ## create a series of regression trading rules different lookbacks

    rules_dict=dict()
    for timewindow in WINDOWS_TO_USE:
        rule_name="regression%d" % timewindow

        min_periods = max(2, int(np.ceil(timewindow / 4.0)))

        new_rule = TradingRule((regression_rule, ["rawdata.get_daily_prices", "rawdata.daily_returns_volatility"],
                                dict(timewindow=timewindow, min_periods=min_periods)))

        rules_dict[rule_name]=new_rule

    my_rules = Rules(rules_dict)

    return my_rules
Пример #12
0
        subsystem_position = stoploss(price, vol, raw_position, Xfactor)

        return subsystem_position[0]


# number of trades per year
def tradesperyear(x):
    y = x != x.shift(1)
    totaly = y.sum()
    years = len(y) / 250.0

    return totaly / years


simple_mav_rule = TradingRule(
    dict(function=simple_mav, other_args=dict(long=40, short=10)))
random_rule = TradingRule(dict(function=randomrule))

# Use a random rule to get number of trades per year for a given 'X'

results = []
for Xfactor in [16 * 0.025]:

    #weights = dict(EDOLLAR=1.0)
    config = Config(
        dict(trading_rules=dict(simple_mav=random_rule),
             Xfactor=Xfactor,
             percentage_vol_target=16.0))

    data = csvFuturesSimData()
Пример #13
0
    def testinitTradingRules(self):
        rule = TradingRule((ewmac_forecast_with_defaults, [], {}))
        assert rule.function == ewmac_forecast_with_defaults
        rule2 = TradingRule(rule)

        assert (rule.function, rule.data,
                rule.other_args) == (rule2.function, rule2.data,
                                     rule2.other_args)

        rule3 = TradingRule(ewmac_forecast_with_defaults,
                            ["data.get_instrument_price"],
                            dict(Lfast=50, Lslow=200))

        assert rule3.data == ["data.get_instrument_price"]

        rule4 = TradingRule(ewmac_forecast_with_defaults,
                            "data.get_instrument_price",
                            dict(Lfast=50, Lslow=200))

        assert rule4.data == ["data.get_instrument_price"]

        try:
            rule4 = TradingRule(ewmac_forecast_with_defaults,
                                "data.get_instrument_price")
            rule5 = TradingRule((ewmac_forecast_with_defaults, ))

            raise Exception("Should have failed with 2 tuple")
        except:
            pass

        rule7 = TradingRule(
            "systems.provided.example.rules.ewmac_forecast_with_defaults", [],
            dict(Lfast=50, Lslow=200))
        assert rule7.function == rule.function

        try:
            rule8 = TradingRule(
                "not.a.real.rule.just.a.string.of.arbitrary.text", [],
                dict(Lfast=50, Lslow=200))
            raise Exception("Should have failed with non existent rule")
        except:
            pass

        rule9 = TradingRule(dict(function=ewmac_forecast_with_defaults))

        try:
            rule10 = TradingRule(
                dict(functionette=ewmac_forecast_with_defaults))
            raise Exception("Should have failed with no function keyword")
        except:
            pass

        rule11 = TradingRule(
            dict(function=
                 "systems.provided.example.rules.ewmac_forecast_with_defaults",
                 other_args=dict(Lfast=50),
                 data=[]))

        rule12 = TradingRule(ewmac_forecast_with_defaults,
                             other_args=dict(Lfast=30))
        rule13 = TradingRule(
            "systems.provided.example.rules.ewmac_forecast_with_defaults",
            data="data.get_pricedata")
        assert rule13.data == ["data.get_pricedata"]

        rule14 = TradingRule(ewmac_forecast_with_defaults)

        try:
            rule15 = TradingRule(set())
            raise Exception("Should have failed with other data type")
        except:
            pass
from systems.provided.moretradingrules.morerules import long_bias
from copy import copy

system = futures_system()
carry_acc = system.accounts.pandl_for_instrument_forecast("US10", "carry")
ewmac8_acc = system.accounts.pandl_for_instrument_forecast("US10", "ewmac8_32")
ewmac16_acc = system.accounts.pandl_for_instrument_forecast(
    "US10", "ewmac16_64")

all_rets = pd.concat([carry_acc, ewmac8_acc, ewmac16_acc], axis=1)

# benchmarking
all_curve = system.accounts.pandl_for_instrument("MXP").percent()
all_curve = 1.5 * all_curve

new_rule = TradingRule(long_bias)
config = copy(system.config)
config.trading_rules['long_bias'] = new_rule

## If you're using fixed weights and scalars

config.forecast_scalar = 1.0
config.forecast_weights = dict(
    long_bias=1.0)  ## all existing forecast weights will need to be updated
config.forecast_div_multiplier = 1.0
system2 = futures_system(config=config)

long_acc = system2.accounts.pandl_for_instrument_forecast("MXP", "long_bias")

both = pd.concat([long_acc, all_curve], axis=1)
both[pd.datetime(1982, 9, 15):pd.datetime(1982, 9, 21)] = np.nan
Пример #15
0
df = my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5)

message = "\nmy_system.rules.get_raw_forecast('EDOLLAR', 'ewmac')"\
            "\nreturns a Pandas DataFrame with the forecast of each day\n"\
            "Example:"\
            "\n{}\n"

print(message.format(df), end=space)
"""
Define a TradingRule
"""

from systems.forecasting import TradingRule
from systems.positionsizing import PositionSizing

ewmac_rule = TradingRule(ewmac)
my_rules = Rules(dict(ewmac=ewmac_rule))
print(ewmac_rule, end=space)

possizer = PositionSizing()
my_system = System([my_rules, possizer], data)
my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5)

print("System position features.", end=space)

df = my_system.positionSize.get_price_volatility("EDOLLAR").tail(5)

message = "\nmy_system.positionSize.get_price_volatility('EDOLLAR')"\
            "\nreturns a Pandas DataFrame with the price volatility of each day\n"\
            "Example:"\
            "\n{}\n"
Пример #16
0
sys = futures_system()
sys.config.forecast_floor = 0
sys.forecastScaleCap.get_capped_forecast("SP500", "ewmac2_8").plot()

from systems.forecasting import create_variations, TradingRule

dict_of_speeds = [
    dict(Lfast=4, Lslow=16),
    dict(Lfast=8, Lslow=32),
    dict(Lfast=16, Lslow=64),
    dict(Lfast=32, Lslow=128),
    dict(Lfast=64, Lslow=256)
]

normmom_base_rule = TradingRule(
    'systems.provided.futures_chapter15.rules.ewmac_calc_vol',
    data=['rawdata.cumulative_norm_return'])
normmom_variations = create_variations(normmom_base_rule,
                                       dict_of_speeds,
                                       key_argname='Lfast',
                                       nameformat="normmom_%s:%s")

TradingRule(rule='systems.provided.futures_chapter15.rules.ewmac_calc_vol',
            data=['rawdata.cumulative_norm_return'],
            other_args=dict(Lfast=4, Lslow=16))

aggmom_base_rule = TradingRule(
    'systems.provided.futures_chapter15.rules.ewmac_calc_vol',
    data=['rawdata.normalised_price_for_asset_class'])
aggmom_variations = create_variations(aggmom_base_rule,
                                      dict_of_speeds,
Пример #17
0
        subsystem_position = stoploss(price, vol, raw_position, Xfactor)

        return subsystem_position[0]


# number of trades per year
def tradesperyear(x):
    y = x != x.shift(1)
    totaly = y.sum().values[0]
    years = len(y) / 250.0

    return totaly / years


simple_mav_rule = TradingRule(
    dict(function=simple_mav, other_args=dict(long=40, short=10)))

weights = dict(EDOLLAR=1.0)
config = Config(
    dict(trading_rules=dict(simple_mav=simple_mav_rule),
         Xfactor=4,
         instrument_weights=weights,
         percentage_vol_target=16.0))

data = csvFuturesSimData()

system = System([
    Account(),
    Portfolios(),
    PositionSizeWithStopLoss(),
    FuturesRawData(),
Пример #18
0
print(my_rules.trading_rules())

my_rules = Rules(dict(ewmac=ewmac))
print(my_rules.trading_rules())

from systems.basesystem import System
my_system = System([my_rules], data)
print(my_system)

print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5))
"""
Define a TradingRule
"""

from systems.forecasting import TradingRule
ewmac_rule = TradingRule(ewmac)
my_rules = Rules(dict(ewmac=ewmac_rule))
ewmac_rule
"""
... or two...
"""

ewmac_8 = TradingRule((ewmac, [], dict(Lfast=8, Lslow=32)))
ewmac_32 = TradingRule(
    dict(function=ewmac, other_args=dict(Lfast=32, Lslow=128)))
my_rules = Rules(dict(ewmac8=ewmac_8, ewmac32=ewmac_32))
print(my_rules.trading_rules()['ewmac32'])

my_system = System([my_rules], data)
my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5)
Пример #19
0
        """

        stop_fraction = self.parent.config.stop_fraction
        price = self.parent.rawdata.get_daily_prices(instrument_code)
        vol = self.parent.rawdata.daily_returns_volatility(instrument_code)
        raw_position = self.get_subsystem_position_preliminary(instrument_code)

        subsystem_position = stoploss(price, vol, raw_position, stop_fraction)

        return subsystem_position[0]


simple_mav_rules = dict([("mac_%d_%d" % (fast_length, fast_length * 4),
                          TradingRule(
                              dict(function=simple_mav,
                                   other_args=dict(long=fast_length * 4,
                                                   short=fast_length))))
                         for fast_length in [2, 4, 8, 16, 32, 64]])

simple_mav_rules_ordered_keys = [
    "mac_%d_%d" % (fast_length, fast_length * 4)
    for fast_length in [2, 4, 8, 16, 32, 64]
]
"""
ewmac_rules = dict([("ewmac_%d_%d" % (fast_length, fast_length*4),
    TradingRule(dict(function = ewmac_calc_vol, other_args = dict(Lfast = fast_length, Lslow = fast_length*4))))
    for fast_length in [2, 4, 8, 16, 32, 64]])

ewmac_rules_ordered_keys = ["ewmac_%d_%d" %(fast_length, fast_length*4)
    for fast_length in [2,4,8,16,32,64]]
"""
Пример #20
0
    returns 95% min and max values for an account curve

    :param account_curve: something produced by system.accounts
    :return: list of two
    """

    annual_SR = account_curve.sharpe()
    len_data = len(account_curve.index)  # working days

    low_SR_estimate, annual_SR, upper_SR_estimate = SR_error_bars_from_values(
        annual_SR, len_data)

    return [low_SR_estimate, annual_SR, upper_SR_estimate]


simple_mav_rule = TradingRule(
    dict(function=simple_mav, other_args=dict(long=64, short=16)))
"""
now run the basic system; 16,64 crossover plus X=8
"""

# weights = dict(EDOLLAR=1.0)
config = Config(
    dict(trading_rules=dict(simple_mav=simple_mav_rule),
         Xfactor=8,
         percentage_vol_target=16.0))

data = csvFuturesSimData()

system = System([
    Account(),
    Portfolios(),
Пример #21
0
    function='systems.provided.moretradingrules.morerules.relative_carry',
    data=['rawdata.smoothed_carry', 'rawdata.median_carry_for_asset_class']))

absolute_carry_rule = dict(carry=save_absolute_carry_rule)

dict_of_speeds = [
    dict(Lfast=4, Lslow=16),
    dict(Lfast=8, Lslow=32),
    dict(Lfast=16, Lslow=64),
    dict(Lfast=32, Lslow=128),
    dict(Lfast=64, Lslow=256)
]

normmom_base_rule = TradingRule(
    'systems.provided.futures_chapter15.rules.ewmac',
    data=[
        'rawdata.cumulative_norm_return', 'rawdata.daily_returns_volatility'
    ])
normmom_variations = create_variations(normmom_base_rule,
                                       dict_of_speeds,
                                       key_argname='Lfast',
                                       nameformat="normmom_%s:%s")

aggmom_base_rule = TradingRule(
    'systems.provided.futures_chapter15.rules.ewmac',
    data=[
        'rawdata.normalised_price_for_asset_class',
        'rawdata.daily_returns_volatility'
    ])
aggmom_variations = create_variations(aggmom_base_rule,
                                      dict_of_speeds,
Пример #22
0
sys = futures_system()
sys.config.forecast_floor = 0
sys.forecastScaleCap.get_capped_forecast("SP500", "ewmac2_8").plot()

from systems.forecasting import create_variations, TradingRule

dict_of_speeds = [
    dict(Lfast=4, Lslow=16),
    dict(Lfast=8, Lslow=32),
    dict(Lfast=16, Lslow=64),
    dict(Lfast=32, Lslow=128),
    dict(Lfast=64, Lslow=256),
]

normmom_base_rule = TradingRule(
    "systems.provided.futures_chapter15.rules.ewmac_calc_vol",
    data=["rawdata.cumulative_norm_return"],
)
normmom_variations = create_variations(normmom_base_rule,
                                       dict_of_speeds,
                                       key_argname="Lfast",
                                       nameformat="normmom_%s:%s")

TradingRule(
    rule="systems.provided.futures_chapter15.rules.ewmac_calc_vol",
    data=["rawdata.cumulative_norm_return"],
    other_args=dict(Lfast=4, Lslow=16),
)

aggmom_base_rule = TradingRule(
    "systems.provided.futures_chapter15.rules.ewmac_calc_vol",
    data=["rawdata.normalised_price_for_asset_class"],
Пример #23
0
                continue
            if original_position_now > 0.0 or original_position_now < 0.0:
                # go long or short
                current_position = original_position_now
                new_position.append(current_position)
                continue

        # already holding a position
        new_position.append(current_position)

    new_position = pd.DataFrame(new_position, position.index)

    return new_position


simple_mav_rule = TradingRule(
    dict(function=simple_mav, other_args=dict(long=2, short=8)))
simple_mav_rule_with_vol = TradingRule(
    dict(function=mav_with_vol,
         data=["rawdata.get_daily_prices", "rawdata.daily_returns_volatility"],
         other_args=dict(long=16, short=4)))
"""
now run the basic system; 16,64 crossover plus X=8
"""

# weights = dict(EDOLLAR=1.0)
config = Config(
    dict(trading_rules=dict(simple_mav=simple_mav_rule),
         Xfactor=8,
         percentage_vol_target=16.0))

config_2 = Config(