Esempio n. 1
0
def main(path_to_data):
    c=0
    ticker_list=get_ticker_list.get_ticker_list(obj.path_to_master_list())
    for ticker in ticker_list:
        print(ticker)
        df=convert_to_df(ticker,path_to_data)
        HA(ticker,df)
    # To find the number of files in the output directory that end with .csv i.e. are the output files of the tickers
    list=os.listdir(obj.path_to_output_dir()+"Heikin-Ashi/")
    length_1=len(fnmatch.filter(list, "*.csv"))
    #to find the number of tickers in the master list
    ticker_list=get_ticker_list.get_ticker_list(obj.path_to_master_list())
Esempio n. 2
0
def drop_headers_print_to_file():
    path_to_stock_master_list = "C:/Users/Rohit/Python_source_code/list of stocks/nifty500_list.csv"
    path_to_historical_data = "C:/Users/Rohit/Python_source_code/historical_stock_5_min_data/"
    path_to_output_directory = r"C:\Users\Rohit\Python_source_code\sanitised data\historical_stock_5_min_data/"
    '''
    path_to_stock_master_list = r"C:/Users/Hp/Desktop/New folder/nifty500_list.csv"
    path_to_historical_data = r"C:/Users/Hp/Desktop/New folder/historical_stock_5_min_data/"
    path_to_output_directory = r"C:/Users/Hp/Desktop/New folder/Sanitized Data/Top500_stock/"
    '''
    ticker_list = get_ticker_list.get_ticker_list(path_to_stock_master_list)
    c = 0
    for ticker in ticker_list:

        c = c + 1
        print(ticker)
        df = pd.read_csv(path_to_historical_data + ticker + ".csv",
                         index_col=False)
        drop_index = []
        for i in df.index:
            if "Close" in df.values[i]:
                drop_index.append(i)
        if len(drop_index) > 0:
            df = df.drop(df.index[drop_index])
        df.to_csv(path_to_output_directory + ticker + ".csv", index=False)
    if c == len(ticker_list):
        print("Every file has been replaced")
Esempio n. 3
0
def main(path_to_data):
    period = 4
    obj = config.config(r"../config.txt")
    ticker_list = get_ticker_list.get_ticker_list(obj.path_to_master_list())
    for ticker in ticker_list:
        print(ticker)
        rsi_obj = RSI(period,ticker,path_to_data + ticker + ".csv")
        rsi_obj.calculate_rsi()
Esempio n. 4
0
def main():
    stdev_list = []
    config_object = config("../config.txt")
    filename = "ind_niftyfmcglist.csv"
    path_to_stock_masterlist = config_object.path_to_master_list()
    print(path_to_stock_masterlist, "\n")
    ticker_list = get_ticker_list.get_ticker_list(path_to_stock_masterlist)
    for ticker in ticker_list[:1]:
        macd_crossover(ticker)
Esempio n. 5
0
def main():
    ticker_list = get_ticker_list.get_ticker_list(path_to_stock_master_list)
    for ticker in ticker_list:
        if '&' in ticker:
            ticker = ticker[:ticker.index('&')] + "_26" + ticker[ticker.index('&') + 1:]
        print(ticker)
        interval_line = resistance(ticker)

        # Appending to the new file created
        with open(path_to_output, "a", newline="") as f:
            writer = csv.writer(f)
            writer.writerow(interval_line)
Esempio n. 6
0
def get_narrow_7(ndays, output_folder):

    strategy_ticker = []
    dates = []
    """Config Object"""
    config_object = config("../config.txt")
    path_to_output_dir = config_object.path_to_output_dir() + output_folder

    path_to_stock_masterlist = config_object.path_to_master_list()
    ticker_list = get_ticker_list.get_ticker_list(path_to_stock_masterlist)

    output_filename = "scanner_output.csv"
    log_filename = "strategy_log.log"
    """Logging Details"""
    log_format = "%(levelname)s - %(message)s"
    logging.basicConfig(filename=path_to_output_dir + log_filename,
                        level=logging.DEBUG,
                        format=log_format,
                        filemode="w")
    logger = logging.getLogger()
    for ticker in ticker_list:
        #Drops the data before the initial date
        df = drop_data(ticker)
        """List containing 5-min data"""

        date_day = df['Date Time'].tolist()
        close_day = df['Close'].tolist()
        open_day = df['Open'].tolist()
        low_day = df['Low'].tolist()

        logger.info(ticker)
        """Code to implement the stock trading strateg"""
        for i in range(0, len(date_day) - ndays + 1):
            c = 0
            range_list = []
            date_list = [ticker]
            # Code Change: Add float to convert values from string
            for j in range(i, i + ndays):
                range_value = abs(close_day[j] - open_day[j])
                range_list.append(range_value)
                logger.debug(date_day[j][:10] + " " + str(range_value))
            if j + 1 < len(date_day) - 1:
                range_value = abs(close_day[j + 1] - open_day[j + 1])
                logger.debug(date_day[j + 1][:10] + " " + str(range_value))
                if range_value < min(range_list):
                    date_list.append(date_day[j + 1][:10])
                    date_list.append(date_day[j + 2][:10])
                    dates.append(date_list)
            logger.debug("\n")
        with open(path_to_output_dir + output_filename, 'w', newline="") as f:
            writer = csv.writer(f)
            for row in dates:
                writer.writerow(row)
