Program Trading: Order Execution Strategy

Charlton

Experienced member
Messages
1,501
Likes
326
The order execution strategy will bring together the results of the other indicators being programmed by Pedro (Pete). Globalvariable values will be stored in each of these "modules" for calculation of position size, ranking of weak and strong stocks to produce a basket of each and buy/sell signals resulting from MLT cycle analysis.

Here is the start of some code that will ultimately glue the whole lot together and generate orders. This is a strategy that will be inserted into the same 1 min $INDU chart as the TT_ENTRY_POINT code.

There are settings within Tradestation that can be ticked on/off to allow the strategy to generate orders and to send them automatically.

AT THIS STAGE DO NOT USE THIS CODE ON A LIVE SYSTEM. ONLY USE IT AGAINST A SIMULATED ACCOUNT

There is a lot more work to be done on this and there is a dependency upon the global variables being generated by other modules, so it is not currently using the global variables but instead using inputs so that some tests can be performed idendently.

I will continue to work on this code, but I thought you might want to see something at this stage in order to feed into the design. I attach an ELD file but also the text for TS2000i users.

Charlton


{This strategy will ultimately receive signals from TT_ENTRYPOINT , TT_POSITION_SIZE and TT_RATE_WEAK_STRONG indicators
using global variables. For the moment these are represented by inputs}

Inputs:
Trigger(" "), //buy/sell trigger from TT_ENTRYPOINT based on cycle analysis
Basket1("IBM"), //this is the first stock in a basket, that will ultimately comprise say, the 5 weakest and 5 strongest stocks
Basket2("DELL"), //this is the second stock in a basket, that will ultimately comprise say, the 5 weakest and 5 strongest
Stop1(0.5), //this could be used for a fixed monetary stop, but has not been used in this script
pos_size(1),//position size will come from TT_POSITION_SIze via a global variable
ATRLENGTH(14), //this illustrates a stop based on ATRs using 14 periods s default
NUMATRS(1.5); //this illustrates that the stop might be set at 1.5 ATRS

VALUE1 = GETPOSITIONQUANTITY(GETSYMBOLNAME, GETACCOUNTID); //Need to know if we have any position open already for symbol and how current holding

{start of outermost loop to loop through each of the 5 (say) stocks. We will have different inputs or global variables for the strong basket
and the weak basket, so more loops wil probably be applid here}
IF GETSYMBOLNAME = Basket1 or value1 > 0 then //checks if current symbol processed is in first basket member or exists in the current portfolio
BEGIN

{start of trigger loop to process if a buy or sell signal received}
SWITCH (Trigger) BEGIN
CASE "BUY":
IF VALUE1 = 0 THEN BEGIN //if there is no open position for the symbol and we have a buy signal create a buy order together with stop
Buy ("LONG BASKET 1") pos_size Contracts next bar at market; //use position size in global variable from TT_POSTIION_SIZE
Sell ("ATR STOP SELL" ) next bar at Close - AvgTrueRange( ATRLength ) * NumATRs stop ;
END;

IF VALUE1 < 0 THEN //if there is an existing short position for symbol then the buy signal generates a buy to cover using existing position size
Buy ("SHORT COVER") VALUE1 Contracts next bar at market;



CASE "SELL":
IF VALUE1 = 0 THEN //if there is no open position for the symbol and we have a sell signal create a short sell order with stop
BEGIN
SELL ("SELL SHORT 1") pos_size Contracts next bar at market;
BUY ("ATR STOP COVER" ) next bar at Close - AvgTrueRange( ATRLength ) * NumATRs stop ;
END;

IF VALUE1 > 0 THEN //if there is an existing long position for symbol then sell that position size
SELL ("SELL") VALUE1 Contracts next bar at market;

{other items to add are conditions for closing 75% of trade and at same time generation of 25% stop order at breakeven + 2 cents profit.
Also need to consider what order types will be used - for the moment market orders are coded in.
Need to look at partly filled/unfilled orders - consider cancellations after x time/bars/ticks}



