def initialize(context):
    """
        A function to define things to do at the start of the strategy
    """
    # set the account currency, only valid for backtests
    set_account_currency("USD")
    
    # universe selection
    context.short_dollar_basket = {
                               symbol('FXCM:AUD/USD'):1,
                               symbol('FXCM:EUR/USD'):1,
                               symbol('FXCM:GBP/USD'):1,
                               symbol('FXCM:NZD/USD'):1,
                               symbol('FXCM:USD/CAD'):-1,
                               symbol('FXCM:USD/CHF'):-1,
                               symbol('FXCM:USD/JPY'):-1,
                             }
    
    # Call rebalance function on the first trading day of each month after 2.5 hours from market open
    schedule_function(rebalance,
                    date_rules.month_start(days_offset=0),
                    time_rules.market_close(hours=2, minutes=30))
    
    # set trading cost and slippage to zero
    set_commission(fx=commission.PipsCost(cost=0.00))
    set_slippage(fx=slippage.FixedSlippage(0.00))
예제 #2
0
def initialize(context):
    """
        A function to define things to do at the start of the strategy
    """
    # set the account currency, only valid for backtests
    set_account_currency("USD")

    # lot-size (mini-lot for most brokers)
    context.lot_size = 1000

    # universe selection
    context.securities = [
        symbol('FXCM:AUD/USD'),
        symbol('FXCM:EUR/CHF'),
        symbol('FXCM:EUR/JPY'),
        symbol('FXCM:EUR/USD'),
        symbol('FXCM:GBP/USD'),
        symbol('FXCM:NZD/USD'),
        symbol('FXCM:USD/CAD'),
        symbol('FXCM:USD/CHF'),
        symbol('FXCM:USD/JPY'),
    ]

    # define strategy parameters
    context.params = {
        'indicator_lookback': 375,
        'indicator_freq': '1m',
        'buy_signal_threshold': 0.5,
        'sell_signal_threshold': -0.5,
        'SMA_period_short': 15,
        'SMA_period_long': 60,
        'RSI_period': 60,
        'trade_freq': 30,
        'leverage': 1,
        'pip_cost': 0.00003
    }

    # variable to control trading frequency
    context.bar_count = 0
    context.trading_hours = False

    # variables to track signals and target portfolio
    context.signals = dict((security, 0) for security in context.securities)
    context.target_position = dict(
        (security, 0) for security in context.securities)

    # set trading cost and slippage to zero
    set_commission(fx=commission.PipsCost(cost=context.params['pip_cost']))
    set_slippage(fx=slippage.FixedSlippage(0.00))

    # set a timeout for trading
    schedule_function(stop_trading, date_rules.every_day(),
                      time_rules.market_close(hours=0, minutes=31))
    # call square off to zero out positions 30 minutes before close.
    schedule_function(daily_square_off, date_rules.every_day(),
                      time_rules.market_close(hours=0, minutes=30))
예제 #3
0
def initialize(context):
    '''
        Called once at the start of the strategy execution. 
        This is the place to define things to do at the start of the strategy.
    '''
    # set the account base currency and strategy parameters
    set_account_currency('USD')
    context.params = {
        'verbose': False,
        'leverage': 1,
        'rebalance_freq': '15m',
        'no_overnight_position': True,
        'pip_cost': 0.00008,
        'rollover_spread': 0.00,
        'BBands_period': 1440,
        'SMA_period_short': 150,
        'SMA_period_long': 600,
        'indicator_lookback': 1440,  # max of all lookbacks!!!
        'indicator_freq': '1m',
        'buy_signal_threshold': 0.5,
        'sell_signal_threshold': -0.5
    }

    # define the strategy instruments universe
    context.universe = [
        symbol('FXCM:AUD/USD'),
        symbol('FXCM:EUR/USD'),
        symbol('FXCM:NZD/USD'),
        symbol('FXCM:USD/CAD'),
        symbol('FXCM:USD/CHF'),
    ]
    context.ccy_universe = [
        'AUD', 'CAD', 'CHF', 'EUR', 'GBP', 'JPY', 'NZD', 'USD'
    ]

    # function to schedule roll-overs, at 5 PM EST or 9 PM UTC (3 hours before midnight)
    schedule_function(compute_rollovers, date_rules.every_day(),
                      time_rules.market_close(hours=3, minutes=0))

    # set up cost structures, we assume a $1 per $10K all-in cost
    set_commission(fx=commission.PipsCost(cost=context.params['pip_cost']))
    set_slippage(fx=slippage.FixedSlippage(spread=0.00))

    # variables to track signals and target portfolio
    context.signals = dict((security, 0) for security in context.universe)
    context.weights = dict((security, 0) for security in context.universe)

    # Call rebalance function, see below under standard helper functions to modify
    rebalance_scheduler(context)

    # make the back-test lighter
    context.perf_tracker.keep_transactions = False
    context.perf_tracker.keep_orders = False
