USEFUL TRADING AGENTS FOR THE PLAT PROJECT

Trading agents and this documentation written by Elliot Feng, September 2004


There are some popular trading strategies that are widely used in real markets. These include various technical analysis trading strategies, market making, and VWAP trading, etc. In this page, we will briefly describe several techinal analysis agents, a market making agent, and a background trading agent that were developed for PLAT. We will assume that you have some background knowledge on these strategies, and won't dip into great details. We will also give instructions for how to attain and use these agents.

We also assume that the readers of this page have read the most recent technical documentation for PXS and clients:

  • Building PXS trading clients and running simulations
  • PXS client API


    Technical Analysis Agents

    The term "Technical Analysis" is a general heading for a myriad of trading techniques. Although with rather different means, such strategies all attempt to predict the price movement of a stock by studing historical data. Four technical analysis agents are discussed below.

    I. Moving Average

    We implemented the simplest moving average strategy, which calculates the moving average for a certain time interval specified by the user. When the current price up-crosses the moving average, the agent will buy in; when it down-crosses the moving average, the agent will sell out.

    The source codes can be found at (all the path information in this page refers to crux):

  • /home/pxs/PAT/Agents/strategy.c.ma
  • /home/pxs/PAT/Agents/strategy.h.ma
    The executable can be found at:
  • /home/pxs/PAT/Agents/ma

    The parameters are as follows:

  • -v: the number of shares of each order this agent puts in
  • -d: the threshold of the crossing
  • -i: the frequency that the agent calculates and updates the moving average, makes the comparison, and decides whether to place order, in seconds (time of one interval)
  • -l: the length of the window the agent calculates the moving average (number of intervals)

    An example:
    ./ma -p 10000 -v 1000 -d 0.0 -i 10 -l 20 >& out.ma &
    The agent will calculate and poll a 20-interval moving average at a 10-seconds pace. At the moment the current price up/down-crosses the moving average, and is 0.01 higher/lower than the moving average value, the agent will place a buy/sell order of 500 shares.

    II. Channel Breakouts

    Also known as "trading range breakouts", this strategy records the highest and lowest prices for a certain time interval. When the current price goes higher than the highest price recorded, or lower than the lowest price recorded, we called that there is a breakout of the channel, and a corresponding buy or sell order is placed in.

    The source codes can be found at:

  • /home/pxs/PAT/Agents/strategy.c.cb
  • /home/pxs/PAT/Agents/strategy.h.cb
    The executable can be found at:
  • /home/pxs/PAT/Agents/cb

    The parameters are as follows:

  • -v: the number of shares of each order this agent puts in
  • -d: the threshold of the breakout
  • -i: the frequency that the agent updates the channel, makes the comparison, and decides whether to place order, in seconds (time of one interval)
  • -l: the length of the channel the agent maintains (number of intervals)

    An example:
    ./cb -p 10000 -v 1000 -d 0.0 -i 10 -l 20 >out.cb &
    The agent will record and update a 20-interval price channel at a 10-seconds pace. At the moment the current price breaks the recorded price channel, and is 0.01 higher/lower than the highest/lowest price, the agent will place a buy/sell order of 500 shares.

    III. Momentum

    This simple strategy will compare the current price with the price at some time span ago. If the current time is higher, it buys in; otherwise, it sells out.

    The source codes can be found at:

  • /home/pxs/PAT/Agents/strategy.c.mo
  • /home/pxs/PAT/Agents/strategy.h.mo
    The executable can be found at:
  • /home/pxs/PAT/Agents/mo

    The parameters are as follows:

  • -v: the number of shares of each order this agent puts in
  • -d: the threshold of the price difference
  • -i: the frequency that the agent compares prices and decides whether to place order, in seconds (time of one interval)
  • -l: the time span in the history where the agent polls the historical price (number of intervals)

    An example:
    ./mo -p 10000 -v 1000 -d 0.01 -i 10 -l 20 >& out.mo &
    The agent will compare the current price with a 20-interval-ago historical price at a 10-seconds pace. At the moment the current price is 0.01 higher/lower than the historical price, the agent will place a buy/sell order of 500 shares.

    IV. Relative Strength Index

    Unlike the three trend-following strategies described above, Relative Strength Index (RSI) is a kind of price oscillators which predicts the overbought/oversold market. The agent will record the number and the spans of Ups and Downs.It employs a formula as follows to calculate the RSI value:
    RSI = 100 - 100/(1+U/D)
    U denotes the average value of Ups and D denotes the average value of Downs. Today most practitioners regard RSI over 70 indicating the market tops while below 30 indicating the bottoms. So they will buy when RSI crosses up the 30 threshold and sell when RSI crosses down the 70 threshold.

    The source codes can be found at:

  • /home/pxs/PAT/Agents/strategy.c.rsi
  • /home/pxs/PAT/Agents/strategy.h.rsi
    The executable can be found at:
  • /home/pxs/PAT/Agents/rsi

    The parameters are as follows:

  • -v: the number of shares of each order this agent puts in
  • -d: the RSI threshold
  • -i: the frequency that the agent calculates and compares the current RSI and previous RSI, and decides whether to place order, in seconds (time of one interval)

    An example:
    ./rsi -p 10000 -v 10000 -d 49 -i 5 >& out.rsi &
    The agent will calculates the RSI value and compare it with the previous RSI at a 10-seconds pace. At the moment the current RSI value is an up-cross/down-cross of the 42/58 RSI threshold, the agent will place a buy/sell order of 5000 shares.

    V. The Market Making Agent

    The market making strategy exploits the price volatility of a stock, rather than predicting the direction of its movement. The agent we developed here will simply put in a pair of buy and sell orders close to the bid/ask at each time interval.

    The source codes can be found at:

  • /home/pxs/PAT/Agents/strategy.c.mm
  • /home/pxs/PAT/Agents/strategy.h.mm
    The executable can be found at:
  • /home/pxs/PAT/Agents/mm

    The parameters are as follows:

  • -v: the number of shares of each order this agent puts in
  • -d: the distance from the order price to the bid/ask
  • -i: the frequency that the agent places order, in seconds (time of one interval)

    An example:
    ./mm -p 10000 -v 500 -d 0.02 -i 10 >& out.mm &
    The agent, at a 10-seconds pace, will place a pair of buy and sell orders of 500 shares at 0.02 lower than the current bid and 0.02 higher than the current ask.


    The Background Agent

    This is not an agent of any trading strategy. Instead, it is a special agent that produces orders on both buy and sell queues with a normal distribution regarding the current bid and ask. It is designed to simulate the general activity of the stock market. This agent needs to read in a file named "distribution.normal" to calculate the prices for orders it generates.

    The source codes can be found at:

  • /home/pxs/PAT/Agents/strategy.c.bg
  • /home/pxs/PAT/Agents/strategy.h.bg
    The executable can be found at:
  • /home/pxs/PAT/Agents/bg
    The files of normal distribtion (mean = 0, standard deviation = 0.05) can be found at:
  • /home/pxs/PAT/Agents/normal_distribution/

    The parameters are as follows:

  • -v: the number of shares of each order this agent puts in
  • -d: the distance from the mean of the normal distribution to the bid/ask
  • -i: the frequency that the agent places order, in seconds (time of one interval)

    An example:
    ./bg -p 10000 -v 500 -d 0.05 -i 5 >& out.bg &
    At a 5-seconds pace, the agent will read in two numbers from the distribution file, and place a pair of buy and sell orders of 500 shares according to these two numbers (as if the mean of the distribution is 0.05 lower than the current bid and 0.05 higher than the current ask).


    The Asymmetric Background Agent

    This is a new background agent for producing orders on both buy and sell queues with deltas of two normal distributions. It is named "abg" (asymmetric background agent for short). It differs from the original symmetric bg agents in the following two pricing aspects: 1. It reads in two distribution files of deltas, one for buy orders and the other for sell orders. These two distributions may have different standard deviations. The two files need to be named as "distribution.buy" and "distribution.sell", and put into the same directory where your abg agent resides. 2. It lets the user to set different distances from the mean of the normal distribution to the bid and the ask (for the original symmetric one, these distnaces are set by one parameter).

    The source codes can be found at:

  • /home/pxs/PAT/Agents/strategy.c.abg
  • /home/pxs/PAT/Agents/strategy.h.abg

    The executable can be found at:

  • /home/pxs/PAT/Agents/abg

    The new delta files of normal distribtion with various standard deviations can be found at:

  • /home/pxs/PAT/Agents/new_distribution_files/
    These files are named as: distributionX.XX-Y, where X.XX denotes the standard deviation and Y is just an index (like the indices of the files we used before for the old bg agent). For example, "distribution0.2-3" is one file of deltas of normal distribution with standard deviation equal to 0.2. Here is an example of preparing these distribution files for running abg. Suppose your current path is where your abg agent is, the following commands:
    cp /home/pxs/PAT/Agents/new_distribution_files/distribution0.08-1 distribution.buy
    cp /home/pxs/PAT/Agents/new_distribution_files/distribution0.15-3 distribution.sell

    will give your abg agent a buy distribution with standard deviation 0.08 and a sell distribution with standard deviation 0.15.

    The parameters for abg are as follows:

  • -v: the number of shares of each order this agent puts in
  • -d: the distance from the mean of the normal distribution to the bid
  • -z: the distance from the mean of the normal distribution to the ask
  • -i: the frequency that the agent places order, in seconds (time of one interval)

    An example:
    ./abg -p 10000 -v 500 -d 0.05 -z 0.07 -i 2 >& out.abg &
    At a 2-seconds pace, the agent will read in two numbers from the two distribution files (distibution.buy and distribution.sell), and place a pair of buy and sell orders of 500 shares according to these two numbers (as if the mean of the buy distribution is 0.05 lower than the current bid, and the mean of the sell distribution is 0.07 higher than the current ask).

    Notes:

  • When running abg, you need to specify the pxs to read data from artificial date "12322004", just as running the old bg agent.
  • We suggest that you set the -i option to 2 when you run abg with your own agent. This is to guarantee abg will trade not less frequently than your agent.


    The Real-Data Background Agent

    This agent is named "realbg", since it generates real-data order stream to produce the background activity. This agent needs to read three files to reconstruct the real-data order stream of a day. These files require to be put in the same directory with the realbg executable, and named as in the following descriptions. 1. File "time" consists of a sequence of timestamps of the order stream. 2. File "volume" consists of a sequence of numbers of shares of the order stream. 3. File "delta" consists of a sequence of deltas, which are the price differences between the current bid/ask (depending on the type of the order) and the incoming orders. Basically, to reconstruct an order, the realbg agent will read in a timestamp (to see if it's time to insert this order), a number of order shares (to determine if it's a buy or sell order and how many shares this order has), and a price delta (to set the price of the order with regard to the current bid/ask) from those three files respectively.

    The source codes can be found at:

  • /home/pxs/PAT/Agents/strategy.c.realbg
  • /home/pxs/PAT/Agents/strategy.h.realbg

    The executable can be found at:

  • /home/pxs/PAT/Agents/realbg

    We provide the 10 days of June 2004 data for DELL, YHOO, and MSFT at:

  • /home/pxs/PAT/Agents/JuneData/
    The files are named as: XXXX.YYYY.ZZZZ, where XXXX denotes the name of the stock, YYYY is the date, and ZZZZ is for the contents of the file ("time", "volume", or "delta"). For example, "yhoo.0602.delta" is the file that contains price delta information for June 2nd YHOO orders. Here is an example of preparation for running realbg agent on June 10th MSFT real data. Suppose your current path is where your realbg agent is, the following commands will have the three files in place:
    cp /home/pxs/PAT/Agents/JuneData/msft.0610.time time
    cp /home/pxs/PAT/Agents/JuneData/msft.0610.volume volume
    cp /home/pxs/PAT/Agents/JuneData/msft.0610.delta delta

    There is NO parameter for the realbg agent, as its pricing, volume, and trade frequency are all determined by the three data files.
    An example:
    ./realbg -p 10000 >& out.realbg &
    The realbg agent will produce the real-data order stream for a day of a pariticular stock for which you have copied the three data files.

    Notes:

  • When you run the realbg agent, you need to specify your pxs to run on "12332004", which is a different artificial date with completely empty information (while "12322004" has a starting book file). However, the stock name, i.e. the -n option in the pxs command line, should still be set as "MSFT" (although you may now actually be running on DELL or YHOO).
  • Be aware not to copy inconsistent data files. You need to have all the "time", "volume", and "delta" files for the same day of a same stock.


    Notes on Running These Agents

  • The PXS was designed to take in Island market data. From the tutorial, we know that we need to specify a date for each simulation for the Island data the simulator will read in. Since we are now using the agents without real Island input, we built a virtual empty date "12322004" in order to cope with the PXS requirement. All the technical analysis agents above use simulator price signal. So if just running them with the real island data input, they won't trade at all. At this point, whenever you wanna run the trading agents described above, you should also run the background agent, and specify the date as 12322004.

  • All the agents above, except the background agent, will liquidate itself from 15:30 to 16:00 (normally the end of the simulation). Liquidation means to clear one's long or short share position.

  • There is a Q value print out in the client output for each agent. Unlike simple P&L, that's a measurement of the return of the agent. This Q value ranges from -1 to positive infinity. Larger value means larger return.

  • You can copy the executable of the agents above to your own directory, or run them directly from their original location.
    These agents can be run together with or without the agent developed by a user. Suppose "mytrader" is the agent developed by the user and the current path contains all the excecutables, the following commands:
    /home/pxs/PAT/stable/pxs -p 10000 -n MSFT -h 12322004093000 -e 160000 -s 3 >& pxs.out &
    ./bg -p 10000 -v 500 -d 0.05 -i 5 >& out.bg &
    ./mo -p 10000 -v 500 -d 0.01 -i 10 -l 20 >& out.mo &
    ./mytrader -p 10000 >& out.mytrader &
    will run the pxs with a background agent, a momentum agent, and the agent developed by the user.

    IMPORTANT NOTE: In the invocation of pxs above, the date argument -h 12322004093000 is a special and fictitious date (Dec 32), and tells pxs to NOT look for a source of real data, but to use only the (in this case three) agents that are subsequently started. Thus, the stock name flag -n MSFT is ignored when the fictitious default date is given.

  • You can also make plots of your client output. We have a plotting script in the pxs directory for plotting the client value (P&L), share position, and the simulator price of the simulation. It's a very useful tool to analyse the behavior of the agents, and you are recommended to use it. Here is an example of using the script, assuming out.cb is the client output file you are interested in, and the file is in your current path:
    /home/pxs/PAT/scripts/bg_scripts/plotall.client.pl out.cb
    This will produce a plot with the name "simulation.ps" that contains the three figures discussed above.

  • We suggest that all the simulations to be run from 09:30 to 16:00. That's because all the agents are deviced to begin trading at 09:30, and liquidate (BG doesn't liquidate though) from 15:30 to 16:00.

  • For all the technical analysis agents (i.e. MA, CB, MO, RSI), when they receive a price signal to place an order, they always place a market order. In other words, they place some buy order with extremely high price or sell order with extremely low price.

  • For all the agents behave properly, one should tune the trading volume of the BG agent large enough compared with the strategy agents (both volume and frequency parameters matter here, check the default settings in the source file). That's a proper way to simulate that the market provides sufficient volume to the traders. Although the traders can trade a lot and impact the market, they should never be able to exhaust the books in either queue at anytime, especially when they are placing market orders.