NexusFi: Find Your Edge


Home Menu

 





ThinkScript - MM-PS Code/Logic Question...


Discussion in ThinkOrSwim

Updated
    1. trending_up 1,986 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 1 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
netarchitech
NY, NY
 
Posts: 68 since Dec 2011
Thanks Given: 27
Thanks Received: 19

Despite working with thinkorswim for over 10 years now, I am obviously still a novice thinkScripter when it comes to crafting custom solutions. With that said, I've been trying to write my own "study", really it's more of a "dashboard" of sorts, to address my need to keep Money Management and Position Sizing front and center when considering a trade...
 
Code
declare lower;

input priceLo = low;
input priceHi = high;
input price = close;
input length = 10;
input averageType = AverageType.WILDERS;
input multiplier = 2.0;
input accountSize = 100000;
input accountRiskTolerance = 0.015;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);
def ATRmultiple = multiplier * ATR;
#def avgPercentMove = (ATR / price) * 100;
def longStopLoss = priceLo - ATRmultiple;
def shortBuyCover = priceHi + ATRmultiple;
def tradeRisk = accountSize * accountRiskTolerance;
def sharesBuyBorrow = tradeRisk / ATRmultiple;
def tradeSizer = (tradeRisk / ATRmultiple) * price; 

AddLabel(yes, "Current Volatility: " + Round(ATR, 2), CreateColor (255, 255, 255));
#AddLabel(yes, "Average Move: " + Round(avgPercentMove, 2) + "%", CreateColor (255, 255, 255));
AddLabel(yes, "Total Per Trade Risk Limit : " + "$ " + Round(tradeRisk, 2), CreateColor (255, 255, 255));
AddLabel(yes, "Shares to Buy or Borrow: " + Round(sharesBuyBorrow, 0), CreateColor (255, 255, 255));
AddLabel(yes, "Trade Size: " + "$ " + Round(tradeSizer, 2), CreateColor (255, 255, 255));
AddLabel(yes, "If Going LONG -> Place Stop at " + Round(longStopLoss, 2), CreateColor (255, 255, 255));
AddLabel(yes, "If Going SHORT -> Place Cover at " + Round(shortBuyCover, 2), CreateColor (255, 255, 255));


While the code basically does provide some helpful information, there are a few inconsistencies:
1. Admittedly, this code employs both the Fixed Fraction and ATR Volatility position sizing strategies...I would prefer to follow the ATR strategy only if possible...
2. Somehow the calculation for "tradeSizer" on occasion exceeds "accountSize"...

Any feedback, thoughts or suggestions to improve this code would be greatly appreciated...

Thanks in advance...


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
CFTC Requests Tag 50 Trader Identity Data From CME and I …
Traders Hideout
Iran Fired a Missile at Israel Last Night. The $8M June …
Prediction Markets & Event Contracts
Six Days to Kickoff: World Cup Prediction Markets Hit $1 …
Prediction Markets & Event Contracts
Saylors 41-Month HODL Breaks: Strategy Sells 32 BTC as $ …
Prediction Markets & Event Contracts
Trump Lands in Beijing on CPI Day: Iran Peace Expires To …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Big Mike in Ecuador
205 thanks
Sober Journey With S&P
21 thanks
30 Sessions
20 thanks
Volume Indicators
8 thanks
Thanks Mike. Godspeed.
7 thanks
  #2 (permalink)
netarchitech
NY, NY
 
Posts: 68 since Dec 2011
Thanks Given: 27
Thanks Received: 19

While I am still trying to sort out the code in terms of Fixed Fraction vs. ATR Volatility, I thought I would post the solution I came up with to resolve the "tradeSizer" exceeding "accountSize" issue in the hopes it might prove useful/helpful to someone else somewhere along the way...

 
Code
AddLabel(yes, if tradeSizer < accountSize then "Trade Size: " + "$ " + Round(tradeSizer, 2) else "Trade Size: Exceeds available funds", CreateColor (255, 255, 255));


Reply With Quote




Last Updated on October 29, 2015


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts