Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
abbass2 committed Oct 29, 2023
1 parent 3020270 commit 64d807a
Show file tree
Hide file tree
Showing 12 changed files with 771 additions and 245 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ for:
- mypy --ignore-missing-imports pyqstrat/
- flake8 --ignore W291,W293,W503,E402,E701,E275,E741 --max-line-length=160 pyqstrat/
# run notebooks and exit on first error
- find pyqstrat/notebooks -name '*.ipynb' | xargs -n1 sh -c 'ipython $0 || exit 255'
- find pyqstrat/notebooks -name '*.ipynb' | xargs -n1 sh -c 'ipython --no-automagic $0 || exit 255'

-
matrix:
Expand Down
9 changes: 6 additions & 3 deletions docs/_modules/pyqstrat/evaluator.html
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ <h1>Source code for pyqstrat.evaluator</h1><div class="highlight"><pre>

<div class="viewcode-block" id="plot_return_metrics">
<a class="viewcode-back" href="../../pyqstrat.html#pyqstrat.evaluator.plot_return_metrics">[docs]</a>
<span class="k">def</span> <span class="nf">plot_return_metrics</span><span class="p">(</span><span class="n">metrics</span><span class="p">:</span> <span class="nb">dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">],</span> <span class="n">title</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">show_points</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">show</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">go</span><span class="o">.</span><span class="n">Figure</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">plot_return_metrics</span><span class="p">(</span><span class="n">metrics</span><span class="p">:</span> <span class="nb">dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Any</span><span class="p">],</span> <span class="n">title</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">show_points</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">show</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">go</span><span class="o">.</span><span class="n">Figure</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Plot equity, rolling drawdowns and and a boxplot of annual returns given the output of compute_return_metrics.</span>
<span class="sd"> </span>
Expand Down Expand Up @@ -758,6 +758,8 @@ <h1>Source code for pyqstrat.evaluator</h1><div class="highlight"><pre>
<span class="n">fig</span><span class="o">.</span><span class="n">add_trace</span><span class="p">(</span><span class="n">go</span><span class="o">.</span><span class="n">Box</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">all_rets</span><span class="p">,</span> <span class="n">boxmean</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">marker_color</span><span class="o">=</span><span class="s1">&#39;blue&#39;</span><span class="p">,</span> <span class="n">line_color</span><span class="o">=</span><span class="s1">&#39;blue&#39;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;All&#39;</span><span class="p">),</span> <span class="n">row</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">col</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="n">show_points</span><span class="p">:</span>
<span class="n">fig</span><span class="o">.</span><span class="n">update_traces</span><span class="p">(</span><span class="n">boxpoints</span><span class="o">=</span><span class="s1">&#39;all&#39;</span><span class="p">,</span> <span class="n">jitter</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">row</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">col</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">fig</span><span class="o">.</span><span class="n">update_traces</span><span class="p">(</span><span class="n">boxpoints</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">row</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">col</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="n">fig</span><span class="o">.</span><span class="n">update_yaxes</span><span class="p">(</span><span class="n">title_text</span><span class="o">=</span><span class="s2">&quot;Equity&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;log&quot;</span><span class="p">,</span> <span class="n">row</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">col</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">fig</span><span class="o">.</span><span class="n">update_yaxes</span><span class="p">(</span><span class="n">title_text</span><span class="o">=</span><span class="s2">&quot;Drawdown&quot;</span><span class="p">,</span> <span class="n">row</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">col</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
Expand All @@ -780,8 +782,9 @@ <h1>Source code for pyqstrat.evaluator</h1><div class="highlight"><pre>

<span class="n">ev</span> <span class="o">=</span> <span class="n">compute_return_metrics</span><span class="p">(</span><span class="n">timestamps</span><span class="p">,</span> <span class="n">rets</span><span class="p">,</span> <span class="n">starting_equity</span><span class="p">)</span>
<span class="n">display_return_metrics</span><span class="p">(</span><span class="n">ev</span><span class="o">.</span><span class="n">metrics</span><span class="p">())</span>
<span class="n">plot_return_metrics</span><span class="p">(</span><span class="n">ev</span><span class="o">.</span><span class="n">metrics</span><span class="p">())</span>

<span class="n">plot_return_metrics</span><span class="p">(</span><span class="n">ev</span><span class="o">.</span><span class="n">metrics</span><span class="p">(),</span> <span class="n">show_points</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">plot_return_metrics</span><span class="p">(</span><span class="n">ev</span><span class="o">.</span><span class="n">metrics</span><span class="p">(),</span> <span class="n">show_points</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

<span class="n">assert_</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">ev</span><span class="o">.</span><span class="n">metric</span><span class="p">(</span><span class="s1">&#39;sharpe&#39;</span><span class="p">),</span> <span class="mi">6</span><span class="p">)</span> <span class="o">==</span> <span class="mf">2.932954</span><span class="p">)</span>
<span class="n">assert_</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">ev</span><span class="o">.</span><span class="n">metric</span><span class="p">(</span><span class="s1">&#39;sortino&#39;</span><span class="p">),</span> <span class="mi">6</span><span class="p">)</span> <span class="o">==</span> <span class="mf">5.690878</span><span class="p">)</span>
<span class="n">assert_</span><span class="p">(</span><span class="n">ev</span><span class="o">.</span><span class="n">metric</span><span class="p">(</span><span class="s1">&#39;annual_returns&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="p">[</span><span class="mi">2018</span><span class="p">])</span>
Expand Down
40 changes: 36 additions & 4 deletions docs/_modules/pyqstrat/pq_types.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ <h1>Source code for pyqstrat.pq_types</h1><div class="highlight"><pre>
<span class="k">return</span> <span class="n">ContractGroup</span><span class="o">.</span><span class="n">_instances</span><span class="p">[</span><span class="n">name</span><span class="p">]</span></div>


<div class="viewcode-block" id="ContractGroup.get_default">
<a class="viewcode-back" href="../../pyqstrat.html#pyqstrat.pq_types.ContractGroup.get_default">[docs]</a>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">get_default</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="n">ContractGroup</span><span class="p">:</span>
<span class="k">return</span> <span class="n">DEFAULT_CG</span></div>


<div class="viewcode-block" id="ContractGroup.exists">
<a class="viewcode-back" href="../../pyqstrat.html#pyqstrat.pq_types.ContractGroup.exists">[docs]</a>
<span class="nd">@staticmethod</span>
Expand Down Expand Up @@ -107,8 +114,14 @@ <h1>Source code for pyqstrat.pq_types</h1><div class="highlight"><pre>
<a class="viewcode-back" href="../../pyqstrat.html#pyqstrat.pq_types.ContractGroup.clear_cache">[docs]</a>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">clear_cache</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">ContractGroup</span><span class="o">.</span><span class="n">contracts</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">ContractGroup</span><span class="o">.</span><span class="n">contracts</span> <span class="o">=</span> <span class="p">{}</span></div>
<span class="n">ContractGroup</span><span class="o">.</span><span class="n">_instances</span> <span class="o">=</span> <span class="p">{}</span></div>


<div class="viewcode-block" id="ContractGroup.clear">
<a class="viewcode-back" href="../../pyqstrat.html#pyqstrat.pq_types.ContractGroup.clear">[docs]</a>
<span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&#39;&#39;&#39;Remove all contracts&#39;&#39;&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">contracts</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span></div>


<span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
Expand Down Expand Up @@ -198,6 +211,22 @@ <h1>Source code for pyqstrat.pq_types</h1><div class="highlight"><pre>
<span class="k">return</span> <span class="n">Contract</span><span class="o">.</span><span class="n">_instances</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">)</span></div>


<div class="viewcode-block" id="Contract.get_or_create">
<a class="viewcode-back" href="../../pyqstrat.html#pyqstrat.pq_types.Contract.get_or_create">[docs]</a>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">get_or_create</span><span class="p">(</span><span class="n">symbol</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
<span class="n">contract_group</span><span class="p">:</span> <span class="n">ContractGroup</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">expiry</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">multiplier</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">1.</span><span class="p">,</span>
<span class="n">components</span><span class="p">:</span> <span class="nb">list</span><span class="p">[</span><span class="nb">tuple</span><span class="p">[</span><span class="n">Contract</span><span class="p">,</span> <span class="nb">float</span><span class="p">]]</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">properties</span><span class="p">:</span> <span class="n">SimpleNamespace</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Contract</span><span class="p">:</span>
<span class="k">if</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="n">Contract</span><span class="o">.</span><span class="n">_instances</span><span class="p">:</span>
<span class="n">contract</span> <span class="o">=</span> <span class="n">Contract</span><span class="o">.</span><span class="n">_instances</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">symbol</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">contract</span> <span class="o">=</span> <span class="n">Contract</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">symbol</span><span class="p">,</span> <span class="n">contract_group</span><span class="p">,</span> <span class="n">expiry</span><span class="p">,</span> <span class="n">multiplier</span><span class="p">,</span> <span class="n">components</span><span class="p">,</span> <span class="n">properties</span><span class="p">)</span>
<span class="k">return</span> <span class="n">contract</span> <span class="c1"># type: ignore</span></div>


<div class="viewcode-block" id="Contract.clear_cache">
<a class="viewcode-back" href="../../pyqstrat.html#pyqstrat.pq_types.Contract.clear_cache">[docs]</a>
<span class="nd">@staticmethod</span>
Expand Down Expand Up @@ -387,8 +416,11 @@ <h1>Source code for pyqstrat.pq_types</h1><div class="highlight"><pre>
<span class="nd">@dataclass</span><span class="p">(</span><span class="n">kw_only</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MarketOrder</span><span class="p">(</span><span class="n">Order</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__post_init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isfinite</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">qty</span><span class="p">)</span> <span class="ow">or</span> <span class="n">math</span><span class="o">.</span><span class="n">isclose</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">qty</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;order qty must be finite and nonzero: </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">qty</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isfinite</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">qty</span><span class="p">)</span> <span class="ow">or</span> <span class="n">math</span><span class="o">.</span><span class="n">isclose</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">qty</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;order qty must be finite and nonzero: </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">qty</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
<span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">ex</span><span class="p">)</span>

<span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">timestamp</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">timestamp</span><span class="p">)</span><span class="o">.</span><span class="n">to_pydatetime</span><span class="p">()</span>
Expand Down
Loading

0 comments on commit 64d807a

Please sign in to comment.