def initialize(context):
    '''
        Called once at the start of the strategy execution. 
        This is the place to define things to do at the start of the strategy.
    '''
    # set the account base currency and strategy parameters
    set_account_currency('USD')
    context.params = {
        'verbose': False,
        'leverage': 2,
        'rebalance_freq': 'monthly',
        'no_overnight_position': False,
        'pip_cost': 0.0001,
        'rollover_spread': 0.00,
        'positions': 2,
        'lookback': 26 * 6
    }

    # define the strategy instruments universe
    context.universe = [
        symbol('FXCM:AUD/USD'),
        symbol('FXCM:EUR/CHF'),
        symbol('FXCM:EUR/JPY'),
        symbol('FXCM:EUR/USD'),
        symbol('FXCM:GBP/JPY'),
        symbol('FXCM:GBP/USD'),
        symbol('FXCM:NZD/USD'),
        symbol('FXCM:USD/CAD'),
        symbol('FXCM:USD/CHF'),
        symbol('FXCM:USD/JPY'),
    ]
    context.ccy_universe = [
        'AUD', 'CAD', 'CHF', 'EUR', 'GBP', 'JPY', 'NZD', 'USD'
    ]

    # function to schedule roll-overs, at 5 PM EST or 9 PM UTC (3 hours before midnight)
    schedule_function(compute_rollovers, date_rules.every_day(),
                      time_rules.market_close(hours=3, minutes=0))

    # set up cost structures, we assume a $1 per $10K all-in cost
    set_commission(fx=commission.PipsCost(cost=context.params['pip_cost']))
    set_slippage(fx=slippage.FixedSlippage(spread=0.00))

    # Call rebalance function, see below under standard helper functions to modify
    rebalance_scheduler(context)
예제 #5
0
def initialize(context):
    """
        function to define things to do at the start of the strategy
    """
    # set the account currency, only valid for backtests
    set_account_currency("USD")
    
    # trading pound parity!
    # this should work after the European sovereign crisis settled down
    # and before the Brexit noise started (2012-2015)
    context.x = symbol('FXCM:GBP/USD')
    context.y = symbol('FXCM:EUR/USD')
    context.leverage = 5
    context.signal = 0

    # Trade entry and exit when the z_score is +/- entry_z_score and exit_z_score respectively
    context.entry_z_score = 2.0
    context.exit_z_score = 0.5

    # Lookback window
    context.lookback = 720

    # used for zscore calculation
    context.z_window = 360

    # Call strategy function after the London open every day
    schedule_function(pair_trading_strategy,
                     date_rules.every_day(),
                     time_rules.market_open(hours=9,minutes=30))

    # set trading cost and slippage to zero
    set_commission(fx=commission.PipsCost(cost=0.00))
    set_slippage(fx=slippage.FixedSlippage(0.00))

    # square off towards to NYC close
    context.trading_hours = False
    
    # set a timeout for trading
    schedule_function(stop_trading,
                    date_rules.every_day(),
                    time_rules.market_close(hours=0, minutes=31))
    # call square off to zero out positions 30 minutes before close.
    schedule_function(daily_square_off,
                    date_rules.every_day(),
                    time_rules.market_close(hours=0, minutes=30))