{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Param search"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "\n",
    "from t_tech.invest.mock_services import MockedSandboxClient\n",
    "from decimal import Decimal\n",
    "from t_tech.invest.strategies.moving_average.strategy_settings import (\n",
    "    MovingAverageStrategySettings,\n",
    ")\n",
    "from t_tech.invest import CandleInterval, MoneyValue\n",
    "from t_tech.invest.strategies.moving_average.signal_executor import (\n",
    "    MovingAverageSignalExecutor,\n",
    ")\n",
    "from t_tech.invest.strategies.moving_average.supervisor import (\n",
    "    MovingAverageStrategySupervisor,\n",
    ")\n",
    "from t_tech.invest.strategies.moving_average.strategy_state import (\n",
    "    MovingAverageStrategyState,\n",
    ")\n",
    "from t_tech.invest.strategies.moving_average.strategy import MovingAverageStrategy\n",
    "from t_tech.invest.strategies.moving_average.trader import MovingAverageStrategyTrader\n",
    "from datetime import timedelta, datetime, timezone\n",
    "from t_tech.invest.typedefs import ShareId, AccountId\n",
    "from t_tech.invest.strategies.base.account_manager import AccountManager\n",
    "from t_tech.invest.strategies.moving_average.plotter import (\n",
    "    MovingAverageStrategyPlotter,\n",
    ")\n",
    "\n",
    "\n",
    "logging.basicConfig(format=\"%(asctime)s %(levelname)s:%(message)s\", level=logging.INFO)\n",
    "logger = logging.getLogger(__name__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "token = "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "figi = ShareId(\"BBG0013HGFT4\")\n",
    "account_id = AccountId(\"1337007228\")\n",
    "settings = MovingAverageStrategySettings(\n",
    "    share_id=figi,\n",
    "    account_id=account_id,\n",
    "    max_transaction_price=Decimal(10000),\n",
    "    candle_interval=CandleInterval.CANDLE_INTERVAL_1_MIN,\n",
    "    long_period=timedelta(minutes=100),\n",
    "    short_period=timedelta(minutes=50),\n",
    "    std_period=timedelta(minutes=30),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Stocks for date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def start_datetime() -> datetime:\n",
    "    return datetime(year=2022, month=2, day=16, hour=17, tzinfo=timezone.utc)\n",
    "\n",
    "\n",
    "real_market_data_test_from = start_datetime() - timedelta(days=1)\n",
    "real_market_data_test_start = start_datetime()\n",
    "real_market_data_test_end = start_datetime() + timedelta(days=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initial balance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "balance = MoneyValue(currency=\"rub\", units=20050, nano=690000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Trader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 57116 characters]\n",
      "text/plain": [
       "<Figure size 1440x1440 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-03-15 21:00:05,649 INFO:9b1ca1a8bebe24b983bd1cd009ddd572 GetSandboxPortfolio\n",
      "2022-03-15 21:00:05,650 INFO:Balance: 20050.69000000\n",
      "2022-03-15 21:00:05,650 INFO:Refreshing data\n"
     ]
    }
   ],
   "source": [
    "with MockedSandboxClient(\n",
    "    token=token,\n",
    "    balance=balance,\n",
    ") as mocked_services:\n",
    "    account_manager = AccountManager(\n",
    "        services=mocked_services, strategy_settings=settings\n",
    "    )\n",
    "    state = MovingAverageStrategyState()\n",
    "    strategy = MovingAverageStrategy(\n",
    "        settings=settings,\n",
    "        account_manager=account_manager,\n",
    "        state=state,\n",
    "    )\n",
    "    supervisor = MovingAverageStrategySupervisor()\n",
    "    signal_executor = MovingAverageSignalExecutor(\n",
    "        services=mocked_services,\n",
    "        state=state,\n",
    "        settings=settings,\n",
    "    )\n",
    "    moving_average_strategy_trader = MovingAverageStrategyTrader(\n",
    "        strategy=strategy,\n",
    "        settings=settings,\n",
    "        services=mocked_services,\n",
    "        state=state,\n",
    "        signal_executor=signal_executor,\n",
    "        account_manager=account_manager,\n",
    "        supervisor=supervisor,\n",
    "    )\n",
    "    plotter = MovingAverageStrategyPlotter(settings=settings)\n",
    "\n",
    "    initial_balance = account_manager.get_current_balance()\n",
    "\n",
    "    for i in range(50):\n",
    "        logger.info(\"Trade %s\", i)\n",
    "        events = list(supervisor.get_events())\n",
    "        plotter.plot(events)\n",
    "        try:\n",
    "            moving_average_strategy_trader.trade()\n",
    "        except Exception:\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "current_balance = account_manager.get_current_balance()\n",
    "assert initial_balance != current_balance\n",
    "logger.info(\"Initial balance %s\", initial_balance)\n",
    "logger.info(\"Current balance %s\", current_balance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
