Beispiel #1
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'])
Beispiel #2
0
def main(subreddit_name, num):
    # create a database connect
    #conn = create_connection(config.database)

    conn, c = connect(config.database)

    # create an instance
    reddit = praw.Reddit(client_id=config.client_id, client_secret=config.client_secret, 
                        user_agent=config.user_agent, redirect_uri=config.redirect_uri, username=config.username, 
                        password=config.password)
        
    with conn:
        # create the two tables
        create_table(conn, c, 'POSTS')
        create_table(conn, c, 'COMMENTS')
        
        #obtain an instance of this class for subreddit 
        subreddit = reddit.subreddit(subreddit_name)
        
        count = 0

        #Obtain the top N posts
        #for post in subreddit.top(limit=num):
        for post in subreddit.new(limit = num):
            count += 1
            # convert timestamp to datetime, convert the post.subreddit to str
            post_set = (post.id, post.title, post.score, str(post.subreddit), post.num_comments, \
                post.selftext, datetime.fromtimestamp(post.created))
            
            # insert the post to table 'POSTS'
            insert_subreddit(conn, c, post_set, count)
            
            submission = reddit.submission(id = post.id)
            comments = []
            for top_level_comment in submission.comments:
                try:
                    comments.append(top_level_comment.body)
                except KeyError:
                    comments = [top_level_comment.body]
            comment_set = (post.id, str(comments))

            # insert the comment to table 'COMMENTS'
            insert_comment(conn, c, comment_set, count)
        
    conn.close()
Beispiel #3
0
def nonrepeating_numbers_except_for_seq(string):
    list = string.rsplit(', ', maxsplit=1)
    table = helpers.create_table(sorted(list[1].split(' ')),
                                 nonrepeating_numbers_except(list[0], False))
    return helpers.table_to_message(table)
Beispiel #4
0
            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__':
    parser = argparse.ArgumentParser(
        description='Candlestick strategy backtesting')
    parser.add_argument('-f',
                        '--fromdate',
Beispiel #5
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'])
Beispiel #6
0
    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__':
    parser = argparse.ArgumentParser(description='Candlestick strategy backtesting')
    parser.add_argument('-f', '--fromdate', metavar='YYYYMMDD', type=mkdate, required=True, help='from date in format YYYYMMDD')
    parser.add_argument('-t', '--todate', metavar='YYYYMMDD', type=mkdate, required=True, help='from date in format YYYYMMDD')
    parser.add_argument('-s', '--shares', metavar='FILENAME', type=str, required=True, help='file with list of shares')
    parser.add_argument('strategies', metavar='STRATEGIES_FILE', type=str, nargs=1, help='')

    args = parser.parse_args()
    backtesting_main(args.shares, args.fromdate, args.todate, args.strategies[0])
Beispiel #7
0
# Connects to DB if exists, creates one if it doesn't and connects to that
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:////{os.getcwd()}/data.db"

engine = create_engine(app.config["SQLALCHEMY_DATABASE_URI"], 
	connect_args={'check_same_thread':False},
	poolclass=StaticPool, echo=False)

db = scoped_session(sessionmaker(bind=engine))

uploads_dir = "uploads"
rejected_uploads = "uploads/rejected"
allowed_extensions = set(['txt'])

#create 'uploads_content' table on start up if none exist
helpers.create_table(db)

# Scan uploads folder on start up to ensure the DB...
# reflects correctly what is in the folder...
# for example, in case user has deleted content from the uploads folder
helpers.rescan_db(db, uploads_dir, allowed_extensions)

'''
Upload 'file' to the uploads_dir folder and returns data to the user
'''
@app.route('/', methods=['POST'])
def upload():
	rows = db.execute("SELECT * FROM uploads_content")
	file = request.files['file']
	filename = secure_filename(file.filename)