vollermist
New member
Here's a strategy I had been working on a while ago. Lots of incomplete code in this, but it seems to more or less work for long positions (stocks), particularly on range bar charts. It includes a variation of my position sizing code mentioned in Position Size Calculator for ThinkorSwim
Have fun everyone.
Have fun everyone.
Code:
##################################################################
##### vm_GrowEquityStrategy version 2
##### coded by vollermist 2021/05/20 @01:57am EDT
#
######################################################
################# INPUTS #######################
######################################################
def nan = Double.NaN;
#input bias = {default Long, Short, Both};
input enableOrders = yes;
input dipBars = 2;
input trendBars = 4;
input showSignals = yes;
input showBubbles = no;
input roundingfactor = 2; #hint roundingfactor: 2 for stocks to the penny, 5 or 6 for forex
# Trade Management inputs
input equity = 25; #hint equitydollars: in thousands
input equityrisk = 2.0; #hint equityrisk: percent
input kelly = 20; #hint kelly ratio percent
input sharerisk_percent = yes; #hint sharerisk_percent: enable risk as a % of price
input sharerisk = 2.0; #hint sharerisk: how many range bars, ATRs, or percent defines trailing stop risk
input target_r = 4.0; #hint target_r: "R" profit per unit of risk
def last = close(priceType = Pricetype.LAST);
######################################################
################# SIGNALS ########################
######################################################
def hlo3 = (high + low + open) / 3;
def redbar = close < open;
def greenbar = close > open;
def doji = close == open;
def redbarstart = greenbar[1] and redbar;
def greenbarstart = redbar[1] and greenbar;
### COUNTING BAR CHANGES
def redbarcount = CompoundValue( 1, if redbar and redbarstart then 1 else if redbar and !redbarstart then redbarcount[1] + 1 else nan, 1);
def greenbarcount = CompoundValue( 1, if greenbar and greenbarstart then 1 else if greenbar and !greenbarstart then greenbarcount[1] + 1 else nan, 1);
def downTrend = high[0] < high[trendBars] and redbarcount >= trendBars - 1; #changed close to high
def upTrend = low[0] > low[trendBars] and greenbarcount >= trendBars - 1; #changed close to low
#def downTrend = high[0] < high[trendBars] and redbarcount >= dipBars; #changed close to high
#def upTrend = low[0] > low[trendBars] and greenbarcount >= dipBars; #changed close to low
#def buySig = downTrend[1] and low < low[1] and close > low[1];
#def sellSig = upTrend[1] and high > high[1] and close < high[1];
def buySig = downTrend[1] and low < low[1] and close > hlc3[1];
def sellSig = upTrend[1] and high > high[1] and close < hlo3[1];
######################################################
################# TRADE PRICING ###############
######################################################
def bp = if buySig then GetValue(Roundup(Max(hlo3[1], hlc3[1]),roundingfactor),0) else nan;
def sp = if sellSig then GetValue(Rounddown(Min(hlc3[1], hlo3[1]),roundingfactor),0) else nan;
######################################################
################# TRADE MANAGEMENT #############
######################################################
def ATR = Roundup(MovingAverage(AverageType.WILDERS, TrueRange(high, close, low), 14),1);
def sharesrounding = 0; #hint sharesrounding: nearest share is 0, nearest 10 shares is -1, nearest 100 shares is -2
def tgtentryprice = hlo3[1];
def portfolio = equity * 1000;
def agg = GetValue(GetAggregationPeriod(), 0);
def Rangechartrisk = agg[1] * sharerisk; #range charts
#def Timechartrisk = Round(ATR,2) * sharerisk; # use for time charts
def Percentrisk = Round(sharerisk / 100 * tgtentryprice,2); #use for percentage-based risk
def r = if sharerisk_percent then Percentrisk else if agg < 60000 then Rangechartrisk else Rangechartrisk;
def targetprofit = r * target_r;
def portfoliorisk = GetValue(Round(portfolio * (equityrisk / 100), 0),0); #200
def maxtradedollars = Round(portfolio * kelly / 100, 0);
def kellyshares = Round(maxtradedollars / tgtentryprice,0);
def riskshares = Round(portfoliorisk / r,0);
def shares = GetValue(Min(riskshares, kellyshares), 0); #
######################################################
################# O R D E R S #################
######################################################
### LONG BIAS
####AddOrder(OrderType.BUY_TO_OPEN, buySig within 2 bars and close >= buyprice, open[-1], shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "LE exp");
#AddOrder(OrderType.BUY_TO_OPEN, buySig within trendBars bars and high >= bp, open[-1], shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "LE");
#AddOrder(OrderType.SELL_TO_CLOSE, sellSig within trendBars bars and low <= sp, open[-1], shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "LX");
AddOrder(OrderType.BUY_TO_OPEN, enableOrders and buySig and greenbar within trendBars bars, open[-1], shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "LE");
AddOrder(OrderType.SELL_TO_CLOSE, enableOrders and sellSig and redbar within trendBars bars, open[-1], shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "LX");
#AddOrder(OrderType.SELL_TO_CLOSE, low < initstop, initstop, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "STOP");
#AddOrder(OrderType.SELL_TO_CLOSE, low < trailstop, trailstop, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "TRAIL");
###############################
### SHORT BIAS
#AddOrder(OrderType.SELL_TO_OPEN, (sellSig or sellSig[1]) and close <= sp, open[-1], shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "SE");
#AddOrder(OrderType.BUY_TO_CLOSE, (buySig or buySig[1]) and high >= bp, open[-1], shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "LE");
### BOTH BIAS
#AddOrder(OrderType.BUY_AUTO, (buySig or buySig[1]) and high >= bp, open[-1], shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "BUY");
#AddOrder(OrderType.SELL_AUTO, (sellSig or sellSig[1]) and close <= sp, open[-1], shares, tickcolor = CreateColor(255, 0, 0), arrowcolor = CreateColor(255, 0, 0), "SELL");
#AddOrder(OrderType.BUY_AUTO, buySig within trendBars bars and high >= bp, high, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "LE");
#AddOrder(OrderType.SELL_AUTO, sellSig within trendBars bars and low <= sp, low, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "LX");
def hardriskshares = portfoliorisk / entryprice();
plot hardstop = ((entryprice() * shares) - portfoliorisk)/shares;
hardstop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hardstop.SetDefaultColor(CreateColor(255,0,0));
hardstop.Hide();
AddLabel(yes, "Max Risk: " + portfoliorisk, Color.LIGHT_RED);
AddLabel(yes, "Hard Stop: " + hardstop, Color.LIGHT_RED);
AddOrder(OrderType.SELL_TO_CLOSE, enableOrders and low < hardstop, hardstop, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "HARDSTOP");
#def initstopL = entryprice() -r;
#def trailnumL = low[1] - r;
#def starttrailL = low > entryprice() + r;
def initstopL = low[1];
def trailnumL = high[2];
#def trailnumL = low[2];
def starttrailL = close > entryprice() + r * target_r;
def trailstpL = if !starttrailL then initstopL else trailnumL;
def trailstopL = if trailstpL > trailstpL[1] then trailstpL else trailstopL[1];
plot stoplineL = trailstopL;
stoplineL.SetDefaultColor(Color.YELLOW);
stoplineL.SetPaintingStrategy(PaintingStrategy.LINE);
AddOrder(OrderType.SELL_TO_CLOSE, enableOrders and high < trailstopL, trailstopL, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "TRAIL");
#AddOrder(OrderType.SELL_TO_CLOSE, close < stopLineL, trailstopL, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "TRAIL");
#####
#def initstopS = entryprice() + r;
#def trailnumS = high[1] - r;
#def starttrailS = high < entryprice() - r;
#def trailstpS = if !starttrailS then initstopS else trailnumS;
#def trailstopS = if trailstpS < trailstpS[1] then trailstpS else trailstopS[1];
#plot stoplineS = trailstopS;
#stoplineS.SetDefaultColor(GetColor(7));
#stoplineS.SetPaintingStrategy(PaintingStrategy.LINE);
#AddOrder(OrderType.BUY_TO_CLOSE, high > initstopS, initstopS, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "STOP");
#AddOrder(OrderType.BUY_TO_CLOSE, low > trailstopS, trailstopS, shares, tickcolor = CreateColor(0, 255, 0), arrowcolor = CreateColor(0, 255, 0), "TRAIL");
######################################################
######################################################
######################################################
########### PAINTING, BUBBLES, LABELS ###########
######################################################
plot buySignal = if showSignals then buySig else nan;
buySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buySignal.SetDefaultColor(Color.WHITE);
buySignal.SetLineWeight(1);
plot sellSignal = if showSignals then sellSig else nan;
sellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellSignal.SetDefaultColor(Color.WHITE);
sellSignal.SetLineWeight(1);
# Trade Pricing
plot buyprice = bp;
buyprice.SetPaintingStrategy(PaintingStrategy.SQUARES);
buyprice.SetDefaultColor(Color.BLUE);
buyprice.SetLineWeight(3);
plot sellprice = sp;
sellprice.SetPaintingStrategy(PaintingStrategy.SQUARES);
sellprice.SetDefaultColor(Color.MAGENTA);
sellprice.SetLineWeight(3);
AddChartBubble(showBubbles and buysig and open <= bp and shares , text = + shares + "@" + bp, "price location" = low, color = Color.GREEN, no);
AddChartBubble(showBubbles and sellsig and open >= sp and shares , text = + shares + "@" + sp, "price location" = high, color = Color.PINK, yes);
# Trade Management
AddLabel(yes, "agg: " + agg, Color.WHITE);
AddLabel(yes, "ATR: " + ATR, Color.WHITE);
#AddLabel(yes, "per share risk: " + persharerisk + " target profit: " + targetprofit);
def riskdollars = (shares * bp) - (shares * hardstop);
AddLabel(yes, "riskdollars: " +riskdollars, Color.PINK);
AddLabel(yes, Concat(shares, " shares with ") + r + " unitrisk and $" + Roundup(shares * r, 0) + " total risk on $" + RoundUp(shares * tgtentryprice, 0) + " investment; target profit of $" + Roundup(targetprofit * shares, 0), Color.WHITE);