#strat_convert_easyL_close_offsets_00c
#https://usethinkscript.com/threads/convert-easylanguage-stacked-momentum.14123/
#Convert EasyLanguage Stacked Momentum
#make strategy
#close trade after 10 bars
def bn = barnumber();
def na = double.nan;
input offset1 = 10;
input offset2 = 20;
input offset3 = 200;
input trade_length_bars = 10;
input tradeSize = 1;
def longenter = if (close < close[offset3] and close > close[offset2] and close < close[offset1] and close > open) then 1 else 0;
def shortenter = if (close > close[offset3] and close < close[offset2] and close[offset1] and close < open) then 1 else 0;
def longbn = if bn == 1 then 0
else if longenter then bn
else longbn[1];
def shortbn = if bn == 1 then 0
else if shortenter then bn
else shortbn[1];
# another enter signal will extend the trade duration
def longexit = if bn >= (longbn + trade_length_bars) then 1 else 0;
def shortexit = if bn >= (shortbn + trade_length_bars) then 1 else 0;
def longcancel = 0;
def shortcancel = 0;
def longtrade = if bn == 1 then 0
else if longexit then 0
else if longenter then 1
else longtrade[1];
def shorttrade = if bn == 1 then 0
else if shortexit then 0
else if shortenter then 1
else shorttrade[1];
def longstart = (!longtrade[1] and longtrade);
def longstop = (longtrade[1] and !longtrade);
def shortstart = (!shorttrade[1] and shorttrade);
def shortstop = (shorttrade[1] and !shorttrade);
input show_orders = yes;
#-------------------------
# long enter
addorder(
OrderType.BUY_TO_OPEN,
show_orders and longstart,
open[-1],
tradeSize,
color.green,
color.green,
"long");
# long exit
#SELL_TO_CLOSE
addorder(
OrderType.sell_TO_close,
show_orders and longstop,
open[-1],
tradeSize,
color.green,
color.green,
"-");
#-----------------------
# short enter
addorder(
OrderType.sell_to_open,
show_orders and shortstart,
open[-1],
tradeSize,
color.red,
color.red,
"short");
# short exit
addorder(
OrderType.buy_TO_close,
show_orders and shortstop,
open[-1],
tradeSize,
color.red,
color.red,
"-");
#-----------------------
input show_arrows = yes;
plot zlongenter = (show_arrows and longstart);
zlongenter.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
zlongenter.SetDefaultColor(Color.green);
zlongenter.setlineweight(3);
zlongenter.hidebubble();
plot zlongexit = (show_arrows and longstop);
zlongexit.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
zlongexit.SetDefaultColor(Color.cyan);
zlongexit.setlineweight(3);
zlongexit.hidebubble();
plot zshortenter = (show_arrows and shortstart);
zshortenter.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
zshortenter.SetDefaultColor(Color.red);
zshortenter.setlineweight(3);
zshortenter.hidebubble();
plot zshortexit = (show_arrows and shortstop);
zshortexit.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
zshortexit.SetDefaultColor(Color.yellow);
zshortexit.setlineweight(3);
zshortexit.hidebubble();
input show_enter_exits_verticals = no;
addverticalline(show_enter_exits_verticals and longstart, "long", color.green);
addverticalline(show_enter_exits_verticals and longstop, "long", color.cyan);
addverticalline(show_enter_exits_verticals and shortstart, "short", color.red);
addverticalline(show_enter_exits_verticals and shortstop, "short", color.yellow);
input show_all_buy_sell_signal_verticals = no;
addverticalline(show_all_buy_sell_signal_verticals and longenter, "long", color.green);
addverticalline(show_all_buy_sell_signal_verticals and longexit, "long", color.cyan);
addverticalline(show_all_buy_sell_signal_verticals and shortenter, "short", color.red);
addverticalline(show_all_buy_sell_signal_verticals and shortexit, "short", color.yellow);
input bubbles1 = no;
addchartbubble(bubbles1, low*0.99,
longtrade
, (if longtrade then color.green else color.gray), no);
addchartbubble(bubbles1, low*0.99,
shorttrade
, (if shorttrade then color.red else color.gray), no);
#AddOrder ( int type, IDataHolder condition, IDataHolder price, IDataHolder tradeSize, CustomColor tickColor, CustomColor arrowColor, String name);
#https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AddOrder
#https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/OrderType
#