Position Sizing Calculator for ThinkorSwim

korygill

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);

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

def diff = highVal-LowVal;

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

Link to the study

https://tos.mx/HwObLl

Thanks,
Kory Gill, @korygill
 
Last edited by a moderator:
G

GetRichOrDieTrying

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

GetRichOrDieTrying

Member
That's what the original thread was about. A position sizing tool.
 
G

GetRichOrDieTrying

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

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.
 
R

rudy seena

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

jdstrader

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

Pelonsax

Member
VIP
I've got a better one. There's three scenarios:
Scenario 1: You can choose your size and your risk, but not your stop
Scenario 2: You can choose your size and your stop, but not your risk
Scenario 3: You can choose your risk and your stop, but not your size

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);
Here's the link:

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;
def units_to_buy_bull1 = RoundDown(units_to_buy_bull,0);
def total_cost_bull = units_to_buy_bull1 * risk_price;
def units_to_buy_bear = (risk_percent_calc * capital_size) / (entry_stop_bear) / risk_price;
def units_to_buy_bear1 = RoundDown(units_to_buy_bear,0);
########################################
 
Last edited:

Similar threads

Top