END;

END;
 

Attachments

  • TT_BUY_SELL STRATEGY.ELD
    5.6 KB · Views: 23
Charlton
Excellent stuff sir.
Those with TS2ki will have to use Ninjatrader, Tradebolt or somesuch for communicating to IB.
Will you be using Tradestation as your broker ?

Glenn
 
BTW - what I'll do now it work on the globals so they have the right settings to tell you which stocks are in the basket.
 
Charlton
Excellent stuff sir.
Those with TS2ki will have to use Ninjatrader, Tradebolt or somesuch for communicating to IB.
Will you be using Tradestation as your broker ?

Glenn

Glenn

At the moment I would be using Tradestation, because there is no API issue. However I also have an IB account, but I have not thought of trying to set up any API between them. It will need to be 2 way as well because it would be nessary for IB to pass position size data back to TS.

Charlton
 
Hi Chartlon

As per my PM - I'm trying to figure out where this goes.

If it goes on the $INDU, then how do we specify which symbol we actually buy ?

From the help in TS - it looks like you can only buy/sell short on the current symbol.

That's why I thought this would need to go on Radar screen.

Cheers

Pete
 
Hi Chartlon

As per my PM - I'm trying to figure out where this goes.

If it goes on the $INDU, then how do we specify which symbol we actually buy ?

From the help in TS - it looks like you can only buy/sell short on the current symbol.

That's why I thought this would need to go on Radar screen.

Cheers

Pete
Pete

TS doesn't allow you to put a strategy into a Radar screen, only on a chart. I get your point about the chart and I am currently trying to achieve the same thing using macros. In Eaysylanguage you can run a macro to:

(a) create an order and/or
(b) to open up a chart with a specific symbol that contains a strategy to create an order.

I am experimenting with this to see if these are suitable and will provide a little demo once I have sorted this out,

Although option (a) might turn out to be relatively straightforward I think option (b) will be required because of the complexities around what the order entry needs to do e.g. consider position size for a basket of stocks.

Charlton
 
LMAO @ Tradestation...

You can't put a strategy on radar screen ? Those crazy guys... :rolleyes:

Unbelievable... What a bunch of muppets.... Surely the whole point of radar screen is... aaah never mind...

OK - I'll set up the basket for you & post the code later today.
 
How we can execute trades.

1. TradeStation 8 + TradeStation account.
2. MultiCharts + Interactive Brokers account.
3. TradeStation 2000i or TradeStation 8 or MultiCharts + Interactive Brokers account using third party dll to place orders and get execution reports.

The last way is more flexible - we can get trade signals from INDU and stocks to be executed from RadarScreen.
 
Charlton

I think I am done.

I changed TT_GET_ARRAY to output the following :

TTCurrentBasket - named int
- Number of stocks in the basket. This is not the same as TTBasket which is set in the position size. Let's say we want to trade 5 stocks - then TTBasket will be 5. If there are only 3 stocks above the $INDU then TTCurrentBasket will be 3.

TTBasketSymbols + number - named string
- The symbol name to trade. So if we want to trade YHOO, NYX, COF, you will have
TTBasketSymbols1 = YHOO
TTBasketSymbols2 = NYX
TTBasketSymbols3 = COF

