# Position Size Calculator for ThinkorSwim

#### QUIKTDR1

##### Active member
VIP
Thank you VOLLERMIST I will look at this tonight

Are we supposed to add this to a strategy or study? When one tries to add to studies it asks for a plot and strategy asks for an Add order

#### vollermist

##### New member
Are we supposed to add this to a strategy or study? When one tries to add to studies it asks for a plot and strategy asks for an Add order
Irrelevant if it ask for a plot...one can just make a label for the variable "shares". In a strategy, just use the variable "shares" in the AddOrder statement instead of the number of shares. I incorporate this code (or a variation of this code) in studies AND strategies. I only trade stocks, so I don't know how it will work on other instruments. I guess you could call this code a "snippet" that can be used standalone by just adding an AddLabel statement. I'm not a programmer, so I don't know if I'm using the correct terms.

In the strategy section, I posted a strategy I had been working on which incorporates a version of this code. Most of what I code is coded for range bar charts, and may be suboptimal for time or tick based charts. GrowEquityStrategy including Kelly-based position sizing

Last edited:

#### Renko_Ranger

##### Member
I came across a script months ago:
but I felt that it was lacking. I just got around to finishing it. I only wish that it could also highlight the price in the active trader.

http://tos.mx/VviLozV

Code:
``````# Simple Position Calculator
# Assembled by BenTen at UseThinkScript.com

input Budget = 2000;
def current_price = close;
def Share_Quantity_purchase_limit =  Budget / current_price;

############# labels

AddLabel(yes, Concat("Shares available for purchase = ", Round(Share_Quantity_purchase_limit)), Color.ORANGE);

###############################################################################################

input profit_goal = 50.00;
plot Close_Position = profit_goal/Share_Quantity_purchase_limit ;
Close_Position.SetDefaultColor(GetColor(0));

AddLabel(yes, Concat("profit goal = ", Round(profit_goal)), Color.orange);
AddLabel(yes, Concat("required price change = ", (Close_Position)), Color.white);

def  yellow = profit_goal/Share_Quantity_purchase_limit;
#####################################################################

plot priceLine = current_price + close_Position;;
priceline.SetPaintingStrategy(PaintingStrategy.line);
priceLine.SetLineWeight(1);

AddLabel(yes, Concat(" Stock price + profit goal = ",Round(current_price + close_Position )), Color.white);

#AddLabel(yes, Concat(" The amount of active trader movement  = ",Round(Share_Quantity_purchase_limit + close_Position )), Color.white);``````
The simplicity of this calculator works very well with my Renko DMI Strategy. Thanks for sharing!

#### Betozenho

##### New member
I reverse engineered what I think the two labels show.

This is the output of that work.

### thinkScript Code

Code:
``````#
# PositionSizingCalculator
#
# Author: Kory Gill, @korygill
#
#

input RiskUnit = 100;
input AggregationPeriod = AggregationPeriod.FIFTEEN_MIN;

def highVal = high(period = AggregationPeriod);
def lowVal = low(period = AggregationPeriod);

yes,
"High: " + highVal + " Low: " + lowVal,
Color.Gray
);

def diff = highVal-LowVal;

yes,
AsDollars(RiskUnit) + " risk with " + AsDollars(diff) + " stop = " + Floor(Round(RiskUnit/diff)) + " shares",
Color.Gray
);``````

https://tos.mx/HwObLl

Thanks,
Kory Gill, @korygill
aMAZING SCRIPT!!! IS IT POSSIBLE TO HAVE IT WORKING ON A TICK CHARTS?

#### grsg2010

##### New member
Hi, I want to add to this code a label to tell me how many shares I have available to buy, so will be TotalShares= GetTotalCash () / GetAveragePrice(); But I can not make work, can somebody help me

Here is the code:
plot entryPrice;

input showDetails = yes;
input showBasis = yes;
input showProfitLoss = yes;
input showFills = no;
input MaxShares = yes;

# Entry Price
entryPrice = GetAveragePrice();
entryPrice.AssignValueColor(if close < entryPrice then Color.DOWNTICK else Color.UPTICK);
entryPrice.SetLineWeight(2);
entryPrice.SetStyle(Curve.SHORT_DASH);

# Basis Calculat ion
def qty = GetQuantity();
def openCost = qty * GetAveragePrice();
def basis = (qty * entryPrice);

def Cash = GetTotalCash ();
def StockPrice = GetAveragePrice ();
def TotalShares= GetAveragePrice () / GetTotalCash ();

#Labels
AddLabel(showDetails, qty + "@" + AsDollars(entryPrice), Color.GRAY);
AddLabel(showBasis and netProfitLossPastTrades == 0.0, "Basis: " + AsDollars(basis), if basis > (qty * close) then Color.DOWNTICK else Color.UPTICK);
AddLabel(showBasis and netProfitLossPastTrades != 0.0, "Basis (adj): " + AsDollars(basis), if basis > (qty * close) then Color.DOWNTICK else Color.UPTICK);
def pl = (close - entryPrice) * qty;
AddLabel(showProfitLoss, "P/L: " + AsDollars(pl), if pl > 0 then Color.UPTICK else if pl == 0 then Color.WHITE else Color.DOWNTICK);
AddLabel(showProfitLoss, "P/L/Share: " + AsDollars(close - entryPrice), if (close - entryPrice) > 0 then Color.UPTICK else if (close - entryPrice) == 0 then Color.WHITE else Color.DOWNTICK);

