# Counting Number of Days A Strategy Is Using

#### Internet Name

##### Member
I'm looking for a code or some guidance on writing a code that will calculate the number of days a strategy is bought in for. So if I'm looking at a 30 day chart, and the strategy buys once and sells 4 days later, a label would return a value of 4.

Solution
Here's two labels with one of them saying the number of days a current trade has been going for (it goes to 0 when not in a trade), and a label that always says the most recent trade's number of days.
Code:
``````def sell = ; #put your sell condition here

AddOrder(OrderType.SELL_TO_CLOSE, sell, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Sell");

def orderDir = CompoundValue(1, if buy then 1 else if sell then -1 else orderDir[1], 0);
def isOrder = orderDir crosses 0;

def curDaysCounter = if IsNaN(curDaysCounter[1]) and orderDir==1 and isOrder then 1 else if orderDir==1 then...``````
Here's two labels with one of them saying the number of days a current trade has been going for (it goes to 0 when not in a trade), and a label that always says the most recent trade's number of days.
Code:
``````def sell = ; #put your sell condition here

AddOrder(OrderType.SELL_TO_CLOSE, sell, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "Sell");

def orderDir = CompoundValue(1, if buy then 1 else if sell then -1 else orderDir[1], 0);
def isOrder = orderDir crosses 0;

def curDaysCounter = if IsNaN(curDaysCounter[1]) and orderDir==1 and isOrder then 1 else if orderDir==1 then curDaysCounter[1]+1 else 0;
def curDays = curDaysCounter;
AddLabel(1, "Current days: " + curDays, Color.GRAY);

Thank you TNT! I am using minute charts, so I edited it a little bit to fit what I was doing, and the final code I went with is below for anyone else who is interested.

Code:
``````input CountNumOfDaysInStrategy = no;

def sell = SellPoint; #put your sell condition here

def orderDir = CompoundValue(1, if buy
then 1
else if sell
then -1
else orderDir[1], 0);

def DaysInStrategy = if IsNaN(DaysInStrategy[1])
then 1
else if orderDir==1 and GetDay() != GetDay()[1]
then DaysInStrategy[1]+1
else DaysInStrategy[1];

AddLabel(1, "Approx Days in Strategy: " + DaysInStrategy, Color.White);
AddChartBubble(orderDir ==1 and GetDay() != GetDay()[1] and CountNumOfDaysInStrategy,close,DaysInStrategy,Color.white);``````

The days in strategy are not 100% accurate on the minute charts because it's impossible to determine how to count half days and such, but it does a great job at giving a ballpark figure.

Edit: I've removed the total trading days because it is starting at 10 on every minute chart except 360days. Really not needed since we can easily see how many days are on the chart. It can be added if needed by adding the below:

then 1
else if GetDay() != GetDay()[1]

Last edited:

87k+ Posts
389 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?