<?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[Machine Learning Strategy]]></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 />
I have some questions on the Machine Learning strategy:</p>
<p dir="auto"><strong>1. When you set up the feature, you treated missing data in one way:</strong></p>
<pre><code>price = data.sel(field="close").ffill('time').bfill('time').fillna(0) # fill NaN
</code></pre>
<p dir="auto"><strong>But when you set up the target, you treated it differently,</strong></p>
<pre><code>price_current = data.sel(field="close").dropna('time') # rm NaN
</code></pre>
<p dir="auto">Doesn't that cause misalignment and introduce possible errors, even though I see you tried to align the datasets after?</p>
<p dir="auto"><strong>2. Why do you use only a buy up target?</strong></p>
<pre><code>class_positive = 1
class_negative = 0
target_is_price_up = xr.where(price_future &gt; price_current, class_positive, class_negative)
</code></pre>
<p dir="auto">That means the ML strategy will never sell? If you have <em>a priori</em> knowledge that crytos have been going up all this time, then it seems you will inevitably have a good performing strategy.<br />
I admit, I did try a basic buy only strategy with no ML and it did not perform as well as the ML strategy to test that theory.<br />
But what will happen when crytos start going down? Maybe that will never happen as more people in the world keep piling in.</p>
<p dir="auto"><strong>3. What does this correlation failure mean?</strong></p>
<pre><code>INFO: 2021-03-30T19:06:59Z: pass started: 655331
INFO: 2021-03-30T19:07:15Z: pass completed: 655331
INFO: 2021-03-30T19:07:17Z: stats received light=false
INFO: 2021-03-30T19:07:17Z: progress: 1.0
INFO: 2021-03-30T19:07:17Z: checking: last pass
INFO: 2021-03-30T19:07:17Z: filter passed: source exists
INFO: 2021-03-30T19:07:17Z: filter passed: output html exists
INFO: 2021-03-30T19:07:17Z: filter passed: output exists
INFO: 2021-03-30T19:07:17Z: filter passed: strategy uses the last data
INFO: 2021-03-30T19:07:17Z: filter passed: in-sample size enough 
INFO: 2021-03-30T19:07:17Z: Sharpe ratio = 1.94598167418714
INFO: 2021-03-30T19:07:17Z: filter passed: sharpe ratio &gt; 1
FAIL: 2021-03-30T19:07:17Z: filter failed: the strategy correlates with other strategies: [{"id":"222363","cofactor":0.9446207002399428,"sharpeRatio":1.7638719827031102},{"id":"222367","cofactor":0.9615233769176826,"sharpeRatio":2.011604789293279}]
</code></pre>
<p dir="auto"><strong>I used a a different ML classifier strategy which has completely different hyperparameters from a Ridge Classifier. By how much do I have to change the template?</strong><br />
Is it correlating with the Quantiacs example or other submitted strategies?</p>
<p dir="auto">Thanks.</p>
]]></description><link>http://quantiacs.com/community/topic/40/machine-learning-strategy</link><generator>RSS for Node</generator><lastBuildDate>Tue, 10 Mar 2026 09:43:44 GMT</lastBuildDate><atom:link href="http://quantiacs.com/community/topic/40.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 31 Mar 2021 17:00:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Machine Learning Strategy on Mon, 19 Apr 2021 15:42:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/5">@vyacheslav_b</a><br />
Thank you! <img src="http://quantiacs.com/community/plugins/nodebb-plugin-emoji/emoji/android/1f389.png?v=933r2hptiik" class="not-responsive emoji emoji-android emoji--tada" title=":tada:" alt="🎉" /> <img src="http://quantiacs.com/community/plugins/nodebb-plugin-emoji/emoji/android/1f389.png?v=933r2hptiik" class="not-responsive emoji emoji-android emoji--tada" title=":tada:" alt="🎉" /></p>
]]></description><link>http://quantiacs.com/community/post/181</link><guid isPermaLink="true">http://quantiacs.com/community/post/181</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Mon, 19 Apr 2021 15:42:30 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Mon, 19 Apr 2021 13:29:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/18">@spancham</a> Hello. Try this</p>
<pre><code>import xarray as xr