AAddLabel(MaxShares, "TotalShares: " + AsDollars(Cash) + " | StockPrice: " + AsDollars(StockPrice), Color.WHITE);

#### om4

##### New member
below is a position script calculator that measures risk unit (ex. R1) based on the difference of previous bar high and low price. I want it to be change to the current bar - can someone please convert it?

Code:
``````#
# PositionSizingCalculator
#
# Author: Kory Gill, [USER=212]@korygill[/USER]
#
# Modified by Jedo64

input RiskUnit = 100;
input AggregationPeriod = AggregationPeriod.FIFTEEN_MIN;

def highVal = high(period = AggregationPeriod);
def lowVal = low(period = AggregationPeriod);
def lo = low;
def hi = high;

no,
AsDollars(RiskUnit) + "=1R",
Color.CYAN
);

# Instead of current bar i changed it to previous bar
def diff = hi[1] - lo[1];
def diff0 = hi - lo;

plot SharesSize = Floor(Round(RiskUnit / diff0));

yes,
"Risk: " + AsDollars(diff * 1.00) + " = " + Floor(Round(RiskUnit / (diff * 1.00))) + " Shares ",
Color.CYAN
);

#added a Green label to see Inside Bar Size for Long trigger

def diffl = (hi[1] - lo[1]) * 1.00;

yes,
" Long = .7R: " + (RoundDown(hi[1] + diffl*.7)) + " 1R: " + (RoundDown(hi[1] + diffl)) + " 2R: " + (RoundDown(hi[1] + diffl*2)) + " 3R: " + (RoundDown(hi[1] + diffl*3))+ " 4R: " + (RoundDown(hi[1] + diffl*4))+ " 5R: " + (RoundDown(hi[1] + diffl*5)),
Color.GREEN
);

# Added red label to see Inside Bar Size for short trigger

def diffs = (hi[1] - lo[1]) * 1.00;

yes,
"SHORT = .7R: " + (rounddown(lo[1] - diffs*.7)) + " 1R: " + (rounddown(lo[1] - diffs)) + " 2R: " + (rounddown(lo[1] - diffs*2)) + " 3R: " + (rounddown(lo[1] - diffs*3)) + " 4R: " + (rounddown(lo[1] - diffs*4)) + " 5R: " + (rounddown(lo[1] - diffs*5)) ,
Color.rED
);``````

Last edited by a moderator:

#### QUIKTDR1

##### Active member
VIP
below is a position script calculator that measures risk unit (ex. R1) based on the difference of previous bar high and low price. I want it to be change to the current bar - can someone please convert it?

Code:
``````#
# PositionSizingCalculator
#
# Author: Kory Gill, [USER=212]@korygill[/USER]
#
# Modified by Jedo64

input RiskUnit = 100;
input AggregationPeriod = AggregationPeriod.FIFTEEN_MIN;

def highVal = high(period = AggregationPeriod);
def lowVal = low(period = AggregationPeriod);
def lo = low;
def hi = high;

no,
AsDollars(RiskUnit) + "=1R",
Color.CYAN
);

# Instead of current bar i changed it to previous bar
def diff = hi[1] - lo[1];
def diff0 = hi - lo;

plot SharesSize = Floor(Round(RiskUnit / diff0));

yes,
"Risk: " + AsDollars(diff * 1.00) + " = " + Floor(Round(RiskUnit / (diff * 1.00))) + " Shares ",
Color.CYAN
);

#added a Green label to see Inside Bar Size for Long trigger

def diffl = (hi[1] - lo[1]) * 1.00;

yes,
" Long = .7R: " + (RoundDown(hi[1] + diffl*.7)) + " 1R: " + (RoundDown(hi[1] + diffl)) + " 2R: " + (RoundDown(hi[1] + diffl*2)) + " 3R: " + (RoundDown(hi[1] + diffl*3))+ " 4R: " + (RoundDown(hi[1] + diffl*4))+ " 5R: " + (RoundDown(hi[1] + diffl*5)),
Color.GREEN
);

# Added red label to see Inside Bar Size for short trigger

def diffs = (hi[1] - lo[1]) * 1.00;

yes,
"SHORT = .7R: " + (rounddown(lo[1] - diffs*.7)) + " 1R: " + (rounddown(lo[1] - diffs)) + " 2R: " + (rounddown(lo[1] - diffs*2)) + " 3R: " + (rounddown(lo[1] - diffs*3)) + " 4R: " + (rounddown(lo[1] - diffs*4)) + " 5R: " + (rounddown(lo[1] - diffs*5)) ,
Color.rED
);``````
How to code a stop segregation?

Px range stop %
1-15 2%
16-30 1.75
31-60 1
61-100 .75

UPDATE:
I believe this may answer my question:

Last edited by a moderator:

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