# Intraday Position Size Calculator Help

#### lambda100

##### New member
Hey everyone,

I have a question, maybe someone can assist, or give any idea.

In this image I uploaded you can see what I did, and what I wish it could be:

In short, I would like to fully convert the Fibonacci Retracement tool to a Risk/Reward tool. I did some of it by a simple study edit of that tool, but I wish also to add automatic indications for the "price difference" between the entry and the stop loss with an automatic calculation of the amount of shares you need to buy (based on your risk)

Somebody maybe know some ThinkScript? Or have any Idea where can I find assistance on that?

Many Thanks!!

that is code snippet is exactly what I was looking for on here awhile back to add to the Ultimate Breakout Indicator so that I could add to the indicator to use like a position sizer. Since Ultimate Breakout Indicator gives an entry point and a stop point. I would like to add this snippet of code to determine position sizing by inputting a dollar risk amount such as \$500 risk per trade. then when the entry and stop points display it would also tell you how many shares to ttrade based on the risk amount per trade. Does anybody know how to accomplish this?

I believe this thread includes part of what you are trying to achieve. Wouldn't know where to start piecing together the code. Good luck.

Last edited:
Hey there,

I'm relatively new to thinkScript and was wondering if someone could help me with a simple Position Size Calculator for day trading. I've been using an ATR based one pulled from this thread: https://usethinkscript.com/threads/position-size-calculator-for-thinkorswim.588/ that typically works for most situations, but at times of high volatility I find that I would rather use a calculator based off a static/predetermined stop loss level. For example:

I want to buy a stock near \$50 with a predetermined stop-loss level below support at \$45. Is there a calculator that will update my preferred position size real-time as price fluctuates based off of this predetermined stop-loss level? Something like:

INPUT: "Account Balance"
STOP LOSS: "Input Price"
SHARES TO PURCHASE: "X"

This would save me a lot of time from having to manually calculate my position size as price is constantly changing. Any help would be appreciated, thanks!

I modified something I used to use to put something together for you. Give this a try.

Ruby:
``````input stop = 0.00;
input riskOverride = 0; # use this to force a fixed dollar amount
input riskPercent = 1;

input accountBalanceFallback = 25000; # this is used automatically if GetTotalCash() fails
input accountBalanceOverride = 0;

# reminder = GetTotalCash only works with premarket data on
def bal = GetTotalCash();
# reminder = GetTotalCash only works with premarket data on

def accountBalance =
if accountBalanceOverride > 0 then accountBalanceOverride
else if !IsNaN(bal) then bal
else accountBalanceFallback
;

def buyingPower = accountBalance * 4;
def risk = RoundDown(if riskOverride == 0 then accountBalance * riskPercent / 100 else riskOverride, 2);

def stopRange = Max(close, stop) - Min(close, stop);
def riskMaxShares = RoundDown(risk / stopRange, 0);

AddLabel(stop > 0, "Shares: " + shares, Color.LIGHT_GRAY);``````

I modified something I used to use to put something together for you. Give this a try.

Ruby:
``````input stop = 0.00;
input riskOverride = 0; # use this to force a fixed dollar amount
input riskPercent = 1;

input accountBalanceFallback = 25000; # this is used automatically if GetTotalCash() fails
input accountBalanceOverride = 0;

# reminder = GetTotalCash only works with premarket data on
def bal = GetTotalCash();
# reminder = GetTotalCash only works with premarket data on

def accountBalance =
if accountBalanceOverride > 0 then accountBalanceOverride
else if !IsNaN(bal) then bal
else accountBalanceFallback
;

def buyingPower = accountBalance * 4;
def risk = RoundDown(if riskOverride == 0 then accountBalance * riskPercent / 100 else riskOverride, 2);

def stopRange = Max(close, stop) - Min(close, stop);
def riskMaxShares = RoundDown(risk / stopRange, 0);

AddLabel(stop > 0, "Shares: " + shares, Color.LIGHT_GRAY);``````
This is exactly what I was looking for! Thanks for taking the time to do this man, works great.

another excellent masterpie
I modified something I used to use to put something together for you. Give this a try.

Ruby:
``````input stop = 0.00;
input riskOverride = 0; # use this to force a fixed dollar amount
input riskPercent = 1;

input accountBalanceFallback = 25000; # this is used automatically if GetTotalCash() fails
input accountBalanceOverride = 0;

# reminder = GetTotalCash only works with premarket data on
def bal = GetTotalCash();
# reminder = GetTotalCash only works with premarket data on

def accountBalance =
if accountBalanceOverride > 0 then accountBalanceOverride
else if !IsNaN(bal) then bal
else accountBalanceFallback
;

def buyingPower = accountBalance * 4;
def risk = RoundDown(if riskOverride == 0 then accountBalance * riskPercent / 100 else riskOverride, 2);

