Navigation

    Quantiacs Community

    • Register
    • Login
    • Search
    • Categories
    • News
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    Machine Learning Strategy

    Strategy help
    5
    13
    1063
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      spancham @spancham last edited by spancham

      @support
      ok, the system let me submit a new strategy:👏

      nn4.PNG

      I hope this one works.
      I can keep working on getting a higher Sharpe Ratio, and update the strategy, right?
      Thanks.

      support 1 Reply Last reply Reply Quote 0
      • support
        support @spancham last edited by

        @spancham

        Yes, you can continue. The system saves a copy when you submit the strategy.

        S 1 Reply Last reply Reply Quote 0
        • S
          spancham @support last edited by spancham

          @support
          Yaay! I got one accepted 🤸
          I know the SR is at the bottom of the barrel on the Leaderboard, but I'm still grateful I got one accepted.
          nn5.PNG

          Ok, I'm inspired that this is doable for me.
          Btw, thanks to everyone on your team for responding to my support requests & helping me understand the Quantiacs platform in a few short weeks.
          🍺 🍺

          S 1 Reply Last reply Reply Quote 0
          • S
            spancham @spancham last edited by

            @support

            1. Can you help pls with an example on how to include more than one feature, such as from the fields (OHLCV)?
            2. And also from the qnt.ta library?
              I am running into a problem converting the feature set to pandas when there are more than one features.
            price = data.sel(field="close").ffill('time').bfill('time').fillna(0) # fill NaN
                    for_result = price.to_pandas()
            

            Thank you.

            support 1 Reply Last reply Reply Quote 0
            • support
              support @spancham last edited by

              @spancham 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.

              S 1 Reply Last reply Reply Quote 0
              • S
                spancham @support last edited by

                Hi @support
                Ok, let me think about what you are suggesting & see if I can get that to work.
                Will let you know.
                Thanks.

                S 1 Reply Last reply Reply Quote 0
                • S
                  spancham @spancham last edited by

                  @support
                  ok guys, I tried what you suggested and I am running into all sorts of problems.
                  I want to pass several features altogether in one dataframe.
                  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?
                  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:

                  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
                  

                  Can you help with some code as to what you are suggesting?
                  Thanks

                  V 1 Reply Last reply Reply Quote 1
                  • V
                    Vyacheslav_B @spancham last edited by Vyacheslav_B

                    @spancham Hello. Try this

                    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 > 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
                    )
                    
                    

                    Here is an example with indicators (Sharpe Ratio = 0.8)

                     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
                    
                    
                    S 1 Reply Last reply Reply Quote 1
                    • S
                      spancham @Vyacheslav_B last edited by spancham

                      @vyacheslav_b
                      Thank you! 🎉 🎉

                      1 Reply Last reply Reply Quote 0
                      • Sjackson3289
                        Sjackson3289 Banned last edited by

                        This post is deleted!
                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB | Contributors
                        • Documentation
                        • About
                        • Career
                        • My account
                        • Privacy policy
                        • Terms and Conditions
                        • Cookies policy
                        Home
                        Copyright © 2014 - 2021 Quantiacs LLC.
                        Powered by NodeBB | Contributors