Profit Taking at 10% Gain from the Market Open

May you please help me and tell me if the following script will allow me to create a study that generates a sell order after the stock price has increased 10% from the market open at 9:30:00 a.m.?

Is "aggregationperiod.day" the best way to specify the market open at 9:30:00 a.m.? Thank you very much for your time and your help.

Code:
input offsetType = {default percent, value, tick};
input target = 0.1;
input aggregationperiod = aggregationperiod.day;

def entryprice = open(period = aggregationperiod);
def mult;
switch (offsetType) {
case percent:
    mult = entryPrice / 100;
case value:
    mult = 1;
case tick:
    mult = tickSize();
}
def targetPrice = entryPrice + target * mult;

plot sell = high >= targetPrice;

AL7y34s.png
 
Solution
May you please help me and tell me if the following script will allow me to create a study that generates a sell order after the stock price has increased 10% from the market open at 9:30:00 a.m.?

Is "aggregationperiod.day" the best way to specify the market open at 9:30:00 a.m.? Thank you very much for your time and your help.

Code:
input offsetType = {default percent, value, tick};
input target = 0.1;
input aggregationperiod = aggregationperiod.day;

def entryprice = open(period = aggregationperiod);
def mult;
switch (offsetType) {
case percent:
    mult = entryPrice / 100;
case value:
    mult = 1;
case tick:
    mult = tickSize();
}
def targetPrice = entryPrice + target * mult;

plot sell = high >= targetPrice;

AL7y34s.png
...
May you please help me and tell me if the following script will allow me to create a study that generates a sell order after the stock price has increased 10% from the market open at 9:30:00 a.m.?

Is "aggregationperiod.day" the best way to specify the market open at 9:30:00 a.m.? Thank you very much for your time and your help.

Code:
input offsetType = {default percent, value, tick};
input target = 0.1;
input aggregationperiod = aggregationperiod.day;

def entryprice = open(period = aggregationperiod);
def mult;
switch (offsetType) {
case percent:
    mult = entryPrice / 100;
case value:
    mult = 1;
case tick:
    mult = tickSize();
}
def targetPrice = entryPrice + target * mult;

plot sell = high >= targetPrice;

AL7y34s.png

hello and welcome

determine the day open,
when formulas use data from 2nd aggregation data, , it can result in unexpected results ( repeating values over many bars)

another way is to use if secondstillTime() to check if time is at some specific time

this reads the open at 9:30 and keeps that value during the rest of the day.
def dayopen = if secondstillTime(start) == 0 then open else dayopen[1];

this draws a red down arrow when price crosses above some % of the open price
( set the percent to a small number , like 1, for testing)

Code:
input sell_percent_gain = 10.0;
input start = 0930;
def dayopen = if secondstillTime(start) == 0 then open else dayopen[1];
def daydiff = round(close - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);

def sell1 = if pergain crosses above sell_percent_gain then 1 else 0;

plot z1 = sell1;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z1.SetDefaultColor(Color.red);
z1.setlineweight(3);
z1.hidebubble();

#------------------------------------
input test1 = no;
addchartbubble(test1,low*0.996,
dayopen + " open\n" +
close + " cls\n" +
daydiff + " diff\n" +
pergain + "  %\n" 
, color.yellow, no);
#
 
Solution

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

hello and welcome

determine the day open,
when formulas use data from 2nd aggregation data, , it can result in unexpected results ( repeating values over many bars)

another way is to use if secondstillTime() to check if time is at some specific time

this reads the open at 9:30 and keeps that value during the rest of the day.
def dayopen = if secondstillTime(start) == 0 then open else dayopen[1];

this draws a red down arrow when price crosses above some % of the open price
( set the percent to a small number , like 1, for testing)

Code:
input sell_percent_gain = 10.0;
input start = 0930;
def dayopen = if secondstillTime(start) == 0 then open else dayopen[1];
def daydiff = round(close - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);

def sell1 = if pergain crosses above sell_percent_gain then 1 else 0;

plot z1 = sell1;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z1.SetDefaultColor(Color.red);
z1.setlineweight(3);
z1.hidebubble();