Esempio n. 7
0
def main():
    ticker_url = ""
    ticker_list = get_ticker_list.get_ticker_list(obj.path_to_master_list())
    path_to_output_dir1 = obj.path_to_output_dir(
    ) + "historical 1 day data/Final data/"
    path_to_output_dir2 = obj.path_to_output_dir(
    ) + "historical 1 day data/Downloaded data/"

    path_to_historical_1_day_data = obj.path_to_historical_1_day_dir()
    for ticker in ticker_list:
        if "_26" in ticker:
            ticker_url = ticker[:ticker.
                                index('_')] + "&" + ticker[ticker.index('_') +
                                                           3:]
        else:
            ticker_url = ticker

        df = pd.read_csv(path_to_output_dir2 + ticker + ".csv", index_col=None)
        path_to_historical_data = path_to_historical_1_day_data + ticker + ".csv"
        df2 = pd.read_csv(path_to_historical_data, index_col=None)
        if "Date Time" not in df2.columns:
            df2.columns = [
                'Date Time', 'Close', 'High', 'Low', 'Open', 'Volume'
            ]
        df2 = df2[['Date Time', 'Close', 'High', 'Low', 'Open', 'Volume']]
        drop_index = []
        for i in df2.index:
            if "Close" in df2.values[i]:
                drop_index.append(i)
        if len(drop_index) > 0:
            df2 = df2.drop(df2.index[drop_index])

        if len(df2.index) != 0:
            last_value = df2.values[-1][0]
            last_index = -1
            for index in df.index:
                if df.values[index][0] == last_value:
                    last_index = index
            if last_index == -1:
                df2 = df2.append(df)
            else:
                df = df.drop(df.index[:last_index + 1])
                df2 = df2.append(df)
        else:
            print("Error : NO data in repository")
            df2 = df
        df2.to_csv(path_to_output_dir1 + ticker + ".csv", index=False)
        print(ticker_url)
    """Change according to the number of tickers"""
    if len(fnmatch.filter(os.listdir(path_to_output_dir1),
                          "*.csv")) == len(ticker_list):
        print("Final Data Complete")
Esempio n. 8
0
def main():

    config_object = config("../config.txt")
    path_to_master_list = config_object.path_to_master_list()
    path_to_historical_data = config_object.path_to_historical_1_day_dir()
    path_to_output_dir = config_object.path_to_output_dir() + "Test 1 day-dir/"
    ticker_list = get_ticker_list.get_ticker_list(path_to_master_list)

    for ticker in ticker_list:
        if '_26' in ticker:
            ticker = ticker[:ticker.
                            index('_')] + "&" + ticker[ticker.index('_') + 3:]
        print(ticker)
        with open(path_to_historical_data + ticker + ".csv", 'r') as infile:
            readstream = csv.reader(infile, delimiter=',')
            with open(path_to_output_dir + ticker + ".csv", 'w',
                      newline="") as output:
                outwriter = csv.writer(output, delimiter=',')
                i = 0
                for row in readstream:
                    if i == 0:
                        i = 1
                        row[0] = "Date Time"
                    outwriter.writerow(row)
