- ago
I get the following error if the price is way above the original entry price and then I have to manually enter the profit target & stop loss orders.



How do I modify the code so it won't error out in the future?

CODE:
foreach (Position foundPosition0 in OpenPositions)             {                if (foundPosition0.PositionTag == 0)                {                   value = (Parameters[4].AsDouble / 100.0) + 1.0;                   ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * value, "Sell at profit target");                   {                      //exit active positions                      if (HasOpenPosition(bars, PositionType.Long))                      {                         CloseAtTrailingStop(LastPosition, TrailingStopTypes.PercentHL, Parameters[5].AsDouble, "Sell at Trailing Stop Loss");                         stops[idx] = LastPosition.TrailingStopPrice;
0
872
18 Replies

Reply

Bookmark

Sort
Glitch8
 ( 7.61% )
- ago
#1
This is a broker error message? Which broker?
0
- ago
#2
That is a WL7 error message from TD Ameritrade
0
- ago
#3
0
- ago
#4
Is there a way to modify the code to avoid this error for the buy and stop orders to still be placed or do I just need to manually adjust them?
0
Glitch8
 ( 7.61% )
- ago
#5
Can you post the complete Strategy? That would give us a better idea of the best way to overcome this. But it looks like the Strategy is placing limit sell orders that are maybe below the current price?
0
mjj38
- ago
#6
This is a TD error. They modified their logic over the past few months to reject limit/stop orders if the stop/limit price is too far away from the last price.
0
Glitch8
 ( 7.61% )
- ago
#7
Right, maybe we can help by adjusting the Strategy. Would need to see it though. But if it’s simply placing orders too far from the current price then probably those orders would never get hit anyway so the error result is likely inconsequential.
0
mjj38
- ago
#8
It's more like if I wanted to buy @ limit price of $100.00 for a stock and the last price is $99.00 it might fail (even though it is clearly be under the limit price).
0
Cone8
 ( 3.70% )
- ago
#9
I don't know if TDA has it (IB does) but this type of order would be supported by a Limit-On-Open order.

There's already a feature request for Limit-On-Close, so it looks like we need another for LOO. https://www.wealth-lab.com/Discussion/Limit-On-Close-LOC-Order-Type-5956
0
- ago
#10
It only happens in the orders that are way outside the original range, so a limit on open order sounds like it would work then. Yesterday I had those two that skyrocketed 25% and made the normal strategy not work since it based profit and stop targets off entry price.

It's a knife juggler type strategy with the above sell code that's runs on the daily scale, but of the price jumps or drops a lot, the code will error out with TD Ameritrade.

0
mjj38
- ago
#11
Correct me if I'm wrong, you have some how coded "Market Open Next Bar" basis for position sizing. Is it possible to grab that value and make the limit price for the order be the Math.Min(LimitPrice, Market Open Next Bar) ?
0
Glitch8
 ( 7.61% )
- ago
#12
The intent of that option is to support brokers that let you specify a position size in a dollar amount rather than a number of shares. It's not intended to let you somehow peek into tomorrow and pluck out the market open price to establish a limit price that you're placing today.

That said, there is nothing preventing a Strategy author from cheating and looking at data at "idx + 1". But you'll need to take care to add some logic to avoid out-of-range errors on the last bar of data, and such logic would never be able to produce signals for tomorrow, for obvious reasons.
1
- ago
#13
My code like shown above is just a profit target % or a trailing stop %. How do I accommodate the above sell code I provided for when the price jumps way above the original limit of the strategy or way below the trailing stop before they get placed the next day which inevitably would be a market order taking profit or one as a stop.

Inevitably I am assuming you have run into this scenario a time or two on your deterministic knife juggler to not have an error when one skyrockets/plummets the same day you buy or do other brokers (not TD Ameritrade) not error this out?
0
mjj38
- ago
#14
QUOTE:
The intent of that option is to support brokers that let you specify a position size in a dollar amount rather than a number of shares.
Ah, that makes sense.
0
Cone8
 ( 3.70% )
- ago
#15
QUOTE:
How do I accommodate the above sell code I provided for when the price jumps way above the original limit of the strategy or way below the trailing stop before they get placed the next day which inevitably would be a market order taking profit or one as a stop.

It's a different experience depending on the broker, but I think it deserves attention as a feature request.

An idea would be to include a new Quotes & Triggers trading option to submit a market order if the first price detected were x% beyond stop/limit triggers.
0
- ago
#16
I have been trying to code this market close option but cannot get it to work. I have just been having to manually enter market orders every time this error happens for live trading.
0
Cone8
 ( 3.70% )
- ago
#17
Is it an end-of-day or intraday strategy?

A MarketClose order in an Intraday strategy is a simple matter. For end-of-day, you already have to know that you're going to sell at the close tomorrow.
0
- ago
#18
A market close intraday to execute if the price is outside the limit or trailing stop range.
0

Reply

Bookmark

Sort