Skip to main content

Strategy Structure

Strategy Class Definition

Each strategy is a Python class that inherits from our base Strategy class:

class MyStrategy(Strategy):

This class contains all the logic, initialization, market response, and cleanup.

MethodWhen It's CalledPurpose
init(self)Once At Strategy StartSetup state, variables, attributes, actions
onNewDay(self)First callback each trading day, immediately after initReload daily datasets, day‑specific flags, reset flags, etc
onCandleClose(self)At completion of each bar for the configured timeframeRun decision logic, place trades, Evaluate indicator signals, call addLeg, squareoff_all, etc.
onEnd(self)End of strategy executionFinal summary, cleanup,persist results, email reports, export to CSV.
minTrigger()Every wall‑clock minute regardless of timeframe.Modify stoploss , trail profit , squareoff
tickTrigger()For every Tick it will run, calculate profit , trail profitOnly available for Live Trading

Strategy Lifecycle Skeleton

from eventManager import Strategy

class Template(Strategy):

def init(self):
# Called once when strategy is initialized
self.daily_trade_count = 0 # Track trades per day

def onNewDay(self):
# Called once at the start of each trading day
self.daily_trade_count = 0 # Reset daily trade counter
self.data_init() # Custom method: load candle data
self.indicator_init() # Custom method: calculate indicators

def minTrigger(self):
# Called every real-time minute, aligned to the clock
if self.candleTime == (15, 15, 0): # 15:15:00
self.squareoff_all() # Exit all open trades before market close

def onCandleClose(self):
# Called after each candle closes (e.g., every 1 min)
if self.position == [] and self.daily_trade_count == 0:
self.addLeg(...) # Place trade if flat and first trade of the day
self.daily_trade_count += 1 # Increment trade count

def onEnd(self):
# Called once when strategy ends (EOD or stopped)
print("Run complete", len(self.trades), "closed trades")


init(self) – Strategy Initialization Hook

When Does It Run?

  • Once per strategy start.
  • In intraday strategies: Once per market day.
  • In positional strategies: Once at the beginning of the multi-day run. The state is pickled and restored automatically on the next session.

Purpose of init():

Think of this as your strategy constructor — use it to:

  • Initialize state variables, counters, flags
  • Prepare dictionaries to manage trade actions
  • Set up persistent containers (lists, dicts, DataFrames)
  • Define configurable parameters
  • Setup internal caches for per-strategy use

onNewDay(self) – Per-Day Strategy Initialization

When Does It Run?

  • Once per trading day, just before the first bar (usually at 09:15).
  • It resets all intraday state, ensuring a clean environment each morning.

Purpose of onNewDay():

  • Clear/reset variables that are valid only for the current trading day
  • Load candle data (data_init)
  • Apply indicators (indicator_init)
  • Reinitialize per-day counters, triggers, buffers
  • Prepare your strategy to evaluate signals afresh

minTrigger(self) – Time-Based Execution Hook

When Does It Run?

Runs every real-time minute, aligned to the wall clock, independent of configured timeframe.

Useful for:

  • Time-based exits (e.g., square-off at 15:15)
  • MTM checks
  • Manual override logic
  • Logs trade status

onCandleClose(self) – Candle-by-Candle Logic

When Does It Run?

  • Runs after each bar closes.

Purpose:

  • Evaluate entry and exit conditions
  • Act on confirmed candle signals
  • Add trade legs, square off positions, or trigger alerts

onEnd(self) – Strategy Termination Hook

When Does It Run?

Runs once at the end of the strategy execution.

Purpose of onEnd():

  • Perform cleanup tasks
  • Log final statistics and summaries
  • Save or export results
  • Release resources if needed
  • Send notifications or reports