# Position Sizing Calculator for ThinkorSwim

#### korygill

##### Active member
VIP
Based on another thread, 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

Last edited by a moderator:

#### BenTen

Staff
VIP
Wow. Thank you for this. Amazing work!

H

#### hockeycoachdoug

##### Member
2019 Donor
VIP
That was great work filling a request. Thank you

#### Zachc

##### Member
2019 Donor
Very nice thanks @korygill for sharing!

G

#### GetRichOrDieTrying

##### Member
Is this a fully functioning position sizer? I'm not in front of my computer to test it.

#### korygill

##### Active member
VIP
Is this a fully functioning position sizer? I'm not in front of my computer to test it.
No clue. What does a position sizer do?

G

G

#### GetRichOrDieTrying

##### Member
It appears to be that. Your code says PositionSizingCalculator

#### korygill

##### Active member
VIP
I don't see anywhere in there what it calculates. High and low over last X minutes? How much the stock can move for +/- \$x.x.x in gain/loss? I just wrote code that seems to calculate what the first image showed. Define the rules, and we can write code to do that. Thanks.

G

#### GetRichOrDieTrying

##### Member
This is what I'm looking for. Something that does with this does

R

#### rudy seena

##### New member
Hi, I copied and pasted in TOS but nothing happens? Any suggestions?

J

##### New member
Hello,

I've been looking for a very simple indicator that can divide my buying power with the price of the stock I'm looking at so it can show me the shares I need to buy quickly. For example, If I have \$100k in buying power, I'd like to set up the indicator to divide \$50k (I scale into positions) with the current price of the stock let's say \$20 so it would show me I need to buy 2,500.
I've been doing this calculation manually every time but I make quick trades and sometimes these seconds I use for my calculations are very costly.

Does anyone know if an indicator like this exists? I haven't been able to find something on my own so far.

Thanks a lot!

S

#### soary

##### Member
@korygill Hi, I added this script to my 1 min chart and it shows the high and low as the same amount, 0.00 for the stop and the number of shares is incorrect. Other than the TF there are no other inputs. What am I missing? Also what is high and low? Is high the that limit target and low the stop loss?

P

#### Pensar

##### Active member
VIP
@jdstrader Here is something I coded up, might do what you want.

Code:
#Simple_ScaleIn_Label
#Pensar, 6/10/2020

input max_size = 100000.00;
input scale_in = yes;
input times_to_scale_in = 4;

def Size = if !scale_in then (max_size / close) else
(max_size / times_to_scale_in) / close;

AddLabel(1, (if scale_in then "Scale In: " else "") + "Buy " + RoundDown(Size, 0) + " shares" + (if scale_in then " " + times_to_scale_in + " times" else "") + "  ---  Total Trade Size: " + AsDollars(max_size), color.white);

#End code

R

#### rlohmeyer

##### New member
This is so elegant. I have used an Excel file I keep floating on my desktop for years. Since I trade 5 min, it worked OK when I didn't have access to NinjaTrader and the like. This is so much nicer.

#### Pelonsax

##### Member
VIP
I've got a better one. There's three scenarios:

Why not make it an indicator that lets you see the result of those decisions? I decided to make one. The only issue I'm uncertain of is whether I am making price what it actually is. I decided to go with Mark

Here's the code:

Code:
#
# PositionSizingCalculator
#
# Author: RamonDV. AKA Pelonsax
#
#

input Risk_Amount = 100;
input Shares = 1000;
input Stop_Price = 100.00;
input Choose_Size_and_Risk = yes; #can't choose stop
input Choose_Size_and_Stop = yes; #can't choose risk
input Choose_Risk_and_Stop = yes; #can't choose size

def Mark = close(PriceType = PriceType.MARK);
def Size  = Risk_Amount / (Mark - Stop_Price);
def Stop = ((Mark * Shares) - Risk_Amount) / Shares;
def Risk = (mark - Stop_Price) * Shares;

#Display current price
AddLabel(yes, "Current Price: " + AsDollars(Mark), color.gray);

#No defined stop
AddLabel(Choose_Size_and_Risk, "No defined stop: " + Shares + " Shares with " + AsDollars(Risk_Amount) + " risk with Stop Loss at " + AsDollars(stop) + "  .", color.gray);

#No defined risk
AddLabel(Choose_Size_and_Stop, "No Defined risk: " + Shares + " Shares with " + AsDollars(risk) + " risk with Stop Loss at " + AsDollars(Stop_Price) + "  .", color.gray);

#No defined size
AddLabel(Choose_Risk_and_Stop, "No defined size: " + Floor(Round(size)) + " Shares with " + AsDollars(Risk_Amount) + " risk with Stop Loss at " + AsDollars(Stop_Price) + "  .", color.gray);

https://tos.mx/l2H5IBg

B

#### betogmgf

##### New member
@jdstrader Good question, am looking for the same thing. No luck yet. Have you found something similar to it. THanks.

C

#### chihaij

##### New member
@korygill Hi guys. need this script but instead of high val price to be current price. can you help me? thanks

B

#### bathtub2007

##### New member
This one I wrote to auto calculate the number of shares to buy based on an input risk % and capital size. You will need to update the stop level based on whatever signal you are using.

Code:
########################################
input capital_size = 25000;
input risk_percent = 1;
def risk_price = close;

def risk_percent_calc = risk_percent / 100;
def entry_stop_bull = (risk_price - bullstop);
def entry_stop_bear = (bearstop - risk_price);
def units_to_buy_bull = (risk_percent_calc * capital_size) / (entry_stop_bull) / risk_price;