TTPosSize + symbolname - named string
For position Size - (from #TT_POSITION_SIZE) - THANKS GLENN !!!!
- integer set up for each stock

Note that the #TT_ENTRYPOINT hasn't been kept up to date - all the code is now in TT_MTL_CYCLE3 - although we may go back to #TT_ENTRYPOINT later.

I presume your code will be tagged onto the end of #TT_MTL_CYCLE3 - in which case use local varliable Action to tell you what to do - it'll either be "Buy", "SellShort" or "None".

TT_GET_ARRAY is called in TT_MTL_CYCLE when a signal occurs - so as long as your code is after that call, you'll have your basket set up of stocks to long or short. TT_GET_ARRAY puts strong stocks in the basket for a Buy & weak stocks in the basket for a sell.

I'll post the ELD for now - once we get properly 'integrated', we can post the code.

There's still print statements in there :

Basket 5.00 Num Elements 39.00
Symbol YHOO, Strength 4.67, Position Size 390.00, Sector Services, Basket Size 1.00
Symbol NYX, Strength 4.44, Position Size 146.00, Sector Financial, Basket Size 2.00
Symbol COF, Strength 3.89, Position Size 111.00, Sector Financial, Basket Size 3.00
Symbol DD, Strength 3.38, Position Size 182.00, Sector Basic Materials, Basket Size 4.00
Symbol CELG, Strength 3.33, Position Size 111.00, Sector Healthcare, Basket Size 5.00
06 Nov 08 12:34 Zone 60 = OS, Dir 60 = F, Zone 30 = OS, Dir 30 = F, Zone 10 = OS, Dir 10 = F, Zone 5 = OS, Dir 5 = F, Zone 3 = OS, Dir 3 = F, Zone 1 = OS, Dir 1 = +, Bias = Long, Trigger = Long, Action = Buy
Cycles -121.37 -102.95 -108.58 -118.39 -121.16 -101.76

Cheers

Pete
 

Attachments

  • TT_9_NOV_08.ELD
    30.9 KB · Views: 22
Last edited:
How we can execute trades.

1. TradeStation 8 + TradeStation account.
2. MultiCharts + Interactive Brokers account.
3. TradeStation 2000i or TradeStation 8 or MultiCharts + Interactive Brokers account using third party dll to place orders and get execution reports.

The last way is more flexible - we can get trade signals from INDU and stocks to be executed from RadarScreen.

You can't put strategies on radar screen, so I'm not sure we can do that.

Then again - can you put buy & sell code in indicator code ? I haven't tried that....

Chartlon - can we put an indicator on radar screen with buy/sell code ?
 
By-passing the use of strategies for order entry

You can't put strategies on radar screen, so I'm not sure we can do that.

Then again - can you put buy & sell code in indicator code ? I haven't tried that....

Chartlon - can we put an indicator on radar screen with buy/sell code ?
You cannot put buy and sell code into an indicator full stop, so therefore you cannot put such an indicator into Radar screen.

What you can do is to create a macro. For example if you go to view > macros
then you can create a command line macro that has syntax such as:

.PlaceOrder "Action='Buy', Symbol='MSFT', SymbolCategory='Equity', OrderType='Limit', LimitPrice=25.00, Quantity=500, Duration='GTC+'


When you run this manually you will see that it places the order.

Now - this is where it get's very exciting :clap:

I came across this document that explains how it is possible to use the placeorder function in various ways within Easylanguage itself.

You canL
(1) use the .placeorder macro (note the fullstop at the beginning) and give it a string describing which symbol etc you want in the order.

(2) use the EL function placeorder(note this has no fullstop at the beginning) to call the macro, but it seems you must enter all parameters, whereas under (1) you don't because the parameters are named

(3) create in advance your own macro and replace some parameters at run time using the EL .placeorder macro.

One fo these would seem to do the trick for the opening of a position on a named symbol and brings the control back to your TT_ENTRY_POINT code.

Here is a link to the documentation

https://www.tradestation.com/Discussions/Topic.aspx?Topic_ID=62739&Page=1

https://www.tradestation.com/Discus...rm=macro run from easylanguage&txtExactMatch=

I have entered such a command into TT_MTL_CYCLE3, as the very last line and it verified ok.

No orders were generated and it appears that strategy back-testing will not be possible, except using print statements and the like. Simulation live testing should be possible though. Here is a quote from the document above confirming this:

"3. As I read other posts, it sounds that a PlaceOrder EL strategy can not be back-tested. Is there alternative wat to do that?

PlaceOrder simply automates some of the order-bar functionality. Backtesting is left to the person writing PlaceOrder code. For example, we have used PlaceOrder to convert backtested strategies into RadarScreen 'trading indicators.' We did some real time simulations of both the strategy and indicator to insure they both generated orders at the same time. "



Finally, for non-TS8.3 users or for users not using TS as a broker, I think we need another team, who can find out what format is required from TS in their APIs from their broker. It then may be possible to issue output statements in a the required format for those brokers.

Charlton
 
Finally, for non-TS8.3 users or for users not using TS as a broker, I think we need another team, who can find out what format is required from TS in their APIs from their broker. It then may be possible to issue output statements in a the required format for those brokers.

Charlton

I found very good solution for Interactive Brokers - TWSLink
twslink

TradeStation can interact with TWSLink.dll the same way as with GlobalVariables.dll
 
I found very good solution for Interactive Brokers - TWSLink
twslink

TradeStation can interact with TWSLink.dll the same way as with GlobalVariables.dll
That's useful. In the past I have successfully linked IB with Amibroker and generated dummy orders in IB. I see it is free for download - would you be able to check if you can send a dummy order to IB.

I have an IB account as well as a TS one, so it is something I can also check out.

If you get it running it is important to check that it is a two way API. We not only need to send orders to IB, but also read information from IB, such as position size on open positions, otherwise it will restrict its use.

Charlton
 
That's useful. In the past I have successfully linked IB with Amibroker and generated dummy orders in IB. I see it is free for download - would you be able to check if you can send a dummy order to IB.

I have an IB account as well as a TS one, so it is something I can also check out.

If you get it running it is important to check that it is a two way API. We not only need to send orders to IB, but also read information from IB, such as position size on open positions, otherwise it will restrict its use.

Charlton

Yes, it is a two way API.

I tried to place dummy orders, get position size and check if this stock shortable. It works.
 
Can we generate orders from indicators via Radar screen? Or the orders have to come from strategy signals through a chart? I am quite new to this API thing.. what's the best way to use it? thanks
 
Can we generate orders from indicators via Radar screen? Or the orders have to come from strategy signals through a chart? I am quite new to this API thing.. what's the best way to use it? thanks
We can generate orders from Radar Screen to Interactive Brokers TWS using TWSLink.dll.
 
GUYS


I have been trying to get hold of sharky for your new sub forum but no joy . I have left him phone messages . email , PM still no joy ,,

so if you wish to open and take ur work to another private forum then I have no problems with that .

Just to let you now

Grey1
 
GUYS


I have been trying to get hold of sharky for your new sub forum but no joy . I have left him phone messages . email , PM still no joy ,,

so if you wish to open and take ur work to another private forum then I have no problems with that .

Just to let you now

Grey1

Thanks for trying - as you said in your earlier post the codification of this is too valuable to leave in open forum and to expose one's edge in a "take-away" for all and sundry who might gain access to this forum.

I'm are also beginning to realise that there are too many directions this could take due to so many indicators that could be used, according to each one's personal preference. There are also too many platforms to consider, styles of trading and money management that there can never be a focused development amongst such a diverse group on open-forum.

So I am sure that the forum can continue to be used as a vehicle for tossing around ideas, but I don't think it will be feasible to develop a trading product in this kind of environment.

Hopefully though this has provided everyone with lots of good ideas on enhancements to the strategy as well as some basic TS code that everyone could make some use of and build upon, if only to enhance the manual signals provided.

In fact the posts show that there were quite a number of issues in using TS, which restricted what we could achieve with TS particularly with multi-timeframe processing at tick level.

Frankly this surprised me because I thought that TS was the bees knees, but I now think that to fully automate the strategy would probably require dropping back to a lower level language such as C++, unless there are more suitable trading languages (perhaps amibroker ?)

Anyway thanks once again - Iraj, for your contributions to the ideas

Charlton
 
Top