#order_2bb_rules_
#https://usethinkscript.com/threads/addorder-based-upon-a-previous-addorder.17600/
#AddOrder based upon a previous AddOrder
#Sergio123 1/1
#I would like to know if I can use If....Then..Else... setting up the condition argument of an AddOrder
#OrderType.SELL_AUTO based upon a previous OrderType.BUY_AUTO in a strategy script.
#For example - for a Bollinger Band Strategy if the previous OrderType.BUY_AUTO was placed based upon a crossing of price above the lower band then I would like to place OrderType.SELL_AUTO on the crossing ABOVE the upper band. However if the OrderType.BUY_AUTO was placed based upon a crossing of price ABOVE the middle line, then I would like to place the OrderType.SELL_AUTO on the price crossing BELOW the upper band.
#Can someone point me to an example of a script that uses conditional orders based upon the previous order?
#rewrite the rules
# trade1
# OrderType.BUY_AUTO , price crosses above the lower b band
# OrderType.SELL_AUTO , price crosses ABOVE the upper band.
# trade2
# OrderType.BUY_AUTO , price crosses ABOVE the middle bb line,
# OrderType.SELL_AUTO , price crosses BELOW the upper b band.
#addlabel(1, "-");
def na = double.nan;
def bn = barnumber();
# ref line
def opn = if bn == 1 then round(open,0) else opn[1];
#-------------------------
# BollingerBands
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
def sDev = stdev(data = price[-displace], length = length);
plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
#-------------------------
# trade1
# 1 BUY , price crosses above the lower b band
# 2 SELL , price crosses ABOVE the upper band.
# trade2
# 3 BUY , price crosses ABOVE the middle bb line,
# 4 SELL , price crosses BELOW the upper b band.
def trade = if bn == 1 then 0
else if close crosses below lowerband then 0
else if close crosses above LowerBand then 1
# else if trade[1] == 3 and close crosses above UpperBand then 2
else if trade[1] == 1 and close crosses above UpperBand then 2
else if trade[1] != 1 and close crosses above MidLine then 3
else if trade[1] == 3 and close crosses below UpperBand then 4
else if trade[1] == 2 or trade[1] == 4 then 0
else trade[1];
def b1 = if bn == 1 then 0 else if trade[1] != 1 and trade[0] == 1 then 1 else 0;
def s1 = if bn == 1 then 0 else if trade[1] == 1 and trade[0] == 2 then 1 else 0;
def b2 = if bn == 1 then 0 else if trade[1] != 3 and trade[0] == 3 then 1 else 0;
def s2 = if bn == 1 then 0 else if trade[1] == 3 and trade[0] == 4 then 1 else 0;
def cancel = if bn == 1 then 0 else
if s1[1] or s2[1] then 0 else
if (trade == 0 and trade[1] > 0) then 1
else 0;
addOrder(
type = OrderType.BUY_to_open,
condition = b1,
price = open[-1],
tradeSize = 1,
tickColor = color.green,
arrowColor = Color.green,
name = "BUY"
);
addOrder(
type = OrderType.sell_to_close,
condition = s1,
price = open[-1],
tradeSize = 1,
tickColor = color.red,
arrowColor = Color.red,
name = "SELL"
);
addOrder(
type = OrderType.BUY_to_open,
condition = b2,
price = open[-1],
tradeSize = 1,
tickColor = color.cyan,
arrowColor = Color.cyan,
name = "BUY"
);
addOrder(
type = OrderType.sell_to_close,
condition = s2,
price = open[-1],
tradeSize = 1,
tickColor = color.yellow,
arrowColor = Color.yellow,
name = "SELL"
);
#addOrder(OrderType.BUY_to_open, b1);
#addOrder(OrderType.sell_to_close, s1);
#addOrder(OrderType.BUY_to_open, b2);
#addOrder(OrderType.sell_to_close, s2);
addverticalline(b1, "BUY", color.green);
addverticalline(s1, "SELL", color.red);
addverticalline(b2, "BUY", color.cyan);
addverticalline(s2, "SELL", color.yellow);
addverticalline(cancel, " CANCEL", color.WHITE, Curve.firm);
#-------------------------
input test_signal_lines = yes;
plot z0 = if test_signal_lines then opn else na;
plot z1 = if test_signal_lines then (opn + trade) else na;
z0.setdefaultcolor(color.orange);
z1.setdefaultcolor(getcolor(2));
#z1.setdefaultcolor(color.dark_red);
#z1.setlineweight(4);
# gains
def openx = if bn == 1 then 0
else if b1 or b2 then close
else openx[1];
def gains = if bn == 1 then 0
else if s1 or s2 or cancel then round(gains[1] + (close - openx),2)
else gains[1];
input gains_line = yes;
plot zg = if gains_line then z0 + gains else na;
zg.SetDefaultColor(Color.cyan);
zg.setlineweight(3);
zg.hidebubble();
def t = 2;
def x = (!isnan(close[t]) and isnan(close[t-1]));
addchartbubble(gains_line and x, (gains[t] + opn[t]),
gains[t]
, color.yellow, no);
addchartbubble(0, opn,
gains
, color.yellow, no);
#