diff --git a/testy/iptest.py b/testy/iptest.py new file mode 100644 index 0000000..59e474d --- /dev/null +++ b/testy/iptest.py @@ -0,0 +1,36 @@ +import socket +from v2realbot.enums.enums import Env +from v2realbot.config import PROD_SERVER_IPS, TEST_SERVER_IPS + +# def get_server_ip(): +# """Retrieve the current server's IP address.""" +# hostname = socket.gethostname() +# current_ip = socket.gethostbyname(hostname) +# print("Current IP:", current_ip, hostname) +# return current_ip + +def get_environment(): + """Determine if the current server is production or test based on IP.""" + current_ip = get_server_ip() + if current_ip in PROD_SERVER_IPS: + return Env.PROD + else: + return Env.TEST + +def get_server_ip(): + """Get the IP address of the server.""" + try: + # Create a dummy socket and connect to an external address + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.connect(("8.8.8.8", 80)) # Google's DNS server + return s.getsockname()[0] + except Exception as e: + return f"Error: {e}" + +# Test the function +#print(get_server_ip()) + +hostname = socket.gethostname() +print(hostname) +current_ip = socket.gethostbyname(hostname) +print(hostname, current_ip) \ No newline at end of file diff --git a/v2realbot/config.py b/v2realbot/config.py index bd2685a..1cc8402 100644 --- a/v2realbot/config.py +++ b/v2realbot/config.py @@ -2,6 +2,11 @@ from alpaca.data.enums import DataFeed from v2realbot.enums.enums import Mode, Account, FillCondition from appdirs import user_data_dir +#'0.0.0.0', +#currently only prod server has acces to LIVE +PROD_SERVER_HOSTNAMES = ['tradingeastcoast','David-MacBook-Pro.local'] #,'David-MacBook-Pro.local' +TEST_SERVER_HOSTNAMES = ['tradingtestprod'] + #TODO vybrane dat do config db a managovat pres GUI #AGGREGATOR filter trades diff --git a/v2realbot/controller/services.py b/v2realbot/controller/services.py index 1248c71..c3f36bc 100644 --- a/v2realbot/controller/services.py +++ b/v2realbot/controller/services.py @@ -363,8 +363,8 @@ def run_batch_stratin(id: UUID, runReq: RunRequest): if runReq.test_batch_id is None and (runReq.bt_from is None or runReq.bt_from.date() == runReq.bt_to.date()): return (-1, "test interval or different days required for batch run") - if runReq.mode != Mode.BT: - return (-1, "batch run only for backtest") + if runReq.mode not in (Mode.BT, Mode.PREP): + return (-1, "batch run only for backtest/prep") #print("request values:", runReq) diff --git a/v2realbot/enums/enums.py b/v2realbot/enums/enums.py index 2ca465e..d0f4095 100644 --- a/v2realbot/enums/enums.py +++ b/v2realbot/enums/enums.py @@ -1,6 +1,10 @@ from enum import Enum from alpaca.trading.enums import OrderSide, OrderStatus, OrderType +class Env(str, Enum): + PROD = "prod" + TEST = "test" + class TargetTRFM(str, Enum): #ponecha as is KEEPVAL = "keepval" diff --git a/v2realbot/main.py b/v2realbot/main.py index ff94e9f..f8fec3b 100644 --- a/v2realbot/main.py +++ b/v2realbot/main.py @@ -16,6 +16,7 @@ from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Depends, HTTPExcept from fastapi.responses import FileResponse from fastapi.staticfiles import StaticFiles from fastapi.security import HTTPBasic, HTTPBasicCredentials +from v2realbot.enums.enums import Env, Mode from typing import Annotated import os import uvicorn @@ -25,6 +26,7 @@ from threading import Thread import asyncio from v2realbot.common.db import insert_queue, insert_conn, pool from v2realbot.utils.utils import json_serial, send_to_telegram, zoneNY, zonePRG +from v2realbot.utils.sysutils import get_environment from uuid import uuid4 from sqlite3 import OperationalError from time import sleep @@ -225,6 +227,10 @@ def _get_stratin(stratin_id) -> StrategyInstance: @app.put("/stratins/{stratin_id}/run", dependencies=[Depends(api_key_auth)], status_code=status.HTTP_200_OK) def _run_stratin(stratin_id: UUID, runReq: RunRequest): + + if runReq.mode == Mode.LIVE and get_environment() != Env.PROD: + raise HTTPException(status_code=status.HTTP_406_NOT_ACCEPTABLE, detail=f"Live MODE disabled for TEST env") + #print(runReq) if runReq.bt_from is not None and runReq.bt_from.tzinfo is None: runReq.bt_from = zonePRG.localize(runReq.bt_from) @@ -233,7 +239,7 @@ def _run_stratin(stratin_id: UUID, runReq: RunRequest): runReq.bt_to = zonePRG.localize(runReq.bt_to) #pokud jedeme nad test intervaly anebo je požadováno více dní - pouštíme jako batch day by day #do budoucna dát na FE jako flag - if runReq.test_batch_id is not None or (runReq.bt_from.date() != runReq.bt_to.date()): + if runReq.mode != Mode.LIVE and runReq.test_batch_id is not None or (runReq.bt_from.date() != runReq.bt_to.date()): res, id = cs.run_batch_stratin(id=stratin_id, runReq=runReq) else: res, id = cs.run_stratin(id=stratin_id, runReq=runReq) diff --git a/v2realbot/utils/sysutils.py b/v2realbot/utils/sysutils.py new file mode 100644 index 0000000..84af719 --- /dev/null +++ b/v2realbot/utils/sysutils.py @@ -0,0 +1,11 @@ +import socket +from v2realbot.enums.enums import Env +from v2realbot.config import PROD_SERVER_HOSTNAMES, TEST_SERVER_HOSTNAMES + +def get_environment(): + """Determine if the current server is production or test based on hostname.""" + hostname = socket.gethostname() + if hostname in PROD_SERVER_HOSTNAMES: + return Env.PROD + else: + return Env.TEST