- ago
I was unable to get SELL orders to execute as expected. I was seeing all the buy orders, but after the orders filled (limit below the last bar) I was expecting a Profit and Stop Loss order to be placed (OCO), nothing. I saw one SELL for "...couldn't find a matching position...".

Here is a simple version of the strategy. I am sure there is a simple error.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript5 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          source = bars.Close;          pct = (100.0 - 3.0) / 100.0;          multSource = source * pct; } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) {          bool condition0;          Position foundPosition0 = FindOpenPosition(0); if (!HasOpenPosition(bars, PositionType.Long)) {             //code your buy conditions here     val = multSource[idx];             PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, val, 0, "Buy at Limit 3% below Close. EntryPrice:"+ Math.Round(val,4) +" basedon:"+ Math.Round((val/pct),6)+" for "+ Math.Round((1-pct)*val,6) +" below Open. "); }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                Backtester.CancelationCode = 49;                ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * ((10.0 / 100.0) + 1.0), "Sell at 10.0% profit target. " + (foundPosition0.EntryPrice * ((10.0 / 100.0) + 1.0)));             //}                //code your sell conditions here             //   PlaceTrade(bars, TransactionType.Sell, OrderType.Market);             }          } }       public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)       { //using the examples for the reference here          t.AutoProfitTargetPrice = executionPrice * 1.03;          t.AutoStopLossPrice = executionPrice * 0.97;       } //declare private variables below       private TimeSeries multSource;       private double val;       private TimeSeries source;       private double pct; } }

My understanding is the sample code would inspect even same bar opportunities. The back-tests indicate that it would exit for that. however, the live trading is requiring too much manual work. TDAmeritrade broker and Tiingo pricing.

Any assistance would be appreciated.
0
807
5 Replies

Reply

Bookmark

Sort
Cone8
 ( 6.32% )
- ago
#1
Your expectation is correct. I'm confident it works as long as:
1. you're Auto-Placing the orders, and,
2. the order is fully filled, and,
3. the Order Manager shows "Filled"

We'll need more information like that, and please tell us what interval you're trading. The TDA API is notoriously slow to activate API orders, but it should work.

Edit - I think Auto-Place is a requirement, but I'll confirm that.
1
- ago
#2
I apologize for the length in advance.

QUOTE:
I'm confident it works as long as:
1. you're Auto-Placing the orders, and,
2. the order is fully filled, and,
3. the Order Manager shows "Filled"


1. Auto-Place used
2. Will have to do this again the next market open. I am confident that this did happen.
3. Order Manager did show filled.

My system was auto patched last night - and so I don't have the data. however, in the TDA/TOS the orders are filled order.

QUOTE:
please tell us what interval you're trading.

I have attempted, 5Min, 10Min, 15 Min, even 1Min. Increased Tiingo for the month to help validate the issue.

I was looking at the API, I "assume" this method is not being used, and that WL is verifying the filled, then POST places the SELL orders for the OCO bracket?

RE: https://developer.tdameritrade.com/content/place-order-samples
Conditional Order: One Triggers A One Cancels Another
Buy 5 shares of XYZ at a Limit price of $14.97 good for the Day. Once filled, 2 sell orders are immediately sent: Sell 5 shares of XYZ at a Limit price of $15.27 and Sell 5 shares of XYZ with a Stop order where the stop price is $11.27. When one of the sell order fills, the other order is immediately cancelled. Both Sell orders are Good till Cancel. Also known as a 1st Trigger OCO order.


This brings up something I have been tinkering with, External logging, using System.IO. a method externalize logs to see the flow, after the system/app restarts. JSON or XML, or even key:value pairs.

Thanks In advance.

0
- ago
#3
QUOTE:
The TDA API is notoriously slow to activate API orders,


Which broker then is more consistent? IB, Alpaca, Tradier or TDAmeritrade? I would move if these issues could clear up.
0
Cone8
 ( 6.32% )
- ago
#4
TDA works fine for longer interval, it's just slow to activate the orders - could take 5 or 10 seconds after placing the order to see it go active. With that much delay I wouldn't count on Auto-Trading 1, 2, or even 3-minute bars at TDA.

Order activation with IB is instantaneous. The disadvantage is commissions ($1 minimum) which I easily overlook because I'm not usually making dozens of trades every day. But If you're making 20 trades per day, that adds up quickly.

I don't have accounts at Alpaca or Tradier, so I can't comment on those; though Alpaca paper trading works quite well.

On your next trade entry, get a screenshot of the Order Manager and check the Log Viewer for messages.
1
Glitch8
 ( 12.10% )
- ago
#5
Tradier is super fast in their sandbox account, we don't have our live account funded yet but I've got to imagine it's the same or better.
1

Reply

Bookmark

Sort