How to make strategy close out a trade near end of day Friday same week trade entered?

dbenz45

New member
I have a strategy backtester I am using, The strategy I am using is for credit spreads. I am wondering is there a way to setup a buy to close or sell to close code that will close out the trade at the closing price on Friday? The friday closing price immediately after trade entry? Would be amazing help! I have the whole rest of the code just need to close out the trades at the closing price on the closest Friday to trade entry, even if its just a 1 day trade. and the strategy backtester should calculate my PL based on if I bought shares when I have the signal to buy shares and then I will sell them at the closing price on the immediate Friday closing price after trade entry.

AddOrder(SELL_TO_CLOSE, "close out the shares purchased immediately at the next Friday closing price"

what is in quotes is what i am not sure how to code, hopefully this is a quick fix, thanks for any help!
 
Solution
@dbenz45
Easiest way should be:
Ruby:
condition = GetDayofWeek(GetYYYYMMDD()) == 5 and SecondsTillTime(1600) == 0
This should have it sell on Friday at 4 p.m. if you were in a long trade.
@dbenz45
Easiest way should be:
Ruby:
condition = GetDayofWeek(GetYYYYMMDD()) == 5 and SecondsTillTime(1600) == 0
This should have it sell on Friday at 4 p.m. if you were in a long trade.
 
Solution

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

@dbenz45
Easiest way should be:
Ruby:
condition = GetDayofWeek(GetYYYYMMDD()) == 5 and SecondsTillTime(1600) == 0
This should have it sell on Friday at 4 p.m. if you were in a long trade.
okay it works on all trades, except for some reason whenever my strategy gives a signal to trade on a friday, meaning it enters the trade on a monday, then it closes the trade out on monday same day. IDK why that is, is there a way to fix that or a way to exclude trade entries on mondays
 
okay it works on all trades, except for some reason whenever my strategy gives a signal to trade on a friday, meaning it enters the trade on a monday, then it closes the trade out on monday same day. IDK why that is, is there a way to fix that or a way to exclude trade entries on mondays
oh okay now I see if it enters a trade on a Friday it just closes it out at the monday open, can I just exclude monday entries?
 
also some weeks are holidays and dont have 5 trading days so then it keeps the trade open for another week until friday close, also it often likes to close out trades at the open price on monday even if not opened on friday
 
If I am trying to test a swing trading strategy in a backtester in TOS, and i want the position closed out on the last trading day of the week at market close, can someone help me figure this out:

If I use this as the exit condition, then it works unless:
1. there is less than 5 days in that week due to holiday or day off then trade stays open for another week and closes out on the next week friday close
2. the trade is opened on Friday - it closes out the trade at the price of Monday open instead of friday close
3. if the trade is opened on a monday - it closes out the trade same day at the price of the previous friday close instead of the friday close upcoming.

this is what I am using now:

def condition = GetDayofWeek(GetYYYYMMDD()) == 5 and SecondsTillTime(1600) == 0;
AddOrder(OrderType.SELL_TO_CLOSE, exitcondition);

Any ideas on how to fix this so I can backtest a variety of strategies I have written for swing trading?
Just need the backtester to reliably close out the trade at the closing price on the last day of trading each week, no matter what day the trade is opened and even if there are less than 5 days in a trading week sometimes. I want these trades to be closed lined up with the last day of each week: the options expiration cycle expiration date
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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