How to limit simultaneous open positions

1,011 views
Skip to first unread message

mandelmus

unread,
Aug 21, 2012, 2:32:35 PM8/21/12
to adaptrad...@googlegroups.com
I'm trying out an AB strategy on a tick chart in TradeStation and it's not "waiting for exit before entering a new trade" as I had selected in the Build options.  Is this because I'm using a tick chart?  I noticed there is an option, in Tradestation, to "Allow up to __ entry orders in the same direction as the currently held position:" under the "Strategy Properties for All" dialog box.  Is that supposed to do the same thing if I set it to 0?  It doesn't seem to be working either.  I don't mind up to 3 simultaneous open positions.  Any ideas for another approach to limiting simultaneous open positions?

Mark Knecht

unread,
Aug 21, 2012, 2:37:58 PM8/21/12
to adaptrad...@googlegroups.com
Have you read the EasyLanguage code generated by Builder to determine
what you believe it's supposed to do?

If you're just looking for Mike to answer you personally then OK.
However this is a group list and if you want any help from the rest of
us you could post some code for discussion.

HTH,
Mark

mandelmus

unread,
Aug 21, 2012, 11:30:42 PM8/21/12
to adaptrad...@googlegroups.com
Sure, anyone can respond.  I posted AB's code below.  I selected the option to "wait for exit before entering a new trade", but that didn't work as I expected it should.  The strategy I posted will keep adding positions until Tradestation rejects the new positions due to exceeding account equity.  I selected to exit all positions at 1458, but I also noticed that the strategy didn't exit at 1458 as I expected it should.  So, I had to manually exit all open positions.  I'm still learning to code Tradestation EL, so go easy on me. Perhaps someone will look at the code and give me some ideas.

I spent months trying to find good combinations of strategies and symbols.  I finally found some "interesting" strategies.  Next, I spent weeks learning how to get Tradestation's "Strategy Performance Report" to mimic Builder's OOS Performance results.  Now, I'm trying to learn how to get Tradestation's demo and live trading performance to mimic the results listed on Tradestation's "Strategy Performance Report".

I know that actual share availability, slippage, partial fills, account equity limitations, etc will ensure that actual strategy performance will never match backtest performance, but I'm looking for some guidance on how to get the actual performance closer to the expected results based on both Builder's strategy performance and Tradestation's "Strategy Performance Report".  I also tried optimizing some of the values from within TS.  While it improved the TS backtest performance, the forward performance did not show the same.

Here's the code.  This is a long-only strategy on 1 tick data.  Try it out on stocks over $200, like GOOG, over the last 2 days.
{
 EasyLanguage Strategy Code
 Population member: 27
 Max bars back: 96

 Created by: Adaptrade Builder version 1.3.0.1
 Created:    8/21/2012 2:28:19 AM

 Compatible with TradeStation 6 or newer

 Price File:  C:\Program Files\Adaptrade Software\Adaptrade Builder 1.3\Examples\1tick (2012.04.25-2012.05.04 downtrend).txt
 Build Dates: 4/25/2012 to 4/25/2012

 Project File: C:\Program Files\Adaptrade Software\Adaptrade Builder 1.3\Examples\1tick 2012.08.20d.gpstrat
}

{ Strategy inputs }
Inputs: FirstEntryTm (830),
        LastEntryTm (1458),
        NBarEn1 (90),
        NATREn (83),
        EntFr (2.7944),
        TargFr (0.9845),
        NATRTrail (96),
        ATRFrTrail (4.1464),
        TrailPct (44.0000),
        NBarEx (17),
        TimeEx (1459),
        PSParam (50.00),
        RoundPS (true),
        RoundTo (1),
        MinSize (1),
        SizeLimit (1);

{ Variables for average true range for entry and exit orders }
Var:    ATREn (0),
        ATRTrail (0);

{ Variables for entry and exit prices }
Var:    EntPrL   (0),
        TargPrL  (0),
        LStop    (0),
        NewLStop (0),
        LTrailOn (false);

{ Variables for entry and exit conditions }
Var:    EntCondL  (false),
        TimeOK    (false);

{ Variables for position sizing }
Var:    NShares  (0);

{ Average true range }
ATREn = AvgTrueRange(NATREn);
ATRTrail = AvgTrueRange(NATRTrail);

{ Entry prices }
EntPrL = WAverage(C, NBarEn1) - EntFr * ATREn;

{ Entry and exit conditions }
EntCondL = true;
TimeOK = time >= FirstEntryTm and time < LastEntryTm;

{ Position sizing calculations }
NShares = PSParam;

If RoundPS and RoundTo > 0 then
   NShares = IntPortion(NShares/RoundTo) * RoundTo;

NShares = MaxList(NShares, MinSize);
NShares = MinList(NShares, SizeLimit);

{ Entry orders }
If MarketPosition = 0 and EntCondL and TimeOK then begin
   Buy("EnLimit-L") NShares shares next bar at EntPrL limit;
end;

