trade strat between intra day time periods

JoseyWales

New member
VIP
Hello,

I am trying to code a strategy that trades only between 7:30 and 12:30 pst and am not getting the results I want.
Other requirements that are popping up are:

Enter only between 7:30 and 12:30 but take any exit signal up until the EOD
If no exit signal then Close the trade at the end of the day

This is a reversing signal trade so I was using the auto.buy order type but with that, I cant figure out how to separate the entry from exit signals.
If I used buy to open type it would not reverse the trade.

There are lines ####out since I was trying different things.

Thanks for any help you can provide

Code:
# Charles Schwab & Co. (c) 2013-2024
#

input price = close;
input fastLength = 5;
input slowLength = 10;
input averageType = AverageType.EXPONENTIAL;
#######################################################

input StartTrading = 0730;
input EndTrading = 1230;
input MinutesTrading = 300;
def Today = if GetDay() == GetLastDay() then 1 else 0;
#def AfterStart = if SecondsFromTime(StartTrading) >= 0 then 1 else 0;
#def BeforeEnd = if SecondstillTime(EndTrading) >= 0 then 1 else 0;
#def OKToTrade = AfterStart and BeforeEnd;

def OKToTrade =SecondsFromTime(StartTrading) >= 0 and SecondstillTime(EndTrading) <= 0;


#########################################################################
plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

AddOrder(OrderType.BUY_AUTO, OKToTrade and FastMA crosses above SlowMA, tickColor = GetColor(1), arrowColor = GetColor(1), name = "buy");
AddOrder(OrderType.SELL_AUTO, OKToTrade and FastMA crosses below SlowMA, tickColor = GetColor(2), arrowColor = GetColor(2), name = "sell");


def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if (afterEnd[-2] == 1 and afterEnd == 0) or (isRollover[-2] and firstBarOfDay[-2]) then 1 else 0;






#close eod

AddOrder(OrderType.SELL_TO_CLOSE, lastBarOfDay, tickcolor = Color.green, arrowcolor = Color.green, name =lastBarOfDay+ "EOD");
AddOrder(OrderType.BUY_TO_CLOSE, lastBarOfDay, tickcolor = Color.green, arrowcolor = Color.green, name = lastBarOfDay+"EOD");
 
Solution
Hello,

I am trying to code a strategy that trades only between 7:30 and 12:30 pst and am not getting the results I want.
Other requirements that are popping up are:

Enter only between 7:30 and 12:30 but take any exit signal up until the EOD
If no exit signal then Close the trade at the end of the day

This is a reversing signal trade so I was using the auto.buy order type but with that, I cant figure out how to separate the entry from exit signals.
If I used buy to open type it would not reverse the trade.

There are lines ####out since I was trying different things.

Thanks for any help you can provide

Code:
# Charles Schwab & Co. (c) 2013-2024
#

input price = close;
input fastLength = 5;
input slowLength = 10;
input averageType =...
Hello,

I am trying to code a strategy that trades only between 7:30 and 12:30 pst and am not getting the results I want.
Other requirements that are popping up are:

Enter only between 7:30 and 12:30 but take any exit signal up until the EOD
If no exit signal then Close the trade at the end of the day

This is a reversing signal trade so I was using the auto.buy order type but with that, I cant figure out how to separate the entry from exit signals.
If I used buy to open type it would not reverse the trade.

There are lines ####out since I was trying different things.

Thanks for any help you can provide

Code:
# Charles Schwab & Co. (c) 2013-2024
#

input price = close;
input fastLength = 5;
input slowLength = 10;
input averageType = AverageType.EXPONENTIAL;
#######################################################

input StartTrading = 0730;
input EndTrading = 1230;
input MinutesTrading = 300;
def Today = if GetDay() == GetLastDay() then 1 else 0;
#def AfterStart = if SecondsFromTime(StartTrading) >= 0 then 1 else 0;
#def BeforeEnd = if SecondstillTime(EndTrading) >= 0 then 1 else 0;
#def OKToTrade = AfterStart and BeforeEnd;

def OKToTrade =SecondsFromTime(StartTrading) >= 0 and SecondstillTime(EndTrading) <= 0;


#########################################################################
plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

AddOrder(OrderType.BUY_AUTO, OKToTrade and FastMA crosses above SlowMA, tickColor = GetColor(1), arrowColor = GetColor(1), name = "buy");
AddOrder(OrderType.SELL_AUTO, OKToTrade and FastMA crosses below SlowMA, tickColor = GetColor(2), arrowColor = GetColor(2), name = "sell");


def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if (afterEnd[-2] == 1 and afterEnd == 0) or (isRollover[-2] and firstBarOfDay[-2]) then 1 else 0;






#close eod

AddOrder(OrderType.SELL_TO_CLOSE, lastBarOfDay, tickcolor = Color.green, arrowcolor = Color.green, name =lastBarOfDay+ "EOD");
AddOrder(OrderType.BUY_TO_CLOSE, lastBarOfDay, tickcolor = Color.green, arrowcolor = Color.green, name = lastBarOfDay+"EOD");


this is a strategy

this was wrong,
#def OKToTrade = SecondsFromTime(StartTrading) >= 0 and SecondstillTime(EndTrading) <= 0;

