<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Share the state between iterations]]></title><description><![CDATA[<p dir="auto">The current implementation of the backtester is stateless and there is no way to share context (variables) between iterations. But some strategies need it. We will provide a solution.</p>
]]></description><link>http://quantiacs.com/community/topic/15/share-the-state-between-iterations</link><generator>RSS for Node</generator><lastBuildDate>Fri, 13 Mar 2026 11:49:24 GMT</lastBuildDate><atom:link href="http://quantiacs.com/community/topic/15.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Mar 2021 22:21:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Share the state between iterations on Tue, 27 Apr 2021 20:46:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/12">@support</a><br />
ok I did what <a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/20">@antinomy</a> said below and the 'state' strategy worked.<br />
<a href="https://quantiacs.com/community/topic/46/macroeconomic-data-with-quantiacs/3?_=1619554449031" rel="nofollow ugc">https://quantiacs.com/community/topic/46/macroeconomic-data-with-quantiacs/3?_=1619554449031</a></p>
<p dir="auto">However, it broke my old strategies. So he further suggested to:<br />
<a href="https://quantiacs.com/community/topic/46/macroeconomic-data-with-quantiacs/5?_=1619556376479" rel="nofollow ugc">https://quantiacs.com/community/topic/46/macroeconomic-data-with-quantiacs/5?_=1619556376479</a><br />
And my old strategies are running again.</p>
<p dir="auto">Ok, so looks like for now for all strategies without a state I have to output and pass None to weights for the state &amp; pass a state variable to the strategy.<br />
Will keep you updated. Thanks for looking into the issue.</p>
]]></description><link>http://quantiacs.com/community/post/213</link><guid isPermaLink="true">http://quantiacs.com/community/post/213</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Tue, 27 Apr 2021 20:46:58 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Tue, 27 Apr 2021 20:22:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/12">@support</a><br />
I am working in the Quantiacs online env in my account, why doesn't it catch the latest libraries?<br />
Anyway, I created a new Jupyter notebook with the strategy as suggested and it still does NOT work, same error msg.</p>
]]></description><link>http://quantiacs.com/community/post/212</link><guid isPermaLink="true">http://quantiacs.com/community/post/212</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Tue, 27 Apr 2021 20:22:19 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Tue, 27 Apr 2021 18:58:48 GMT]]></title><description><![CDATA[<p dir="auto">Hello.</p>
<p dir="auto">I guess there is a warning on the strategy <strong>"The strategy uses outdated libraries. Clone it to update the libraries."</strong></p>
<p dir="auto">Just clone this strategy, it will create a new environment with the latest library.</p>
<p dir="auto">Regards.</p>
]]></description><link>http://quantiacs.com/community/post/211</link><guid isPermaLink="true">http://quantiacs.com/community/post/211</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Tue, 27 Apr 2021 18:58:48 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Tue, 27 Apr 2021 14:26:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/12">@support</a><br />
Sorry friend, that still does not work.<br />
I am working in the Quantiacs online cloud Jupyter environment.<br />
I am NOT working locally.<br />
I copied your example template as is and just added load_data().<br />
Below is what I am running:</p>
<pre><code>import xarray as xr
import numpy as np
import qnt.data as qndata
import qnt.backtester as qnbt

def load_data(period):
    data = qndata.futures_load_data(tail=period)
    return data

def strategy(data, state):
    close = data.sel(field="close")
    last_close = close.ffill('time').isel(time=-1)

    # state may be null, so define a default value
    if state is None:
        state = {
            "ma": last_close,
            "ma_prev": last_close,
            "output": xr.zeros_like(last_close)
        }

    ma_prev = state['ma']
    ma_prev_prev = state['ma_prev']
    output_prev = state['output']

    # align the arrays to prevent problems in case the asset list changes
    ma_prev, ma_prev_prev, last_close = xr.align(ma_prev, ma_prev_prev, last_close, join='right') 

    ma = ma_prev.where(np.isfinite(ma_prev), last_close) * 0.97 + last_close * 0.03

    buy_signal = np.logical_and(ma &gt; ma_prev, ma_prev &gt; ma_prev_prev)
    stop_signal = ma &lt; ma_prev_prev

    output = xr.where(buy_signal, 1, output_prev)
    output = xr.where(stop_signal, 0, output)

    next_state = {
        "ma": ma,
        "ma_prev": ma_prev,
        "output": output
    }
    return output, next_state


weights, state = qnbt.backtest(
    competition_type="futures",  # Futures contest
    lookback_period=365,  # lookback in calendar days
    start_date="2006-01-01",
    strategy=strategy,
    analyze=True,
    build_plots=True,
    collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states
)
</code></pre>
<p dir="auto">Error I'm getting is:</p>
<pre><code>---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-1-434dffafaefc&gt; in &lt;module&gt;
     50     analyze=True,
     51     build_plots=True,
---&gt; 52     collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states
     53 )

