Algorithmic Trading & Indicators Advanced

Nifty Options 200 EMA + Supertrend + RSI + VWAP Execution Engine (Single Trade Per Candle) in Python

Production-grade Python execution state machine combining 200 EMA regime, Supertrend(10,3), RSI(14), and VWAP with strict single-trade-per-candle lock.

#pandas#numpy

Problem Statement

In live WebSocket option bots, tick-by-tick price fluctuations inside the same 5-minute bar can accidentally fire 10 duplicate buy orders if a candle-timestamp lock is not enforced alongside the 4-indicator confluence check.

Formula / Core Logic

Long_Condition = (Close > EMA_200) & (Close > VWAP) & (Supertrend == 1) & (RSI > 55) Enforces `last_traded_candle_time != current_candle_time` and `current_position != 1`.

Installation

pip install pandas numpy
supertrend-200-ema-vwap-single-trade-per-candle-bot-python.py
import pandas as pd
import numpy as np

def execute_quad_confluence_backtest(df):
    # Assumes cols: Close, EMA_200, VWAP, ST_Dir (1/-1), RSI
    df = df.copy()
    trades = []
    position = 0
    last_candle_ts = None
    
    for ts, row in df.iterrows():
        if ts == last_candle_ts:
            continue  # Strict single-trade-per-candle guard
            
        bull_ok = (row['Close'] > row['EMA_200']) and (row['Close'] > row['VWAP']) and (row['ST_Dir'] == 1) and (row['RSI'] > 55)
        bear_ok = (row['Close'] < row['EMA_200']) and (row['Close'] < row['VWAP']) and (row['ST_Dir'] == -1) and (row['RSI'] < 45)
        
        if bull_ok and position != 1:
            position = 1
            last_candle_ts = ts
            trades.append({'Time': ts, 'Action': 'BUY_CE', 'Price': row['Close']})
        elif bear_ok and position != -1:
            position = -1
            last_candle_ts = ts
            trades.append({'Time': ts, 'Action': 'BUY_PE', 'Price': row['Close']})
    return pd.DataFrame(trades)

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: Why check both `position != 1` and `ts == last_candle_ts`?

`position != 1` prevents pyramid entries in the same trend direction, while `last_candle_ts` ensures a stop-loss and re-entry never happen on the exact same candle.