Buy at Candle Open, Sell at Profit Target or Candle Close For ThinkOrSwim

keepthefaith613

New member
Long time lurker, first time poster. Tried searching and googling my answer but couldn't seem to. Perhaps I'm thinking about this wrong and you can teach me how to code it properly.

I'd like to backtest a strategy that buys at a candle's open, sells when it hits a profit target or the candle closes (whichever comes first).

The impetus for this was to backtest buying /ES at open (i.e., 6 PM EST), closing if it hits a particular target, or if the day ends.

Thanks!
 
Try this... might not be perfect but it'll be a good start. Might want to play around with what you use for your entry and closing prices. However, as far as market times and setting your stop, I think it's good. (emphasis on think) Also, probably want to put a stop for loss in there as well, unless you're all in on buys all the time :)

Ruby:
input OpenTime = 0930; #hint OpenTime: Opening time of market
input CloseTime = 1600; #hint CloseTime: Closing time of market
input StopPoints = 3.0; #hint: StopPoints: Close position if target hit
input TradeSize = 1; #hint : TradeSize: How many shares/contract

# Set market hours
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);

# Only use market hours when using intraday timeframe
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !isIntraDay then 1 else if  isIntraDay and Begin > 0 and End > 0 then 1 else 0;

def FirstCandle = !MarketOpen[1] and MarketOpen;
def LastCandle = MarketOpen and !MarketOpen[-1];
def EP = entryPrice();

AddOrder(OrderType.BUY_TO_OPEN, FirstCandle, Open, TradeSize, name = "BTO: " + Close);
AddOrder(OrderType.SELL_TO_CLOSE, close > EP + StopPoints, Close, Tradesize, name="Target: " + Close);
AddOrder(OrderType.SELL_TO_CLOSE, LastCandle, Close, Tradesize, name="MKT Close: " + Close);
 
Wow! That's fantastic! Thank you! Is there any way to adjust the OpenTime to be when the futures market opens? I could adjust it to 0001, but if I wrote in anything higher than 1600 (e.g., 1800), nothing happened. Thank you!!!
 
Wow! That's fantastic! Thank you! Is there any way to adjust the OpenTime to be when the futures market opens? I could adjust it to 0001, but if I wrote in anything higher than 1600 (e.g., 1800), nothing happened. Thank you!!!

Ok, try this and see if it works better over the night hours.

Ruby:
input OpenTime = 0930; #hint OpenTime: Opening time of market
input CloseTime = 1600; #hint CloseTime: Closing time of market
input StopPoints = 3.0; #hint: StopPct: Close position if target hit
input TradeSize = 1; #hint : TradeSize: How many shares/contract

# Set market hours
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);

def SpansMidnight = if CloseTime < OpenTime then 1 else 0;

def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;

def MarketOpen = if !isIntraDay then 1 else if !SpansMidnight and (Begin[-1] == 0 or Begin[-1] > 0) and (End > 0) then 1 else if SpansMidnight and (((Begin == 0 or Begin > 0) and (End < 0)) OR (Begin < 0 and End > 0)) then 1 else 0;

def FirstCandle = Begin[1] < 0 and (Begin == 0 or Begin > 0);
def LastCandle = MarketOpen[-1] == 0;
def EP = entryPrice();

AddOrder(OrderType.BUY_TO_OPEN, FirstCandle == 1, Close, TradeSize, name = "BTO: " + Close);
AddOrder(OrderType.SELL_TO_CLOSE, close > EP + StopPoints, Close, Tradesize, name="Target: " + Close);
AddOrder(OrderType.SELL_TO_CLOSE, LastCandle == 1, Close, Tradesize, name="MKT Close: " + Close);
 
Yes! That worked! Thanks so much!
FWIW, here's the outcome of my backtesting (all on /ES, take profit at 4 pts, 180d on 5m chart):
6PM-12A
Winrate 75.34%
AvgWin $268.07
AvgLoss -$665.28
MaxLoss ($1625.00)
High $5537.50
Low ($3637.50)
Total P/L $5,537.50

6P-830A
Winrate 86.21%
AvgWin $275.20
AvgLoss -$1,524.38
MaxLoss ($2837.50)
High $6062.50
Low ($1425.00)
Total P/L $3,912.50

12A-4P
Winrate 93.15%
AvgWin $282.63
AvgLoss -$2,828.75
MaxLoss ($6737.50)
High $10150.00
Low ($4162.50)
Total P/L $10,150.00
 
Yes! That worked! Thanks so much!
FWIW, here's the outcome of my backtesting (all on /ES, take profit at 4 pts, 180d on 5m chart):
6PM-12A
Winrate 75.34%
AvgWin $268.07
AvgLoss -$665.28
MaxLoss ($1625.00)
High $5537.50
Low ($3637.50)
Total P/L $5,537.50

6P-830A
Winrate 86.21%
AvgWin $275.20
AvgLoss -$1,524.38
MaxLoss ($2837.50)
High $6062.50
Low ($1425.00)
Total P/L $3,912.50

12A-4P
Winrate 93.15%
AvgWin $282.63
AvgLoss -$2,828.75
MaxLoss ($6737.50)
High $10150.00
Low ($4162.50)
Total P/L $10,150.00
Could you share the code for this? Interested in how you set the take profits and stop loss.
 
Hey keepthefaith613,

Cool idea. Thanks for your post. I too am a lurker for the moment, but gaining knowledge as I go. I hope to be answering others some day.

Did you happen to track the commissions for your back testing results? TDAmeritrade has rather high commissions for /ES trades. Thinkorswim allows you to view your commissions in the Monitor tab. I have tested a few strategies that at first appear to be nicely profitable, but once I took into account commissions, were not profitable. Just wondering what your results might show taking into account commissions. Obviously the more trades that higher the commissions.
 
Thanks! Yeah, I guess if I was paying attention I could have just calculated that for myself, huh? Sorry for the dumb question. :)
Have a great evening!
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
328 Online
Create Post

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