Making a variable hold a value

PBtrading111

New member
Hello all, im new to thinkscript. My request is pretty straight forward.

I am working on coding out my own strategy, and im now working on the exit signal, now that the entry signal is working pretty well.

What I need thinkscript to do is to hold the entry price in a variable and remain constant.

Right now I am using

def entrypoint = if trigger then GetValue(data = open, 0) else Double.NaN;

and this is pulling the open price when my trigger conditions are a go, but this also changes on the very next bar where those conditions are true.

I need this because in other parts of the code im calculating my risk by basically doing this logic
Maximum dollars at risk / Entry point - Stop loss point = Max shares

So what I want this variable to be held constantly for is so I can do something like :

Goalpoint = Entrypoint + say 2 ATRS or something ;
Then finally something like
AddOrder(OrderType.SELL_TO_CLOSE, close > goalpoint);


appreciate any light that can be shed on this. Thanks
 
Hello all, im new to thinkscript. My request is pretty straight forward.

I am working on coding out my own strategy, and im now working on the exit signal, now that the entry signal is working pretty well.

What I need thinkscript to do is to hold the entry price in a variable and remain constant.

Right now I am using

def entrypoint = if trigger then GetValue(data = open, 0) else Double.NaN;

and this is pulling the open price when my trigger conditions are a go, but this also changes on the very next bar where those conditions are true.

I need this because in other parts of the code im calculating my risk by basically doing this logic
Maximum dollars at risk / Entry point - Stop loss point = Max shares

So what I want this variable to be held constantly for is so I can do something like :

Goalpoint = Entrypoint + say 2 ATRS or something ;
Then finally something like
AddOrder(OrderType.SELL_TO_CLOSE, close > goalpoint);


appreciate any light that can be shed on this. Thanks
There are a few options that should help..

1. Recursive - This retains the value for future use. It uses a 'def' statement that references itself

Change
def entrypoint = if trigger then GetValue(data = open, 0) else Double.NaN;
To Recursive
def entrypoint = if trigger then GetValue(data = open, 0) else entrypoint[1];

2. See the following explanation of the TOS built-in entryprice function. In a strategy study, it will retain the last price that TOS uses in addorder. This is usually open[-1], which is the next bar's open after the trigger bar.

EntryPrice​

EntryPrice ();

Description​

Returns the price of the entry order. For several entry orders in the same direction as the currently held position the function returns the average price for all of them.

Example​

AddOrder(OrderType.SELL_TO_CLOSE, close > EntryPrice() + 3 or close < EntryPrice() - 9);
Adds a Sell order for closing a long position when the Close price is either greater than the entry price by 3 (for taking profits) or less by 9 (for safety).
 

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