Esempio n. 9
0
def BB_Scanner(ndays, period, output_folder, threshold):

    strategy_ticker = []
    dates = []
    skip_ticker = False
    """Config Object"""
    config_object = config("../config.txt")
    path_to_output_dir = config_object.path_to_output_dir() + output_folder

    path_to_stock_masterlist = config_object.path_to_master_list()
    ticker_list = get_ticker_list.get_ticker_list(path_to_stock_masterlist)

    output_filename = "scanner_output.csv"
    log_filename = "strategy_log.log"
    """Logging Details"""
    log_format = "%(levelname)s %(asctime)s - %(message)s"
    logging.basicConfig(filename=path_to_output_dir + log_filename,
                        level=logging.DEBUG,
                        format=log_format,
                        datefmt='%Y-%m-%d',
                        filemode="w")
    logger = logging.getLogger()
    for ticker in ticker_list:
        #Drops the data before the initial date
        print(ticker)
        df = drop_data(ticker)
        df = tech_indi_BBANDS.main(
            config_object.path_to_historical_1_day_dir(), period, ticker)
        per_ba = (df['Close'] - df['LowerBB']) / (df['UpperBB'] -
                                                  df['LowerBB'])
        """List containing 5-min data"""

        date_day = df['Date Time'].tolist()
        logger.info(ticker)
        """Code to implement the stock trading strateg"""
        for i in range(period - 1, len(date_day) - ndays + 1):
            date_list = []
            date_list.append(ticker)
            for j in range(i, i + ndays):
                if per_ba[j] < threshold:
                    if per_ba[j] < -10:
                        skip_ticker = True
                        logger.info(
                            "The value of %b dropped below -10. Some problem in data. Moving to next stock"
                        )
                        break
                    logger.debug("Value = " + str(per_ba[j]) + " on date " +
                                 date_day[j] + " for ticker " + ticker)
                    date_list.append(date_day[j][:10])
                else:
                    break
            if skip_ticker == True:
                skip_ticker = False
                break
            if len(date_list) == ndays + 1:
                if j + 1 <= len(date_day) - 1:
                    logger.debug("Value = " + str(per_ba[j + 1]) +
                                 " on date " + date_day[j + 1] +
                                 " for ticker " + ticker)
                    date_list.append(date_day[j + 1][:10])
                    dates.append(date_list)
    with open(path_to_output_dir + output_filename, 'w', newline="") as f:
        writer = csv.writer(f)
        for row in dates:
            writer.writerow(row)
def get_daily_closing_high(no_of_days, output_folder):

    strategy_ticker = []
    dates = []
    """Config Object"""
    config_object = config("../config.txt")
    path_to_output_dir = config_object.path_to_output_dir() + output_folder

    path_to_stock_masterlist = config_object.path_to_master_list()
    ticker_list = get_ticker_list.get_ticker_list(path_to_stock_masterlist)

    output_filename = "scanner_output.csv"
    log_filename = "strategy_log.log"
    """Logging Details"""
    log_format = "%(levelname)s - %(message)s"
    logging.basicConfig(filename=path_to_output_dir + log_filename,
                        level=logging.DEBUG,
                        format=log_format,
                        filemode="w")
    logger = logging.getLogger()
    for ticker in ticker_list:
        # Drops the data before the initial date
        df = drop_data(ticker)
        """List containing 5-min data"""

        date_day = df['Date Time'].tolist()
        close_day = df['Close'].tolist()
        open_day = df['Open'].tolist()
        low_day = df['Low'].tolist()
        """Code to implement the stock trading strateg"""
        for i in range(1, len(date_day) - no_of_days + 1):
            c = 0
            date_list = [ticker, date_day[i - 1][:10]]
            open_price = open_day[i - 1]
            # Code Change: Add float to convert values from string
            for j in range(0, no_of_days - 1):
                next_day = i + j + 1
                current_day = i + j
                previous_day = i + j - 1
                if float(close_day[current_day]) > float(open_day[current_day])\
                        and float(close_day[previous_day]) > float(open_day[previous_day]):
                    day_change = float(close_day[previous_day]) - float(
                        open_day[previous_day])
                    if (float(close_day[previous_day]) - float(open_day[current_day])) < (.98 * day_change) \
                            and float(low_day[current_day]) > float(open_day[previous_day]):
                        date_list.append(date_day[current_day][:10])
                        c += 1
                if c == no_of_days - 1:
                    close_price = close_day[current_day]
                    # Percentage change in the close price of final day and the opening price of the first day
                    per = (close_price - open_price) / open_price
                    date_list.append(date_day[next_day][:10])
                    # Rounded it off to 4 decimal places
                    date_list.append(round(per, 4))
                    dates.append(date_list)
            if c == no_of_days - 1:
                strategy_ticker.append(ticker)

    if len(dates) == 0:
        logger.error("No companies followed this strategy")
    else:
        c = 1
        for i in range(0, len(dates) - 1):
            if dates[i][0] != dates[i + 1][0]:
                c = c + 1

        logger.info("Strategy is followed by = " + str(c) + " companies")
        with open(path_to_output_dir + output_filename, 'w', newline="") as f:
            writer = csv.writer(f)
            for row in dates:
                writer.writerow(row)