import qnt.backtester as qnbt
import qnt.data as qndata
import numpy as np
import pandas as pd
import logging


def load_data(period):
    return qndata.cryptofutures.load_data(tail=period)


def predict_weights(market_data):

    def get_ml_model():
        # you can use any machine learning model
        from sklearn.linear_model import RidgeClassifier
        model = RidgeClassifier(random_state=18)
        return model

    def get_features_dict(data):
        def get_features_for(asset_name):
            data_for_instrument = data.copy(True).sel(asset=[asset_name])

            # Feature 1
            price = data_for_instrument.sel(field="close").ffill('time').bfill('time').fillna(0)  # fill NaN
            price_df = price.to_dataframe()

            # Feature 2
            vol = data_for_instrument.sel(field="vol").ffill('time').bfill('time').fillna(0)  # fill NaN
            vol_df = vol.to_dataframe()

            # Merge dataframes
            for_result = pd.merge(price_df, vol_df, on='time')
            for_result = for_result.drop(['field_x', 'field_y'], axis=1)

            return for_result

        features_all_assets = {}

        asset_all = data.asset.to_pandas().to_list()
        for asset in asset_all:
            features_all_assets[asset] = get_features_for(asset)

        return features_all_assets

    def get_target_classes(data):
        # for classifiers, you need to set classes
        # if 1 then the price will rise tomorrow

        price_current = data.sel(field="close").dropna('time')  # rm NaN
        price_future = price_current.shift(time=-1).dropna('time')

        class_positive = 1
        class_negative = 0

        target_is_price_up = xr.where(price_future &gt; price_current, class_positive, class_negative)
        return target_is_price_up.to_pandas()

    data = market_data.copy(True)

    asset_name_all = data.coords['asset'].values
    features_all_df = get_features_dict(data)
    target_all_df = get_target_classes(data)

    predict_weights_next_day_df = data.sel(field="close").isel(time=-1).to_pandas()

    for asset_name in asset_name_all:
        target_for_learn_df = target_all_df[asset_name]
        feature_for_learn_df = features_all_df[asset_name][:-1]  # last value reserved for prediction

        # align features and targets
        target_for_learn_df, feature_for_learn_df = target_for_learn_df.align(feature_for_learn_df, axis=0,
                                                                              join='inner')

        model = get_ml_model()
        try:
            model.fit(feature_for_learn_df.values, target_for_learn_df)

            feature_for_predict_df = features_all_df[asset_name][-1:]

            predict = model.predict(feature_for_predict_df.values)
            predict_weights_next_day_df[asset_name] = predict
        except:
            logging.exception("model failed")
            # if there is exception, return zero values
            return xr.zeros_like(data.isel(field=0, time=0))

    return predict_weights_next_day_df.to_xarray()


weights = qnbt.backtest(
    competition_type="cryptofutures",
    load_data=load_data,
    lookback_period=18,
    start_date='2014-01-01',
    strategy=predict_weights,
    analyze=True,
    build_plots=True
)

</code></pre>
<p dir="auto">Here is an example with indicators (Sharpe Ratio = 0.8)</p>
<pre><code> def get_features_for(asset_name):
    data_for_instrument = data.copy(True).sel(asset=[asset_name])

    # Feature 1
    price = data_for_instrument.sel(field="close")
    price = qnt.ta.roc(price, 1)
    price = price.ffill('time').bfill('time').fillna(0)
    price_df = price.to_pandas()

    # Feature 2
    vol = data_for_instrument.sel(field="vol")
    vol = vol.ffill('time').bfill('time').fillna(0)  # fill NaN
    vol_df = vol.to_pandas()

    # Merge dataframes
    for_result = pd.merge(price_df, vol_df, on='time')

    return for_result

