Example #1
0
def main(train_stock,
         val_stock,
         window_size,
         batch_size,
         ep_count,
         strategy="t-dqn",
         model_name="model_debug",
         pretrained=False,
         debug=False,
         cpu=False):
    """ Trains the stock trading bot using Deep Q-Learning.
    Please see https://arxiv.org/abs/1312.5602 for more details.

    Args: [python train.py --help]
    """

    if cpu:
        switch_k_backend_device()

    print("#TF Version: ", tf.__version__)

    print("Using device: ")
    print(device_lib.list_local_devices())
    agent = Agent(window_size,
                  strategy=strategy,
                  pretrained=pretrained,
                  model_name=model_name)

    train_data = get_stock_data(train_stock)
    val_data = get_stock_data(val_stock)

    initial_offset = val_data[1] - val_data[0]

    for episode in range(1, ep_count + 1):
        train_result = train_model(agent,
                                   episode,
                                   train_data,
                                   ep_count=ep_count,
                                   batch_size=batch_size,
                                   window_size=window_size)
        val_result, _, actionCollection = evaluate_model(
            agent, val_data, window_size, debug)
        show_train_result(train_result, val_result, initial_offset)
def quick_train(train_stock):

	generate(train_stock)

	training_stock = 'training_data/' + train_stock + '.csv'
	strategy = 'double-dqn'
	window_size = 10
	batch_size = 32
	ep_count = 10
	model_name = 'model_double-dqn_GOOG_50'
	pretrained = False
	debug = False

	coloredlogs.install(level="DEBUG")
	switch_k_backend_device()
	print(training_stock)
	
	main(training_stock, window_size, batch_size,
			 ep_count, strategy=strategy, model_name=model_name, 
			 pretrained=pretrained, debug=debug)
Example #3
0

if __name__ == "__main__":
    args = docopt(__doc__)

    train_stock = args["<train-stock>"]
    val_stock = args["<val-stock>"]
    strategy = args["--strategy"]
    window_size = int(args["--window-size"])
    batch_size = int(args["--batch-size"])
    ep_count = int(args["--episode-count"])
    model_name = args["--model-name"]
    pretrained = args["--pretrained"]
    debug = args["--debug"]

    coloredlogs.install(level="DEBUG")
    switch_k_backend_device()

    try:
        main(train_stock,
             val_stock,
             window_size,
             batch_size,
             ep_count,
             strategy=strategy,
             model_name=model_name,
             pretrained=pretrained,
             debug=debug)
    except KeyboardInterrupt:
        print("Aborted!")
Example #4
0
def fn(ticker,strategy,ep_count,year):
    stock = '{}'.format(ticker)
    model_name = '{}_{}_{}'.format(ticker,strategy,ep_count)
    test_stock = 'data/Nifty50/Split/{}_{}.csv'.format(ticker,year)
    window_size = 10
    debug = True
    tf.keras.backend.clear_session()
    K.clear_session()
    agent = Agent(window_size, pretrained=True, model_name=model_name)

    # read csv into dataframe
    df = pd.read_csv(test_stock)
    # df = df.iloc[:55]
    # filter out the desired features
    df = df[['Date', 'Adj Close']]
    # rename feature column names
    df = df.rename(columns={'Adj Close': 'actual', 'Date': 'date'})
    # convert dates from object to DateTime type
    dates = df['date']
    dates = pd.to_datetime(dates, infer_datetime_format=True)
    df['date'] = dates

    df.head()



    coloredlogs.install(level='DEBUG')
    switch_k_backend_device()

    test_data = get_stock_data(test_stock)
    initial_offset = test_data[1] - test_data[0]

    test_result, history = evaluate_model(agent, test_data, window_size, debug)
    show_eval_result(model_name, test_result, initial_offset)

    def visualize(df, history, title="trading session"):
        # add history to dataframe
        position = [history[0][0]] + [x[0] for x in history]
        actions = ['HOLD'] + [x[1] for x in history]
        df['position'] = position
        df['action'] = actions

        # specify y-axis scale for stock prices
        scale = alt.Scale(domain=(min(min(df['actual']), min(df['position'])) - 50, max(max(df['actual']), max(df['position'])) + 50), clamp=True)

        # plot a line chart for stock positions
        actual = alt.Chart(df).mark_line(
            color='green',
            opacity=0.5
        ).encode(
            x='date:T',
            y=alt.Y('position', axis=alt.Axis(format='$.2f', title='Price'), scale=scale)
        ).interactive(
            bind_y=False
        )

        # plot the BUY and SELL actions as points
        points = alt.Chart(df).transform_filter(
            alt.datum.action != 'HOLD'
        ).mark_point(
            filled=True
        ).encode(
            x=alt.X('date:T', axis=alt.Axis(title='Date')),
            y=alt.Y('position', axis=alt.Axis(format='$.2f', title='Price'), scale=scale),
            color='action'
        ).interactive(bind_y=False)

        # merge the two charts
        chart = alt.layer(actual, points, title=title).properties(height=300, width=1000)

        return chart

    chart = visualize(df, history, title=test_stock)

    cap = [0]
    inv = 0
    ret = 0
    b = 0
    for i in range(len(df)):
        if df.iloc[i]['action']=='BUY':
            cap.append(cap[-1]+df.iloc[i]['actual'])
            inv+=df.iloc[i]['actual']
            b+=1
        if df.iloc[i]['action']=='SELL' and b>0:
            cap.append(cap[-1]-df.iloc[i]['actual'])
            ret += df.iloc[i]['actual']
            b-=1

    req_cap = max(cap)

    prof = ret+(df['action'].value_counts().get('BUY',0)-df['action'].value_counts().get('SELL',0))*df.iloc[-1]['actual']-inv

    return pd.DataFrame([[ticker,strategy,ep_count,year,inv,ret,req_cap, prof, (prof/req_cap)*100]],columns=['ticker','strategy','ep_count','year','investment','returns','required capital','profit','profit percentage'])