Risk a fixed dollar amount instead of a fixed number of shares

Cliff

New member
VIP
I would like to risk a fixed dollar amount instead of a fixed number of shares. The first definition of Quantity works fine, but not the second or third. The buy and sell arrows do appear correctly on the chart, but nothing appears in the FPL window. Does anyone know what I'm doing wrong?
Thanks,
Cliff

Code:
input Dollars = 5000;
def EntryPrice = EntryPrice();
#def Quantity = 100;  # This works
#def Quantity = Dollars / EntryPrice;  # This does not give any info in the FPL window
def Quantity = Round(Dollars / EntryPrice, 0); # This does not give any info in the FPL window
 
Last edited:
Solution
I would like to risk a fixed dollar amount instead of a fixed number of shares. The first definition of Quantity works fine, but not the second or third. The buy and sell arrows do appear correctly on the chart, but nothing appears in the FPL window. Does anyone know what I'm doing wrong?
Thanks,
Cliff

Code:
input Dollars = 5000;
def EntryPrice = EntryPrice();
#def Quantity = 100;  # This works
#def Quantity = Dollars / EntryPrice;  # This does not give any info in the FPL window
def Quantity = Round(Dollars / EntryPrice, 0); # This does not give any info in the FPL window
@Cliff I believe that EntryPrice() is created AFTER an order is opened, and it appears (from the limited code you've posted) that there is no defined...
I would like to risk a fixed dollar amount instead of a fixed number of shares. The first definition of Quantity works fine, but not the second or third. The buy and sell arrows do appear correctly on the chart, but nothing appears in the FPL window. Does anyone know what I'm doing wrong?
Thanks,
Cliff

Code:
input Dollars = 5000;
def EntryPrice = EntryPrice();
#def Quantity = 100;  # This works
#def Quantity = Dollars / EntryPrice;  # This does not give any info in the FPL window
def Quantity = Round(Dollars / EntryPrice, 0); # This does not give any info in the FPL window
@Cliff I believe that EntryPrice() is created AFTER an order is opened, and it appears (from the limited code you've posted) that there is no defined price at which you would like to Open an Order. Without the rest of your code, it's not possible to determine where the issue is.

See the example below which I use as a baseline script when creating backtest strategies. See the use of entryPrice to set a buy price and the use of EntryPrice() to determine when the Order will Close.

'entryPrice' is my variable that defines the specific amount to trigger an Order Open, which is this case is the 'close' price.

'EntryPrice()' is the TOS function that recalls the price at which my Order was opened.

PS: I do not advise using the strategy below to place live orders...it's just an example.

Code:
# Start Script
# Inputs
input amountToRisk = 5000;
input entryPrice = close;
input lengthMA = 21;
input avgTypeMA = AverageType.Exponential;

# Calculate Shares to Buy
def sharesToBuy = amountToRisk / entryPrice; 

# Create a Buy Trigger
def buyTrigger = If entryPrice crosses above MovingAverage(avgTypeMA, entryPrice, lengthMA) then 1 else 0;

#Create a Sell trigger
def sellTrigger = EntryPrice() * 1.5; 

# Open an Order
AddOrder(OrderType.BUY_TO_OPEN, buyTrigger, entryPrice, sharesToBuy, name = "Buy");

# Close an Order
AddOrder(OrderType.SELL_TO_CLOSE, sellTrigger, Open[-1], sharesToBuy, name = "Sell");

# End Script
 
Solution

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
241 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