Trading with automation (with IB) - II - spreads

Discussion in 'Journals' started by fullautotrading, Jan 8, 2018.

  1. Following our previous illustration thread: Trading with automation (with IB)
    I am going to start a completely new one, in order to test some new ideas and functionalities on algorithmic trading.

    This will be an occasion to both possibly confirm our general approach, and, also, to test a few new ideas aimed to do something (else) against the possibility of worrying drawdowns. It will also allow us to develop further the new ideas and add relevant functionalities, while we proceed.

    This new illustration should run at least for a couple of years or more (well unless we crash either the account or the platform before that). So if you are interested, just relax, and patiently enjoy the journey.

    For obvious (and already explained reasons), we will make this illustration using a paper trading account from IB. Since this is algorithmic trading, outcome on a real account would be practically identical (apart slightly more efficient execution on "real" trading accounts).

    I will now briefly recall what is the general approach we follow, and summarize what are the new ideas being tested and developed now.

    1. General approach. Source of the edge: we exploit price curve oscillations and, in the long term, we recover all the stop loss orders (orders use to hedge the current position) which is possible to recover. This would intuitively cause a slight unbalance, in the long term, in the natural "random" win-loss ratio of 50/50. The unbalance is supposed to fully compensate also the trading expenses, so that we can have a profit to finally emerge in the long run. Powerful hedging functionalities are in place, such as "player superposition" and "layer overlay", as discussed in the previous thread.

    2. New ideas being tested. While the general approach seems to work fine with suitably chosen instruments (that is, instruments which are "rich" with price oscillations), the usual problem is always the same: 1. we need a lot of patience (for the idea to work in the relatively long term) 2. we need relatively large funds to withstand the product price oscillations in wide ranges (even though solid hedging mechanisms are in place).
    Clearly, patience is a virtue of the most experienced investors, but everybody tends to be a bit allergic to drawdown, although they understand that in some sense it is a "necessary" evil for systematic profit.

    Now, in the spirit of increasing the hedging capabilities of the platform (which, at this point, are already quite developed), I have now added the implementation of a new feature. In fact, in the meantime, many people have been asking me the possibility to trade spreads.

    I understand that the general reason for this kind of requests is the worry about significant drawdowns. However, trading spreads is not going to solve in general the problem, because it's like trading a new virtual instrument defined as the difference of a pair of instruments. So all the problems just reappear with the new "instrument", and possibly amplified by the unsuitability of the price curve being traded.

    Rather, while we can take inspiration from the "general" idea of the spread to create a type of hedging action, we certainly need much more flexibility in our own implementation (and interpretation) in order to apply it to our preferred instruments (such as the commodity futures, the mkt indices futures and so on).

    So for starters, I have implemented, for each traded instrument, the possibility to designate one or more other instruments to provide a "deterministic" hedging action, through a position which is a user sizable factor of the instrument being hedged:

    - Instrument (the one which is being hedged)
    - Other Instrument (the one which is hedging, or "hedger")

    The "hedger" will automatically take a position equal to K * P where P is the position of the hedged instrument and K a user factor (in other words the programmed rules of the trading game for opening positions will be overridden and ignored, while, at least for the moment, we let the open players close automatically, following normally the game rules.)

    For any instrument (included the hedging instrument) multiple instrument, and not just one, can be designated as "hedgers".

    For now, it's as simple as that. I am sure that, while we proceed experimenting with this new possibility, we will have several new ideas and variations to refine further this mechanism.

    - Initial account status -

    We start where we left this account (IB statement):


    which is also reflected by our application display of account situation (retrieved via IB API):


    Most of the time, we will be using a small fraction of the available funds ($6,172,520.74), so for performances purposes we might, for instance, use ratios which use (at denominator) the maximum decline of the NetLiquidation value, or similar stuff.

    (Feel free to jump in anytime with questions, doubts, corrections or new ideas.)
  2. traider


    What was the sharpe ratio and P&L of your 1st version?
  3. Hello traider. Thank you for the question.

    You find the complete broker report (IB) attached to the last link posted in the previous post (see very first line).

    There is also the chart of the complete equity curve and of the final PNL of all instruments active (which had been rolled over during the period).

    [Sharpe was not computed (I am not much a "believer" of that kind of metric), but might probably be inferred from the available data.]
  4. Now that we have introduced the "new" ideas in very general terms, let's see some practical examples and some details of the current implementation. So that you can also start proposing possible corrections and variations on the theme.

    Let's start with the most trivial example.

    Assume for instance that you take these 2 futures:

    F1: Crude Oil (CL), with expiry D1
    F2: Crude Oil (CL), with expiry D2

    and start doing buy 1 of F1, sell 1 of F1, and so on. This will be like trading the difference of the two instruments (calendar spread).

    We are not interested in doing exactly that, but we take inspiration from this general idea to add a (deterministic) hedging mechanism (much more flexible).

    So, for instance, given the future CLH8 (CL FUT 201803 NYMEX 1000), we define one, or more, instruments which will "play against" this one. Let's call them in general: H(1), H(2), ..., H(j), ... (hedgers). These instruments, in principle can be anything that is correlated with CLH8.

    For each of these hedgers, we assign a (positive) fraction, say f(j) which expresses of much of the value of CLH8 is targeted to be "covered" by an opposite value of the hedger H(j).

    - Basic example -

    For simplicity, let's make a simple example, with one "hedger" only and with f=1.

    So we have CLH8, and imagine the designed hedger is CLJ8.
    We start by trading normally CLH8, which will play its programmed trading game (as seen in previous thread). Imagine that, at a certain time, its current position value is V (this is resulting from the multiplication of the position * price * contract multiplier).

    Now, we define the hedger CLJ8 to take position in such a way that it will constantly attempt to bring its position to have value equal to -V. So CLJ8 will play a trading game, whose entries are dictated by the need of covering CLJ8, but, on the other end, it will be free to close the active player by using its own close rules.

    - Generalizations -

    G1 This is generalized by allowing CLJ8 to have a position value which is not necessarily the opposite of V, but any fraction of that: -f * V.

    G2 This is also generalized by considering not necessarily a unique hedger, but multiple hedgers, each of one "covering" some fraction of the current position value of the hedged instrument CLH8.

    G3 Another generalization is that the hedgers need not to be instruments different from the one being hedged, but they can be identical "copies" (clones) of it.

    G4 Another generalization consist of the fact that, if we wish, even the hedgers (being instruments like any other one) can, in turn, be hedged similarly, thus creating a sort of "cascade" of hedging actions.
  5. If we call this mechanism an "hedging relationship", we can also identify (for now) the following "formal" properties:

    - An "hedger" can target only one instrument
    - An instrument can be hedged by multiple instruments
    - An "hedger" can, in turn, be hedged ("cascade")
    - If I is hedged by H(j), we cannot set vice versa (in general, in a cascade of hedging relationships, no "loop" would be functional)

    Also, the we can note that:

    - On rollover, the hedging settings are inherited by the new instruments
    - On clonation, the hedging settings are not transmitted to the clone

    - Trading activity -

    This afternoon I have finally started the trading activity (by removing the "suspension" flag), after letting the ticks flow for more than a day. To start, I have defined a simple folio with some of my preferred futures: CL, ES, NG, NKD, SI as shown in the picture below.

    For CL and SI, I have defined 2 additional layers, useful to test and compare our new mechanism. So we have

    1 layer of CL: trading the usual game called we have called "Shield"
    2 layer of CL: trading a modified version of "Shield" with no protective players
    3 layer of CL: playing the role of hedger for layer 2, with factor f = 0.3

    Same thing for SI.

    [ I have also defined a few ETFs with high decay, which for now are not trading (just ticking), but in case we will see if the opportunity arise to do some semi-algorithmic treatment of them, in case they do some large move.]

    So, for now, the situation looks like the following:


    and the various PNLs (meaningless for now):


    [Separately, I am also running a few simulations to understand better how the hedging mechanism works out and the best choice of parameters and implementation details.]
  6. traider


    Can we use machine learning to create more hedging variations?
  7. Can you make an example to explain better what you mean ? (In general terms, I tend to think that the mkt does not yield information useful to give any advantage to extract profit. It's rather about the use of the trading information that the trading agent (a trader or any algorithm) creates itself while trading.)
  8. The fact the we set f=0.3, that is we aim to "cover" 30% of the value (we will discuss later what "cover" can mean in terms of implementation details), has the practical implication that for the first entry on the "hedger" we need a position of at least 3 on the layer being hedged.

    This has happened yesterday, since at a certain point CL (the "hedged" layer) reached a position, while momentarily shooting up, of -3:


    and therefore the "hedger" we defined kicked in with a long position:


    Instead, the CL layer that is running without hedgers, with the "Shield" game, did not bother to throw a "protective" (buy) player:


    Overall PNL curve (we have been trading a couple of days, currently happily fluctuating around 3.8K):


    Instruments' PNLs:

    epsimatic88 likes this.
  9. traider


    The trading information that an agent creates is derived from mkt prices. Isn't that the same as the trading info from mkt itself?
  10. Nice question.
    Yes, it is true that "The trading information that an agent creates is derived from mkt prices."

    However, it is not "the same as the trading info from mkt". More than "derived", I would say caused by effect of the "trading rules" which the agent enforces.

    Some arguments are the following. If the trading agent did not exist, then the "trading information that an agent creates" did not exist either, but, the info from mkt would still exist. On the other hand, if I feed random data to a trading agent, it will still create useful information, even though the random data is providing none.

    (Another point, I am not doubting the existence of the information (in the sense of pure processing of incoming data) arising from the mkt, but it's independent usefulness to the purpose of generating systematic a long term profit.)
    #10     Jan 12, 2018