Beispiel #1
0
    def test_short_strategy(self):
        from_date = datetime(2012, 1, 1)
        to_date = datetime(2012, 12, 31)
        symbols = ['AZN.L', 'FRES.L', 'IAG.L']
        init_marketdata(symbols, from_date, to_date)

        sr = StrategyRunner('CDL3WHITESOLDIERS', 100, 3, 0,
                            0.02)(symbols, from_date, to_date)
        self.assertAlmostEqual(295.808, sr.balance, 2)
Beispiel #2
0
    def test_long_strategy(self):
        from_date = datetime(2012, 1, 1)
        to_date = datetime(2012, 1, 31)
        symbols = ['BRBY.L', 'CNA.L', 'MGGT.L']
        init_marketdata(symbols, from_date, to_date)

        sr = StrategyRunner('CDL3LINESTRIKE', -100, 9, 1,
                            0.02)(symbols, from_date, to_date)
        self.assertAlmostEqual(-353.443, sr.balance, 2)
Beispiel #3
0
def main(fname, from_date, to_date):
    symbols = load_symbols(fname)
    init_marketdata(symbols, from_date, to_date)

    palg = talib_candlestick_funcs()

    c = CandlestickPatternEvents(symbols, palg, from_date, to_date)()

    diff_level = 0.02  # output patterns where up/down > diff_level
    min_cnt = 5  # output patterns with > min_cnt events

    output_results(c.average_changes, diff_level, min_cnt)
Beispiel #4
0
    def test_CandlestickPatternEvents(self):
        from_date = datetime(2012, 1, 1)
        to_date = datetime(2012, 1, 31)
        symbols = ['ABF.L', 'ADM.L']
        init_marketdata(symbols, from_date, to_date)

        palg = talib_candlestick_funcs()

        c = CandlestickPatternEvents(symbols, palg, from_date, to_date)()
        changes = list(c.average_changes)
        self.assertEquals(20, len(changes))
        self.assertEquals('CDLSPINNINGTOP:100', changes[19][0])
        self.assertEquals(4, changes[19][1].cnt())
        self.assertAlmostEquals(1.02994350282, changes[19][1].average('open')[-1])
Beispiel #5
0
    def test_CandlestickPatternEvents(self):
        from_date = datetime(2012, 1, 1)
        to_date = datetime(2012, 1, 31)
        symbols = ['ABF.L', 'ADM.L']
        init_marketdata(symbols, from_date, to_date)

        palg = talib_candlestick_funcs()

        c = CandlestickPatternEvents(symbols, palg, from_date, to_date)()
        changes = list(c.average_changes)
        self.assertEquals(17, len(changes))
        self.assertEquals('CDLSPINNINGTOP:100', changes[16][0])
        self.assertEquals(3, changes[16][1].cnt())
        self.assertAlmostEquals(1.02994350282,
                                changes[16][1].average('open')[-1])
Beispiel #6
0
def events_main(fname, from_date, to_date):
    symbols = load_symbols(fname)
    init_marketdata(symbols, from_date, to_date)

    palg = talib_candlestick_funcs()

    c = CandlestickPatternEvents(symbols, palg, from_date, to_date)()

    diff_level = 0.02  # output patterns where up/down > diff_level
    min_cnt = 10  # output patterns with > min_cnt events

    average_changes = list(
        filter_average_changes(c.average_changes, diff_level, min_cnt))

    output_results(average_changes, [fname, from_date, to_date])
Beispiel #7
0
        ], txns, ['%s', '%s', '%s', '%f', '%f', '%f'])


def strategy_runner((outpath, symbols, from_date, to_date, strategy)):
    sr = StrategyRunner(*strategy)(symbols, from_date, to_date)
    output_transactions(
        outpath,
        (strategy[0], strategy[1], strategy[2], strategy[3], strategy[4]),
        sr.txns)
    return (strategy[0], strategy[1], strategy[2], strategy[3], strategy[4],
            sr.balance)


def backtesting_main(fname, from_date, to_date, strategies, async=False):
    symbols = load_symbols(fname)
    init_marketdata(symbols, from_date, to_date)
    strategies_cfg = load_strategies(strategies)
    outpath = create_result_dir('backtesting')

    pool = Pool(4)
    res = pool.map(strategy_runner, [(outpath, symbols, from_date, to_date, x)
                                     for x in strategies_cfg])

    with open(os.path.join(outpath, 'backtesting.html'), 'w') as f:
        create_table(f, [
            'Pattern', 'Pattern params', 'Hold days', 'Buy side', 'Limit',
            'Profit'
        ], res, ['%s', '%d', '%d', '%d', '%f', '%f'])

    profitable = [x for x in res if x[5] > 1.0]
    with open(os.path.join(outpath, 'profit.html'), 'w') as f:
Beispiel #8
0

def output_transactions(outpath, sparams, txns):
    with open(os.path.join(outpath, 'txns_%s_%d_%d_%d_%d.html' % (sparams[0], sparams[1], sparams[2], sparams[3], sparams[4])), 'w') as f:
        create_table(f, ['Symbol', 'Buy date', 'Sell date', 'Buy price', 'Sell prive', 'Profit'], txns, ['%s', '%s', '%s', '%f', '%f', '%f'])


def strategy_runner((outpath, symbols, from_date, to_date, strategy)):
    sr = StrategyRunner(*strategy)(symbols, from_date, to_date)
    output_transactions(outpath, (strategy[0], strategy[1], strategy[2], strategy[3], strategy[4]), sr.txns)
    return (strategy[0], strategy[1], strategy[2], strategy[3], strategy[4], sr.balance)


def backtesting_main(fname, from_date, to_date, strategies, async=False):
    symbols = load_symbols(fname)
    init_marketdata(symbols, from_date, to_date)
    strategies_cfg = load_strategies(strategies)
    outpath = create_result_dir('backtesting')

    pool = Pool(4)
    res = pool.map(strategy_runner, [(outpath, symbols, from_date, to_date, x) for x in strategies_cfg])

    with open(os.path.join(outpath, 'backtesting.html'), 'w') as f:
        create_table(f, ['Pattern', 'Pattern params', 'Hold days', 'Buy side', 'Limit', 'Profit'], res, ['%s', '%d', '%d', '%d', '%f', '%f'])

    profitable = [x for x in res if x[5] > 1.0]
    with open(os.path.join(outpath, 'profit.html'), 'w') as f:
        create_table(f, ['Pattern', 'Pattern params', 'Hold days', 'Buy side', 'Limit', 'Profit'], profitable, ['%s', '%d', '%d', '%d', '%f', '%f'])


if __name__ == '__main__':