Пример #1
0
def initialize(context):
    """
        A function to define things to do at the start of the strategy
    """
    # universe selection
    context.securities = [symbol('NIFTY-I')]

    # 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,
        'BBands_period': 300,
        'trade_freq': 5,
        'leverage': 2
    }

    # variable to control trading frequency
    context.bar_count = 0

    # 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)
    context.entry_price = dict(
        (security, 0) for security in context.securities)
    context.entry_side = dict((security, 0) for security in context.securities)
    context.stoploss = 0.01  # percentage stoploss

    # set trading cost and slippage to zero
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0))
    set_slippage(slippage.FixedSlippage(0.00))
def initialize(context):
    """
        API function to define things to do at the start of the strategy.
    """
    # set strategy parameters
    context.lookback_data = 60
    context.lookback_long = 20
    context.leverage = 2.0

    # reset everything at start
    daily_reset(context)

    # create our universe
    create_universe(context)

    # schedule calculation at the end of opening range (30 minutes)
    schedule_function(calculate_trading_metrics, date_rules.every_day(), 
                            time_rules.market_open(hours=0, minutes=30))
    
    # schedule entry rules
    schedule_function(no_more_entry, date_rules.every_day(), 
                            time_rules.market_open(hours=1, minutes=30))
    
    # schedule exit rules
    schedule_function(unwind, date_rules.every_day(), 
                            time_rules.market_close(hours=0, minutes=30))
    
    # set trading costs
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0))
    set_slippage(slippage.FixedSlippage(0.00))
def initialize(context):
    '''
        A function to define things to do at the start of the strategy
    '''
    # universe selection
    context.universe = [symbol('NIFTY-I'), symbol('BANKNIFTY-I')]

    # 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': 300,
        'BBands_period': 300,
        'ADX_period': 120,
        'trade_freq': 15,
        'leverage': 1
    }

    # variable to control trading frequency
    context.bar_count = 0

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

    # set trading cost and slippage to zero
    set_commission(commission.PerShare(cost=0.0, min_trade_cost=0.0))
    set_slippage(slippage.FixedSlippage(0.00))

    # create the list of experts as well as the agent controlling them
    expert1 = Advisor('bbands_ea', expert_advisor_1, context.universe)
    expert2 = Advisor('maxover_ea', expert_advisor_2, context.universe)
    expert3 = Advisor('rsi_ea', expert_advisor_3, context.universe)
    expert4 = Advisor('sup_res_ea', expert_advisor_4, context.universe)
    context.agent = Agent([expert1, expert2, expert3, expert4])

    # schedule agent weights updates
    schedule_function(update_agent_weights, date_rules.week_start(),
                      time_rules.market_close())