예제 #1
0
def preprocess(source,
               index,
               start_date,
               end_date,
               force_rerun=True,
               force_rerun_ta=True):
    # load symbol name
    with open(make_input_file(index)) as csvfile:
        symbols = [
            row[0]
            for ind, row in enumerate(csv.reader(csvfile.read().splitlines()))
            if ind > 0
        ]

    # download Yahoo finance trading data for all symbols in the index
    if force_rerun:
        # download trading data
        download_batch(source, symbols, index, start_date, end_date, 'd',
                       make_output_file(index, 'daily'))
        daily_data = pd.read_csv(make_output_file(index, 'daily'))

        if source == 'yahoo':  # only yahoo provide weekly data
            download_batch(source, symbols, index, start_date, end_date, 'w',
                           make_output_file(index, 'weekly'))
            weekly_data = pd.read_csv(make_output_file(index, 'weekly'))

    if force_rerun_ta:
        # calculate TA indicators for each symbol
        symbols = daily_data.Symbol.unique()
        directory = os.path.join(settings['folder']['trading'], index)
        if not os.path.exists(directory):
            os.mkdir(directory)
        for symbol in symbols:
            fname = os.path.join(directory, symbol + '_ta.csv')

            if source in ('g_1year', 'quandl'):  # avoid overwrite
                if force_rerun == False and os.path.exists(fname):
                    continue
                df = daily_data.loc[daily_data['Symbol'] == symbol]
                df = df.iloc[::-1].reset_index().drop('index', axis=1)
                df = ma(df, 20)
                df = ma(df, 50)
                df = b_bands(df, 20)
                df = rsi(df, 14)
                df = sto_k(df, 14)
                df = sto_d(df, 14)
                df = macd(df, 12, 26)
                df.to_csv(fname, index=False)

            if source == 'yahoo':  #only yahoo provides weekly data
                wfname = os.path.join(directory, symbol + '_weekly_ta.csv')
                wdf = weekly_data.loc[weekly_data['Symbol'] == symbol]
                wdf = wdf.iloc[::-1].reset_index().drop('index', axis=1)
                wdf = ma(wdf, 20)
                wdf = ma(wdf, 50)
                wdf = rsi(wdf, 14)
                wdf.to_csv(wfname, index=False)
예제 #2
0
def trigger_index_stats(index, a, b, success_window):
    with open(make_input_file(index)) as csvfile:
        symbols = [
            row[0]
            for ind, row in enumerate(csv.reader(csvfile.read().splitlines()))
            if ind > 0
        ]

    stats_table = [[
        'Trigger_name', 'Index', 'Avg_gain', 'Success_rate', 'Frequency'
    ]]

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_sporadic(symbol, index, success_window)
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_sporadic', index, weighted_gain / cnt, weighted_rate / cnt,
        cnt
    ])

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_macd_trend(symbol, index, a, b,
                                              success_window)
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_macd_trend', index, weighted_gain / cnt, weighted_rate / cnt,
        cnt
    ])

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_rsi_trend(symbol, index, a, b,
                                             success_window)
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_rsi_trend', index, weighted_gain / cnt, weighted_rate / cnt,
        cnt
    ])

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_rsi_low(symbol, index, success_window)
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_rsi_low', index, weighted_gain / cnt, weighted_rate / cnt, cnt
    ])

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_rsi_high(symbol, index, success_window)
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_rsi_high', index, weighted_gain / cnt, weighted_rate / cnt,
        cnt
    ])

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_bband_lowerflat(symbol, index, a,
                                                   success_window)
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_bband_lowerflat', index, weighted_gain / cnt,
        weighted_rate / cnt, cnt
    ])

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_bband_upbreak(symbol, index, a,
                                                 success_window)
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_bband_upbreak', index, weighted_gain / cnt,
        weighted_rate / cnt, cnt
    ])

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_bband_downbreak(symbol, index, a,
                                                   success_window)
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_bband_downbreak', index, weighted_gain / cnt,
        weighted_rate / cnt, cnt
    ])

    weighted_gain = weighted_rate = cnt = 0
    for symbol in symbols:
        trigger = triggers.trigger_above_20wma(
            symbol, index, 10, 2
        )  # weekly data has n=10, successwindow=2, different from daily data
        stats = success_rate(trigger, success_window)
        weighted_gain += stats.avg_gain * stats.frequency
        weighted_rate += stats.success_rate * stats.frequency
        cnt += stats.frequency
    stats_table.append([
        'trigger_above_20wma', index, weighted_gain / cnt, weighted_rate / cnt,
        cnt
    ])

    with open(
            os.path.join(settings['folder']['results'],
                         index + '_results.csv'), 'wt') as csvfile:
        csv.writer(csvfile, delimiter=",").writerows(stats_table)
    return stats_table
예제 #3
0
import datetime
import os
import pandas as pd
import sys

index = 'dow30'
source = 'quandl'
today = datetime.date.today()
start_date = str(today - datetime.timedelta(365))
end_date = str(today)
trigger_start_date = str(today - datetime.timedelta(14))
a, b, success_window = 5, 2, 0  # success_window=0 for forecast realtime
# a and b is for particular trigger, e.g. GS, macd corss up, 5 consecutive neg values followed by 2 positive values

# load symbols
with open(make_input_file(index)) as csvfile:
    symbols = [
        row[0]
        for ind, row in enumerate(csv.reader(csvfile.read().splitlines()))
        if ind > 0
    ]

# get recent data and ta indicators
preprocess(source,
           index,
           start_date,
           end_date,
           force_rerun=True,
           force_rerun_ta=True)

# apply trigger based on ta indicators
from __future__ import absolute_import

from collections import namedtuple
import csv

from strategy_platform.preprocess.yahoo_download import download_batch
from strategy_platform.preprocess.make_file import make_input_file, make_output_file

start_date = '1997-01-01'
end_date = '2017-01-15'
index = 'nasdaq100'

# load symbol name, note that nasdaq and sp500 has overlap, take overlap off nasdaq
with open(make_input_file('nasdaq100')) as csvfile:
    nasdaq100_symbols = [
        row[0]
        for ind, row in enumerate(csv.reader(csvfile.read().splitlines()))
        if ind > 0
    ]
with open(make_input_file('sp500')) as csvfile:
    sp500_symbols = [
        row[0] for ind, row in enumerate(csv.reader(csvfile)) if ind > 0
    ]
symbols = list(set(nasdaq100_symbols) - set(sp500_symbols))

# download trading data
daily_data = download_batch(symbols, index, start_date, end_date, 'd',
                            make_output_file(index, 'daily'))

weekly_data = download_batch(symbols, index, start_date, end_date, 'w',
                             make_output_file(index, 'weekly'))