# template_buysell3_long_short
# add short trades
# buy signals based on averages crossing
# sell signals based on gain and loss target levels
def na = Double.NaN;
def bn = BarNumber();
# /////////////////////////////
# test data
# for buying , short avg crossing above long avg
def price = close;
input ma1_len = 9;
input ma1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);
input ma2_len = 33;
input ma2_type = AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);
input show_lines = yes;
plot z1 = if show_lines then ma1 else na;
z1.SetDefaultColor(GetColor(1));
#z1.setlineweight(1);
z1.HideBubble();
plot z2 = if show_lines then ma2 else na;
z2.SetDefaultColor(GetColor(2));
#z2.setlineweight(1);
z2.HideBubble();
def ma1xup = (ma1 crosses above ma2);
def ma1xdwn = (ma1 crosses below ma2);
# /////////////////////////////
# configure buy , sell vars
def long_buy = ma1xup;
def short_buy = ma1xdwn;
#-----------------------------
# numbers added to the buy price to determine sell levels
input stop_gain = 0.5;
input stop_loss = -0.2;
#-----------------------------------
# long trades
# when a long buy happens, record the close. use that close as a ref for the stops +- $x
# just for long trades
def long_buy_pr;
def long_sell_gain_pr;
def long_sell_loss_pr;
def long_trade;
def long_gain;
def long_gain_total;
def long_buy_cnt;
def long_sell_gain_cnt;
def long_sell_loss_cnt;
if bn == 1 then {
long_buy_pr = 0;
long_sell_gain_pr = 0;
long_sell_loss_pr = 0;
long_trade = 0;
long_gain = 0;
long_gain_total = 0;
long_buy_cnt = 0;
long_sell_gain_cnt = 0;
long_sell_loss_cnt = 0;
} else if long_buy and !long_trade[1] then {
# not in a trade, process a buy. record the close as the buy price
long_buy_pr = close;
long_sell_gain_pr = long_buy_pr + stop_gain;
long_sell_loss_pr = long_buy_pr + stop_loss;
long_trade = 1;
long_gain = 0;
long_gain_total = long_gain_total[1];
long_buy_cnt = long_buy_cnt[1] + 1;
long_sell_gain_cnt = long_sell_gain_cnt[1];
long_sell_loss_cnt = long_sell_loss_cnt[1];
} else if long_trade[1] and close > long_sell_gain_pr[1] then {
# price reached target, sell for gain
long_buy_pr = 0;
long_sell_gain_pr = 0;
long_sell_loss_pr = 0;
long_trade = 0;
long_gain = close - long_buy_pr[1];
long_gain_total = long_gain_total[1] + long_gain;
long_buy_cnt = long_buy_cnt[1];
long_sell_gain_cnt = long_sell_gain_cnt[1]+1;
long_sell_loss_cnt = long_sell_loss_cnt[1];
} else if long_trade[1] and close < long_sell_loss_pr[1] then {
# price fell below stop, sell for loss
long_buy_pr = 0;
long_sell_gain_pr = 0;
long_sell_loss_pr = 0;
long_trade = 0;
long_gain = close - long_buy_pr[1];
long_gain_total = long_gain_total[1] + long_gain;
long_buy_cnt = long_buy_cnt[1];
long_sell_gain_cnt = long_sell_gain_cnt[1];
long_sell_loss_cnt = long_sell_loss_cnt[1]+1;
} else if !isnan(close) then {
long_buy_pr = long_buy_pr[1];
long_sell_gain_pr = long_sell_gain_pr[1];
long_sell_loss_pr = long_sell_loss_pr[1];
long_trade = long_trade[1];
long_gain = close - long_buy_pr[1];
long_gain_total = long_gain_total[1];
long_buy_cnt = long_buy_cnt[1];
long_sell_gain_cnt = long_sell_gain_cnt[1];
long_sell_loss_cnt = long_sell_loss_cnt[1];
} else {
long_buy_pr = 0;
long_sell_gain_pr = 0;
long_sell_loss_pr = 0;
long_trade = 0;
long_gain = 0;
long_gain_total = 0;
long_buy_cnt = 0;
long_sell_gain_cnt = 0;
long_sell_loss_cnt = 0;
}
#----------------------------
#----------------------------
# short trades
# when a short buy happens, record the close. use that close as a ref for the stops +- $x
# just for short trades
def short_buy_pr;
def short_sell_gain_pr;
def short_sell_loss_pr;
def short_trade;
def short_gain;
def short_gain_total;
def short_buy_cnt;
def short_sell_gain_cnt;
def short_sell_loss_cnt;
if bn == 1 then {
short_buy_pr = 0;
short_sell_gain_pr = 0;
short_sell_loss_pr = 0;
short_trade = 0;
short_gain = 0;
short_gain_total = 0;
short_buy_cnt = 0;
short_sell_gain_cnt = 0;
short_sell_loss_cnt = 0;
} else if short_buy and !short_trade[1] then {
# not in a trade, process a buy. record the close as the buy price
short_buy_pr = close;
short_sell_gain_pr = short_buy_pr - stop_gain;
short_sell_loss_pr = short_buy_pr - stop_loss;
short_trade = 1;
short_gain = 0;
short_gain_total = short_gain_total[1];
short_buy_cnt = short_buy_cnt[1] + 1;
short_sell_gain_cnt = short_sell_gain_cnt[1];
short_sell_loss_cnt = short_sell_loss_cnt[1];
} else if short_trade[1] and close < short_sell_gain_pr[1] then {
# price reached target, sell for gain
short_buy_pr = 0;
short_sell_gain_pr = 0;
short_sell_loss_pr = 0;
short_trade = 0;
short_gain = short_buy_pr[1] - close;
short_gain_total = short_gain_total[1] + short_gain;
short_buy_cnt = short_buy_cnt[1];
short_sell_gain_cnt = short_sell_gain_cnt[1]+1;
short_sell_loss_cnt = short_sell_loss_cnt[1];
} else if short_trade[1] and close > short_sell_loss_pr[1] then {
# price fell below stop, sell for loss
short_buy_pr = 0;
short_sell_gain_pr = 0;
short_sell_loss_pr = 0;
short_trade = 0;
short_gain = short_buy_pr[1] - close;
short_gain_total = short_gain_total[1] + short_gain;
short_buy_cnt = short_buy_cnt[1];
short_sell_gain_cnt = short_sell_gain_cnt[1];
short_sell_loss_cnt = short_sell_loss_cnt[1]+1;
} else if !isnan(close) then {
short_buy_pr = short_buy_pr[1];
short_sell_gain_pr = short_sell_gain_pr[1];
short_sell_loss_pr = short_sell_loss_pr[1];
short_trade = short_trade[1];
short_gain = short_buy_pr[1] - close;
short_gain_total = short_gain_total[1];
short_buy_cnt = short_buy_cnt[1];
short_sell_gain_cnt = short_sell_gain_cnt[1];
short_sell_loss_cnt = short_sell_loss_cnt[1];
} else {
short_buy_pr = 0;
short_sell_gain_pr = 0;
short_sell_loss_pr = 0;
short_trade = 0;
short_gain = 0;
short_gain_total = short_gain_total[1];
short_buy_cnt = 0;
short_sell_gain_cnt = 0;
short_sell_loss_cnt = 0;
}
#---------------------------
def lgain = round(long_gain,2);
def sgain = round(short_gain,2);
def lgainttl = round(long_gain_total,2);
def sgainttl = round(short_gain_total,2);
# --------------------
addlabel(1, " ", color.black);
addlabel(1, "LONG trades", color.green);
addlabel(1, "Buys " + long_buy_cnt, color.white);
addlabel(1, "Sells , gain " + long_sell_gain_cnt , color.green);
addlabel(1, "Sells , loss " + long_sell_loss_cnt , color.red);
addlabel(1, "profit " + lgainttl, color.yellow);
addlabel(1, " ", color.black);
addlabel(1, "SHORT trades", color.red);
addlabel(1, "Buys " + short_buy_cnt, color.white);
addlabel(1, "Sells , gain " + short_sell_gain_cnt , color.green);
addlabel(1, "Sells , loss " + short_sell_loss_cnt , color.red);
addlabel(1, "profit " + sgainttl, color.yellow);
addlabel(1, " ", color.black);
#---------------------
# lines
plot zlb = if long_buy_pr > 0 then long_buy_pr else if long_buy_pr[1] > 0 then long_buy_pr[1] else na;
zlb.SetDefaultColor(Color.white);
zlb.setlineweight(2);
zlb.hidebubble();
plot zlstopg = if long_buy_pr > 0 then long_sell_gain_pr else if long_buy_pr[1] > 0 then long_sell_gain_pr[1] else na;
zlstopg.SetDefaultColor(Color.green);
zlstopg.setlineweight(1);
zlstopg.hidebubble();
plot zlstopl = if long_buy_pr > 0 then long_sell_loss_pr else if long_buy_pr[1] > 0 then long_sell_loss_pr[1] else na;
zlstopl.SetDefaultColor(Color.red);
zlstopl.setlineweight(1);
zlstopl.hidebubble();
# ---------------------
plot zsb = if short_buy_pr > 0 then short_buy_pr else if short_buy_pr[1] > 0 then short_buy_pr[1] else na;
zsb.SetDefaultColor(Color.white);
zsb.setlineweight(2);
zsb.hidebubble();
plot zsstopg = if short_buy_pr > 0 then short_sell_gain_pr else if short_buy_pr[1] > 0 then short_sell_gain_pr[1] else na;
zsstopg.SetDefaultColor(Color.green);
zsstopg.setlineweight(1);
zsstopg.hidebubble();
plot zsstopl = if short_buy_pr > 0 then short_sell_loss_pr else if short_buy_pr[1] > 0 then short_sell_loss_pr[1] else na;
zsstopl.SetDefaultColor(Color.red);
zsstopl.setlineweight(1);
zsstopl.hidebubble();
# ---------------------
input show_buy_arrows = yes;
def arroff = 11;
def y = arroff * ticksize();
plot zlbuy = if show_buy_arrows and long_buy then low-y else na;
zlbuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlbuy.SetDefaultColor(Color.green);
zlbuy.setlineweight(3);
zlbuy.hidebubble();
plot zsbuy = if show_buy_arrows and short_buy then high+y else na;
zsbuy.SetPaintingStrategy(PaintingStrategy.ARROW_down);
zsbuy.SetDefaultColor(Color.cyan);
zsbuy.setlineweight(3);
zsbuy.hidebubble();
#x.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#x.SetDefaultColor(Color.red);
#x.setlineweight(1);
#x.hidebubble();
#----------------------
# sell shapes
# long
# sell gain - triangle
def lsellgain = if long_trade[1] and close > long_sell_gain_pr[1] then 1 else 0;
plot zlsg = if lsellgain then high + y else na;
zlsg.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
zlsg.SetDefaultColor(Color.green);
zlsg.setlineweight(4);
zlsg.hidebubble();
# sell loss - square
def lsellloss = if long_trade[1] and close < long_sell_loss_pr[1] then 1 else 0;
plot zlsl = if lsellloss then low - y else na;
zlsl.SetPaintingStrategy(PaintingStrategy.SQUARES);
zlsl.SetDefaultColor(Color.red);
zlsl.setlineweight(4);
zlsl.hidebubble();
#--------------------
# sell
# short
# sell gain - triangle
def ssellgain = if short_trade[1] and close < short_sell_gain_pr[1] then 1 else 0;
plot zssg = if ssellgain then low - y else na;
zssg.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
zssg.SetDefaultColor(Color.cyan);
zssg.setlineweight(4);
zssg.hidebubble();
# sell loss - square
def ssellloss = if short_trade[1] and close > short_sell_loss_pr[1] then 1 else 0;
plot zssl = if ssellloss then high + y else na;
zssl.SetPaintingStrategy(PaintingStrategy.SQUARES);
zssl.SetDefaultColor(Color.magenta);
zssl.setlineweight(4);
zssl.hidebubble();
#-------------------
# profit bubbles
input show_profit_bubbles = yes;
# longs
def longsell = if long_trade[1] and (close > long_sell_gain_pr[1] or close < long_sell_loss_pr[1]) then 1 else 0;
addchartbubble(show_profit_bubbles and longsell, low*0.99,
"Long\n" +
lgain + "\n" +
lgainttl + " TTL"
, color.cyan, no);
# shorts
def shortsell = if short_trade[1] and (close < short_sell_gain_pr[1] or close > short_sell_loss_pr[1]) then 1 else 0;
addchartbubble(show_profit_bubbles and shortsell, high*1.01,
"Short\n" +
sgain + "\n" +
sgainttl + " TTl"
, color.orange, yes);
#