#------------------------------------
input test1 = no;
addchartbubble(test1,low*0.996,
dayopen + " open\n" +
close + " cls\n" +
daydiff + " diff\n" +
pergain + "  %\n"
, color.yellow, no);
#
Hi @halcyonguy,

Thank you very much for this information, this is very helpful and educational. I am new to this so I hope you don’t mind that I ask some questions to learn more.

a) May you please tell me why the “def dayopen” needs to be an if statement? Why can’t we just say: def dayopen = secondstillTime(start) == 0;

b) Also, I tested this on a stock where there was a 10% gain in the first candle of the day, however a red arrow did not appear on top of that candle. Is there anyway to adjust this so that it is able to sell during the first candle of the market open?

c) Also, may you please tell me why it is important to round in “def daydiff” and “def pergain”?

d) In addition, may you please tell me, is everything below "#------------------------------------" also included in the study?

Thank you very much for your time and your help @halcyonguy, I really appreciate it.
 
Last edited:
Hi @halcyonguy,

Thank you very much for this information, this is very helpful and educational. I am new to this so I hope you don’t mind that I ask some questions to learn more.

a) May you please tell me why the “def dayopen” needs to be an if statement? Why can’t we just say: def dayopen = secondstillTime(start) == 0;

b) Also, I tested this on a stock where there was a 10% gain in the first candle of the day, however a red arrow did not appear on top of that candle. Is there anyway to adjust this so that it is able to sell during the first candle of the market open?

c) Also, may you please tell me why it is important to round in “def daydiff” and “def pergain”?

d) In addition, may you please tell me, is everything below "#------------------------------------" also included in the study?

Thank you very much for your time and your help @halcyonguy, I really appreciate it.


i (and others) don't mind at all, that's why this site exists, for people to come and learn.

a) May you please tell me why the “def dayopen” needs to be an if statement?
Why can’t we just say: def dayopen = secondstillTime(start) == 0;
it doesn't need to be. either version can be used. both create values of 0 or 1.
i often use the short method, but sometimes write out formulas with if then, as it might be easier for new people to understand.


b) Also, I tested this on a stock where there was a 10% gain in the first candle of the day, however a red arrow did not appear on top of that candle. Is there anyway to adjust this so that it is able to sell during the first candle of the market open?
yes it is possible. i used the word 'crosses', to determine when price went above a level. this needs 2 bars of data to calculate, so it won't work on the first bar. i don't look at stocks that move 10% in 1 bar, so i didn't think to check for that. (i watch SP100 stocks)
just need to add a variable and another condition to the sell1 formula,

def sell1 = if daystart and pergain > sell_percent_gain then 1
else if pergain crosses above sell_percent_gain then 1
else 0;


c) Also, may you please tell me why it is important to round in “def daydiff” and “def pergain”?
you are asking the question wrong.
..instead of 'why it is important'
..just ask 'why'

it isn't important. when creating studies, i often use bubbles to display some variables, to verify the values. i don't like looking at numbers with different decimal places, 2,3,4... so i used round() to limit all the numbers to 2 decimal places, in my test bubbles. round isn't needed for the formulas to work.


d) In addition, may you please tell me, is everything below "#------------------------------------" also included in the study?
you are asking the wrong question.
..should ask, 'why did you include that code after the ---------- line?'

yes, what i posted is all in 1 study.

no, that bubble code is not needed for the study to run. i left it in as an example of how to use a bubble to display values, for debugging new code.


updated version, to draw an arrow on first bar , if the gain is high enough.

Code:
# ten_per_after_open_00b
input sell_percent_gain = 10.0;
input start = 0930;
def daystart = secondstillTime(start) == 0;
def dayopen = if daystart then open else dayopen[1];
def daydiff = round(close - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);

def sell1 = if daystart and pergain > sell_percent_gain then 1
 else if pergain crosses above sell_percent_gain then 1
 else 0;

plot z1 = sell1;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z1.SetDefaultColor(Color.red);
z1.setlineweight(3);
z1.hidebubble();


#------------------------------------
# test code, 
# example of how to use a bubble to display values, for debugging new code.
input test1 = no;
addchartbubble(test1,low*0.996,
dayopen + " open\n" +
close + " cls\n" +
daydiff + " diff\n" +
pergain + "  %\n" + 
sell_percent_gain
, color.yellow, no);
#
 
