{"id":265376,"date":"2015-09-10T16:11:03","date_gmt":"2015-09-10T12:11:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=265376"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=265376","title":{"rendered":"\u0421\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0438\u043d\u0433 \u043d\u0430 Python \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c. \u0427\u0430\u0441\u0442\u044c 3"},"content":{"rendered":"<p>       <a href=\"http:\/\/habrahabr.ru\/company\/itinvest\/blog\/266623\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/ecc\/936\/679\/ecc9366797aa47e2a868041d4cd98c7b.png\"\/><\/a><\/p>\n<p>  \u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0438\u043d\u0433\u0430, \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438 <a href=\"http:\/\/habrahabr.ru\/company\/itinvest\/blog\/263097\/\">\u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/a>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u043d\u0435\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0438 \u043e\u0431\u0441\u0443\u0434\u0438\u043b\u0438 \u0442\u043e, \u043a\u0430\u043a \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b <a href=\"http:\/\/habrahabr.ru\/company\/itinvest\/blog\/264141\/\">\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0440\u044b\u043d\u043e\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/a> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u043b\u044f \u00ab\u0436\u0438\u0432\u043e\u0439\u00bb \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430 \u0431\u0438\u0440\u0436\u0435.<\/p>\n<p>  \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043e\u043f\u0438\u0448\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 NaivePortfolio, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0432 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0438\u0445 \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043f\u0440\u0438\u043a\u0430\u0437\u044b \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0430\u043a\u0446\u0438\u0439.<a name=\"habracut\"><\/a><\/p>\n<h4>\u041e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0438 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u043e\u0440\u0434\u0435\u0440\u0430\u043c\u0438<\/h4>\n<p>  \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0440\u0434\u0435\u0440\u0430\u043c\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0435\u0440\u0430. \u0415\u0435 \u0440\u043e\u043b\u044c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u0440\u044b\u043d\u043e\u0447\u043d\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0438 \u0438\u0445 \u0440\u044b\u043d\u043e\u0447\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0438\u0437 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430 \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0435\u0440\u0430, \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u043a\u0432\u0438\u0434\u0430\u0446\u0438\u043e\u043d\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0437\u0438\u0446\u0438\u0438.<\/p>\n<p>  \u041f\u043e\u043c\u0438\u043c\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0444\u0430\u043a\u0442\u043e\u0440\u044b \u0440\u0438\u0441\u043a\u0430 \u0438 \u0443\u043c\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0442\u0435\u0445\u043d\u0438\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u2014 \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0440\u0434\u0435\u0440\u043e\u0432, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u043d\u0430 \u0440\u044b\u043d\u043e\u043a \u0447\u0435\u0440\u0435\u0437 \u0442\u043e\u0440\u0433\u043e\u0432\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0431\u0440\u043e\u043a\u0435\u0440\u0430.<\/p>\n<p>  \u041e\u0431\u044a\u0435\u043a\u0442 <code>Portfolio<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0443\u043c\u0435\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b <code>SignalEvent<\/code>, \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b <code>OrderEvent<\/code> \u0438 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b <code>FillEvent<\/code>, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0437\u0438\u0446\u0438\u0438. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b Portfolio \u043e\u0431\u044b\u0447\u043d\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043e\u0431\u044a\u0435\u043c\u043d\u044b\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0438\u043d\u0442\u0430 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430.<\/p>\n<h4>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h4>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b portfolio.py \u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u2014 \u0442\u0435 \u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0433\u043e \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u0447\u0442\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435. \u041d\u0443\u0436\u043d\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e floor \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 math, \u0447\u0442\u043e\u0431\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043f\u0440\u0438\u043a\u0430\u0437\u044b. \u0422\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u044b <code>FillEvent<\/code> \u0438 <code>OrderEvent<\/code> \u2014 \u043e\u0431\u044a\u0435\u043a\u0442 <code>Portfolio<\/code> \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<pre><code class=\"python\"># portfolio.py  import datetime import numpy as np import pandas as pd import Queue  from abc import ABCMeta, abstractmethod from math import floor  from event import FillEvent, OrderEvent <\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f Portfolio \u0438 \u0434\u0432\u0430 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 <code>update_signal<\/code> \u0438 <code>update_fill<\/code>. \u041f\u0435\u0440\u0432\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u0442\u043e\u0440\u0433\u043e\u0432\u044b\u0435 \u0441\u0438\u0433\u043d\u0430\u043b\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043e\u0431 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0445 \u043e\u0440\u0434\u0435\u0440\u0430\u0445, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u044b\u0445 \u0438\u0437 \u0434\u0432\u0438\u0436\u043a\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430-\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430.<\/p>\n<pre><code class=\"python\"># portfolio.py  class Portfolio(object):     &quot;&quot;&quot;     \u041a\u043b\u0430\u0441\u0441 Portfolio \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0438 \u0440\u044b\u043d\u043e\u0447\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u0441\u0435\u0445 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0431\u0430\u0440\u043e\u0432: \u0441\u0435\u043a\u0443\u043d\u0434\u0430, \u043c\u0438\u043d\u0443\u0442\u0430, 5 \u043c\u0438\u043d\u0443\u0442, 30 \u043c\u0438\u043d, 60 \u043c\u0438\u043d\u0443\u0442 \u0438\u043b\u0438 \u0434\u0435\u043d\u044c.     &quot;&quot;&quot;      __metaclass__ = ABCMeta      @abstractmethod     def update_signal(self, event):         &quot;&quot;&quot;         \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 SignalEvent \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u043e\u0432\u044b\u0445 \u043e\u0440\u0434\u0435\u0440\u043e\u0432 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e.          &quot;&quot;&quot;         raise NotImplementedError(&quot;Should implement update_signal()&quot;)      @abstractmethod     def update_fill(self, event):         &quot;&quot;&quot;         \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0438 \u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0432 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435      FillEvent.          &quot;&quot;&quot;         raise NotImplementedError(&quot;Should implement update_fill()&quot;)  <\/code><\/pre>\n<p>  \u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u0435\u0433\u043e\u0434\u043d\u044f\u0448\u043d\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u2014 \u043a\u043b\u0430\u0441\u0441 <code>NaivePortfolio<\/code>. \u041e\u043d \u0441\u043e\u0437\u0434\u0430\u043d \u0434\u043b\u044f \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0438 \u0437\u0430\u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u043e\u0440\u0433\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043a\u0430\u0437\u043e\u0432 \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043c\u0430\u043d\u0435\u0440\u0435 \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044f \u0438\u0445 \u0432 \u0431\u0440\u043e\u043a\u0435\u0440\u0441\u043a\u0443\u044e \u0442\u043e\u0440\u0433\u043e\u0432\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0430\u043a\u0446\u0438\u0439. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0438\u0440\u0435 \u0432\u0441\u0435 \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u043d\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043f\u043e\u043d\u044f\u0442\u044c, \u043a\u0430\u043a \u0434\u043e\u043b\u0436\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u0440\u0438\u043a\u0430\u0437\u043e\u0432 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u0432 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u0430\u0445.<\/p>\n<p>  <code>NaivePortfolio<\/code> \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u0430\u043f\u0438\u0442\u0430\u043b\u0430 \u2014 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043e\u043d\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0430 \u043d\u0430 $100000. \u0422\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u0434\u0435\u043d\u044c \u0438 \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0431\u043e\u0442\u044b.<\/p>\n<p>  \u041f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 <code>all_positions<\/code> \u0438 <code>current_positions<\/code>. \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0439, \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043a\u0435 \u0440\u044b\u043d\u043e\u0447\u043d\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u0438\u043d\u0430\u043d\u0441\u0432\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430. \u041d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442, \u0447\u0442\u043e \u0430\u043a\u0446\u0438\u0438 \u043f\u0440\u043e\u0434\u0430\u043d\u044b \u00ab\u0432 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e\u00bb. \u0412\u0442\u043e\u0440\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u0430\u0440\u043e\u0432.<\/p>\n<p>  \u0412 \u0434\u043e\u0431\u0430\u0432\u043e\u043a \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u043c \u0437\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u0438, \u0432 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0440\u044b\u043d\u043e\u0447\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 (holdings). \u00ab\u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0440\u044b\u043d\u043e\u0447\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u00bb \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0446\u0435\u043d\u0443 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0431\u0430\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439, \u043d\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0430\u0432\u0434\u043e\u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439 \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. \u042d\u043b\u0435\u043c\u0435\u043d\u0442 <code>all_holdings<\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u0441\u0435\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439, \u0430 <code>current_holdings<\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0441\u0432\u0435\u0436\u0438\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"python\"># portfolio.py  class NaivePortfolio(Portfolio):     &quot;&quot;&quot;      \u041e\u0431\u044a\u0435\u043a\u0442 NaivePortfolio \u0441\u043e\u0437\u0434\u0430\u043d \u0434\u043b\u044f \u0441\u043b\u0435\u043f\u043e\u0439 (\u0442.\u0435. \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u043e\u0433\u043e \u0440\u0438\u0441\u043a-\u043c\u0435\u043d\u0435\u0434\u0436\u043c\u0435\u043d\u0442\u0430)  \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u043f\u0440\u0438\u043a\u0430\u0437\u043e\u0432 \u043d\u0430 \u043f\u043e\u043a\u0443\u043f\u043a\u0443\/\u043f\u0440\u043e\u0434\u0430\u0436\u0443 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u043a\u0446\u0438\u0439, \u0432 \u0431\u0440\u043e\u043a\u0435\u0440\u0441\u043a\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0439 \u0432\u0440\u043e\u0434\u0435 BuyAndHoldStrategy.     &quot;&quot;&quot;          def __init__(self, bars, events, start_date, initial_capital=100000.0):         &quot;&quot;&quot;         \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0438\u0437 \u0431\u0430\u0440\u043e\u0432 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0422\u0430\u043a\u0436\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0430\u0442\u0443 \u0438 \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u0430\u043f\u0438\u0442\u0430\u043b\u0430 (\u0432 \u0434\u043e\u043b\u043b\u0430\u0440\u0430\u0445, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u0430 \u0434\u0440\u0443\u0433\u0430\u044f \u0432\u0430\u043b\u044e\u0442\u0430).          Parameters:         bars - The DataHandler object with current market data.         events - The Event Queue object.         start_date - The start date (bar) of the portfolio.         initial_capital - The starting capital in USD.         &quot;&quot;&quot;         self.bars = bars         self.events = events         self.symbol_list = self.bars.symbol_list         self.start_date = start_date         self.initial_capital = initial_capital                  self.all_positions = self.construct_all_positions()         self.current_positions = dict( (k,v) for k, v in [(s, 0) for s in self.symbol_list] )          self.all_holdings = self.construct_all_holdings()         self.current_holdings = self.construct_current_holdings() <\/code><\/pre>\n<p>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 construct_all_positions \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0444\u0438\u043d\u0430\u043d\u0441\u043e\u0432\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430, \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043d\u043e\u043b\u044c \u0430 \u0437\u0430\u0442\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043b\u044e\u0447 \u0434\u0430\u0442\u044b \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439 Python.<\/p>\n<pre><code class=\"python\"># portfolio.py      def construct_all_positions(self):         &quot;&quot;&quot;         \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0437\u0438\u0446\u0438\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f start_date \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u043c\u0435\u043d\u0442\u0430, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441.         &quot;&quot;&quot;         d = dict( (k,v) for k, v in [(s, 0) for s in self.symbol_list] )         d['datetime'] = self.start_date         return [d] <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0436 <code>construct_all_hldings<\/code> \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435, \u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043b\u044e\u0447\u0438 \u0434\u043b\u044f \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0445 \u0441\u0440\u0435\u0434\u0441\u0442\u0432, \u043a\u043e\u043c\u0438\u0441\u0441\u0438\u0439 \u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0434\u0435\u043d\u0435\u0433 \u043d\u0430 \u0441\u0447\u0435\u0442\u0443 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0441\u0434\u0435\u043b\u043e\u043a, \u043e\u0431\u0449\u0443\u044e \u0443\u043f\u043b\u0430\u0447\u0435\u043d\u043d\u0443\u044e \u043a\u043e\u043c\u0438\u0441\u0441\u0438\u044e \u0438 \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0451\u043c \u0438\u043c\u0435\u044e\u0449\u0438\u0445\u0441\u044f \u0430\u043a\u0442\u0438\u0432\u043e\u0432 (\u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0438 \u0434\u0435\u043d\u044c\u0433\u0438). \u041a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u00ab\u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u044b\u0435\u00bb. \u0412\u0435\u043b\u0438\u0447\u0438\u043d\u044b <code>starting cash<\/code> \u0438 <code>total account<\/code> \u0440\u0430\u0432\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u043a\u0430\u043f\u0438\u0442\u0430\u043b\u0443:<\/p>\n<pre><code class=\"python\"># portfolio.py      def construct_all_holdings(self):         &quot;&quot;&quot;         \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u043b\u0438\u0447\u0438\u043d \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f start_date \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u043c\u0435\u043d\u0442\u0430, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441.         &quot;&quot;&quot;         d = dict( (k,v) for k, v in [(s, 0.0) for s in self.symbol_list] )         d['datetime'] = self.start_date         d['cash'] = self.initial_capital         d['commission'] = 0.0         d['total'] = self.initial_capital         return [d] <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 <code>construct_current_holdings<\/code> \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0435 \u00ab\u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u00bb \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a:<\/p>\n<pre><code class=\"python\"># portfolio.py      def construct_current_holdings(self):         &quot;&quot;&quot;         \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u043f\u043e \u0432\u0441\u0435\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u043c.                &quot;&quot;&quot;         d = dict( (k,v) for k, v in [(s, 0.0) for s in self.symbol_list] )         d['cash'] = self.initial_capital         d['commission'] = 0.0         d['total'] = self.initial_capital         return d <\/code><\/pre>\n<p>  \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u00ab\u0443\u0434\u0430\u0440\u0435 \u0441\u0435\u0440\u0434\u0446\u0430\u00bb, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0440\u044b\u043d\u043e\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>DataHandler<\/code>, \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0440\u044b\u043d\u043e\u0447\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439. \u0412 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0442\u0440\u0435\u0439\u0434\u0438\u043d\u0433\u0430 \u044d\u0442\u0443 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0430\u0442\u044c \u0438 \u043f\u0430\u0440\u0441\u0438\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0437 \u0431\u0440\u043e\u043a\u0435\u0440\u0441\u043a\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u043d\u043e \u0434\u043b\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0438\u043d\u0433\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0438\u0437-\u0437\u0430 \u0441\u043f\u0440\u0435\u0434\u043e\u0432 \u0431\u0438\u0434\u043e\u0432\/\u0430\u0441\u043a\u043e\u0432 \u0438 \u043b\u0438\u043a\u0432\u0438\u0434\u043d\u043e\u0441\u0442\u0438, \u0442\u0430\u043a\u043e\u0439 \u0432\u0435\u0449\u0438, \u043a\u0430\u043a \u00ab\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0440\u044b\u043d\u043e\u0447\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u00bb \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0435\u0433\u043e \u043e\u0446\u0435\u043d\u0438\u0432\u0430\u0442\u044c \u0443\u043c\u043d\u043e\u0436\u0430\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0430\u043a\u0442\u0438\u0432\u0430 \u043d\u0430 \u00ab\u0446\u0435\u043d\u0443\u00bb. \u0412 \u043d\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0446\u0435\u043d\u0430 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0431\u0430\u0440\u0430.\u0414\u043b\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0439 \u0442\u043e\u0440\u0433\u043e\u0432\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u043d\u044f \u044d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0440\u0433\u043e\u0432\u043b\u0438 \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043a\u0430\u0445 \u0431\u043e\u043b\u044c\u0448\u0435 \u0434\u043d\u044f, \u0432\u0441\u0435 \u0443\u0436\u0435 \u043d\u0435 \u0441\u0442\u043e\u043b\u044c \u043f\u0440\u0430\u0432\u0434\u043e\u043f\u043e\u0434\u043e\u0431\u043d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0446\u0435\u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0446\u0435\u043d\u044b \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0431\u0430\u0440\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 <code>update_timeindex<\/code> \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u043e\u0432\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439. \u041e\u043d \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0446\u0435\u043d\u044b \u0438\u0437 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0440\u044b\u043d\u043e\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438, \u0438 \u043f\u0440\u0438\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044f \u00ab\u043d\u043e\u0432\u044b\u0435\u00bb \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043a \u00ab\u0442\u0435\u043a\u0443\u0449\u0438\u043c\u00bb \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u043c. \u042d\u0442\u0430 \u0441\u0445\u0435\u043c\u0430 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 <code>FillEvent<\/code>. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u043a \u0441\u043f\u0438\u0441\u043a\u0443 <code>all_positions<\/code>. \u0417\u0430\u0442\u0435\u043c \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0445\u043e\u0436\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0440\u044b\u043d\u043e\u0447\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u043d\u0430 \u0446\u0435\u043d\u0443 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0431\u0430\u0440\u0430 (<code>self.current_positions[s] * bars[s][0][5]<\/code>). \u041d\u043e\u0432\u044b\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a \u0441\u043f\u0438\u0441\u043a\u0443 <code>all_holdings<\/code>:<\/p>\n<pre><code class=\"python\"># portfolio.py      def update_timeindex(self, event):         &quot;&quot;&quot;         \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0431\u0430\u0440\u0430 \u0440\u044b\u043d\u043e\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u041f\u0420\u0415\u0414\u042b\u0414\u0423\u0429\u0418\u0419 \u0431\u0430\u0440, \u0442.\u0435. \u043d\u0430 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0434\u0438\u0438 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u0432\u0441\u0435 \u0440\u044b\u043d\u043e\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (OLHCVI). \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f MarketEvent \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\u0439 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.          &quot;&quot;&quot;         bars = {}         for sym in self.symbol_list:             bars[sym] = self.bars.get_latest_bars(sym, N=1)          # Update positions         dp = dict( (k,v) for k, v in [(s, 0) for s in self.symbol_list] )         dp['datetime'] = bars[self.symbol_list[0]][0][1]          for s in self.symbol_list:             dp[s] = self.current_positions[s]          # Append the current positions         self.all_positions.append(dp)          # Update holdings         dh = dict( (k,v) for k, v in [(s, 0) for s in self.symbol_list] )         dh['datetime'] = bars[self.symbol_list[0]][0][1]         dh['cash'] = self.current_holdings['cash']         dh['commission'] = self.current_holdings['commission']         dh['total'] = self.current_holdings['cash']          for s in self.symbol_list:             # Approximation to the real value             market_value = self.current_positions[s] * bars[s][0][5]             dh[s] = market_value             dh['total'] += market_value          # Append the current holdings         self.all_holdings.append(dh) <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 <code>update_positions_from_fill<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a\u0430\u043a\u0438\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0431\u044b\u043b <code>FillEvent<\/code> (\u043f\u043e\u043a\u0443\u043f\u043a\u0430 \u0438\u043b\u0438 \u043f\u0440\u043e\u0434\u0430\u0436\u0430), \u0430 \u0437\u0430\u0442\u0435\u043c \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c current_positions, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u043a\u0446\u0438\u0439:<\/p>\n<pre><code class=\"python\"># portfolio.py      def update_positions_from_fill(self, fill):         &quot;&quot;&quot;         \u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 FillEvent \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u043e\u0442\u0440\u0430\u0436\u0430\u043b\u0430 \u043d\u043e\u0432\u044b\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438.                 Parameters:         fill - The FillEvent object to update the positions with.         &quot;&quot;&quot;         # Check whether the fill is a buy or sell         fill_dir = 0         if fill.direction == 'BUY':             fill_dir = 1         if fill.direction == 'SELL':             fill_dir = -1          #\u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438         self.current_positions[fill.symbol] += fill_dir*fill.quantity <\/code><\/pre>\n<p>  \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <code>update_holdings_from_fill<\/code> \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435, \u043d\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 <i>holdings<\/i>. \u0414\u043b\u044f \u0441\u0438\u043c\u0443\u043b\u044f\u0446\u0438\u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u043c\u0435\u0442\u043e\u0434 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0446\u0435\u043d\u0443, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441 <code>FillEvent<\/code>. \u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0438\u0445\u043e\u0434\u0438\u0442? \u0412 \u0441\u0440\u0435\u0434\u0435 \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0438\u043d\u0433\u0430 \u0446\u0435\u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0446\u0435\u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u00ab\u0442\u0435\u043a\u0443\u0449\u0430\u044f \u0440\u044b\u043d\u043e\u0447\u043d\u0430\u044f \u0446\u0435\u043d\u0430\u00bb (\u0446\u0435\u043d\u0430 \u0437\u0430\u043a\u0440\u044b\u0442\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0431\u0430\u0440\u0430). \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430 \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u0438\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a \u0446\u0435\u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u043d\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0446\u0435\u043d\u043d\u044b\u0445 \u0431\u0443\u043c\u0430\u0433 \u0432 \u043e\u0440\u0434\u0435\u0440\u0435.<\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0446\u0435\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 holdings, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u0438 \u043e\u0431\u0449\u0438\u0445 \u0432\u0435\u043b\u0438\u0447\u0438\u043d \u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c\u0441\u044f. \u0422\u0430\u043a\u0436\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u0449\u0430\u044f \u043a\u043e\u043c\u0438\u0441\u0441\u0438\u044f:<\/p>\n<pre><code class=\"python\"># portfolio.py      def update_holdings_from_fill(self, fill):         &quot;&quot;&quot;         \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 FillEvent \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 holdings \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.      \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:         fill - \u041e\u0431\u044a\u0435\u043a\u0442 FillEvent, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0439.         &quot;&quot;&quot;         # Check whether the fill is a buy or sell         fill_dir = 0         if fill.direction == 'BUY':             fill_dir = 1         if fill.direction == 'SELL':             fill_dir = -1          # Update holdings list with new quantities         fill_cost = self.bars.get_latest_bars(fill.symbol)[0][5]  # Close price         cost = fill_dir * fill_cost * fill.quantity         self.current_holdings[fill.symbol] += cost         self.current_holdings['commission'] += fill.commission         self.current_holdings['cash'] -= (cost + fill.commission)         self.current_holdings['total'] -= (cost + fill.commission) <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <code>update_fill<\/code> \u0438\u0437 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0433\u043e \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 <code>Portfolio<\/code>. \u041e\u043d \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0434\u0432\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u0430 <code>update_positions_from_fill<\/code> \u0438 <code>update_holdings_from_fill<\/code>:<\/p>\n<pre><code class=\"python\"># portfolio.py      def update_fill(self, event):         &quot;&quot;&quot;         \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0432 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u0438 \u0438\u0445 \u0440\u044b\u043d\u043e\u0447\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 FillEvent.               &quot;&quot;&quot;         if event.type == 'FILL':             self.update_positions_from_fill(event)             self.update_holdings_from_fill(event)  <\/code><\/pre>\n<p>  \u041e\u0431\u044a\u0435\u043a\u0442 Portfolio \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u0440\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u044f <code>FillEvent<\/code>, \u043d\u043e \u0435\u0449\u0435 \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>OrderEvent<\/code> \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0441\u0438\u0433\u043d\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 <code>SignalEvent<\/code>. \u041c\u0435\u0442\u043e\u0434 generate_naive_order \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u0438\u0433\u043d\u0430\u043b \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u0438\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0444\u0438\u043d\u0430\u043d\u0441\u043e\u0432\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0438 \u0437\u0430\u0442\u0435\u043c \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043e\u0440\u0434\u0435\u0440 \u0441\u043e 100 \u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0430\u043a\u0442\u0438\u0432\u0430. 100 \u0437\u0434\u0435\u0441\u044c \u2014 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0412 \u0445\u043e\u0434\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0442\u043e\u0440\u0433\u043e\u0432\u043b\u0438 \u043e\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e \u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439 \u0440\u0438\u0441\u043a-\u043c\u0435\u043d\u0435\u0434\u0436\u043c\u0435\u043d\u0442\u0430 \u0438\u043b\u0438 \u043c\u043e\u0434\u0443\u043b\u0435\u043c \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b \u043f\u043e\u0437\u0438\u0446\u0438\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u0432 <code>NaivePortfolio<\/code> \u043c\u043e\u0436\u043d\u043e \u00ab\u043d\u0430\u0438\u0432\u043d\u043e\u00bb \u043f\u043e\u0441\u044b\u043b\u0430\u0442\u044c \u043f\u0440\u0438\u043a\u0430\u0437\u044b \u043f\u0440\u044f\u043c\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u043e\u0433\u043e \u0440\u0438\u0441\u043a-\u043c\u0435\u043d\u0435\u0434\u0436\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u0434\u043b\u0438\u043d\u043d\u044b\u0445 \u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432\u044b\u0445\u043e\u0434 \u0438\u0437 \u043d\u0438\u0445 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0444\u0438\u043d\u0430\u043d\u0441\u043e\u0432\u043e\u0433\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430. \u0417\u0430\u0442\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 <code>OrderEvent<\/code>:<\/p>\n<pre><code class=\"python\"># portfolio.py      def generate_naive_order(self, signal):         &quot;&quot;&quot;         \u041f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 OrderEvent \u043a\u0430\u043a \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0430\u043a\u0446\u0438\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0438\u0433\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0431\u0435\u0437 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0440\u0438\u0441\u043a\u043e\u0432.                 \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:         signal - \u0421\u0438\u0433\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f SignalEvent.         &quot;&quot;&quot;         order = None          symbol = signal.symbol         direction = signal.signal_type         strength = signal.strength          mkt_quantity = floor(100 * strength)         cur_quantity = self.current_positions[symbol]         order_type = 'MKT'          if direction == 'LONG' and cur_quantity == 0:             order = OrderEvent(symbol, order_type, mkt_quantity, 'BUY')         if direction == 'SHORT' and cur_quantity == 0:             order = OrderEvent(symbol, order_type, mkt_quantity, 'SELL')                 if direction == 'EXIT' and cur_quantity &gt; 0:             order = OrderEvent(symbol, order_type, abs(cur_quantity), 'SELL')         if direction == 'EXIT' and cur_quantity &lt; 0:             order = OrderEvent(symbol, order_type, abs(cur_quantity), 'BUY')         return order <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 <code>update_signal<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u043c\u0435\u0442\u043e\u0434 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043a\u0430\u0437 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<pre><code class=\"python\"># portfolio.py      def update_signal(self, event):         &quot;&quot;&quot;         \u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 SignalEvent \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043a\u0430\u0437\u044b \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043b\u043e\u0433\u0438\u043a\u043e\u0439 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e.                &quot;&quot;&quot;         if event.type == 'SIGNAL':             order_event = self.generate_naive_order(event)             self.events.put(order_event) <\/code><\/pre>\n<p>  \u0424\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432 <code>NaivePortfolio<\/code> \u2014 \u044d\u0442\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u0440\u0438\u0432\u043e\u0439 \u043a\u0430\u043f\u0438\u0442\u0430\u043b\u0430. \u0421\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0442\u043e\u043a \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043e \u043f\u0440\u0438\u0431\u044b\u043b\u0438, \u0447\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0434\u043b\u044f \u0440\u0430\u0441\u0447\u0435\u0442\u043e\u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438, \u0437\u0430\u0442\u0435\u043c \u043a\u0440\u0438\u0432\u0430\u044f \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u043d\u0442\u043d\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u0435. \u041f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0441\u0447\u0435\u0442\u0430 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u043d\u044b\u043c 1.0:<\/p>\n<pre><code class=\"python\"># portfolio.py      def create_equity_curve_dataframe(self):         &quot;&quot;&quot;         \u0421\u043e\u0437\u0434\u0430\u0435\u0442 pandas DataFrame \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439 all_holdings.          &quot;&quot;&quot;         curve = pd.DataFrame(self.all_holdings)         curve.set_index('datetime', inplace=True)         curve['returns'] = curve['total'].pct_change()         curve['equity_curve'] = (1.0+curve['returns']).cumprod()         self.equity_curve = curve <\/code><\/pre>\n<p>  \u041e\u0431\u044a\u0435\u043a\u0442 <code>Portfolio<\/code> \u2014 \u044d\u0442\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0430\u0441\u043f\u0435\u043a\u0442 \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0435\u0440\u0430. \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0437\u0434\u0435\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435. <\/p>\n<p>  \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0447\u0430\u0441\u0442\u044c \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u2014 \u043e\u0431\u044a\u0435\u043a\u0442 <code>ExecutionHandler<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b <code>OrderEvent<\/code> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438\u0437 \u043d\u0438\u0445 <code>FillEvent<\/code>.<\/p>\n<p>  <i>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442\u2026<\/i><\/p>\n<p>  P. S. \u0420\u0430\u043d\u0435\u0435 \u0432 \u043d\u0430\u0448\u0435\u043c \u0431\u043b\u043e\u0433\u0435 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435 \u043c\u044b \u0443\u0436\u0435 <a href=\"http:\/\/habrahabr.ru\/company\/itinvest\/blog\/224353\/\">\u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b\u0438<\/a> \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u044d\u0442\u0430\u043f\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0442\u043e\u0440\u0433\u043e\u0432\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c. <a href=\"http:\/\/www.itinvest.ru\/promo\/adv\/\">ITinvest<\/a> \u0438 \u043d\u0430\u0448\u0438 \u043f\u0430\u0440\u0442\u043d\u0435\u0440\u044b \u043f\u0440\u043e\u0432\u043e\u0434\u044f\u0442 <a href=\"http:\/\/www.itinvest.ru\/education\/schedule\/\">\u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441\u044b<\/a> \u043f\u043e \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0435.       <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/266623\/\"> http:\/\/habrahabr.ru\/post\/266623\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       <a href=\"http:\/\/habrahabr.ru\/company\/itinvest\/blog\/266623\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/ecc\/936\/679\/ecc9366797aa47e2a868041d4cd98c7b.png\"\/><\/a><\/p>\n<p>  \u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0431\u044d\u043a\u0442\u0435\u0441\u0442\u0438\u043d\u0433\u0430, \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438 <a href=\"http:\/\/habrahabr.ru\/company\/itinvest\/blog\/263097\/\">\u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043a\u043b\u0430\u0441\u0441\u043e\u0432<\/a>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u043d\u0435\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0438 \u043e\u0431\u0441\u0443\u0434\u0438\u043b\u0438 \u0442\u043e, \u043a\u0430\u043a \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b <a href=\"http:\/\/habrahabr.ru\/company\/itinvest\/blog\/264141\/\">\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0440\u044b\u043d\u043e\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/a> \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0434\u043b\u044f \u00ab\u0436\u0438\u0432\u043e\u0439\u00bb \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430 \u0431\u0438\u0440\u0436\u0435.<\/p>\n<p>  \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043e\u043f\u0438\u0448\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 NaivePortfolio, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0432 \u043f\u043e\u0440\u0442\u0444\u043e\u043b\u0438\u043e \u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0438\u0445 \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043f\u0440\u0438\u043a\u0430\u0437\u044b \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0430\u043a\u0446\u0438\u0439.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-265376","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/265376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=265376"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/265376\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=265376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=265376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=265376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}