TS code help - scaling out of position

Discussion in 'Strategy Development' started by chessman, Jan 31, 2004.

  1. chessman

    chessman Guest

    I need help with writing Tradestation easy lang code to scale out of positions. For example if the system buys 4 contracts at 70, I would like to exit 1 contract with a $500 profit, second contract with $1000 profit and the remaining 2 contracts ride until the exit is hit.

    I have tried the following on 5 min charts and have run into problems:

    1) Sell 1 contract at next bar entryprice + 500 limit;

    * ts documentation says that this order is only good for the next bar and is then cancelled. I would like a persistent limit order until the price is hit.

    2) SetProfitTarget( ProfitTargetAmt ) ;

    * this function dumps the entire position & then triggers another signal if the condition is met again. I would like to only scale out of 1 or 2 contracts and then wait for the next 'sell' signal.

    Any help with sample code would be greatly appreciated.
     
  2. I think this will work, so let me know if it doesn't.


    If you know the contract size, this should be easy. Let's say you're using the ES ($50). This would equate to a 10 pt move from the entry price. Easylanguage would be something like:

    inputs: tradesize(4);

    If Barssinceentry>0 and currentcontracts=tradesize{4 contracts} then sell ("$500 LX") 1 contract next bar at entryprice+10 limit;
    If Barssinceentry>0 and currentcontracts=tradesize-1{3 contracts} then sell ("$1000 LX") 1 contract next bar at entryprice+20 limit;

    ...then you can just plug your last exit in, and it should take you out of the rest of the position.

    (if contract size will vary, there are things you can do to have TS automatically calculate that as well...also you could create an input so you could manually adjust the size of targets 1 and 2.)

    Also, this will not exit you on the entrybar, as EL needs one bar to figure out that your position exists....

    Good luck!
     
  3. chessman

    chessman Guest

    Thanks for your reply. Your sample code makes sense, I need to make a few changes to my existing strategy code to incorporate what you sent. I will be in touch. All the best.