Chabah on Automated Trading Redux

Discussion in 'Automated Trading' started by Chabah, Jan 8, 2010.

  1. Chabah

    Chabah

    Imagine you are not a trader, but rather someone hired to operate a stock trading strategy. What kinds of things should you be able to do? Also, if the real trader calls on the phone and asks you to do something, what types of requests are likely?

    As an Operator I can:
    1. View current strategy and actual positions.
    2. Correct discrepancies between strategy and actual positions.
    3. View current (daily) Profit/Loss by position, strategy (across positions), and portfolio.
    4. Enable/Disable trading for a specific security at any time.
    5. Enable/Disable trading for a specific strategy (across all securities) at any time (without touching each position).
    6. Enable/Disable trading for the entire system at any time (without touching each position).
    7. Control the overall portfolio exposure with a single setting.
    8. Control how aggressively positions are defended (especially profits) across the entire system.
    9. Control the size of a Risk Unit (1R) for all strategies with a single setting.
    10. Control the maximum number of Risk Units available to any strategy/security combination with a single setting.
    11. Set a minimum position size with a single setting.
    12. Set a maximum position size with a single setting.
    13. Set a minimum Reward-Risk ratio for all strategies with a single setting.

    ___________________

    I'll try to be brief without leaving anything out. Numbers below correspond to User Stories listed above.

    1. Standard TradeStation view. No coding required.
    2. Ability to do basic math and enter basic orders. May need to call for a manual fill (sell short). No coding required.
    3. By position and portfolio are standard views. No coding required for standard views; "by strategy" might be tricky and won't be an initial deliverable.
    4. Turn off the strategy at the chart and manually close the position. No coding required.
    5. Each strategy has a function to disable it, and all positions will automatically close. Custom coding.
    6. System-wide control, which will cause all positions to close. Emergency version is to hit "Close all positions" and then close the application, but we'd like something a little more elegant (won't cause strategy vs actual position discrepancies).
    7. Part of the insurance for the entire portfolio. We'll get more into this later.
    8. Usually strategies will handle this, but at certain times we might want to tighten or loosen profit taking. Example would be tightening controls during a major speech just in case the market took off on news. Custom coding.
    9. Basic position sizing control to ensure consistency across positions without worrying about stock prices. Custom coding.
    10. Ensure that multi-unit strategies (pyramid) don't get out of control. Custom coding.
    11. If risk parameters end up calculating a position that is too small to trade, don't. Custom coding.
    12. If risk parameters end up calculating a position that is too large to trade, reduce the size to the maximum. Also helps to prevent entering orders that would create an invalid portfolio (leverage beyond allowable limits).
    13. Prevent low-potential strategies from eating up valuable portfolio allocation when high-potential strategies are trading. Example would be shutting down low reward range trading strategies when breakout or trend-following strategies are firing.

    That wasn't so bad. That finishes off the User Stories. I'll post a summary once I feel I am done tweaking.

    We're about ready for some basic coding, but I don't have platform access yet so it may be a little bit still. I'll consider posting function outlines (do you want them?), or just sitting patiently and quietly until coding starts. I just bought The Black Swan, so I have some reading to do.

    Enjoy,

    Damien
     
    #21     Jan 19, 2010
  2. Corey

    Corey

    User Stories ... you must be a software engineer at heart :D

    I am enjoying the read so far!
     
    #22     Jan 20, 2010
  3. Chabah

    Chabah

    Yes Corey, I am trying to leverage my programming expertise while marginalizing my relative lack of trading experience. I am fairly new to Agile programming, but I really like the quick delivery times so that's how I am running this project. Note how breaking it up like this would allow easy integration of additional team members or stakeholders (not that I am planning to partner with anyone).

    Tonight I did some research and development on my Monte Carlo simulator. I found a darling little site here: http://excelmontecarlo.com/

    I implemented my Monte Carlo simulator in Google Documents. Take a look:
    http://spreadsheets.google.com/ccc?key=0AtOGnLq8Mf_ydG1uN2s0dFdIc1hVUnlNRGRkVDBVUkE&hl=en

    My simulator is based on a fictional set of my actual trading results. Each day, I would paste in my portfolio balance on the "Actual Results" tab, ensuring to copy the % Change value as well.

    The "Monte Carlo" page automatically updates to accommodate the number of populated rows, shown at the top next to "Rows". Below, we have 100 runs, each broken up into 10, 20...100 trades, compounded. So you can see that based on your actual trading results, what range you could expect over 10 through 100 trades. As you can see from my sample values, which are based on small, consistent gains and large, rare losses, there can be quite a range of results.

    Since this data "wins" it is no surprise that the Average and Median are positive - however the Min Compounded shows that these exact trades could easily result in a big loss.

    Press Control-R on a numeric cell to force the entire sheet to recalculate. This is an easy way to see many more than 100 cases. One could also just replicate the rows a couple hundred more times for more comprehensive results.

    One additional use for this, which we'll get to later, is to take backtesting results and paste them into such a simulator to get a better idea how a certain set of trades could have performed over many possible runs.

    I also want to add the calculation to show what percentile of potential scenarios the actual results fall in to. That should be an easy one.

    So enjoy the spreadsheet and let me know if you find anything interesting. If I find anything nifty I will add it.

    Damien
     
    #23     Jan 21, 2010
  4. Chabah

    Chabah

    If you're just skimming along, this is an important post. Also, a good example of applying programmer skills to stock trading.

    Decision Matrices

    One way to formulate a strategy is to identify a series of conditions that then determine your position. For example:

    If the price is less than $10
    and the price is greater than $5
    and the price is less than the 50 day moving average
    then Go Short.

    Something like that. You might have a variety of such conditions, some of which go short, others that go long, etc. Once these are organized, you can code it yourself or you can send it off to a contractor.

    This process can be improved by using what I call a Decision Matrix. The idea is that you organize your conditions in a spreadsheet. The spreadsheet then formats that matrix into code, which you can cut and paste into your program (TradeStation for me). While it won't be 100% done at that point, it will save you a lot of effort and help avoid errors.

    I've prepared a simple pair of decision matrices, which you can share from here:

    http://spreadsheets.google.com/ccc?key=0AtOGnLq8Mf_ydGsxRHA2U0VGOHIzWnd5RkFUSWVWRnc&hl=en

    The first tab is the PDM, for Points Decision Matrix. The idea with this type of strategy is that different conditions contribute to an overall view. For example:

    Price < $10: +1
    Price > $5: -1
    Price < 50 day MA: +1

    If the total is 2, go short 2 units. If the total is 1, go short 1 unit. Otherwise, stay flat.

    On the spreadsheet you enter these conditions and their values on the left side. The right side then populates with code that can be copy-and-paste into a strategy. I haven't tested the actual code yet, but once I do I will correct any syntax errors.

    The second tab is the BDM, or Binary Decision Matrix. In this case, we combine conditions with an AND statement. These are then organized into multiple scenarios. While logically they may not be mutually exclusive, in terms of matrix computation they are forced to be mutually exclusive. The details are on the BDM Calculations tab, but don't worry too much about that unless you're interested. The resulting code is on the BDM Code To Copy tab.

    The final tab, the Condition Library, is the first step to setting up the Condition values for the matrices. This is far from complete, but gives an idea of how one might create strategy criteria.

    As far as I know this is pretty original stuff - I haven't read about this type of matrix anywhere, at least related to stock trading. To you non-coder traders out there, this might even look a little crazy. I like it for many reasons, including the enforced mental organization and copy-and-paste coding. When I am designing strategies, I want to focus on the criteria, not the syntax. This is a big step towards automating criteria into code.

    Have you seen anything like this? What do you think?

    Enjoy,

    Damien
     
    #24     Jan 23, 2010
  5. Chabah

    Chabah

    Here is a summary, without commentary, on the core system thus far:

    Path Independence
    Systems should be able to determine the current position at any given time, regardless of trade history. It should not matter what the position was last period - the current period is controlling. An exception to this is buffer zones (areas around inflection points where positions are not changed in order to avoid over-trading), provided the buffer zones are determined independent of the trading history.


    Pre-determined Risk
    Each position should have a specific risk measure which, if lost, triggers the close of the position. In such cases, a new position of the same sort should not be opened immediately, but rather be governed by a safety period or manual reset. This is intended to prevent over-trading in adverse conditions or technical failure. Additionally, whenever possible the exit order should be entered into the brokerage in case of system failure - the system should not rely on being connected in order to enter exit orders (this does not apply to profit taking).


    Risk Robustness
    When the market is closed, a given position should not pose a risk to the portfolio greater than the system setting, based on its own initial size or pre-determined risk (whichever is larger). For long positions, this applies if the maximum position risk is less than 100%. Short positions should always be insured prior to the market close. This robustness is implemented as a system governance function.
    Long example: If long 100 shares at $100, if maximum position risk is 50%, then 100 put options at $50 should be held during market close. Therefore if the stock drops below $50 the options will cover the long position losses below $50 per share.
    Short example: If short 100 shares at $100, if maximum position risk is 50%, then 100 call options at $150 should be held during market close. Therefore if the stock were to surpass $150 the options will cover the short position losses beyond $50 per share.
    In either example, since a 50% loss should be extremely rare, the given option should be very inexpensive. Such coverage can be either automated or set-up as part of manual market close procedures (provided the order is executed prior to market close, not after).


    Portfolio Risk Consistency
    The basic risk measure, R, should be consistent across system strategies. While certain conditions may justify the increase or decrease of risk of a given position, the basic amount (1R) should be consistent.


    Empirical Optimization
    Optimization should be used only to answer well-formed empirical questions. In the absence of a specific question to answer, variables should not be optimized. In particular, strategies should not rely on fitting variable inputs in order to be viable.


    Roles
    Strategy designer/programmer
    Researcher/Tester
    Operator
    Investor

    User Stories
    As an Operator I can:
    -View current strategy and actual positions.
    -Correct discrepancies between strategy and actual positions.
    -View current (daily) Profit/Loss by position, strategy (across positions), and portfolio.
    -Enable/Disable trading for a specific security at any time.
    -Enable/Disable trading for a specific strategy (across all securities) at any time (without touching each position).
    -Enable/Disable trading for the entire system at any time (without touching each position).
    -Control the overall portfolio exposure with a single setting.
    -Control how aggressively positions are defended (especially profits) across the entire system.
    -Control the size of a Risk Unit (1R) for all strategies with a single setting.
    -Control the maximum number of Risk Units available to any strategy/security combination with a single setting.
    -Set a minimum position size with a single setting.
    -Set a maximum position size with a single setting.
    -Set a minimum Reward-Risk ratio for all strategies with a single setting.

    As a Strategy designer/programmer I can:
    -Set Stop Loss exit criteria to be managed by the system.
    -Set Profit Target(s) to be managed by the system.
    -Send notifications via the system.
    -Open a position via the system.
    -Close a position via the system.
    -Expect the system to manage position sizing.
    -Expect the system to correct mis-alignment of strategy and actual positions.
    -Expect the system to manage end-of-day activities (notifications, profit taking, risk mitigation, etc).
    -Expect the system to manage overall position risk (against major market moves).

    As a researcher/tester I can:
    -Test a strategy over any past period using system functions (Start and End dates).
    -Test using a non-production version of code (built-in development/test environment support).
    -Perform Monte Carlo analysis on any strategy/security combination.

    As an Investor I can:
    -View my portfolio balance and uninvested cash at any time.
    -View my performance by:
    -Time period
    -Strategy
    -Security
    -View my total market exposure.

    _______________________________

    This weekend I will be prioritizing User Stories and coding some of the most basic functions. I still don't have platform access, so I won't be posting the code until after I have tested it.

    I'm also still reading The Black Swan, which is a bit more serious piece of work than Fooled By Randomness. Very good stuff though, and it encouraged me to shelve the "Pre-Determined Targets" tenet - I don't want to encourage looking into the future.

    Enjoy,

    Damien

    PS There was a question about calculating risk - the code outline I post will show how this is calculated. Forthcoming.
     
    #25     Jan 29, 2010
  6. Chabah

    Chabah

    I prioritized the User Stories this weekend, classifying as "High" those that are required to get a minimal system up and running.

    I coded my Close Position function, which handles closing any position. As mentioned it isn't yet in TS, but I'll post once I debug it. It handles Limit, Stop, and Market orders in a compact set - should be handy. Open Position will be a lot more challenging.

    I am currently still reading The Black Swan (great stuff!), along with a handy EasyLanguage primer called "EL_Essentials.pdf" which you can Google and download for free. This is a bit more readable than the language reference. I've found some pretty handy stuff in there I haven't used before, such as writing data to a file, querying other symbols to determine positions across the portfolio, and how alerts can be managed (not real happy they they have to be set at the chart level, though...and that they can't be part of a strategy). Commentary analysis also looks interesting for walking through backtests.

    So that's the plan for now - continue coding functions (about 1 every day or two), reading theory (Black Swan and then Dynamic Hedging), and learning language specifics. I should be up with platform access next week, at which time getting a "Walking Skeleton" will become the priority.

    Damien
     
    #26     Jan 31, 2010
  7. Great thread. Keep it coming
     
    #27     Jan 31, 2010
  8. Chabah

    Chabah

    Here's a little code to illustrate how I see the system operating on a daily basis. One aspect is controlling system variables. I see the Operator having one Desktop, called "Controls", that has the production version of the dzd_sg__Controls function open. To edit a setting he goes to that tab, updates the value (for example, changing MaxPositionSize from 10000 to 5000), then recompiles the function. Positions will then re-calibrate based on the change (positions will reduce to be less than $5k).

    I created the code in Google spreadsheets, using cell references to minimize typos and to allow me to change the name of a function or variable without having to update a bunch of code references. Kinda cool for now, though it may prove cumbersome once I have the platform up.

    Here is the "compiled" (concatenated cells) code. Sorry for any typos - this isn't compiled yet, but rather is meant to be illustrative:

    dzd_sg__Controls {Settings for system functions }
    { " }
    Inputs:
    RequestType ( "" );
    Variables:
    AllowTrading ( TRUE ), { Change to False to stop all trading }
    MaxRiskUnits ( 2 ), { Adjust max up or down }
    MaxPositionSize ( 5000 ), { Max position in dollars }
    MinPositionSize ( 1000 ), { Min position in dollars }
    RiskUnit ( 100 ), { Risk amount assuming normal conditions}
    StrategyStartDate ( 1090101 ), { YYYMMDD where YYY is YYYY - 1900 }
    StrategyEndDate ( 1090331 ); { YYYMMDD where YYY is YYYY - 1900 }

    switch ( RequestType ) begin
    default : dzd_sg__Controls = FALSE ;
    case "AllowTrading" : dzd_sg__Controls = AllowTrading ;
    case "MaxRiskUnits" : dzd_sg__Controls = MaxRiskUnits ;
    case "MaxPositionSize" : dzd_sg__Controls = MaxPositionSize ;
    case "MinPositionSize" : dzd_sg__Controls = MinPositionSize ;
    case "RiskUnit" : dzd_sg__Controls = RiskUnit ;
    case "StrategyStartDate" : dzd_sg__Controls = StrategyStartDate ;
    case "StrategyEndDate" : dzd_sg__Controls = StrategyEndDate ;
    end;
     
    #28     Feb 1, 2010
  9. impressive thread. keep it coming.
     
    #29     Feb 2, 2010
  10. Chabah

    Chabah

    Whew, long slog writing the Open_Position function. Handles a lot, such as computing risk, allowing for open P/L when adjusting in the same direction, casting orders as Market, Stop, or Limit (Long or Short), and a couple other tricks. There are doubtless typos galore so I won't share the entire thing yet, but since someone asked about risk calculation I will post that portion:

    {Get and check Risk Units}
    RiskUnits = dzd_sg_MaxRiskUnits ( RequestedRiskUnits ) ;
    {Compute risk amount}
    RiskAmount = RiskUnits * dzd_sg_RiskUnit ;
    RiskPerShare = ABS ( ActionPrice - ExitPrice ) ;
    {Adjust by negative P/L if trading in same direction}
    if ( MarketPosition = 1 and
    Direction = "Long" ) or
    ( MarketPosition = -1 and
    Direction = "Short" ) then begin
    OpenPosPL = GetPositionOpenPL(GetSymbolName,GetAccountID);
    if OpenPosPL < 0 then
    RiskAmount = RiskAmount - OpenPosPL ;
    end;
     
    #30     Feb 2, 2010