Counting Number of Days A Strategy Is Using

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

def buy = ; #put your buy condition here

AddOrder(OrderType.BUY_TO_OPEN, buy, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Buy");
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

def buy = ; #put your buy condition here

AddOrder(OrderType.BUY_TO_OPEN, buy, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Buy");
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 recTradeCounter = if IsNaN(recTradeCounter[1]) and orderDir==1 and isOrder then 1 else if orderDir==1 then recTradeCounter[1]+1 else recTradeCounter[1];
def curDays = curDaysCounter;
def recDays = recTradeCounter;
AddLabel(1, "Current days: " + curDays, Color.GRAY);
AddLabel(1, "Most recent trade days: " + recDays, Color.GRAY);
aqcabQe.png

BZp9q3w.png
 
Solution
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 buy = BuyPoint; #put your buy 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:

def TotalTradingDays = if IsNaN(TotalTradingDays[1])
then 1
else if GetDay() != GetDay()[1]
then TotalTradingDays[1]+1
else TotalTradingDays[1];

AddLabel(1, "Most recent trade days: " + TotalTradingDays, Color.GRAY)
 
Last edited:

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
389 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