Subtract a number of days from YYYYMMDD?

What I'm looking for is a way to restrict my number of trades placed per day to a limit of 3, no more. I have most of the logic for this done already, but the last thing I'm missing is a way to determine what "day number" the current day is.

Example, let's say we're on the 1 minute (or any intraday) chart, and we scroll all the way to the left, roughly 30 days ago. That would be considered "day 1" on the chart, and if my "total trades placed" was 3, and we divided that by the day number of 1, we would get 3, so no more trades are allowed for that day. When the new day starts after midnight, that should be considered "day 2", and when we place another 3 trades totalling the count to 6 total trades, divided by "day 2" = 3, no more trades for that day, etc etc.

What I thought you could do is something like this assuming you're on the 1 minute chart with 30 days of history showing:

Code:
def currentDayNumber = DaysFromDate(GETYYYYMMDD() - 30) + 1;

...where today's date - 30 would make 0, +1 to get an appropriate divisor.

Any ideas?

I should reiterate, this is to be used for a strategy, not an indicator. Also that formula doesn't really make sense as it'll end up with negative numbers, but I have no clue how to subtract ints from datetimes in thinkscript...
 
Solution
What I'm looking for is a way to restrict my number of trades placed per day to a limit of 3, no more. I have most of the logic for this done already, but the last thing I'm missing is a way to determine what "day number" the current day is.

Example, let's say we're on the 1 minute (or any intraday) chart, and we scroll all the way to the left, roughly 30 days ago. That would be considered "day 1" on the chart, and if my "total trades placed" was 3, and we divided that by the day number of 1, we would get 3, so no more trades are allowed for that day. When the new day starts after midnight, that should be considered "day 2", and when we place another 3 trades totalling the count to 6 total trades, divided by "day 2" = 3, no more...
What I'm looking for is a way to restrict my number of trades placed per day to a limit of 3, no more. I have most of the logic for this done already, but the last thing I'm missing is a way to determine what "day number" the current day is.

Example, let's say we're on the 1 minute (or any intraday) chart, and we scroll all the way to the left, roughly 30 days ago. That would be considered "day 1" on the chart, and if my "total trades placed" was 3, and we divided that by the day number of 1, we would get 3, so no more trades are allowed for that day. When the new day starts after midnight, that should be considered "day 2", and when we place another 3 trades totalling the count to 6 total trades, divided by "day 2" = 3, no more trades for that day, etc etc.

What I thought you could do is something like this assuming you're on the 1 minute chart with 30 days of history showing:

Code:
def currentDayNumber = DaysFromDate(GETYYYYMMDD() - 30) + 1;

...where today's date - 30 would make 0, +1 to get an appropriate divisor.

Any ideas?

I should reiterate, this is to be used for a strategy, not an indicator. Also that formula doesn't really make sense as it'll end up with negative numbers, but I have no clue how to subtract ints from datetimes in thinkscript...

in your method of using total trades and total days, what happens if a day has 1 trade? a day after that could be allowed to have 5.

after you go past a weekend, the counts will be off.


if you want to limit a counter to 3 , each day, why don't you,...
count to 3 each day?

add this to the end of your study.
change trigger , so it is equal to the variable you use to trigger a buy order.

if you don't change my trigger = code line, it will cause an error.


will have to change the addorder that close the trades, so they don't try to close a non existent 4th trade and cause a short trade.


Code:
def bn = barnumber();

# normal trading hours
def rth =  GetTime() >= RegularTradingStart(GetYYYYMMDD()) and GetTime() < RegularTradingEnd(GetYYYYMMDD());

def trigger = ... your true/false buy signal used in buy addorder() function ...

def diffday = GetDay() <> GetDay()[1];

input trade_only_during_normal_hours = yes ;
def tradeok = if !trade_only_during_normal_hours then 1 else rth;


input day_limit = 3;

def cnt =
if bn = 1 then 0
else if diffday and trigger and tradeok then 1
else if diffday then 0
else if cnt[1] == day_limit then cnt[1]
else if trigger and tradeok then cnt[1] + 1
else cnt[1];

def enable = (cnt < day_limit and tradeok);

# use buyx in a new addorder()
def buyx = (enable and trigger);
# addorder(    , buyx,    );

# new sell variable for addorder
# def sellx = (enable  and sell  variable);
# addorder(    , sellx,    );

addlabel(1,
cnt + " trades today",
(if enable then color.green else color.red));

addchartbubble(buyx, high, "buy", color.green, yes);
#

(just typing on my cell, so forgive typos)
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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