</code></pre>
]]></description><link>http://quantiacs.com/community/post/180</link><guid isPermaLink="true">http://quantiacs.com/community/post/180</guid><dc:creator><![CDATA[Vyacheslav_B]]></dc:creator><pubDate>Mon, 19 Apr 2021 13:29:59 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Sun, 18 Apr 2021 17:45:10 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 guys, I tried what you suggested and I am running into all sorts of problems.<br />
I want to pass several features altogether in one dataframe.<br />
Are you guys thinking that I want to 'test' one feature at a time and that is why you are suggesting working with more than one dataframe?<br />
Here is an example of some code I tried, but I would still have to merge the dataframes in order to pass the feature set to the classifier:</p>
<pre><code>def get_features(data):
        # let's come up with features for machine learning
        # take the logarithm of closing prices
        def remove_trend(prices_pandas_):
            prices_pandas = prices_pandas_.copy(True)
            assets = prices_pandas.columns
            print(assets)
            for asset in assets:
                print(prices_pandas[asset])
                prices_pandas[asset] = np.log(prices_pandas[asset])
            return prices_pandas
        
        # Feature 1
        price = data.sel(field="close").ffill('time').bfill('time').fillna(0) # fill NaN
        price_df = price.to_dataframe()
        
        # Feature 2
        vol = data.sel(field="vol").ffill('time').bfill('time').fillna(0) # fill NaN
        vol_df = vol.to_dataframe()
        
        # Merge dataframes
        for_result = pd.merge(price_df, vol_df, on='time')
        for_result = for_result.drop(['field_x', 'field_y'], axis=1)
            
        features_no_trend_df = remove_trend(for_result)
        return features_no_trend_df
</code></pre>
<p dir="auto">Can you help with some code as to what you are suggesting?<br />
Thanks</p>
]]></description><link>http://quantiacs.com/community/post/179</link><guid isPermaLink="true">http://quantiacs.com/community/post/179</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Sun, 18 Apr 2021 17:45:10 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Mon, 12 Apr 2021 19:01:13 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 />
Ok, let me think about what you are suggesting &amp; see if I can get that to work.<br />
Will let you know.<br />
Thanks.</p>
]]></description><link>http://quantiacs.com/community/post/169</link><guid isPermaLink="true">http://quantiacs.com/community/post/169</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Mon, 12 Apr 2021 19:01:13 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Mon, 12 Apr 2021 18:33:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/18">@spancham</a> Hello, could you elaborate more on your request? In principle, you could just repeat the procedure you use for the "close" and you will work with more dataframes.</p>
]]></description><link>http://quantiacs.com/community/post/168</link><guid isPermaLink="true">http://quantiacs.com/community/post/168</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Mon, 12 Apr 2021 18:33:38 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Tue, 06 Apr 2021 14:18:12 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>
<ol>
<li>Can you help pls with an example on how to include more than one feature, such as from the fields (OHLCV)?</li>
<li>And also from the qnt.ta library?<br />
I am running into a problem converting the feature set to pandas when there are more than one features.</li>
</ol>
<pre><code>price = data.sel(field="close").ffill('time').bfill('time').fillna(0) # fill NaN
        for_result = price.to_pandas()
