Trying to understand trading
-
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
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();
-
@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.
-
@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.
-
@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();