symbol1 = "MSFT" symbol2 = "AAPL" clientlowest = 1 clienthighest = 5 Flag to control…
- User
symbol1 = "MSFT" symbol2 = "AAPL" client_lowest = 1 client_highest = 5
Flag to control scheduler execution
development_mode = False # Set to True to pause the scheduler during development
Initialize the stock_data
stock_data = pd.DataFrame()
Initialize the last_run variable
last_run = None
Define the quantity variable
quantity_1 = 1 quantity_2 = 1
Initialize CSV logs
orders_log_file = 'orders_log.csv' positions_log_file = 'positions_log.csv' last_run_file = 'last_run.pkl'
ib = None # Wird beim Start der Sitzung initialisiert
scheduled_time = "16:10" # Geplante Zeit definieren
############################################################################################################## ##############################################################################################################
nest_asyncio.apply()
################################ Definiere einen neuen Log-Level###############################
Definiere neue Log-Levels
TRADING = 25 # Zwischen INFO (20) und WARNING (30) DEAD = 60 # Ein Wert oberhalb CRITICAL (50), um eindeutig zu sein ist in warheit DEAD
Füge die neuen Level zum Logging-Modul hinzu
logging.addLevelName(TRADING, 'TRADING') logging.addLevelName(DEAD, 'FATAL')
Methode für TRADING hinzufügen
def trading(self, message, *args, **kwargs): if self.isEnabledFor(TRADING): self._log(TRADING, message, args, **kwargs)
Methode für FATAL hinzufügen
def dead(self, message, *args, **kwargs): if self.isEnabledFor(DEAD): self._log(DEAD, message, args, **kwargs)
Füge die neuen Methoden zur Logger-Klasse hinzu
logging.Logger.trading = trading logging.Logger.dead = dead
Setup logging
logging.basicConfig( filename='trading_log_file.log', level=logging.INFO, # Setze das Log-Level auf DEBUG für detaillierte Logs format='%(asctime)s:%(levelname)s:%(message)s' ) #####################################################################################################################
Funktion zum Laden des letzten Ausführungszeitpunkts
def load_last_run(): if os.path.exists(last_run_file): with open(last_run_file, 'rb') as f: return pickle.load(f) return None
Funktion zum Speichern des letzten Ausführungszeitpunkts
def save_last_run(timestamp): with open(last_run_file, 'wb') as f: pickle.dump(timestamp, f)
Function to write headers to CSV files if they don't exist or are empty
def initialize_logs(): try: # Check and write header for orders_log_file if not os.path.exists(orders_log_file) or os.path.getsize(orders_log_file) == 0: with open(orders_log_file, mode='w', newline='') as file: writer = csv.writer(file) writer.writerow(['Timestamp', 'MagicNumber', 'TradeID', 'Action', 'Quantity', 'Symbol', 'Status', 'OrderID', 'FillPrice', 'Commission', 'OrderType', 'TIF', 'Exchange'])
- Assistant