Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- discounts are available after registering.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Can someone please help me find out how to do an OCO order inside easylanguage for an automated strategy? I can't seem to find the information anywhere!
thanks!
Can you help answer these questions from other members on NexusFi?
In the above example, setstopcontract (or setstopshare) sets the following code on a "per contract" or "per share" basis. If you want to set based on the overall position basis use "setstopposition;"
You can also combine profit targets with trailing stops (there's a variety on TS).
The OSO (supplemental) order is inherent as well. You simply code for your entry and then use the if MP then.....statement to ensure you immediately enact a "supplemental" order to cover your exit.
Keep in mind that when you have more than one pending order (like a typical bracket) that it violates TS's 15 second rule, so your orders will not be maintained on the tradeserver in the event of a loss of connection. if you use a single trailing stop, without IOG=True, then your trailing stop will only update every 15 seconds by default (unless of course you turn on IOG and then it will be tick/tick). Any exit code, with IOG on violates the 15 second rule.
I wouldn't suggest using the following code for automated trading (it might not work), but perhaps it can get you started until more knowledgeable NexusFi (formerly BMT) members chime in
You can also choose to replace the MarketPosition reserved word with MarketPosition_at_broker (See PowerLanguage Help for more about this word).
Regards,
Edit: RM99 beat me to it. Funny to see the differences in approaches.
In Jura's example, instead of using a fixed or "inputtable" stop order, he uses a criteria based order where...
The code searches the lowest value of the last 11 bars (10 bars previous to the last bar...[1]),.
As you can see in TS, there's virtually a limitless permutation of possibilities with respect to exits and money management.
Also take note that the order is a "stop" order....which has specific meanings in EL. It means better for entry orders and worse for exit orders (i.e. if you have a trailing stop on a long position, the stop exit order takes place at the price or worse (lower).
I wouldn't suggest using the following code for automated trading (it might not work), but perhaps it can get you started until more knowledgeable NexusFi (formerly BMT) members chime in
vars: setupCondition(false);
if .... mysetup... then setupCondition = true else setupCondition = false;
// Submit entry orders as long as the setupCondition = true *and* there is no market position if setupCondition = true and MarketPositon = 0 then begin Buy ("EnterLong") 1 contract next bar at Highest(high, 10)[1] limit; // buy breakout of highest high latest 10 bars Sellshort ("EnterShort") 1 contract next bar at Lowest(Low, 10)[1] limit; // sell breakout of lowest low of latest 10 bars end;
if MarketPosition = 1 then begin // Manage long order here // For example a stop below the Lowest low of the latest 10 bars Sell("Stop") next bar at Lowest(Low, 10)[1] stop; end;
if MarketPosition = -1 then begin // short orders end;
You can also choose to replace the MarketPosition reserved word with MarketPosition_at_broker (See PowerLanguage Help for more about this word).
Regards,
Edit: RM99 beat me to it. Funny to see the differences in approaches.
I'm not familiar with marketposition_at_broker and I can't find it in EasyLanguage. Please esplain
I'm not familiar with marketposition_at_broker and I can't find it in EasyLanguage. Please esplain
My bad , I didn't realize the topic starter was using TradeStation instead of MultiCharts.
Here's what the MultiCharts help says about marketposition_at_broker:
Quoting
MarketPosition_at_Broker
Returns a numerical value, indicating the type of the specified position at the broker for the symbol. A value of 1 indicates a long position, -1 indicates a short position, and 0 is returned only if the current position is specified and indicates that the current position is flat.
Usage MarketPosition_at_Broker
Notes
This function can only be used in signals and functions.
This function differs from the marketposition keyword in that it cannot take an argument to reference past values.
This function can only be used with Interactive Brokers, Patsystems, and Zen-Fire.
Example
MarketPosition_at_Broker will return a 1 if the current position at the broker for the strategy is long.
MarketPosition_at_Broker will return a -1 if the current position at the broker for the strategy is short.
MarketPosition_at_Broker will return a 0 if the current position at the broker for the strategy is flat.
With using MarketPosition_at_broker you can check, for example, if the order is executed by the broker.
My bad , I didn't realize the topic starter was using TradeStation instead of MultiCharts.
Here's what the MultiCharts help says about marketposition_at_broker:
With using MarketPosition_at_broker you can check, for example, if the order is executed by the broker.
Ahhh, TS addresses this issue in the setting tabs and also with GetAppInfo(aiRealTimeCalc);
In TS when you automate a strategy that has 2 exit orders, like the profit target (limit order) and stop loss (stop order), TS will only place one active order at a time, or so I thought, unless I am missing something....?
I thought this was inherent and unavoidable and of course a potentially bad situation if you get disconnected.
What happens is that trade manager will decide whether to activate the stop or the target order depending on which is closest to the price (or whether you are in profit or loss, can't remember which). This is only at best a sort of OCO order, more a "one or the other". It doesn't provide protection if you get disconnected.
The only way to get an OCO from a strategy is to issue a .placeorder macro from within the strategy, but in that case (1) many of the conventional trade management statements don't work and (2) the strategy cannot be backtested.
In TS when you automate a strategy that has 2 exit orders, like the profit target (limit order) and stop loss (stop order), TS will only place one active order at a time, or so I thought, unless I am missing something....?
I thought this was inherent and unavoidable and of course a potentially bad situation if you get disconnected.
What happens is that trade manager will decide whether to activate the stop or the target order depending on which is closest to the price (or whether you are in profit or loss, can't remember which). This is only at best a sort of OCO order, more a "one or the other". It doesn't provide protection if you get disconnected.
The only way to get an OCO from a strategy is to issue a .placeorder macro from within the strategy, but in that case (1) many of the conventional trade management statements don't work and (2) the strategy cannot be backtested.
Excellent points...the strategy toggles between the 2 orders, and like you, I'm not particularly familiar with the specific criteria for when it switches from one order to another.
In fairness, if you lose connection, even manually using the OCO from the matrix, the order(s) that are on the tradeserver still don't function properly. That is to say, if you enter and have a simple bracket placed out on the tradeserver, and you lose connection, whichever exit is triggered first, there's no way for the tradeserver to receive a cancellation order for the other end of the bracket.
In the end, there's really nothing that you can do to get around some of the issues with loss of connection. Even with a .placeorder, you get the orders maintained and you get temporary protection, but you still run the risk that if you're disconnected for an extended period of time, the open position can still potentially hit one of your stops, then enter a new trade based upon hitting the other stop.
I guess the only way to eliminate this issue is to simply run a singular stop (with a macro), like a trailing stop or fixed stoploss. Then use criteria based profit exits....that way if you lose connection, you're still protected by your stop, but your criteria based exits won't be active until you re-establish connection.