Generated Python and tests
How AI-generated strategy code is structured, what the test file does, and how to read it.
Every Coinrule strategy compiles to a Python file that subclasses Strategy, plus an auto-generated test file required before the engine loads it. This article explains the code structure, how to view it, and how the test file is created.
How is the generated code structured?
Every Coinrule strategy is a Python file that subclasses Strategy from coinrule_x_strategies.core.
from strategy import Strategy, Signal
from coinrule_x_indicators import RSI, EMA
class AIStrategy(Strategy):
def indicators(self):
return {
"rsi": RSI(14),
"ema": EMA(50),
}
def evaluate(self, indicators, position=None):
rsi = indicators["rsi"]
ema = indicators["ema"]
# entry logic
if position is None and rsi < 30 and self.price > ema:
return Signal.OPEN_LONG
# exit logic
if position is not None and rsi > 70:
return Signal.CLOSE_LONG
return Signal.WAITKey points:
indicators()returns a dict of indicator instances imported fromcoinrule_x_indicators.evaluate(indicators, position)is called on each candle close.positionis aPositionContext(orNoneif no position is open).- Do not use instance variables for state that must persist across ticks — use the
PERSIST_ATTRSmechanism instead.
How do you view the generated code?
On the interpret screen, click the code icon to expand the generated Python. On the strategy detail page, the code viewer shows the currently deployed version.
What is the test file?
A test file is generated in the background after you launch a strategy (using claude-sonnet-4-6). The test file is required before the execution engine loads the strategy — if test generation fails, the strategy will not start.
Tests exercise the strategy logic against synthetic candle data using pytest. They run when the engine runner loads the strategy module at startup.
Can you modify the generated code?
The generated code is owned by your strategy config — you cannot directly edit it in the UI. To change the logic, re-describe the strategy and re-parse. Contact support if you need to work with the raw code directly.