</code></pre>
<p dir="auto">Thank you.</p>
]]></description><link>http://quantiacs.com/community/post/162</link><guid isPermaLink="true">http://quantiacs.com/community/post/162</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Tue, 06 Apr 2021 14:18:12 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Thu, 01 Apr 2021 20:55:46 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 />
Yaay! I got one accepted  <img src="http://quantiacs.com/community/plugins/nodebb-plugin-emoji/emoji/android/1f938.png?v=933r2hptiik" class="not-responsive emoji emoji-android emoji--person_doing_cartwheel" title=":person_doing_cartwheel:" alt="🤸" /><br />
I know the SR is at the bottom of the barrel on the Leaderboard, but I'm still grateful I got one accepted.<br />
<img src="/community/assets/uploads/files/1617310250671-nn5.png" alt="nn5.PNG" class="img-responsive img-markdown" /></p>
<p dir="auto">Ok, I'm inspired that this is doable for me.<br />
<em><strong>Btw, thanks to everyone on your team for responding to my support requests &amp; helping me understand the Quantiacs platform in a few short weeks.</strong></em><br />
<img src="http://quantiacs.com/community/plugins/nodebb-plugin-emoji/emoji/android/1f37a.png?v=933r2hptiik" class="not-responsive emoji emoji-android emoji--beer" title=":beer:" alt="🍺" /> <img src="http://quantiacs.com/community/plugins/nodebb-plugin-emoji/emoji/android/1f37a.png?v=933r2hptiik" class="not-responsive emoji emoji-android emoji--beer" title=":beer:" alt="🍺" /></p>
]]></description><link>http://quantiacs.com/community/post/161</link><guid isPermaLink="true">http://quantiacs.com/community/post/161</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Thu, 01 Apr 2021 20:55:46 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Thu, 01 Apr 2021 20:06:29 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">Yes, you can continue. The system saves a copy when you submit the strategy.</p>
]]></description><link>http://quantiacs.com/community/post/160</link><guid isPermaLink="true">http://quantiacs.com/community/post/160</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Thu, 01 Apr 2021 20:06:29 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Thu, 01 Apr 2021 19:58:08 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, the system let me submit a new strategy:<img src="http://quantiacs.com/community/plugins/nodebb-plugin-emoji/emoji/android/1f44f.png?v=933r2hptiik" class="not-responsive emoji emoji-android emoji--clap" title=":clap:" alt="👏" /></p>
<p dir="auto"><img src="/community/assets/uploads/files/1617306898790-nn4.png" alt="nn4.PNG" class="img-responsive img-markdown" /></p>
<p dir="auto">I hope this one works.<br />
<em><strong>I can keep working on getting a higher Sharpe Ratio, and update the strategy, right?</strong></em><br />
Thanks.</p>
]]></description><link>http://quantiacs.com/community/post/159</link><guid isPermaLink="true">http://quantiacs.com/community/post/159</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Thu, 01 Apr 2021 19:58:08 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Thu, 01 Apr 2021 17:35:25 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 />
Thank you. I'll try that.<img src="http://quantiacs.com/community/plugins/nodebb-plugin-emoji/emoji/android/1f575.png?v=933r2hptiik" class="not-responsive emoji emoji-android emoji--sleuth_or_spy" title=":sleuth_or_spy:" alt="🕵" /></p>
]]></description><link>http://quantiacs.com/community/post/158</link><guid isPermaLink="true">http://quantiacs.com/community/post/158</guid><dc:creator><![CDATA[spancham]]></dc:creator><pubDate>Thu, 01 Apr 2021 17:35:25 GMT</pubDate></item><item><title><![CDATA[Reply to Machine Learning Strategy on Thu, 01 Apr 2021 15:08:42 GMT]]></title><description><![CDATA[<p dir="auto">Hello.</p>
<p dir="auto">This strategy correlates with the examples.</p>
<p dir="auto">The cofactor(correlation factor) must be lower than 0.9 or the Shape Ratio of your strategy must be higher (for the last 3 years).</p>
<p dir="auto">Try to use the other features: volume, ROC(rate of change), or other technical indicators.</p>
<p dir="auto">Regards.</p>
]]></description><link>http://quantiacs.com/community/post/155</link><guid isPermaLink="true">http://quantiacs.com/community/post/155</guid><dc:creator><![CDATA[support]]></dc:creator><pubDate>Thu, 01 Apr 2021 15:08:42 GMT</pubDate></item></channel></rss>