```"""Helper to simulate trades without going through the whole backtesting integration."""

import datetime
from decimal import Decimal

from tradeexecutor.strategy.pricing_model import PricingModel

"""

[docs]    def __init__(self,
state: State,
pricing_model: PricingModel):
self.state = state
self.nonce = 1
self.pricing_model = pricing_model

ts: datetime.datetime,
amount_in_usd: Decimal) -> TradeExecution:
"""Buy token (trading pair) for a certain value."""

price_structure = self.pricing_model.get_buy_price(ts, pair, amount_in_usd)
price = price_structure.price

reserve_currency, exchange_rate = self.state.portfolio.get_default_reserve_currency()

ts=datetime.datetime.utcnow(),
pair=pair,
assumed_price=price,
quantity=None,
reserve=amount_in_usd,
reserve_currency=reserve_currency,
reserve_currency_price=1.0)

[docs]    def sell(self, pair: TradingPairIdentifier, quantity: Decimal) -> TradeExecution:
"""Sell token token (trading pair) for a certain quantity."""

reserve_currency, exchange_rate = self.state.portfolio.get_default_reserve_currency()

ts=datetime.datetime.utcnow(),
pair=pair,
assumed_price=1.0,
quantity=-quantity,
reserve=None,
reserve_currency=reserve_currency,
reserve_currency_price=1.0)

def simulate_execution(self, state: State, trade: TradeExecution, price_impact=1):
assert price
assert quantity

# 2. Capital allocation
txid = "0x0"
nonce = self.nonce
self.state.start_execution(ts, trade, txid, nonce)
self.nonce += 1

# 3. Executed
executed_price = price * price_impact
executed_quantity = quantity * Decimal(price_impact)
executed_reserve = Decimal(0)
else:
executed_quantity = quantity
executed_reserve = abs(quantity * Decimal(executed_price))