Navigation

    Quantiacs Community

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

    Trying to understand trading

    Support
    2
    4
    244
    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.
    • M
      mobile.mr_mime last edited by

      Hi!

      I'm trying to understand how long/short future trading works, by creating an optimal long-only strategy that can see future prices, and invests if there will be profit. This is an unrealistic strategy for the sake of learning.

      By looking at the formulas at the link below, we should gain equity if the price of a stock bough long at open is greater at close, and at the open of the next day (ignoring slippage).
      https://quantiacs.com/documentation/en/theory/theoretical_basis.html
      frml.png

      However, the performance of said strategy is very poor/lossy for some reason (even going constantly long performs way better).

      I'm trying to find out why. There is a minimal working example of the strategy below.

      Thanks in advance.

      import xarray as xr
      
      import qnt.stats as qns
      import qnt.output as qnout
      import qnt.data as qndata
      
      # single-stock trading
      data = qndata.futures.load_data(min_date="2005-01-01", assets=["F_ES"])
      
      # attempting an optimal (unrealistic) long-only strategy
      # by looking at future prices, and investing only if there will be profit
      price_open = data.sel(field="open")
      price_close = data.sel(field="close")
      next_price_open = data.sel(field="open").shift(time=-1)
      weights = xr.where((price_open < price_close) &
                         (price_close < next_price_open),
                         1.0, 0.0)
      
      weights = qnout.clean(weights, data)
      qnout.check(weights, data)
      qnout.write(weights)
      
      stats = qns.calc_stat(
          data, weights,
          # ignoring slippage for simplicity
          slippage_factor=0, roll_slippage_factor=0)
      
      stats.loc[:, "equity"].plot.step();
      
      support 2 Replies Last reply Reply Quote 2
      • support
        support @mobile.mr_mime last edited by

        @mobile-mr_mime Hello, the issue should simply be related to the fact that the weights in the code are computed after the CLOSE at (t-1) and applied at the OPEN(t). If you want to do a looking forward, then you need to lag the OPEN by one step more. However, please allow us to check a little better.

        1 Reply Last reply Reply Quote 1
        • support
          support @mobile.mr_mime last edited by

          @mobile-mr_mime For a systematic looking forward, consider that the weights are defined immediately after the CLOSE(t). They are translated into positions (nr. contracts bought/sold) at the OPEN(t+1) of the next trading day. You can simulate a looking forward by going long on the weights at time t when the OPEN(t+2) is larger than the OPEN(t+1):

          next_price_open = data.sel(field="open").shift(time=-1)
          next2_price_open = data.sel(field="open").shift(time=-2)
          weights = xr.where(next_price_open < next2_price_open, 1.0, 0.0)

          This will guarantee that your equity curve (taken at the open) will be monotonically growing assuming that slippage=0.

          M 1 Reply Last reply Reply Quote 1
          • M
            mobile.mr_mime @support last edited by

            @support Thanks for the detailed answer, that seems to be it, here is the final code:

            import xarray as xr
            
            import qnt.stats as qns
            import qnt.output as qnout
            import qnt.data as qndata
            
            # single-stock trading
            data = qndata.futures.load_data(min_date="2005-01-01", assets=["F_ES"])
            
            # attempting an optimal (unrealistic) long-only strategy
            # by looking at future prices, and investing only if there will be profit
            next_price_open = data.sel(field="open").shift(time=-1)
            next2_price_open = data.sel(field="open").shift(time=-2)
            weights = xr.where(next_price_open < next2_price_open, 1.0, 0.0)
            # sell short when optimal:
            # weights = xr.where(next_price_open > next2_price_open, -1.0, weights)
            
            weights = qnout.clean(weights, data)
            qnout.check(weights, data)
            qnout.write(weights)
            
            stats = qns.calc_stat(
                data, weights,
                # ignoring slippage for simplicity
                slippage_factor=0, roll_slippage_factor=0)
            
            stats.loc[:, "equity"].plot.step();
            
            1 Reply Last reply Reply Quote 1
            • 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