Esempio n. 11
0
def main():
    # Extracting arguments from command line
    parser = argparse.ArgumentParser(description="download data")
    parser.add_argument("seconds", type=int, help="Number of seconds")
    parser.add_argument("days", type=int, help="Number of days")

    args = parser.parse_args()
    sec = args.seconds
    day = args.days

    # Names for the directory
    date = str(datetime.datetime.today())[:10]
    path_to_final_data = obj.path_to_output_dir() + "final data " + str(
        sec) + " " + date + "/"
    path_to_downloaded_data = obj.path_to_output_dir(
    ) + "downloaded data " + str(sec) + " " + date + "/"

    # Making required directory
    os.makedirs(path_to_final_data, exist_ok=True)
    os.makedirs(path_to_downloaded_data, exist_ok=True)

    # Condition for whether input seconds data exists or not
    if sec == 300:
        path_to_historical_data = obj.path_to_historical_5_min_dir()
    elif sec == 1800:
        path_to_historical_data = obj.path_to_historical_30_min_dir()
    elif sec == 86400:
        path_to_historical_data = obj.path_to_historical_1_day_dir()
    else:
        print("No such stock data present")
        exit(-1)

    ticker_url = ""
    ticker_list = get_ticker_list.get_ticker_list(obj.path_to_master_list())
    for ticker in ticker_list:
        if "_26" in ticker:
            ticker_url = ticker[:ticker.
                                index('_')] + "&" + ticker[ticker.index('_') +
                                                           3:]
        else:
            ticker_url = ticker

        param = {
            'q': ticker_url,  # Stock symbol (ex: "AAPL")
            'i':
            str(sec),  # Interval size in seconds ("86400" = 1 day intervals)
            'x':
            "NSE",  # Stock exchange symbol on which stock is traded (ex: "NASD")
            'p': str(day) + "d"  # Period (Ex: "1Y" = 1 year)
        }
        # get price data (return pandas dataframe)
        df = get_price_data(param)
        df.index.name = "Date Time"
        df = df[['Close', 'High', 'Low', 'Open', 'Volume']]
        df.to_csv(path_to_downloaded_data + ticker + ".csv")
        if len(df.index) == 0:
            print("Error : No data online")
        print(ticker_url)

    if len(fnmatch.filter(os.listdir(path_to_downloaded_data),
                          "*.csv")) == len(ticker_list):
        print("Downloaded Data Complete")

    for ticker in ticker_list:
        if "_26" in ticker:
            ticker_url = ticker[:ticker.
                                index('_')] + "&" + ticker[ticker.index('_') +
                                                           3:]
        else:
            ticker_url = ticker

        df = pd.read_csv(path_to_downloaded_data + ticker + ".csv",
                         index_col=None)
        df2 = pd.read_csv(path_to_historical_data + ticker + ".csv",
                          index_col=None)
        if "Date Time" not in df2.columns:
            df2.columns = [
                'Date Time', 'Close', 'High', 'Low', 'Open', 'Volume'
            ]
        df2 = df2[['Date Time', 'Close', 'High', 'Low', 'Open', 'Volume']]
        drop_index = []
        for i in df2.index:
            if "Close" in df2.values[i]:
                drop_index.append(i)
        if len(drop_index) > 0:
            df2 = df2.drop(df2.index[drop_index])

        if len(df2.index) != 0:
            last_value = df2.values[-1][0]
            last_index = -1
            for index in df.index:
                if df.values[index][0] == last_value:
                    last_index = index
            if last_index == -1:
                df2 = df2.append(df)
            else:
                df = df.drop(df.index[:last_index + 1])
                df2 = df2.append(df)
        else:
            print("Error : NO data in repository")
            df2 = df
        df2.to_csv(path_to_final_data + ticker + ".csv", index=False)
        print(ticker_url)
    """Change according to the number of tickers"""
    if len(fnmatch.filter(os.listdir(path_to_final_data),
                          "*.csv")) == len(ticker_list):
        print("Final Data Complete")