i (and others) don't mind at all, that's why this site exists, for people to come and learn.


it doesn't need to be. either version can be used. both create values of 0 or 1.
i often use the short method, but sometimes write out formulas with if then, as it might be easier for new people to understand.



yes it is possible. i used the word 'crosses', to determine when price went above a level. this needs 2 bars of data to calculate, so it won't work on the first bar. i don't look at stocks that move 10% in 1 bar, so i didn't think to check for that. (i watch SP100 stocks)
just need to add a variable and another condition to the sell1 formula,

def sell1 = if daystart and pergain > sell_percent_gain then 1
else if pergain crosses above sell_percent_gain then 1
else 0;



you are asking the question wrong.
..instead of 'why it is important'
..just ask 'why'

it isn't important. when creating studies, i often use bubbles to display some variables, to verify the values. i don't like looking at numbers with different decimal places, 2,3,4... so i used round() to limit all the numbers to 2 decimal places, in my test bubbles. round isn't needed for the formulas to work.



you are asking the wrong question.
..should ask, 'why did you include that code after the ---------- line?'

yes, what i posted is all in 1 study.

no, that bubble code is not needed for the study to run. i left it in as an example of how to use a bubble to display values, for debugging new code.


updated version, to draw an arrow on first bar , if the gain is high enough.

Code:
# ten_per_after_open_00b
input sell_percent_gain = 10.0;
input start = 0930;
def daystart = secondstillTime(start) == 0;
def dayopen = if daystart then open else dayopen[1];
def daydiff = round(close - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);

def sell1 = if daystart and pergain > sell_percent_gain then 1
 else if pergain crosses above sell_percent_gain then 1
 else 0;

plot z1 = sell1;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z1.SetDefaultColor(Color.red);
z1.setlineweight(3);
z1.hidebubble();


#------------------------------------
# test code,
# example of how to use a bubble to display values, for debugging new code.
input test1 = no;
addchartbubble(test1,low*0.996,
dayopen + " open\n" +
close + " cls\n" +
daydiff + " diff\n" +
pergain + "  %\n" +
sell_percent_gain
, color.yellow, no);
#
Hi @halcyonguy,

Thank you very much for your responses, this is extremely helpful. I have a few more questions please:

a) If I wanted the 10% price gain to trigger at the high of the candle instead of the close, I believe I would change, “def daydiff = round(close - dayopen,2);” to “def daydiff = round(high - dayopen,2);” Is this correct?

Also, may you please tell me how I would need to change: close + " cls\n" + if I wanted to use the high of the candle instead of the close?

b) When I try and use this study in my paper trading account, should I set the condition to execute when the condition is “true” or is there another option I should select? I have attached a screenshot of the area I am questioning.

KVYVar0.png


c) Or is the following a better way to insert the study as a condition:

e1CxrS3.png


d) In addition, when I am applying the study in the paper account, is it important to select the same timeframe as the timeframe of the chart I initially had the buy order execute on? For example, if I had the buy order execute on a 5-minute chart, do I need to select a 5-minute timeframe for everything? I have attached a screenshot below of the section I am questioning.

0EGLuGw.png


Thank you very much for your time and your help @halcyonguy, I really appreciate it.
 
Last edited:
Hi @halcyonguy,

Thank you very much for your responses, this is extremely helpful. I have a few more questions please:

a) If I wanted the 10% price gain to trigger at the high of the candle instead of the close, I believe I would change, “def daydiff = round(close - dayopen,2);” to “def daydiff = round(high - dayopen,2);” Is this correct?

Also, may you please tell me how I would need to change: close + " cls\n" + if I wanted to use the high of the candle instead of the close?

b) When I try and use this study in my paper trading account, should I set the condition to execute when the condition is “true” or is there another option I should select? I have attached a screenshot of the area I am questioning.

c) Or is the following a better way to insert the study as a condition:


