# calc_future_shares_same_risk_01
#https://usethinkscript.com/threads/excel-calculating-adding-to-current-position.14311/
#Excel: Calculating adding to Current position.
#------------------------
# determine a desired risk level, then calculate quantity of shares to buy
# figure in a 2nd buy, with a new stop
# calculate an adjusted risk for trade 1, and available risk dollars, to determine shares2
# test symbol - ACET
def na = double.nan;
def bn = barnumber();
def clsx = if isnan(close) then clsx[1] else close;
#-----------------------------
# trade 1
input desired_risk_dollars = 100;
input buy1 = 0.0;
input stop1 = 0.0;
#input buy1 = 6.94;
#input stop1 = 6.59;
def spread1 = (buy1 - stop1);
def shares1 = floor(desired_risk_dollars / (buy1 - stop1));
def risk1 = round(shares1 * (buy1 - stop1), 2);
def cost1 = round(buy1 * shares1, 2);
# --------------------------------
# trade2
#input buy2 = 0.0;
#input stop2 = 0.0;
input buy2 = 12.9;
input stop2 = 12.4;
#hint buy2: Enter buy price for trade #2.\n or 0 to track with current price.
#hint stop2: Enter a stop price.\n or 0 for using the same spread as in trade 1.
def buy2x = if buy2 == 0 then clsx else buy2;
def stop2x = if stop2 == 0 then (buy2x - (buy1 - stop1)) else stop2;
def spread2 = buy2x - stop2x;
def spread1_adj = (buy1 - stop2x);
def risk1_adj = shares1 * (buy1 - stop2x);
def risk2 = (desired_risk_dollars - risk1_adj);
def shares2 = floor(risk2 / (buy2x - stop2x));
def cost2 = (buy2x * shares2);
#--------------------
# totals
def total_cost = (cost1 + cost2);
def total_shares = (shares1 + shares2);
def avg_share_cost = (total_cost / total_shares);
#--------------------------------------------
# define a set of bars, after last bar, for buy lines
def lineoff = 1;
def linelen = 20;
def linex = (!isnan(close[(lineoff + linelen)]) and isnan(close));
def buboff = 3;
input show_bubbles = yes;
def bubx = show_bubbles and buy1 > 0 and (!isnan(close[(buboff+1)]) and isnan(close[buboff]));
#--------------------------
# trade1 - buy line
plot zl0 = if linex then buy1 else na;
zl0.SetDefaultColor(Color.green);
#zl0.setlineweight(1);
#zl0.hidebubble();
addchartbubble(bubx, buy1,
shares1 + " @ " + asdollars(buy1) + " / " + asdollars(stop1) + "\n" +
"risk " + asdollars(risk1) + " buy1\n" +
"cost " + asdollars(cost1)
, color.green, no);
#--------------------------
# trade2 - buy line
# buy more price , default is current price
plot zl1 = if linex then buy2x else na;
zl1.SetDefaultColor(Color.yellow);
#zl1.setlineweight(1);
#zl1.hidebubble();
addchartbubble(bubx, buy2x,
shares2 + " @ " + asdollars(buy2x) + " / " + asdollars(stop2x) + "\n" +
"risk " + asdollars(risk2) + " buy2\n" +
"cost " + asdollars(cost2)
, color.yellow, no);
#-------------------------
addchartbubble(bubx, buy1,
"spread1_adj " + spread1_adj + "\n" +
"risk1_adj " + asdollars(risk1_adj)
, color.yellow, yes);
addchartbubble(bubx, buy2x,
"total cost " + asdollars(total_cost) + "\n" +
"total shares " + total_shares + "\n" +
"avg share cost " + asdollars(avg_share_cost)
, color.magenta, yes);
#-------------------------
input show_labels = yes;
def labels = show_labels and buy1 > 0;
addlabel(labels, " ", color.black);
addlabel(labels, "max risk $: " + desired_risk_dollars, color.yellow);
addlabel(labels, " ", color.black);
addlabel(labels, "Buy1:", color.green);
addlabel(labels, asdollars(buy1) + " / " + asdollars(stop1), color.green);
addlabel(labels, "shares1: " + shares1, color.green);
addlabel(labels, "risk1: " + asdollars(risk1), color.green);
addlabel(labels, "cost1: " + asdollars(cost1), color.green);
addlabel(labels, " ", color.black);
addlabel(labels, "Buy2:", color.yellow);
addlabel(labels, asdollars(buy2x) + " / " + asdollars(stop2x), color.yellow);
addlabel(labels, "shares1: " + shares2, color.yellow);
addlabel(labels, "risk1: " + asdollars(risk2), color.yellow);
addlabel(labels, "cost1: " + asdollars(cost2), color.yellow);
addlabel(labels, " ", color.black);
addlabel(labels, "total cost " + asdollars(total_cost), color.magenta);
addlabel(labels, "total shares " + total_shares, color.magenta);
addlabel(labels, "avg share cost " + asdollars(avg_share_cost), color.magenta);
addlabel(labels, " ", color.black);
#