Strategy Help?

bri8htf1y

New member
Hi all, just need a little help with an intraday strategy I'm developing. I've run into a wall with a few issues:

Market hours only:

1. Only one trade per day: a buy_to_open only if there has not been a trade yet that day, then a sell_to_close, with no more trades afterwards that day.

2. No trades that day if / after low falls to a given price.

3. A trailing stop loss.


#2 also leads to a question - what if invalidating low occurs in the same bar as my entry buy signal? If I use a lower time frame to see which occurs first, I get less historical bars. Which leads to a bigger question. Should I be using OnDemand instead? Or market alerts based on the actual conditional orders I will be using? I've been using strategy scripts with floating PL underneath / exporting the native "report" to a spreadsheet for metrics as a filter of sorts to quickly test ideas before backtesting. Is this the best approach using TOS?

Thanks in advance for any help on this!
 
Hi all, just need a little help with an intraday strategy I'm developing. I've run into a wall with a few issues:

Market hours only:

1. Only one trade per day: a buy_to_open only if there has not been a trade yet that day, then a sell_to_close, with no more trades afterwards that day.

2. No trades that day if / after low falls to a given price.

3. A trailing stop loss.


#2 also leads to a question - what if invalidating low occurs in the same bar as my entry buy signal? If I use a lower time frame to see which occurs first, I get less historical bars. Which leads to a bigger question. Should I be using OnDemand instead? Or market alerts based on the actual conditional orders I will be using? I've been using strategy scripts with floating PL underneath / exporting the native "report" to a spreadsheet for metrics as a filter of sorts to quickly test ideas before backtesting. Is this the best approach using TOS?

Thanks in advance for any help on this!


here is something to start with. it does some of what you asked for.
you didn't provide any code or buy rules or sell rules, so i had to create everything from scratch.

1. limits signals to just the first buy signal

2. i made up a stop rule.
if low goes below yesterdays low, then disable variable goes true, and disables buy signal, and stop line turns red.

3. trail - didn't do this.


create test signals from 2 averages crossing

draws horizontal lines for low of day and prev day low.
draws vertical line on buy signals

i made this without using 2nd aggregation.


Code:
# buysell_once_day_00c

#https://usethinkscript.com/threads/strategy-help.15187/
#Strategy Help?


#Market hours only:

#1. Only one trade per day: a buy_to_open only if there has not been a trade yet that day, then a sell_to_close, with no more trades afterwards that day.

#2. No trades that day if / after low falls to a given price.

#3. A trailing stop loss.


#2 also leads to a question - what if invalidating low occurs in the same bar as my entry buy signal? If I use a lower time frame to see which occurs first, I get less historical bars. Which leads to a bigger question. Should I be using OnDemand instead? Or market alerts based on the actual conditional orders I will be using? I've been using strategy scripts with floating PL underneath / exporting the native "report" to a spreadsheet for metrics as a filter of sorts to quickly test ideas before backtesting. Is this the best approach using TOS?


def bn = BarNumber();
def na = Double.NaN;
def big = 99999;

#-------------------------------------

input reset_type = { default rth , daily };

def daytime = (gettime() >= RegularTradingStart(GetYYYYMMDD()) and gettime() < RegularTradingend(GetYYYYMMDD()));
def difftime = daytime != daytime[1];
def timefirst = daytime and !daytime[1];
def timelast = daytime and !daytime[-1];


def diffday = GetDay() != GetDay()[1];

# true on 1st bar of a time period
#def reset = if reset_type == reset_type.rth and difftime then 1
def reset = if reset_type == reset_type.rth and timefirst then 1
 else if reset_type == reset_type.daily and diffday then 1
 else 0;

# define a range of bars
def period = if  reset_type == reset_type.daily then 1
 else if reset_type == reset_type.rth then daytime
 else period[1];

#addverticalline(reset, "-");


#-----------------------

# stop level
# use prev day low

def data2 = low;

def daylow = if bn == 1 then 0
 else if reset then data2
 else if period and data2 < daylow[1] then data2
 else daylow[1];

def prevdaylow = if reset then daylow[1]
else prevdaylow[1];


def disable = if bn == 1 then 0
 else if reset and data2 >= prevdaylow then 0
 else if reset and data2 < prevdaylow then 1
 else if disable[1] == 1 then 1
 else if period and data2 < prevdaylow then 1
 else disable[1];


# -----------------------
# -----------------------
# test data
def emaprice = close;
input avgshortlen = 8;
input avgshorttype =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(avgshorttype, emaprice, avgshortlen);

input avglonglen = 21;
input avglongtype =  AverageType.EXPONENTIAL;
def ma2 = MovingAverage(avglongtype, emaprice, avglonglen);

def buy1 = ma1 crosses above ma2;
def sell1 = ma1 crosses below ma2;

#  plot ema lines
input show_test_data_lines = yes;
plot ma1x = ma1;
ma1x.SetDefaultColor(Color.light_gray);
ma1x.HideBubble();
ma1x.SetHiding(!show_test_data_lines);

plot ma2x = ma2;
ma2x.SetDefaultColor(Color.light_gray);
ma2x.HideBubble();
ma2x.SetHiding(!show_test_data_lines);

# -----------------------
# -----------------------


def trigger = buy1;


#def data = ma1;
def data = close;

# hold a price from the first time a signal happens during a time period
def hold = if BarNumber() == 1 or !period then 0
  else if reset then 0
  else if hold[1] != 0 then hold[1]
 else if low < prevdaylow then -1
  else if trigger then data
  else hold[1];

# create a buy signal
def trigger_first = if hold > 0 and hold[1] == 0 then 1 else 0;

def buy = if disable then 0 else trigger_first;

#-----------------------

#------------------------
# test stuff


input test_day_data2_lines = no;
plot zdl = if test_day_data2_lines and daylow > 0 then daylow else na;
zdl.SetDefaultColor(Color.white);

input test_prev_day_data2_lines = yes;
plot zpdl = if test_prev_day_data2_lines and prevdaylow > 0 then prevdaylow else na;
#zpdl.SetDefaultColor(Color.magenta);
zpdl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zpdl.AssignValueColor(if disable then color.red else color.orange);



input test_vert_lines = yes;
AddVerticalLine(test_vert_lines and period and buy, "B U Y", (if buy then Color.green else Color.magenta));


input test1 = no;
AddChartBubble(test1 and period, low,
period + "\n" +
hold
, (if hold > 0 then Color.YELLOW else Color.GRAY), no);


input test1_daytime = no;
plot zd = if test1_daytime and daytime then high*1.01 else double.nan;
zd.SetPaintingStrategy(PaintingStrategy.POINTS);
zd.SetDefaultColor(Color.white);
zd.setlineweight(3);
zd.hidebubble();


input test2_hold_bubble = no;
addchartbubble(test2_hold_bubble and period, low,
hold
, color.yellow, no);


input test3_hold_line = yes;
plot zhold = if hold > 0 then hold else na;
zhold.SetDefaultColor(Color.cyan);
zhold.hidebubble();
#

9KsES0z.jpg
 

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