[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Portfolio Trading Functions in TradeStation Part 1



PureBytes Links

Trading Reference Links

At 05:10 AM 5/7/2004, Adrian Pitt wrote:

>Can I assume from this Bob that you actively use TS for your model testing & 
>design & portfolio testing? Along with Excel of course.

Yes. I first began using this about two years ago to select the best combination of the 30 Fidelity select funds and have used it quite a bit since.

At 09:38 AM 5/7/2004, Sam West wrote:

>> The Buy/Sell functions I referred to are my own functions that have
>nothing to do with the TradeStation commands. I keep track of the portfolio
>arithmetic in the functions.
>>
>> Pretty simple really...
>
>Bob, could you expand on this a little?

I have many versions so I will describe a recent version. This supports only market orders but could easily be modified to handle stop. limit, and at close orders. It can trade up to 50 data streams in a single chart. It works in TradeStation 7. Alex said that the call-by-reference feature is faulty in TradeStation 2000i so it might not work there.

At the beginning of a program is a call to my "xExecuteOrders" function

   ID1 = xExecuteOrders(xNum, xSh, xCash, xLong, xShort, 
         xPort, xChange, jMax, Comm, PrntMode);

This executes market orders at the opening prices for any position changes entered on the previous bar. All of the arguments beginning with "x" are simply passing values to and from the function using the call-by-reference feature. The last three arguments are values that are global to all functions and set as inputs to the main program.

xNum (array) stores the changes to be made in each data series
xSh  _array) stores the share values for each of the (up to 50) data streams
xCash keeps the cash balance
xLong is the value of all long positions
xShort is the value of all short positions
xPort is the value of the portfolio including xCash
xChange is a TrueFalse flag to indicate if there has been a change in any position on this bar.
jMax is the maximum number of data series used.
Comm is the commission per share/contract
PrntMode is a value to indicate what is to be printed, if anything.

I have simple buy and sell functions that are only useful on futures:

    ID5 = xBuyAtMkt(Num, s, xNum, PrntMode, Futures);
    ID5 = xSellAtMkt(Num, s, xNum, PrntMode, Futures);

Num is the number of contracts to buy/sell
s is the data series to buy/sell

"Futures" is a TrueFalse flag that is used to check that these orders are only used for futures. For example, to go from long 3 to short 7 requires simply selling 10 contracts. (No complexity of sell short, etc.)

I also have a "xSetPositionAtMkt" function I like:

  ID2 = xSetPositionAtMkt(Num, s, xNum, xSh, PrntMode, Futures);

This causes a position to become that specified from whatever state it is in. It handles the complexity of stocks, etc. For example to go from long 3 to short 7 requires two orders: Sell 3 and Sell short 7. The logic handles that complexity.

There is also a "xExitAllAtMkt" function which does what it says.

   ID5 = xExitAllAtMkt(xSh, xNum, jMax, PrntMode, Futures);

Most of these calls use the call-by-reference feature which simply passes the address of the variable or array so the values can be modified in the function and returned to the calling program as part of the array.


The following is a full debugging printout of a system trading a spread of INTC and QQQ. You get the idea. 

Further down below is the printout of just the orders to be placed.

Hope this is helpful. I will post a chart on part 2 of this.

Bob Fulks

------------------------------------------------------------------------


