This detailed breakdown covers the lifecycle of a trade within the application, from signal parsing through multi-order placement, dynamic TP adjustments, breakeven logic, modification handling, and final closure synchronization. The use of strategies, order groups, and scenario-based adjustments are key complexities.
TELEGRAM_CHANNEL_ID
in the configuration.TelegramClient
monitors the designated channel for new messages (events.NewMessage
).Signal
record with the same message_id
already exists in the database to prevent duplicate processing.Signal
record is created in the database with status = SignalStatus.NEW
, storing the raw message text, channel ID, message ID, and timestamp.parse_signal
function) using a pre-defined prompt (likely fine-tuned, model ID: ft:gpt-4o-mini-2024-07-18:personal:ttmt-gtmo:B7lt3QCz
).ParsedSignal
object. This includes:
action
: The primary intent (e.g., new_trade
, modify_trade
, nothing
).command_type
: A more specific command for modify_trade
actions (e.g., set_breakeven
, adjust_sl
).symbol
: The trading instrument (defaults to config.SYMBOL
if not detected).direction
: buy
(long) or sell
(short).zone
: An optional entry price range (e.g., "1850.5 - 1848.0").zone_top
, zone_bottom
: Numerical values extracted from the zone
.stop_loss
(SL): A specific price level or potentially a descriptive adjustment (like "+20 pips").take_profits
(TPs): A list of potential take profit price levels (e.g., [1860.0, 1875.5]
).tp_open
: A boolean flag indicating if the final TP is open-ended (not explicitly used in placement logic shown, but parsed).risk_level
: 1
(normal) or 2
(high risk), affecting lot size.parsed_data
field of the Signal
record, and its status is updated to SignalStatus.PARSED
.Signal
status is updated to SignalStatus.FAILED
, and the error message is stored in meta_data
.process_new_trades
) periodically polls the database for signals with status = SignalStatus.PARSED
.action == "new_trade"
)status == SignalStatus.PARSED
and action == "new_trade"
is picked up by the trade_processor
.SignalStatus.PROCESSING
.CHANNEL_STRATEGY
configuration setting (e.g., DefaultStrategy
, GTMOStrategy
, BENStrategy
, BENGabrielStrategy
).trade_id
is generated (typically trade_
+ signal_id
).client_id
is generated using the strategy name, symbol, and signal ID for linking broker orders/positions back to the trade.Trade
record is created in the database with status = TradeStatus.OPEN
, linking it to the signal_id
.compute_trade_parameters
method is called.ParsedSignal
data and the current market_price
(fetched from MetaAPI).config.py
(e.g., TP1_DEFAULT
, SL_DEFAULT
) are applied relative to the market price.risk_level
(using DEFAULT_LOT_SIZE
or HIGH_RISK_LOT_SIZE
from config).zone
information.Trade
record.compute_order_definitions
method is called.xa
: Targets TP1xb
: Targets TP2xc
: Targets TP3 (or might be TP2/Open TP depending on strategy)layer_volumes
).zone
(e.g., distributed between zone_top
and zone_bottom
).ORDER_GROUP_2_DISTANCE
).tp_price
) and the corresponding level (tp_level
, e.g., "tp1").TradeExecutor
iterates through the order definitions.create_market_buy_order
or create_market_sell_order
.create_limit_buy_order
or create_limit_sell_order
with their calculated limit prices.client_id
is attached to each order request.Order
record is created in the database, storing details like MetaAPI orderId
, type, direction, volume, price, SL, TP, tp_level
, name
(e.g., "order1a"), and order_group
. The client_id
is also stored. The initial status is OrderStatus.OPEN
.Order
record status is updated to OrderStatus.FILLED
.Position
record is created in the database. It shares the same ID as the filled market order (position_id
== MetaAPI orderId
/positionId
). It stores entry price, volume, SL, TP, tp_level
, name
, order_group
, etc. The status is PositionStatus.OPEN
.position_id
is added to the positions
list in the parent Trade
record.orderId
s is added to the orders
list in the parent Trade
record.filled_order_groups
list in the Trade
record is initialized (typically with [1]
because market orders are filled).Trade
record is updated.