conditional order executed at market close


Category:
0
0

Hi,

I’m having grief building a conditional order to sell when RSI of daily aggregation > 55.
That much is pretty easy; this code will do it:

input RSIlength = 4;
input exitScore = 55;
input RSIavg = AverageType.WILDERS;

def price = close;

def NetChgAvg = MovingAverage(RSIavg, price – price[1], RSIlength);
def TotChgAvg = MovingAverage(RSIavg, AbsValue(price – price[1]), RSIlength);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

plot SELLINGsignal = RSI > exitScore;

# ——————————————————————————

However, the trading rule says “ON THE CLOSE”.

MOC orders don’t allow conditions.

So, I’m trying to use MARKET with conditions. And, this will work for that part of the puzzle:

input marketCloseTime = 1600;
input lastMinutes = 2;

def secondsTillClose = SecondsTillTime(marketCloseTime);
def minutesTillClose = secondsTillClose / 60;
def TIME_OK = minutesTillClose < lastMinutes;

plot SELLINGsignal = TIME_OK;

# ————————————————————————————-

However, THAT hast to run on an aggregation period smaller than DAY, or it won’t work.

So, I’m down to trying to get the DAILY level RSI in a sub-day-level aggregation period.

If I use this (screen shot 1):

def price = close(period = AggregationPeriod.DAY);

I won’t get TODAY’s RSI score until actual CLOSE of the daily bar – which means we can’t trade based on the information. (And, GTC Extended can’t use conditions, either).

It appears the puzzle is about calculating DAILY period RSI, but within a sub-daily aggregation period.

I tried this experiment (on a one-hour chart):

declare lower;

input crStartTime = 1500;
input crEndTime = 1558;

def CR = if SecondsFromTime(crStartTime) > 0 and SecondsFromTime(crEndTime) <= 0
then close else CR[1];

plot line = CR;

plot day = close(period = aggregationperiod.DAY);
# ————————————————————————————-

On the one-hour chart, the lines are not exactly the same, but the values at close-of-day are identical

However, if I use this CR value to calculate RSI, everything goes off the rails (screen shot 2):

declare lower;

input length = 4;
input averageType = AverageType.WILDERS;

input crStartTime = 1500;
input crEndTime = 1558;

def day = close(period = AggregationPeriod.DAY);

def dNetChgAvg = MovingAverage(averageType, day – day[1], length);
def dTotChgAvg = MovingAverage(averageType, AbsValue(day – day[1]), length);
def dChgRatio = if dTotChgAvg != 0 then dNetChgAvg /dTotChgAvg else 0;

plot dRSI = 50 * (dChgRatio + 1);

def CR = if SecondsFromTime(crStartTime) > 0 and SecondsFromTime(crEndTime) <= 0
then close else CR[1];

def crNetChgAvg = MovingAverage(averageType, CR – CR[1], length);
def crTotChgAvg = MovingAverage(averageType, AbsValue(CR – CR[1]), length);
def crChgRatio = if crTotChgAvg != 0 then crNetChgAvg / crTotChgAvg else 0;

plot crRSI = 50 * (crChgRatio + 1);

# ————————————————————————————————————–

I’ve got a second-level issue in that I’d like to limit this thing also to trading only on the last trading day of the week, and I’d like not to miss a trade due to a holiday (Thursday LDOW rather than Friday). But, at this point, that’s secondary to the main puzzle.

Any help would be appreciated!

 

 

Attachments:
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on November 9, 2018 7:22 pm
283 views
0
Private answer

I really appreciate you taking the time to express the entire thought process. It really helps me clearly understand your goals and that gives me the freedom to explore solutions.

It seems that you have a solution here which you list as your first section of code. Your only hurdle is that you want to send that order at the close of the market if the condition is true. I see the rest of your attempts are driven to overcome this hurdle. Within those attempts I see you are willing to accept a solution where the order is submitted 2 minutes before the close.

Attached screenshot shows a setting, just above the place where you set the custom study, that permits you to select the exact date and time for your order to be sent to market. Combine this setting with your study condition and you should find that it sends this as a market order 2 minutes before the close only if your study condition is true. TEST THIS TO MAKE SURE.

This seems to me the best solution for your request. You might even experiment with that time. You can set it down to 15:59:55 and see if 5 seconds is enough time for Thinkorswim servers to process the condition and submit the order right at the close.

If you are looking for a code only solution here I am afraid you are asking a bit too much of Thinkorswim. More advanced platforms can handle this easily, however you would be required to have the platform open, the chart strategy loaded for the targeted ticker symbol and hope and pray you don’t lose internet connectivity.

The huge benefit provided by Thinkorswim is that these conditional orders are handled by their servers. So you don’t even need to be logged in for the orders to execute.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on November 10, 2018 7:48 am
0

Hi, Pete,

Thanks very much for your answer.

I’m sorry I didn’t mention trying the solution you suggest. It’s very simple and straightforward.

The problem with it is that it’s specific to a given date. In other words, the entire order – study and all – has to be re-input and the trigger time re-set for the next day … EVERY SINGLE DAY.

I often travel for days or even weeks at a time, during which I’m sometimes unable to get an Internet connection. Also, as you are probably also painfully aware as I am – EVERY iteration of order entry is another opportunity for making a costly mistake.

So, I’m looking for a solution that will allow me to set (and triple-check) the market conditions and time to execute and then leave it until I receive a notice that it’s executed – and I can then input the reverse order to let it go again.

I remain hopeful that there is such a solution to be had – not only for END OF DAY, but also for END OF WEEK.

Thanks again for any feedback!

( at November 10, 2018 3:25 pm)
0

Given these added details I can tell you that Thinkorswim is not even close to the technology you need. You need institutional level technology to get there. You need to have a fully automated trade system that runs on a server. This narrows the list of options significantly.

About the only platform I know of that can handle the requirements without being prohibitively expensive is Interactive Brokers. But you will need to hire a developer to build your automated trading system. (not me)

However the massively lower commissions from IB should more than redeem developer expense over time. Just depends how serious you are about this, and how much capital you are committing to this sort of trade setup.

( at November 10, 2018 3:45 pm)