TypeError: backtest() got an unexpected keyword argument 'collect_all_states'
</code></pre>
<p dir="auto">The reason I want to get the 'state' working is that I have a pairs trading strategy that needs it.<br />
Any help would be greatly appreciated.<br />
Thanks.</p>
<p dir="auto">In a related issue, when I run the macroeconomic example, I get the following error:</p>
<pre><code>---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-1-62eeefb4e65f&gt; in &lt;module&gt;
     76     strategy=strategy,
     77     analyze=True,
---&gt; 78     build_plots=True
     79 )

~/book/qnt/backtester.py in backtest(competition_type, strategy, load_data, lookback_period, test_period, start_date, window, step, analyze, build_plots)
     66     data, time_series = extract_time_series(data)
     67     print("Run pass...")
---&gt; 68     result = strategy(data)
     69     if result is None:
     70         log_err("ERROR! Strategy output is None!")

TypeError: strategy() missing 1 required positional argument: 'state'
</code></pre>
]]></description><link>http://quantiacs.com/community/post/209</link><guid isPermaLink="true">http://quantiacs.com/community/post/209</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Tue, 27 Apr 2021 14:26:57 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Tue, 27 Apr 2021 10:02:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/18">@spancham</a></p>
<p dir="auto">Hello. Sorry, there was a typo. Try this:</p>
<pre><code class="language-bash">conda install 'quantiacs-source::qnt&gt;=0.0.250' -n qntdev
</code></pre>
<p dir="auto">(double ':' )</p>
<p dir="auto">Regards.</p>
]]></description><link>http://quantiacs.com/community/post/203</link><guid isPermaLink="true">http://quantiacs.com/community/post/203</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Tue, 27 Apr 2021 10:02:47 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Mon, 26 Apr 2021 22:49:08 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/12">@support</a><br />
Thanks but I tried that and it still did not work.<br />
Just so you understand, I do NOT work locally, I only work in the Quantiacs online cloud Jupyter env.<br />
I ran the update you suggested in my Quantiacs online Jupyter and it still did not work. I'm guessing you guys keep the env updated though.<br />
I'm still getting the same error:<br />
TypeError: backtest() got an unexpected keyword argument 'collect_all_states'</p>
<p dir="auto">Any suggestions pls?</p>
]]></description><link>http://quantiacs.com/community/post/202</link><guid isPermaLink="true">http://quantiacs.com/community/post/202</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Mon, 26 Apr 2021 22:49:08 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Fri, 23 Apr 2021 09:26:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/18">@spancham</a><br />
Helllo.</p>
<p dir="auto">Update the library, please. run this in you environmnent:</p>
<pre><code class="language-bash">conda install 'quantiacs-source:qnt&gt;=0.0.250'
</code></pre>
<p dir="auto">Regards.</p>
]]></description><link>http://quantiacs.com/community/post/197</link><guid isPermaLink="true">http://quantiacs.com/community/post/197</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Fri, 23 Apr 2021 09:26:13 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Thu, 22 Apr 2021 14:12:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/12">@support</a><br />
Please advise what to do here. Thanks.</p>
<pre><code>---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-1-434dffafaefc&gt; in &lt;module&gt;
     50     analyze=True,
     51     build_plots=True,
---&gt; 52     collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states
     53 )

TypeError: backtest() got an unexpected keyword argument 'collect_all_states'
</code></pre>
]]></description><link>http://quantiacs.com/community/post/194</link><guid isPermaLink="true">http://quantiacs.com/community/post/194</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Thu, 22 Apr 2021 14:12:52 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Fri, 09 Apr 2021 15:58:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/12">@support</a></p>
<p dir="auto">This feature has been implemented.<br />
See: <a href="https://quantiacs.com/documentation/en/reference/evaluation.html#stateful-multi-pass-backtesting" rel="nofollow ugc">https://quantiacs.com/documentation/en/reference/evaluation.html#stateful-multi-pass-backtesting</a></p>
]]></description><link>http://quantiacs.com/community/post/163</link><guid isPermaLink="true">http://quantiacs.com/community/post/163</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Fri, 09 Apr 2021 15:58:32 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Sat, 06 Mar 2021 10:28:24 GMT]]></title><description><![CDATA[<p dir="auto">related topic: <a href="https://quantiacs.com/community/topic/12/please-advise-on-p-settings-thanks/13" rel="nofollow ugc">Please advise on p settings. Thanks.</a></p>
]]></description><link>http://quantiacs.com/community/post/76</link><guid isPermaLink="true">http://quantiacs.com/community/post/76</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Sat, 06 Mar 2021 10:28:24 GMT</pubDate></item><item><title><![CDATA[Reply to Share the state between iterations on Tue, 02 Mar 2021 22:23:04 GMT]]></title><description><![CDATA[<p dir="auto">related topic:  <a href="/community/topic/13/different-sharpe-ratios-in-backtest-and-competition-filter">Different Sharpe ratios in backtest and competition filter</a></p>
]]></description><link>http://quantiacs.com/community/post/49</link><guid isPermaLink="true">http://quantiacs.com/community/post/49</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Tue, 02 Mar 2021 22:23:04 GMT</pubDate></item></channel></rss>