{ Exit orders, long trades }
If MarketPosition > 0 then begin

   If BarsSinceEntry = 0 then begin
      LStop = 0;
      LTrailOn = false;
   end;

   If C - EntryPrice > ATRFrTrail * ATRTrail then
      LTrailOn = true;

   If LTrailOn then begin
      NewLStop = EntryPrice + TrailPct * (C - EntryPrice)/100.;
      LStop = MaxList(LStop, NewLStop);
   end;

   If time >= TimeEx then
      Sell("ExTime-L") next bar at market;

   If BarsSinceEntry >= NBarEx then
      Sell("ExNBars-L") next bar at market;

   If LTrailOn then
      Sell("ExTrail-L") next bar at LStop stop;

   TargPrL = EntryPrice + TargFr * TrueRange;
   Sell("ExTarg-L") next bar at TargPrL limit;
end;

Mark Knecht

unread,
Aug 22, 2012, 10:50:12 AM8/22/12
to adaptrad...@googlegroups.com
On Tue, Aug 21, 2012 at 8:30 PM, mandelmus <gmb...@gmail.com> wrote:
<SNIP>
> I selected the option
> to "wait for exit before entering a new trade", but that didn't work as I
> expected it should.
<SNIP>

Then maybe your expectations are wrong?

This code:

[QUOTE]

If MarketPosition = 0 and EntCondL and TimeOK then
begin
Buy("EnLimit-L") NShares shares next bar at EntPrL limit;
end;

[/QUOTE]

contains the only 'Buy' order in your code and CLEARLY it only enters
when MarketPosition = 0;

I do not see how that code can:

<SNIP>
> keep adding positions until
> Tradestation rejects the new positions due to exceeding account equity.
<SNIP>

Again, it's just my opinion but just because Builder builds some code
doesn't mean that we users don't have a responsibility to understand
how the code works. Have you read and understood your code? I've only
glanced at it for 2 minutes but I don't see how it can do what you
suggest.

I then copied the code into a strategy file and applied it to a 1-tick
GOOG chart for the last 2 days. I never holds multiple positions of
adds positions that I can see.

It seems to me that nothing you are saying here is making any sense to
me so I suggest you take a deep breath, figure out what your issue is,
and then state it clearly with code, data, charts or something to
demonstrate it in a repeatable manner or else there's no way the rest
of us can participate. (Or at least I will not participate anymore
until those things clearly happen.) :-)

And BTW...

1) Do you really want to trade GOOG multiple times per minute? 3 days
produced 1075 trades?

2) Just my opinion, but if you were interested in trading this live
you had better look VERY deeply at how these limit orders are being
created and what the chances of getting filled really are. Running
this code on a 1-tick chart boggles my mind, but maybe there's a good
idea back there that I just don't understand.

3) This code buy single contracts. Is that what you really intend?
Question: Will market makers really do that in real life? Odd lot
fills and such?

Good luck!

Cheers,
Mark

mandelmus

unread,
Aug 22, 2012, 12:43:01 PM8/22/12
to adaptrad...@googlegroups.com
I responded within the text below, but basically, I'm asking you to run this in a live forward-test on a demo account.  It sounds like you only backtested the code.  The backtest results are fine -- it's the forward-test performance that has issues.


On Wednesday, August 22, 2012 9:50:12 AM UTC-5, LGTrader wrote:
On Tue, Aug 21, 2012 at 8:30 PM, mandelmus <gmb...@gmail.com> wrote:
<SNIP>
> I selected the option to "wait for exit before entering a new trade", but that didn't work as I expected it should.
<SNIP>

Then maybe your expectations are wrong?

This code:
If MarketPosition = 0 and EntCondL and TimeOK then
begin
   Buy("EnLimit-L") NShares shares next bar at EntPrL limit;
end;
 
contains the only 'Buy' order in your code and CLEARLY it only enters when MarketPosition = 0;

I do not see how that code can:
<SNIP>
> keep adding positions until
> Tradestation rejects the new positions due to exceeding account equity.
<SNIP>


Exactly, the code looks ok, but Tradestation keeps adding positions.  Why?

 
Again, it's just my opinion but just because Builder builds some code
doesn't mean that we users don't have a responsibility to understand
how the code works. Have you read and understood your code? I've only
glanced at it for 2 minutes but I don't see how it can do what you
suggest.

That's my point ... Tradestation keeps adding positions, but the code says not to.
 

I then copied the code into a strategy file and applied it to a 1-tick
GOOG chart for the last 2 days. I never holds multiple positions of
adds positions that I can see.

I know.  The backtest is fine ... forward test the code on a demo account and then let me know
 

It seems to me that nothing you are saying here is making any sense to
me so I suggest you take a deep breath, figure out what your issue is,
and then state it clearly with code, data, charts or something to
demonstrate it in a repeatable manner or else there's no way the rest
of us can participate. (Or at least I will not participate anymore
until those things clearly happen.) :-)

And BTW...

1) Do you really want to trade GOOG multiple times per minute? 3 days
produced 1075 trades?