09/26/2002 1615 *Set Pos       110 Shares of INTC     next bar at Market
09/26/2002 1615  Buy           110 Shares of INTC     next bar at Market
09/26/2002 1615 *Set Pos      -100 Shares of QQQ      next bar at Market
09/26/2002 1615  SellShort     100 Shares of QQQ      next bar at Market
09/27/2002 1615  -Bought       110 Contracts of INTC     at Open, Position =      110
09/27/2002 1615  -Sold         100 Contracts of QQQ      at Open, Position =     -100
09/27/2002 1615   1    110   -100   100603.00     1608.20    -2131.00   100080.20
09/27/2002 1615 *Set Pos      -110 Shares of INTC     next bar at Market
09/27/2002 1615  Sell          110 Shares of INTC     next bar at Market
09/27/2002 1615  SellShort     110 Shares of INTC     next bar at Market
09/27/2002 1615 *Set Pos       100 Shares of QQQ      next bar at Market
09/27/2002 1615  BuyToCover    100 Shares of QQQ      next bar at Market
09/27/2002 1615  Buy           100 Shares of QQQ      next bar at Market
09/30/2002 1615  -Sold         220 Contracts of INTC     at Open, Position =     -110
09/30/2002 1615  -Bought       200 Contracts of QQQ      at Open, Position =      100
09/30/2002 1615  -1   -110    100    99524.00     2072.00    -1527.90   100068.10
10/04/2002 1615 *Set Pos       110 Shares of INTC     next bar at Market
10/04/2002 1615  BuyToCover    110 Shares of INTC     next bar at Market
10/04/2002 1615  Buy           110 Shares of INTC     next bar at Market
10/04/2002 1615 *Set Pos      -100 Shares of QQQ      next bar at Market
10/04/2002 1615  Sell          100 Shares of QQQ      next bar at Market
10/04/2002 1615  SellShort     100 Shares of QQQ      next bar at Market
10/07/2002 1615  -Bought       220 Contracts of INTC     at Open, Position =      110
10/07/2002 1615  -Sold         200 Contracts of QQQ      at Open, Position =     -100
10/07/2002 1615   1    110   -100   100555.40     1520.20    -2016.00   100059.60
10/08/2002 1615 *Set Pos      -110 Shares of INTC     next bar at Market
10/08/2002 1615  Sell          110 Shares of INTC     next bar at Market
10/08/2002 1615  SellShort     110 Shares of INTC     next bar at Market
10/08/2002 1615 *Set Pos       100 Shares of QQQ      next bar at Market
10/08/2002 1615  BuyToCover    100 Shares of QQQ      next bar at Market
10/08/2002 1615  Buy           100 Shares of QQQ      next bar at Market
10/09/2002 1615  -Sold         220 Contracts of INTC     at Open, Position =     -110
10/09/2002 1615  -Bought       200 Contracts of QQQ      at Open, Position =      100
10/09/2002 1615  -1   -110    100    99427.40     2006.00    -1480.60    99952.80
10/09/2002 1615 *Set Pos       110 Shares of INTC     next bar at Market
10/09/2002 1615  BuyToCover    110 Shares of INTC     next bar at Market
10/09/2002 1615  Buy           110 Shares of INTC     next bar at Market
10/09/2002 1615 *Set Pos      -100 Shares of QQQ      next bar at Market
10/09/2002 1615  Sell          100 Shares of QQQ      next bar at Market
10/09/2002 1615  SellShort     100 Shares of QQQ      next bar at Market
10/10/2002 1615  -Bought       220 Contracts of INTC     at Open, Position =      110
10/10/2002 1615  -Sold         200 Contracts of QQQ      at Open, Position =     -100
10/10/2002 1615   1    110   -100   100479.60     1559.80    -2108.00    99931.40


Restricting the printing to just the actual orders to be placed results in the following:

09/26/2002 1615  Buy           110 Shares of INTC     next bar at Market
09/26/2002 1615  SellShort     100 Shares of QQQ      next bar at Market
09/27/2002 1615  Sell          110 Shares of INTC     next bar at Market
09/27/2002 1615  SellShort     110 Shares of INTC     next bar at Market
09/27/2002 1615  BuyToCover    100 Shares of QQQ      next bar at Market
09/27/2002 1615  Buy           100 Shares of QQQ      next bar at Market
10/04/2002 1615  BuyToCover    110 Shares of INTC     next bar at Market
10/04/2002 1615  Buy           110 Shares of INTC     next bar at Market
10/04/2002 1615  Sell          100 Shares of QQQ      next bar at Market
10/04/2002 1615  SellShort     100 Shares of QQQ      next bar at Market
10/08/2002 1615  Sell          110 Shares of INTC     next bar at Market
10/08/2002 1615  SellShort     110 Shares of INTC     next bar at Market
10/08/2002 1615  BuyToCover    100 Shares of QQQ      next bar at Market
10/08/2002 1615  Buy           100 Shares of QQQ      next bar at Market
10/09/2002 1615  BuyToCover    110 Shares of INTC     next bar at Market
10/09/2002 1615  Buy           110 Shares of INTC     next bar at Market
10/09/2002 1615  Sell          100 Shares of QQQ      next bar at Market
10/09/2002 1615  SellShort     100 Shares of QQQ      next bar at Market
10/10/2002 1615  Sell          110 Shares of INTC     next bar at Market
10/10/2002 1615  SellShort     110 Shares of INTC     next bar at Market
10/10/2002 1615  BuyToCover    100 Shares of QQQ      next bar at Market
10/10/2002 1615  Buy           100 Shares of QQQ      next bar at Market
10/15/2002 1615  BuyToCover    110 Shares of INTC     next bar at Market