change it to be,
SecondstillTime(EndTrading) > 0

if something doesn't work, don't be afraid to try some things.
sometimes i add lines or bubbles to get a visual of what is going on.
i added this,
addverticalline(oktotrade, "-");
to verify the oktotrade period. and it was wrong.

can choose to allow only 1 trade at a time or many
input open_trades = {default one, many};

can show a cyan line during the trade period, a little lower than the candles. ( to verify the trade opens)

i changed the addorders to be specific, BUY_to_open , sell_to_open

a trade close only happens if there is an open trade

all trades are closed on last bar of day


Code:
#strat_buy_inam_sell_dayend

#https://usethinkscript.com/threads/trade-strat-between-intra-day-time-periods.19631/
#trade strat between intra day time periods

def na = Double.NaN;
def bn = barnumber();

#######################################################

input open_trades = {default one, many};

input StartTrading = 0730;
input EndTrading = 1230;
#input MinutesTrading = 300;
#def Today = if GetDay() == GetLastDay() then 1 else 0;
#def AfterStart = if SecondsFromTime(StartTrading) >= 0 then 1 else 0;
#def BeforeEnd = if SecondstillTime(EndTrading) >= 0 then 1 else 0;
#def OKToTrade = AfterStart and BeforeEnd;

# fix this,
#def OKToTrade =SecondsFromTime(StartTrading) >= 0 and SecondstillTime(EndTrading) <= 0;
def OKToTrade = SecondsFromTime(StartTrading) >= 0 and SecondstillTime(EndTrading) > 0;


#addverticalline(oktotrade, "-");
input trade_period_line = yes;
plot z1 = if trade_period_line and oktotrade then ExpAverage((low*0.997),3) else na;
z1.SetDefaultColor(Color.cyan);
#z1.setlineweight(1);
z1.hidebubble();

#########################################################################

def price = close;
input fastLength = 5;
input slowLength = 10;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));


def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if (afterEnd[-2] == 1 and afterEnd == 0) or (isRollover[-2] and firstBarOfDay[-2]) then 1 else 0;

def long_open = OKToTrade and FastMA crosses above SlowMA;
def short_open = OKToTrade and FastMA crosses below SlowMA;

def lo = if bn == 1 then 0
 else if lastBarOfDay then 0
 else if long_open then 1
 else lo[1];

def so = if bn == 1 then 0
 else if lastBarOfDay then 0
 else if short_open then 1
 else so[1];


# use this to only use the first buy signal
#   (!lo[1] and lo)

def l_open;
def s_open;
switch (open_trades) {
case one:
 l_open = (!lo[1] and lo);
 s_open = (!so[1] and so);
case many:
 l_open = long_open;
 s_open = short_open;
}


# long
#AddOrder(OrderType.BUY_AUTO, long_open, tickColor = GetColor(1), arrowColor = GetColor(1), name = "buy");
#AddOrder(OrderType.BUY_to_open, long_open, tickColor = GetColor(1), arrowColor = GetColor(1), name = "buy");
AddOrder(OrderType.BUY_to_open, l_open , tickColor = GetColor(1), arrowColor = GetColor(1), name = "Long open");

# short
#AddOrder(OrderType.SELL_AUTO, OKToTrade and FastMA crosses below SlowMA, tickColor = GetColor(2), arrowColor = GetColor(2), name = "sell");
#AddOrder(OrderType.SELL_to_open, short_open, tickColor = GetColor(2), arrowColor = GetColor(2), name = "sell");
AddOrder(OrderType.SELL_to_open, s_open, tickColor = GetColor(2), arrowColor = GetColor(2), name = "Short open");


# only close a trade if one was opened
def long_close = (lo[1] > 0 and lastBarOfDay);
def short_close = (so[1] > 0 and lastBarOfDay);

#close eod
#AddOrder(OrderType.SELL_TO_CLOSE, lastBarOfDay, tickcolor = Color.green, arrowcolor = Color.green, name =lastBarOfDay+ "EOD");
#AddOrder(OrderType.BUY_TO_CLOSE, lastBarOfDay, tickcolor = Color.green, arrowcolor = Color.green, name = lastBarOfDay+"EOD");
AddOrder(OrderType.SELL_TO_CLOSE, long_close, tickcolor = Color.green, arrowcolor = Color.green, name =lastBarOfDay + "EOD");
AddOrder(OrderType.BUY_TO_CLOSE, short_close, tickcolor = Color.green, arrowcolor = Color.green, name = lastBarOfDay + "EOD");
#
 

Attachments

  • img1.JPG
    img1.JPG
    100.1 KB · Views: 152
Solution

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

Hello,

Okay so not sure why but I have to set the time at
start 1030
end 1530

to get the 730 1230 time frame working.
I am pst time zone.
I unhashed the line in the code to show the lines and kept adjusting.

Also not sure about the number of trades input. I tried both one and many and the results are the same.

Thanks again for looking at this

1726693033537.png
 
Hello again,

It also doesn't appear to be taking the opposite trade.
I tried the one,many settings both ways.

here is an example.

IT took the short but didn't reverse it. It did close end of day though.

Thanks

1726788324869.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
350 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