from flask import Flask, jsonify, render_template_string from threading import Thread from SmartApi import SmartConnect import pyotp from datetime import datetime, timedelta import time app = Flask(__name__) latest_analysis = {} order_status = None MAX_RETRIES = 3 RETRY_DELAY = 30 PRICE_UPDATE_INTERVAL = 10 def initialize_connection(): try: obj = SmartConnect(api_key="dr7D546c") totp = pyotp.TOTP("5H6OII4J2VB7MDMZDPDEM5U76I").now() obj.generateSession("SOTJ1018", "4772", totp) return obj except: return None def retry_connection(): attempts = 0 while attempts < MAX_RETRIES: obj = initialize_connection() if obj: return obj attempts += 1 time.sleep(RETRY_DELAY) return None def fetch_candle_data(obj, start_time, end_time, symbol_token="99926000"): historic_params = { "exchange": "NSE", "symboltoken": symbol_token, "interval": "FIVE_MINUTE", "fromdate": start_time, "todate": end_time, } try: response = obj.getCandleData(historic_params) if response and "data" in response: return response["data"] return [] except: return [] def analyze_prices(): global latest_analysis, order_status while True: try: current_date = datetime.now().strftime("%Y-%m-%d") start_time = f"{current_date} 09:00" end_time = f"{current_date} 15:25" obj = retry_connection() if not obj: time.sleep(PRICE_UPDATE_INTERVAL) continue candles = fetch_candle_data(obj, start_time, end_time) if not candles: time.sleep(PRICE_UPDATE_INTERVAL) continue formatted_candles = [ {"time": candle[0], "open": candle[1], "high": candle[2], "low": candle[3], "close": candle[4], "volume": candle[5]} for candle in candles ] morning_candles = [ candle for candle in formatted_candles if parse_datetime(candle["time"]).time() >= datetime.strptime("09:00", "%H:%M").time() and parse_datetime(candle["time"]).time() <= datetime.strptime("10:00", "%H:%M").time() ] if not morning_candles: time.sleep(PRICE_UPDATE_INTERVAL) continue morning_high_candle = max(morning_candles, key=lambda x: x["high"]) morning_low_candle = min(morning_candles, key=lambda x: x["low"]) morning_high = morning_high_candle["high"] morning_low = morning_low_candle["low"] confirmation_candles = [ candle for candle in formatted_candles if parse_datetime(candle["time"]).time() > datetime.strptime("10:00", "%H:%M").time() ] if not confirmation_candles: time.sleep(PRICE_UPDATE_INTERVAL) continue confirmation_high = max(candle["high"] for candle in confirmation_candles) confirmation_low = min(candle["low"] for candle in confirmation_candles) entry_price = confirmation_high if confirmation_high > morning_high else confirmation_low entry_time = confirmation_candles[0]["time"] if confirmation_candles else None latest_analysis = { "morning_high": morning_high, "morning_low": morning_low, "confirmation_high": confirmation_high, "confirmation_low": confirmation_low, "entry_price": entry_price, "entry_time": entry_time, "last_updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), } if not order_status and entry_price: order_status = place_order(obj, "99926000", "BUY", entry_price) except: pass time.sleep(PRICE_UPDATE_INTERVAL) def place_order(obj, symbol_token, transaction_type, price): order_params = { "variety": "NORMAL", "tradingsymbol": symbol_token, "symboltoken": symbol_token, "transactiontype": transaction_type, "exchange": "NSE", "ordertype": "LIMIT", "producttype": "INTRADAY", "duration": "DAY", "price": price, "quantity": 1, } try: response = obj.placeOrder(order_params) return response except Exception as e: return f"Order placement failed: {e}" def parse_datetime(datetime_str): if "+" in datetime_str: datetime_str = datetime_str.split("+")[0] return datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%S") @app.route("/") def index(): html = """
Morning High: Loading...
Morning Low: Loading...
Confirmation High: Loading...
Confirmation Low: Loading...
Entry Price: Loading...
Entry Time: Loading...