# Profit Calculator

#### bmcgraw393

##### New member
VIP
Does anyone know how to write a script that adds and subtracts the price of candles at specific points?
For instance, I have an indicator that signals a buy and sell point. I want to be able to write a script that will display the percent profit from the buy and sell point. From there, I want the script to add up every buy-sell profit in a certain range (ex the last 5 days). As of now, I make a script and then manually enter a month worth of information into excel to see if the script is profitable; I want to make something that gets around the manual labor.

I have written many programs, but this is one concept that I have yet to discover.

Thanks y'all

#### BenTen

Staff member
Staff
VIP
If you can implement a backtesting strategy for your indicator, you can get the entry and exit points. Then, export it to Google Sheet or Excel and calculate the specific points there.

#### bmcgraw393

##### New member
VIP
Hey Ben,

That's where I am looking for help. For simplicity, let's say my indicator is the HMA. How can I implement a back testing strategy that will grab the price (buy) when the HMA first moves up and the price (sell) when the indicator moves down?
I think something like this would be of great use to everyone if I can get help starting it.

I found a very useful link to do this. I was able to implement it on one of my indicators. https://ryanclouser.com/2019/04/12/Backtesting-with-thinkscript/

Last edited by a moderator:

#### Gabrielx77

##### Active member
@bmcgraw393 Would you be able to share the indicator that's used for your entry and exit signal? Thank you

#### bmcgraw393

##### New member
VIP
@Gabrielx77 I figured it out! I have been using back testing for a number of different indicators. Below is the fundamental code I use (not my actual indicator just the general idea):

def TradeSize = inset dollar amount / close;
input price = close;
input length = 20;
input displace = 0;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

def earlyBuy = SecondsFromTime(0930) >= 0 and SecondsFromTime(1600) <= 0;

#### bmcgraw393

##### New member
VIP
I am now looking to add a trail stop backtesting function. Do either of you know of any forums that could explain how this is done?
@Gabrielx77 @BenTen

#### Gabrielx77

##### Active member
I am now looking to add a trail stop backtesting function. Do either of you know of any forums that could explain how this is done?
@Gabrielx77 @BenTen
I don't I'm sorry :/

## 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?