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

Sparrow

New member
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!
 
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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

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;
 
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
 
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!
 
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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
433 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top