My whole point is that Tradestation is not forward-trading this strategy as I think the code is instructing.  The backtest does fine, it's the forward test that is not cooperating.  I don't really care how many trades the strategy produces.  If there is positive expectancy, even if I only get a % of those trades filled, it could still be profitable.  And, if I really wanted to trade with that high frequency, I would definitely need a platform faster and better than TS.  Anyway, if there are problems with 1075 trades, are there the same problems with 1074 or 107?  What's the magic number of trades where there are no problems?
 
2) Just my opinion, but if you were interested in trading this live
you had better look VERY deeply at how these limit orders are being
created and what the chances of getting filled really are.

That's the whole point of forward-testing any strategy ... to answer those questions.  Now that I'm forward-testing the strategy, I'm asking these questions.
 
Running this code on a 1-tick chart boggles my mind, but maybe there's a good
idea back there that I just don't understand.

There is  ;)
 
3) This code buy single contracts. Is that what you really intend?
Question: Will market makers really do that in real life? Odd lot
fills and such?

This code buys shares at any number you want ... you'll have to change the minimum shares to around 10-15 in order to break-even/overcome commissions (r/t commission / expected avg trade profit).  But that's irrelevant to me at this point.  If it's making net profits at 1 share, all I have to do later is up the shares until it overcomes commissions.  I'm just trying to figure out how to configure Builder and/or TS to run the code as Builder is outputting it and as TS is showing in its own backtest reports.
 
Good luck!

Thanks
 
Cheers,
Mark

Michael R. Bryant

unread,
Aug 22, 2012, 1:25:12 PM8/22/12
to adaptrad...@googlegroups.com

Sounds like a setting in TS, but I don’t know off-hand why it would do that. I would check some of the settings in TS under Strategy Properties and maybe consult the associated documentation. You could also try searching the TS forum or even posting to it, but I’d suggest trying a bit longer to track down the issue on your own before posting on the TS forum.

 

Mike Bryant

 

Subject: Re: How to limit simultaneous open positions

Mark Knecht

unread,
Aug 22, 2012, 3:21:04 PM8/22/12
to adaptrad...@googlegroups.com
On Wed, Aug 22, 2012 at 9:43 AM, mandelmus <gmb...@gmail.com> wrote:
<SNIP>
>> I do not see how that code can:
>> <SNIP>
>> > keep adding positions until
>> > Tradestation rejects the new positions due to exceeding account equity.
>> <SNIP>
>>
>
> Exactly, the code looks ok, but Tradestation keeps adding positions. Why?
>

I did run it live, although not live trading, but I ran it on a live
market. The strategy did not add additional positions.

If you are asking me to run it such that it trades live, I won't do that.

<SNIP>
>
> That's my point ... Tradestation keeps adding positions, but the code says
> not to.
>

Not here...

>>
>>
>> I then copied the code into a strategy file and applied it to a 1-tick
>> GOOG chart for the last 2 days. I never holds multiple positions of
>> adds positions that I can see.
>
>
> I know. The backtest is fine ... forward test the code on a demo account
> and then let me know
>

I'll see about doing that, possibly tomorrow but probably later than
that. NO PROMISES!!!

That said, I've traded enough EL stuff live, both in simulation
accounts and real money accounts, to be fairly sure there's no problem
with the code you provided. However maybe someone else here has more
time than I do to help you out.

- Mark

mandelmus

unread,
Aug 25, 2012, 4:42:50 AM8/25/12
to adaptrad...@googlegroups.com
Well, I tried y'all's suggestions which helped in some ways.  I also posted some questions on the TradeStation forums.  Apparently, TS is not meant to handle rapid-fire orders that, I guess, many of my Builder strategies generated.  The @TS team suggested that what is probably happening with my strategies is that orders were being filled between being quickly placed and cancelled.  During the milliseconds time between the strategy sending the buy order and sending the cancel order, the orders were actually being filled.  As the conditions for the indicators change from go to no-go to go to no-go states, the strategy keeps sending buys and cancels.  I found an article discussing this challenge.  Another TS forum post suggested using a global variable to increment with each order as a check mechanism to prevent multiple orders.  I guess the strategies I'm using are assuming the order was not filled and/or cancelled so places another order in rapid succession.  When TS notices the mismatch between positions and strategy, it loads the pop-up screen asking what to do with the existing filled order -- I usually just select to close the open positions -- which often leads to closing with a loss on those trades.  If I keep the positions open, I soon max out available equity and the trades become unlinked from the strategy automation.  I'm guessing this scenario is the cause of multiple orders being opened simultaneously even when the strategy code is written to prevent that.  Using a strategy that trades less frequently has not solved this problem because as soon as the right conditions are met, the strategy just fires away.

The backtest of the same strategies in TradeStation are very close to what Builder's performance shows.  The problem is only in forward testing the strategy in my demo account.  LGTrader said the strategy I posted did not open multiple positions on his system, but at least 7 different Builder strategies are opening multiple positions even with the "MarketPosition = 0" code included.  I will continue to check setting in TS and research a solution on the TS forums.  Let me know if you have any tips for a work-a-round/solution to this challenge.
Reply all
Reply to author
Forward
0 new messages