import os
import pandas as pd
import datetime as dt


def symbol_to_path(symbol, base_dir=None):
    """Return CSV file path given ticker symbol."""
    if base_dir is None:
        base_dir = os.environ.get("MARKET_DATA_DIR", '../data/')
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))

def get_data(path, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    df_temp = pd.read_csv(path,
            index_col='time',
            parse_dates=True,
            usecols=['time', 'open', 'high', 'low', 'close'],
            na_values=['nan'])
    df = df.join(df_temp)
    return df

def plot_data(df, title="Stock prices", xlabel="Date", ylabel="Price"):
    import mplfinance as mpf
    mpf.plot(df, type='candle', mav=(9, 24))

def test_code():
    sd = dt.datetime(2020,1,1)
    ed = dt.datetime(2020,8,30)
    dates = pd.date_range(sd, ed)
    prices_all = get_data(symbol_to_path('BTCUSD_Coinbase'), dates)
    plot_data(prices_all)

if __name__ == "__main__":
    test_code()

