# 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();
#