Only trigger a trade between 10 AM and 3 PM in thinkscript?

Sparrow

New member
VIP
I have a custom study that I have tweaked to exit a position when conditions are met. Is there a way to restrict the trade time to only trigger between 10 AM and 3 PM? I have an issue where the opening volatility causes me to get shaken out too early. Thank you so much for any pointers!
 

mashume

Well-known member
VIP
As part of your entry / exit conditions (the buy/sell functions), put in something like:
Code:
def in_trade_window = SecondsFromTime(1000) >= 0 and SecondsTillTime(1500) >= 0;
the times are eastern, and 24 hour format, no decimals so 3:30 pm would be written as 1530, just FYI.
this will return a true if the time is inside that window and a false outside
so you include it in the buy/sell
Code:
addOrder(in_trade_window AND other_condition ...

Hope that helps,

-mashume
 

Sparrow

New member
VIP
Well, I have wrestled with this for several days with no luck. The issue is that my study uses moving averages and macd on a daily chart to decide when to sell. All the time functions I have found like "SecondsFromTime() and GetTime()" seem to require a minute chart. They just return 0 with any other time frame. I contacted TOS support and they were very nice and worked on it with me, but we didn't find a solution. If I change my study to use a minute chart then it would sell several times a day. /stalled.

Code I used to trade between 11:00 AM and 3:30 PM attempt:
Code:
def in_trade_window = SecondsFromTime(1100) > 0 and SecondsTillTime(1530) > 0;
plot sellIt = if (in_trade_window and .....) then yes else no;
 

mashume

Well-known member
VIP
Well, I have wrestled with this for several days with no luck. The issue is that my study uses moving averages and macd on a daily chart to decide when to sell. All the time functions I have found like "SecondsFromTime() and GetTime()" seem to require a minute chart. They just return 0 with any other time frame. I contacted TOS support and they were very nice and worked on it with me, but we didn't find a solution. If I change my study to use a minute chart then it would sell several times a day. /stalled.

Code I used to trade between 11:00 AM and 3:30 PM attempt:
Code:
def in_trade_window = SecondsFromTime(1100) > 0 and SecondsTillTime(1530) > 0;
plot sellIt = if (in_trade_window and .....) then yes else no;
You can call the MACD and Averages using the Aggregation Period DAY and borrowing liberally from the TD Script for MACD:

Code:
declare lower;

input agg = AggregationPeriod.DAY;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

plot Value = MovingAverage(averageType, close(period = agg), fastLength) - MovingAverage(averageType, close(period = agg), slowLength);
plot Avg = MovingAverage(averageType, Value, MACDLength);

plot Diff = Value - Avg;
plot ZeroLine = 0;

All we've done here is to add the aggregation period to the close price in the calculation of the MACD lines. I omitted the style and colouring parts of the TD Script. You can add them yourself... this was just to point out how to adjust the agg period for the macd study.

perhaps this will help you. You are correct that you can't run seconds calculation on day charts, but you can run day aggregations on intraday charts. :)


happy trading,
mashume
 

Sparrow

New member
VIP
Oh my, you are a wonderful help to the community! I came up against a wall trying to run intra-day functions like GetTime() in an intra-bar chart and assumed it was both ways. That looks fairly easy. I will test it. Thank you so much!
 

Sparrow

New member
VIP
I converted my study to an intra-day study and got the time, MACD, and moving averages working. What I am struggling with is getting an ATR trailing stop to work on both a minute chart and a daily chart. If it works on one, then it does not on the other. I began with the stock TrailingStopLX in think or swim and added the day aggregation to the close. Do you see what keeps it from working on both charts?

Code:
############################################
# ATR Trailing Stop
############################################
input agg = AggregationPeriod.DAY;
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 2.5;
input firstTrade = {default long, short};
input tsAverageType = AverageType.WILDERS;

Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);

def HiLo = Min(high(period = agg) - low(period = agg), 1.5 * Average(high(period = agg) - low(period = agg), ATRPeriod));
def HRef = if low(period = agg) <= high(period = agg)[1]
    then high(period = agg) - close(period = agg)[1]
    else (high(period = agg) - close(period = agg)[1]) - 0.5 * (low(period = agg) - high(period = agg)[1]);
def LRef = if high(period = agg) >= low(period = agg)[1]
    then close(period = agg)[1] - low(period = agg)
    else (close(period = agg)[1] - low(period = agg)) - 0.5 * (low(period = agg)[1] - high(period = agg));

def trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high(period = agg), close(period = agg), low(period = agg));
}
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
    if (!IsNaN(loss)) {
        switch (firstTrade) {
        case long:
            state = state.long;
            trail =  close(period = agg) - loss;
        case short:
            state = state.short;
            trail = close(period = agg) + loss;
        }
    } else {
        state = state.init;
        trail = Double.NaN;
    }
case long:
    if (close(period = agg) > trail[1]) {
        state = state.long;
        trail = Max(trail[1], close(period = agg) - loss);
    } else {
        state = state.short;
        trail = close(period = agg) + loss;
    }
case short:
    if (close(period = agg) < trail[1]) {
        state = state.short;
        trail = Min(trail[1], close(period = agg) + loss);
    } else {
        state = state.long;
        trail =  close(period = agg) - loss;
    }
}

plot tsBuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
plot tsSellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);

plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Buy", GetColor(0));
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
    then TrailingStop.Color("Sell")
    else TrailingStop.Color("Buy"));
 

Similar threads

Top