d) In addition, when I am applying the study in the paper account, is it important to select the same timeframe as the timeframe of the chart I initially had the buy order execute on? For example, if I had the buy order execute on a 5-minute chart, do I need to select a 5-minute timeframe for everything? I have attached a screenshot below of the section I am questioning.

Thank you very much for your time and your help @halcyonguy, I really appreciate it.


a) If I wanted the 10% price gain to trigger at the high of the candle instead of the close, I believe I would change, “def daydiff = round(close - dayopen,2);” to “def daydiff = round(high - dayopen,2);” Is this correct?
yes.
if you want to change the price constant, i would use an input to set a variable,
input price1 = high;
then use price1 in the formulas.
def daydiff = round(price1 - dayopen,2);


Also, may you please tell me how I would need to change: close + " cls\n" + if I wanted to use the high of the candle instead of the close?
this is just what is displayed in a bubble, and doesn't affect the calculations.
high + " hi\n" +
i don't use concat() to join text. it only has 2 parameters. just use + in between data.
if wanting to display the value of a formula, wrap it in ( )
ex.
((high+low)/2) + " hl2"


b) When I try and use this study in my paper trading account, should I set the condition to execute when the condition is “true” or is there another option I should select? I have attached a screenshot of the area I am questioning.
no
?? i have never used that screen. (edit condition)
just load the study onto the chart,

edit studies
default is studies,
...at the top. if working on a strategy, click strategies. strategies are studies that have addorder() functions, to simulate buying and selling.
create button (at bottom)
make sure thinkscript editor button is selected
delete default code - plot Data = close;
paste in study code
give it a unique name. can use underlines. has to start with a letter
click ok


d) is it important to select the same timeframe as the timeframe of the chart I initially had the buy order execute on?
no. look at whatever you want to look at.
i tend to look at 15 min chart when searching for a play. then look at charts of smaller times when watching for an exit.
 
yes.
if you want to change the price constant, i would use an input to set a variable,
input price1 = high;
then use price1 in the formulas.
def daydiff = round(price1 - dayopen,2);



this is just what is displayed in a bubble, and doesn't affect the calculations.
high + " hi\n" +
i don't use concat() to join text. it only has 2 parameters. just use + in between data.
if wanting to display the value of a formula, wrap it in ( )
ex.
((high+low)/2) + " hl2"



no
?? i have never used that screen. (edit condition)
just load the study onto the chart,

edit studies
default is studies,
...at the top. if working on a strategy, click strategies. strategies are studies that have addorder() functions, to simulate buying and selling.
create button (at bottom)
make sure thinkscript editor button is selected
delete default code - plot Data = close;
paste in study code
give it a unique name. can use underlines. has to start with a letter
click ok



no. look at whatever you want to look at.
i tend to look at 15 min chart when searching for a play. then look at charts of smaller times when watching for an exit.
Hi @halcyonguy,

Thank you very much for your responses, this is very helpful information, I really appreciate it. I have a couple of more questions please, thank you again for your help.

a) When I apply the thinkScript to the chart, an arrow appears on the first candle from the open where a 10% gain occurred, which is awesome, thank you very much for your help with this. I have also noticed that sometimes arrows appear after the first occurrence of a 10% gain from the open. For example, in the screenshot below, there is an arrow at the first candle of the day because a 10% gain occurred from the open price. Then there are more red arrows at approximately 12:00 p.m. and 12:15 p.m. Did these arrows appear because this is where the stock price crossed from less than a 10% gain to greater than a 10% gain from the market open?

sT4xBhj.png


b) Also, if I wanted to adjust the thinkScript you provided to sell when the stock price decreases 10% from the market open, would the following thinkScript be appropriate:

Code:
input sell_percent_gain = -10.0;
input start = 0930;
def daystart = secondstillTime(start) == 0;
def dayopen = if daystart then open else dayopen[1];
def daydiff = round(close - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);

def sell1 = if daystart and pergain < sell_percent_gain then 1
 else if pergain crosses below sell_percent_gain then 1
 else 0;

plot z1 = sell1;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z1.SetDefaultColor(Color.red);
z1.setlineweight(3);
z1.hidebubble();


Thank you very much for all of your time and your help @halcyonguy, I really appreciate it.
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
467 Online
Create Post

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