Skip to content

Instantly share code, notes, and snippets.

@shurkin18
Last active July 11, 2023 15:31
Show Gist options
  • Save shurkin18/2e2a9acf06853347db5ed372fa531149 to your computer and use it in GitHub Desktop.
Save shurkin18/2e2a9acf06853347db5ed372fa531149 to your computer and use it in GitHub Desktop.
Python script which will read a number from temp file, then append it to CSV file (I use this for my tradingbot to collect and store daily trading results)
from datetime import datetime
import time
import os
import pytz
########################################################################################################################
# THIS PROGRAM WILL READ a NUMBER FROM TEMP FILE, THEN PLACE THAT NUMBER INTO A CSV (APPEND) FILE WITH DATE/TIME STAMP #
#
# NOTE: You may need to modify permissions/ownership of the folder where the files will be located
# To find out which user the python script is running as, comment out everything and just add these 2 lines:
# import getpass
# print(getpass.getuser())
#
# Modify folder ownership (if you are running the script on Ubuntu, most likely the username will be 'ubuntu'):
# chown -R ubuntu:ubuntu /path/to/folder/where/files/are
# Modify fodler permissions (read/write permissions to users/groups, read only to others)
# chmod 664 /path/to/folder/where/files/are
########################################################################################################################
# Obtain UTC linux time and Convert time to CT
curr_date_time = datetime.now(pytz.timezone('US/Central'))
curr_raw2 = curr_date_time.strftime("%Y-%m-%d %H:%M:%S")
curr_time_ct = curr_raw2.replace(" ", " | ")
# Open temp /path/to/temp_file.txt file and pass the data (number) to a VAR
print("opening temp log file /path/to/temp_file.txt...")
temp_file = open("/path/to/temp_file.txt", "r")
# Read from temp stocks_daily_gain_temp.txt file
print("reading data from temp log file /path/to/temp_file.txt...")
temp_file_data_number = str(temp_file.read())
# Close temp stocks_daily_gain_temp.txt file
print("closing temp log file /path/to/temp_file.txt...")
temp_file.close()
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# Open + Write (append) data from VAR to /path/to/main_csv_data_file.csv file, then close the file
print("...opening /path/to/main_csv_data_file.csv...")
write_to_file = open("/path/to/main_csv_data_file.csv", "a")
print("...writing data to /path/to/main_csv_data_file.csv...")
write_to_file.write("TD Ameritrade Daily TOTAL: " + str(curr_time_ct) + "," + str(temp_file_data_number) + "\n")
print("...closing /path/to/main_csv_data_file.csv...")
write_to_file.close()
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# Clean up/remove /path/to/temp_file.txt file once passed data to main CSV
print("...1sec pause... before cleanup")
time.sleep(1)
print("Removing /path/to/temp_file.txt file as a clean up...")
os.remove("/path/to/temp_file.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment