Thinkscript code for incrementing or decrementing a calculation

Iguana

New member
Sorry in advance for lenght of my question. I use the following fixed variables:
MaxRisk: $40
Funds: $2000
Variables: Riskcents = ATR * X
Shares = MaxRisk / Riskcents
MaxInvest= candle "High" * Shares

I'm trying to generate a calculation of variable "X" such that the "Shares" * candle "High" will a MaxInvest that equals Funds. Obviously I can have a fixed user input for X to start the evaluation; but, then need the code that will evaluate and increment/decrement "X" so that Maxinvest = Funds. Any assistance is greatly appreciated.

I want to add that when intraday trading, and using a fixed "X" of 2, the ATR can become so small that the number of "Shares" results in a MaxInvest that exceed Funds. That where the "rub" is...I'd like the code to calculate the adjustment needed.
 
Last edited:
Solution
@Iguana give this a shot.
Ruby:
input X = 2;
def ATR = reference ATR.ATR;
def MaxRisk = 40;
def Funds = 2000;
def Riskcents = ATR * X;
def Shares = MaxRisk / Riskcents;
def MaxInvest = High * Shares;

def AutoX = fold i = 0 to 100 with internalX = X while (high*(MaxRisk/(ATR * internalX))) > Funds do internalX + 1;
def AdjustedMaxInvest = if MaxInvest > Funds then (high*(MaxRisk/(ATR * AutoX))) else MaxInvest;

addchartbubble(yes,high,"Auto X: "+AutoX,if MaxInvest > Funds then color.white else color.green,yes);
addchartbubble(yes,high,"Adjusted MaxInvest: "+AdjustedMaxInvest,if MaxInvest > Funds then color.white else color.green,yes);
addchartbubble(yes,high,"MaxInvest: "+MaxInvest,if MaxInvest > Funds then color.white else...
@Iguana give this a shot.
Ruby:
input X = 2;
def ATR = reference ATR.ATR;
def MaxRisk = 40;
def Funds = 2000;
def Riskcents = ATR * X;
def Shares = MaxRisk / Riskcents;
def MaxInvest = High * Shares;

def AutoX = fold i = 0 to 100 with internalX = X while (high*(MaxRisk/(ATR * internalX))) > Funds do internalX + 1;
def AdjustedMaxInvest = if MaxInvest > Funds then (high*(MaxRisk/(ATR * AutoX))) else MaxInvest;

addchartbubble(yes,high,"Auto X: "+AutoX,if MaxInvest > Funds then color.white else color.green,yes);
addchartbubble(yes,high,"Adjusted MaxInvest: "+AdjustedMaxInvest,if MaxInvest > Funds then color.white else color.green,yes);
addchartbubble(yes,high,"MaxInvest: "+MaxInvest,if MaxInvest > Funds then color.white else color.green,yes);
 
Solution
Svanoy,
I was thinking that I should post my complete code so that you could see my changes as well as possibly benefit any other that may be interested. Thanks again for your assistance. So, here it is:

input X = .5;
input Funds = 2000;
input MaxRisk = 40;

def Entry = high +.02;
def ATR = reference ATR.ATR;
def Riskcents = ATR * X;
def Shares = MaxRisk / Riskcents;
def MaxInvest = Entry * Shares;

# Code below contributed by Svanoy
def AutoX = fold i = 0 to 100 with internalX = X while (Entry * (MaxRisk / (ATR * internalX))) > Funds do internalX + 1;
def AdjustedMaxInvest = if MaxInvest > Funds then (Entry * (MaxRisk / (ATR * AutoX))) else MaxInvest;

AddLabel (yes, "ATR " + Round(ATR, 2), Color.LIGHT_GRAY);
AddLabel (yes, "Entry : $" + roundup(Entry), color.white);
AddLabel (yes, "RiskCents : $" + roundup((Maxrisk/(Maxrisk/(ATR*AutoX))),2), Color.orange);
AddLabel (yes, “MaxShares:" + rounddown((MaxRisk/(ATR * AutoX)),-1), Color.green);
AddLabel (yes, “MaxInvest:" + rounddown(AdjustedMaxInvest,-1), Color.green);
AddLabel (yes, “Risk @ MaxShares: $" + ((Maxrisk/(Maxrisk/(ATR*AutoX)))* ((MaxRisk/(ATR * AutoX)))) , Color.green);
AddLabel (yes, “TgT 1: $" + roundup( Entry +((Maxrisk/(Maxrisk/(ATR*AutoX)))*2),2) , Color.green);
AddLabel (yes, “TgT 1 Profit: $" + rounddown(((MaxRisk/(ATR * AutoX)) *((Maxrisk/(Maxrisk/(ATR*AutoX)))*2)),-1),Color.green);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
553 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top