def stopRange = Max(close, stop) - Min(close, stop);
def riskMaxShares = RoundDown(risk / stopRange, 0);

AddLabel(stop > 0, "Shares: " + shares, Color.LIGHT_GRAY);
[
[/QUOTE]``````

I modified something I used to use to put something together for you. Give this a try.

Ruby:
``````input stop = 0.00;
input riskOverride = 0; # use this to force a fixed dollar amount
input riskPercent = 1;

input accountBalanceFallback = 25000; # this is used automatically if GetTotalCash() fails
input accountBalanceOverride = 0;

# reminder = GetTotalCash only works with premarket data on
def bal = GetTotalCash();
# reminder = GetTotalCash only works with premarket data on

def accountBalance =
if accountBalanceOverride > 0 then accountBalanceOverride
else if !IsNaN(bal) then bal
else accountBalanceFallback
;

def buyingPower = accountBalance * 4;
def risk = RoundDown(if riskOverride == 0 then accountBalance * riskPercent / 100 else riskOverride, 2);

def stopRange = Max(close, stop) - Min(close, stop);
def riskMaxShares = RoundDown(risk / stopRange, 0);

AddLabel(stop > 0, "Shares: " + shares, Color.LIGHT_GRAY);``````
I modified something I used to use to put something together for you. Give this a try.

Ruby:
``````input stop = 0.00;
input riskOverride = 0; # use this to force a fixed dollar amount
input riskPercent = 1;

input accountBalanceFallback = 25000; # this is used automatically if GetTotalCash() fails
input accountBalanceOverride = 0;

# reminder = GetTotalCash only works with premarket data on
def bal = GetTotalCash();
# reminder = GetTotalCash only works with premarket data on

def accountBalance =
if accountBalanceOverride > 0 then accountBalanceOverride
else if !IsNaN(bal) then bal
else accountBalanceFallback
;

def buyingPower = accountBalance * 4;
def risk = RoundDown(if riskOverride == 0 then accountBalance * riskPercent / 100 else riskOverride, 2);

def stopRange = Max(close, stop) - Min(close, stop);
def riskMaxShares = RoundDown(risk / stopRange, 0);

AddLabel(stop > 0, "Shares: " + shares, Color.LIGHT_GRAY);``````

Good Morning Team... So I think this script will be very helpful... Is their a script that would be about to be made with active trader. I use 1 trg bracket so when I place my order it will automatically give me a limit and stop order based on my risk level per trade. At the time I have to look on a paper that I have for my share size adjustment per risk. The question would be is their a script that once I put my order in it calculates my risk which is the previous candle range. Willing to pay for script... Thank You .... My number is 3256684011 Louis just in case I didn't word this correctly. Thank You

@voltron Welcome to the usethinkscript forums... Unfortunately, Thinkscript does not provide us any means of writing code that will directly interact with Active Trader, or any Thinkorswim modules for that matter... Thinkscript is for basic calculations and painting on charts, scanning, watchlist columns, etc... It appears that your request would be beyond the scope of the capabilities of the platform...

There may, however, be a way yo do this calculation and display the results on your chart... We would need more details, however...

Last edited:
@voltron Welcome to the usethinkscript forums... Unfortunately, Thinkscript does not provide us any means of writing code that will directly interact with Active Trader, or any Thinkorswim modules for that matter... Thinkscript is for basic calculations and painting on charts, scanning, watchlist columns, etc... It appears that your request would be beyond the scope of the capabilities of the platform...

There may, however, be a way yo do this calculation and display the results on your chart... We would need more details, however...
What details would you need?

What details would you need?

Would need to know your risk profile data... Feel free to contact me via Discord if you'd like to discuss this there... I'm usually there...

What details would you need?
The code I provided above puts a label on your chart that tells you the range between stop and entry and how many shares to trade based on the risk you defined. The person who requested it knew where they wanted their stop and wanted to input that and have calculations for the rest. If you have a formula to determine your stop based on price action or other indicators then that part can be automated as well so no input is required.

The code I provided above puts a label on your chart that tells you the range between stop and entry and how many shares to trade based on the risk you defined. The person who requested it knew where they wanted their stop and wanted to input that and have calculations for the rest. If you have a formula to determine your stop based on price action or other indicators then that part can be automated as well so no input is required.
So I copied the script but is their a script for the risk being the previous candle and not the current.. Or both would be great?

So I copied the script but is their a script for the risk being the previous candle and not the current.. Or both would be great?

I'm assuming you mean to use the range (high minus low) of a single candle as the stop range. I removed the input for stop entry and replaced it with a candle index input. It'll set the range based on `(high-low)+.02` of the selected candle to give a penny above and below the candle. You can adjust or remove the .02 in the code. If you want the current candle, set the input to zero. Previous candle, set it to one. And so on. If you want both you can add this study to the chart twice, one set to zero and one set to one. I also made it display the range it calculated so you can use that for the price offset in Active Trader.

Ruby:
``````input candleIndex = 0;
input riskOverride = 0; # use this to force a fixed dollar amount
input riskPercent = 1;

input accountBalanceFallback = 25000; # this is used automatically if GetTotalCash() fails
input accountBalanceOverride = 0;

# reminder = GetTotalCash only works with premarket data on
def bal = GetTotalCash();
# reminder = GetTotalCash only works with premarket data on

def accountBalance =
if accountBalanceOverride > 0 then accountBalanceOverride
else if !IsNaN(bal) then bal
else accountBalanceFallback
;

def buyingPower = accountBalance * 4;
def risk = RoundDown(if riskOverride == 0 then accountBalance * riskPercent / 100 else riskOverride, 2);

def stopRange = (high[candleIndex] - low[candleIndex]) + .02;
def riskMaxShares = RoundDown(risk / stopRange, 0);

AddLabel(yes, "Range: " + stopRange + " | Shares: " + shares, Color.LIGHT_GRAY);``````

I'm assuming you mean to use the range (high minus low) of a single candle as the stop range. I removed the input for stop entry and replaced it with a candle index input. It'll set the range based on `(high-low)+.02` of the selected candle to give a penny above and below the candle. You can adjust or remove the .02 in the code. If you want the current candle, set the input to zero. Previous candle, set it to one. And so on. If you want both you can add this study to the chart twice, one set to zero and one set to one. I also made it display the range it calculated so you can use that for the price offset in Active Trader.

Ruby:
``````input candleIndex = 0;
input riskOverride = 0; # use this to force a fixed dollar amount
input riskPercent = 1;

input accountBalanceFallback = 25000; # this is used automatically if GetTotalCash() fails
input accountBalanceOverride = 0;

# reminder = GetTotalCash only works with premarket data on
def bal = GetTotalCash();
# reminder = GetTotalCash only works with premarket data on

def accountBalance =
if accountBalanceOverride > 0 then accountBalanceOverride
else if !IsNaN(bal) then bal
else accountBalanceFallback
;

def buyingPower = accountBalance * 4;
def risk = RoundDown(if riskOverride == 0 then accountBalance * riskPercent / 100 else riskOverride, 2);

def stopRange = (high[candleIndex] - low[candleIndex]) + .02;
def riskMaxShares = RoundDown(risk / stopRange, 0);

AddLabel(yes, "Range: " + stopRange + " | Shares: " + shares, Color.LIGHT_GRAY);``````
Thank you you are amazing with script. Last question is their a script were I can hoover over any candle and show me the Risk based on that candle. Thank you

@voltron Thinkscript does not provide us any means of writing studies that will be interactive with the platform-controlled elements or any ThinkOrSwim modules... Custom modifications are beyond the scope of the capabilities of the platform...

Last edited:
Thank you you are amazing with script. Last question is their a script were I can hoover over any candle and show me the Risk based on that candle. Thank you

As Merry indicated, we can't do anything on hover. However, TOS already gives you what you're asking for. If you look at the top of the chart, below the symbol entry field, it shows Symbol, Timeframe, Date, Open, High, Low, Close, Range. Range is what you care about. Just add .02 to it if you're keeping that extra two cents.

Alternatively, you could have a lower study that shows you the calculated range as a plot. To see what that would look like, try adding the ATR study to your chart. The numbers won't be what you're looking for. This is just to demonstrate what it would look like and how you can hover over past candles to see the values for those periods in the lower indicator's title bar.

With this range plus a couple cents calculation being simple mental math I just do as I described in the first paragraph.

As Merry indicated, we can't do anything on hover. However, TOS already gives you what you're asking for. If you look at the top of the chart, below the symbol entry field, it shows Symbol, Timeframe, Date, Open, High, Low, Close, Range. Range is what you care about. Just add .02 to it if you're keeping that extra two cents.

Alternatively, you could have a lower study that shows you the calculated range as a plot. To see what that would look like, try adding the ATR study to your chart. The numbers won't be what you're looking for. This is just to demonstrate what it would look like and how you can hover over past candles to see the values for those periods in the lower indicator's title bar.

With this range plus a couple cents calculation being simple mental math I just do as I described in the first paragraph.
Thank You ... Really the most important feature would be if it can calculate the previous candle Risk calculator instead of the current one. The script I would love to have is to calculate the previous candle. Thank you
Also is their a way to position the Trg bracket in descending order. Everytime I use one of the drop down box it keep the most current one a the top which is out of order.

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