Trading with automation (with IB)

Discussion in 'Journals' started by fullautotrading, Sep 8, 2015.

  1. Before pulling the trigger, and let it trade by itself, let me summarize a bit the approach, so especially those new to these concepts can follow. In time, I have introduced a lot of new terminology, so when something is not clear, please, don't be afraid to request clarifications.

    The general idea is that we have "rules" to open and close a trade. These rules are called a "game" and they are specified by the user of the application. When an order is made to "open", we associate to that order a virtual entity called a "player", which maintains in time all the trading information about that order. When the "trade" ends, the player also has finished its life cycle and it is disposed.

    Ahead, we will see that there is a lot more with "players", but for now let's stick to the above simplified version of what they are.

    By design, players (and therefore the associate trades) can only close when the user's take profit rules are met. There are no rules instead to "take losses". Protection against runaways is provided through a mechanism of "player superposition", controlled by the user rules (the "game"). Player superposition in practice means that we will have both long and short players at the same time, with each team of players providing some "protection" against the runaways on the other side.

    Clearly, it's obvious that we can't have total "protection", and most often one side will be "loading up" more than the other, just because we need to take profit and close the trades on the "winning" side.

    On top of the player superposition, we may add the so called "layer overlay", which, in general, means we can have for instance two or more layers playing different "games", on the same or different (correlated) instruments.

    This is what I wanted to experiment in this thread. So, for instance, we might, for instance, trade ES, with 2 layers. On layer 1 we will trade ES with a long-only-position constraint, and on layer 2 with a short-only-position constraint, and keep collecting the scalps.

    There are various ways to define such constraints. A very strict and naive definition would be to allow long only trades on one side and short only trades on the other side. Normally, we do not use such version (which is anyway possible), because that would destroy the superposition logic of the players on the same layer (and therefore conflict with the general logic of the approach).

    Instead, we allow both long and short players (trades) on each layer, but we constrain the overall resulting position to be always either long or short on each layer. Again, this cannot provide a total "protection", but it may add up to the hedging capabilities.
     
    Last edited: Sep 8, 2015
    #11     Sep 8, 2015
  2. So, to make an example, I just turned into "auto" mode the 2 layers of ES. On one layer I have specified to maintain a long-only position and on the other layer a short-only position.
    Right now, only the rule to open a long player has triggered and in fact we have our first order:

    Code:
    ES FUT 201512 GLOBEX 50 BUY 1 @ 1954.5 LMT DAY
    right after the order, the API report:

    Code:
    
     - Current values (received on: Tue 08 Sep 2015 21:52:51:689 [ Tue 08 Sep 2015 15:52:51:689 edt ]) -
    
    AccruedCash                         0.00 USD                      [Min: 0.00, Max: 0.00]
    AccruedDividend                     0.00 USD                      [Min: 0.00, Max: 0.00]
    BuyingPower                10,152,293.33 USD                      [Min: 10,151,793.33, Max: 10,152,293.33]  (6.68 x 1,519,236.20)
    FullAvailableFunds          1,519,236.20 USD                      [Min: 1,519,161.20, Max: 1,519,236.20]
    FullExcessLiquidity         1,520,438.80 USD                      [Min: 1,520,363.80, Max: 1,520,438.80]
    FullInitMarginReq               6,013.00 USD                      [Min: 6,013.00, Max: 6,013.00]
    FullMaintMarginReq              4,810.40 USD                      [Min: 4,810.40, Max: 4,810.40]
    NetLiquidation              1,525,249.20 USD                      [Min: 1,525,174.20, Max: 1,525,249.20]
    
    (from which we can also immediately deduce the margin requirements for this contract).

    Image of the 2 ES layers:

    ES_1.png

    So the blue square appeared in the top layer, represents what we call a (long) player. For now, one can simply think of it as an "open" order equipped with automatic rules to behave and close, and also maintaining important trading information.
     
    #12     Sep 8, 2015
  3. Since we have still few orders at this time, the situation is ideal to look a bit into the working of the application.

    We have our 2 ES layers active, and last time we looked we had a long player.
    This morning, I found the following situation, where we have an additional long player, and one new short player on the other layer.

    So, as you can see, we can have players holding opposite positions (this clearly all "virtual", as the actual position in the account is the algebraic sum of all such "virtual" positions) on the same instrument, and each one following the rules of its own game:

    ES_2.png

    What the players are waiting for is now the possibility to close (that is, "take profit"), based on the rules that the user has specified to take profit.
    (The application also allows the user to intervene and manually close the players if he wishes so, or even "inject" new players, but for now let's ignore this possibility, and let it go by itself.)

    In the top picture, the horizontal blue line with the text 2 @ 1.963, is telling us the position the average of our players. Same for the red line in the picture below: -1 @ 1965.25.
    (These are overall averages for all players of one layer: it is also possible, optionally, to display separately the averages for long and short players of each layer.)
     
    Last edited: Sep 9, 2015
    #13     Sep 9, 2015
  4. A few hours later, we have a slightly different situation: one of the buy players on the first layer has been closed, so we now have 1 long on the first layer and 1 short on the second layer. Clearly, such players are "virtual" entities, as in this case the account is actually "flat". They just represent a convenient conceptual device to maintain the relevant trading information.

    ES_3.png

    Obviously, the underlying account would be flat. In fact, here is the information coming from the API:

    Code:
     - Current values (received on: Wed 09 Sep 2015 15:14:36:826 [ Wed 09 Sep 2015 09:14:36:826 edt ]) -
    
    AccruedCash                         0.00 USD                      [Min: 0.00, Max: 0.00]
    AccruedDividend                     0.00 USD                      [Min: 0.00, Max: 0.00]
    BuyingPower                10,172,162.60 USD                      [Min: 10,136,092.00, Max: 10,172,162.60]  (6.67 x 1,525,824.39)
    FullAvailableFunds          1,525,824.39 USD                      [Min: 1,519,161.20, Max: 1,525,824.39]
    FullExcessLiquidity         1,525,824.39 USD                      [Min: 1,520,363.80, Max: 1,525,824.39]
    FullInitMarginReq                   0.00 USD                      [Min: 0.00, Max: 6,013.00]
    FullMaintMarginReq                  0.00 USD                      [Min: 0.00, Max: 4,810.40]
    NetLiquidation              1,525,824.39 USD                      [Min: 1,525,174.20, Max: 1,525,824.39]
    
    ================================================== DAILY FOLIO UPDATES SENT BY IB ==================================================
    
    Symb                                    Pos/AbsPos             AvgCost                Real                 Unr          Real + Unr     Curr
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    ES        [ESZ5]                                 0                0.00              654.42                   0              654.42      USD
    ····································································································································
                                                     0                                                                     654.42 USD
    ____________________________________________________________________________________________________________________________________
                                                     0                                                                     654.42 USD
    ====================================================================================================================================

    Lately, I have adopted, as "default" view in the "trading monitor", the view of the players. However, it is also just straightforward to change to the "order view" (just uncheck the relevant checkbox). For instance, in this case the "order view" of the first layer (picture below) would show also the two orders that represent the open / close of the first player, now disposed. The two orders (which are hidden in the "player view", represent in essence the "life" of that player which now is no more in existence):

    PlayerLife.png

    And finally, we can also look at the whole history of the combined PNL so far:

    PNL_1.png

    Now, that I have explained some basics, I think we can proceed by also activating the CL layers and increase the complexity of our trading scenario.
     
    Last edited: Sep 9, 2015
    #14     Sep 9, 2015
  5. I have now activated CL too. Both layers have taken a position pretty soon, so we have actually no exposure on CL yet (we need to wait for one player to take profit):

    CL_1.png

    ES momentarily is pointing down and a new sell player has appeared on our second layer:

    ES_4.png

    Still thinking about SI. After all, I think we can afford the one with multiplier 5000, as the price is low (14.6). SIZ5 is the one where the larger volume is reported. Last time I checked SI can be a pretty wild beast, so let's wait a bit, to see where ES and CL take us.
     
    #15     Sep 9, 2015
  6. Let's see where we are this morning. ES first.
    A few players have closed and we have right now:

    ES_5.png

    Here we may notice that also the second layer has turned into a positive position, although we had our short-position constraint. This is because. as we have said. we have been allowing also long players (as long as the overall layer position remains negative), as well as to go into positive territory if short players need to be closed.

    This is and optional behavior, and the user can setup his own preference. (In case one might disable the possibility to turn positive due to close of long players.) In this case, I preferred to use the milder version of the "constraint", because usually we want to maximize the scalping activity [there is a "layer option dialog" where one can chose this behavior].

    What happened on the second layer can be seen passing to the "order view" of the second picture, where we can observe all the orders executed:

    ES_Orders_1.png

    You see here that 3 players have open and closed. So we have taken those 3 scalps on the second layer, and the corresponding players are now disposed (therefore not shown in the "player view").

    In this particular case, we have (momentarily) both layers showing a positive profit essentially by effect of the scalps taken on both layer (prevalence of "horizontal" price moves).

    Since two layers have a long position, while I'd like to have some other layer constantly providing extra protection respect to the larger side, I am going to add another ES layer, with a short constraint (it actually opened a new short player, while I was finishing to write this post):

    ES_6.png

    So, now we have 3 ES layers active.
     
    Last edited: Sep 10, 2015
    #16     Sep 10, 2015
  7. As to CL, we have the following situation ("order view"):

    CL_2.png

    We can see here that the first layer has closed 2 players (that is, 2 "scalps" have been taken) and there is a sell player "alive" (order in the green circle). The second layer has instead the long player we have seen before. Essentially we are momentarily "flat" on CL.

    Account and margin situation (according to API messages):

    Code:
     - Current values (received on: Thu 10 Sep 2015 12:41:06:100 [ Thu 10 Sep 2015 06:41:06:100 edt ]) -
    
    AccruedCash                         0.00 USD                      [Min: 0.00, Max: 0.00]
    AccruedDividend                     0.00 USD                      [Min: 0.00, Max: 0.00]
    BuyingPower                10,145,088.00 USD                      [Min: 10,008,557.10, Max: 10,173,677.93]  (6.67 x 1,520,560.60)
    FullAvailableFunds          1,520,560.60 USD                      [Min: 1,495,145.77, Max: 1,526,051.69]
    FullExcessLiquidity         1,521,763.20 USD                      [Min: 1,501,283.57, Max: 1,526,051.69]
    FullInitMarginReq               6,013.00 USD                      [Min: 0.00, Max: 30,689.00]
    FullMaintMarginReq              4,810.40 USD                      [Min: 0.00, Max: 24,551.20]
    NetLiquidation              1,526,573.60 USD                      [Min: 1,524,495.34, Max: 1,528,720.14]
    PNL history view (cyan line is the PNL trajectory):

    PNL_2.png
     
    Last edited: Sep 10, 2015
    #17     Sep 10, 2015
  8. Not much activity today. It stayed hedged most of the time:

    PNL_3.png

    Let's see if something happens tomorrow...
     
    #18     Sep 10, 2015
  9. Nothing happening today either. Just "small" (relative to game rules) price fluctuations: the application did not place any new player, nor closed any one, under the current rules, and we remained hedged where we were yesterday. We end the week this way:

    PNL_4.png

    margin usage currently pretty low (4-6K):

    Code:
    - Current values (received on: Sat 12 Sep 2015 00:39:53:217 [ Fri 11 Sep 2015 18:39:53:217 edt ]) -
    
    AccruedCash                         0.00 USD                      [Min: 0.00, Max: 0.00]
    AccruedDividend                     0.00 USD                      [Min: 0.00, Max: 0.00]
    BuyingPower                10,149,092.47 USD                      [Min: 10,008,557.10, Max: 10,175,058.60]  (6.67 x 1,521,161.27)
    FullAvailableFunds          1,521,161.27 USD                      [Min: 1,495,145.77, Max: 1,526,258.79]
    FullExcessLiquidity         1,522,363.87 USD                      [Min: 1,501,283.57, Max: 1,526,258.79]
    FullInitMarginReq               6,013.00 USD                      [Min: 0.00, Max: 30,689.00]
    FullMaintMarginReq              4,810.40 USD                      [Min: 0.00, Max: 24,551.20]
    NetLiquidation              1,527,174.27 USD                      [Min: 1,524,495.34, Max: 1,528,720.14]
    
     
    Last edited: Sep 11, 2015
    #19     Sep 11, 2015
  10. No further orders at this time, as both ES and CL are fluctuating in a "relatively small" range:

    ES_7.png

    In our (historic) charts (eg., top picture) the horizontal green lines always represent a 1% move, and this gives an immediate idea of the relative moves.

    Our current game is not very high frequency, with some good spacing between entries (also automatically expanding with volatility). At this time, it may appear a bit boring, but as we have learnt in previous experiences it's not much wise to overload in these situation, as when there is an explosion of volatility we may find ourselves with a large position and forced to take losses. In general, it's better to stick with the game we have decided and don't be overly anxious to trade.

    In case, one can slightly modify the game rules to rise the frequency, but it's always advisable to test thoroughly the new game before switching to it, and in particular compare the new game's avg daily frequency and the avg max DD with the one currently in use, to avoid nasty surprises.
     
    #20     Sep 15, 2015