Auto-calculate amount of Futures contracts I can buy with my balance?

kelso

New member
Hi All,

I'm trying to come up with an automatic way to tell me how much futures I can buy with my current balance/margin balance.
Futures act differently than regular stocks, so the market price isn't really the price it will cost to buy a margin, but rather the margin requirement needs to be calculated. any suggestions on which variables to use inside thinkscript? I can't find one that will give me what I need.

Thanks!
 
I just have $2000 in my account per MES contract. That way if the price goes in the opposite direction I can go negative for a bit without getting margin called.
 

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

The GetTotalCash() function will get your cash balance. I only trade stocks so I'm not familiar with whatever might be different for futures. I hacked up parts of one of my scripts to give you the pieces I think you need. You may need to adjust for your buying power.

Ruby:
input accountBalanceFallback = 0; # used in OnDemand or if premarket data not on
input accountBalanceOverride = 0; # if you want to force a lower value

# reminder = GetTotalCash only works with premarket data on
def cash = GetTotalCash();
# reminder = GetTotalCash only works with premarket data on
def accountBalance =
  if accountBalanceOverride > 0 then accountBalanceOverride
  else if !IsNaN(cash) then cash
  else accountBalanceFallback
;

def buyingPower = accountBalance * 4;
def buyPowerMaxShares = RoundDown(buyingPower / close, 0);

AddLabel(yes, "Contracts: " + buyPowerMaxShares);

If the cash balance isn't relevant GetNetLiq() will get the total value of your account which you can then multiply by your margin to get total buying power. For buying power already used you can get the sizes of your open positions using GetQuantity( Symbol symbol) and try to calculate how much margin is used by those and then subtract from your total buying power.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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