MACD Strategy For ThinkOrSwim

kthomasking

New member
Ruby:
#MACD Study with Fractals
input TargetRatio = 1.5;
input DF_MultxATR = 1.0;
input price = close;
input EMA_Length = 200 ;
def displace = 0;
def showBreakoutSignals = no;
input MaxDrawdown = .50;
def EMA1 = ExpAverage(price[-displace], EMA_Length);
def EMA2 = ExpAverage(price[-displace], 250);
#Directional Strength Code: ---------------------------------------------------------------------
def UD = close[15] > EMA1[15] and close[20] > EMA1[20] and close[30] > EMA1[30] and close[40] > EMA1[40] and close[50] > EMA1[50] and close > EMA1;

#Fractal Code: ----------------------------------------------------------------------
def sequenceCount = 1;
def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1
with count1
while count1 != sequenceCount and
count1 != -1
do if GetValue(high, -i1) > high or
(GetValue(high, -i1) == high and
count1 == 0)
then -1
else if GetValue(high, -i1) < high
then count1 + 1
else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1
with count2
while count2 != sequenceCount and
count2 != -1
do if GetValue(high, i2) > high or
(GetValue(high, i2) == high and
count2 >= 1)
then -1
else if GetValue(high, i2) < high
then count2 + 1
else count2;
def downRightSide = fold i3 = 1 to maxSideLength + 1
with count3
while count3 != sequenceCount and
count3 != -1
do if GetValue(low, -i3) < low or
(GetValue(low, -i3) == low and
count3 == 0)
then -1
else if GetValue(high, -i3) > low
then count3 + 1
else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1
with count4
while count4 != sequenceCount and
count4 != -1
do if GetValue(low, i4) < low or
(GetValue(low, i4) == low and
count4 >= 1)
then -1
else if GetValue(low, i4) > low
then count4 + 1
else count4;

def UF = if upRightSide == sequenceCount and
upLeftSide == sequenceCount
then high
else UF[1];
def pUF = if UF != UF[1]
then UF[1]
else pUF[1];
def DF = if downRightSide == sequenceCount and
downLeftSide == sequenceCount
then low
else DF[1];
def pDF = if DF != DF[1]
then DF[1]
else pDF[1];
def UpFractal = UF;
def DownFractal = DF;
def trend = if UF > pUF and DF > pDF
then 1
else if UF < pUF and DF < pDF
then -1
else 0;

#Slow MACD Code: -------------------------------------------------------------------------
def MacdAverageType = AverageType.EXPONENTIAL;
def ZeroLine = 0;
#def fastLength = 12;
#def slowLength = 26;
#def MacdLength = 9;
#def Value = MovingAverage(MacdAverageType, close, fastLength) - MovingAverage(MacdAverageType, close, slowLength); # Blue MACD Line is 12 - 26 period moving average
#def Avg = MovingAverage(MacdAverageType, Value, MacdLength); # Red Signal Line is 9 period EMA of the MACD value

#def ExpPrice = close;
#def AvgExp200 = ExpAverage(close(), 200);

#Fast MACD Code: -----------------------------------------------------------------
def fastLength2 = 12;
def slowLength2 = 26;
def MacdLength2 = 9;
def Value2 = MovingAverage(MacdAverageType, close, fastLength2) - MovingAverage(MacdAverageType, close, slowLength2); # Blue line -
def Avg2 = MovingAverage(MacdAverageType, Value2, MacdLength2); # Red
#def SellScanFast = DD and Value2 crosses below Avg2 and Value2 > ZeroLine ; # blue crosses under red above the zero line

#Long Code: --------------------------------------------------------------------
def BuyLONGScanTrending = UD and Value2 crosses above Avg2 and Value2 < ZeroLine ; #

def GetLONGEntryPrice = if BuyLONGScanTrending # the entry price
then price
else GetLONGEntryPrice[1];

plot LONGENTRYPRICE = if BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12] then GetLONGEntryPrice else Double.NaN;
LONGENTRYPRICE.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LONGENTRYPRICE.AssignValueColor(Color.magenta);
LONGENTRYPRICE.SetLineWeight(5);
#plot LongEntryValues = LongEntryPrice;
#LongEntryValues.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

def getLONGStopPrice = if BuyLONGScanTrending # sell if the price goes up to the last swing high - stop loss
then (DownFractal - DF_MultxATR* ATR())
else getLONGStopPrice[1];

plot LONGStopTarget = if BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12] then getLONGStopPrice else Double.NaN;
LONGStopTarget.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LONGStopTarget.AssignValueColor(Color.blue);
LONGStopTarget.SetLineWeight(5);

def UPLONGTarget = if BuyLONGScanTrending then Round((price +
TargetRatio * AbsValue(( price - getLONGStopPrice)) ), 2) else Double.NaN;;

def getLONGTargetPrice = if BuyLONGScanTrending # sell if the price drops below the target price - target for the short position
then UPLONGTarget
else getLONGTargetPrice[5];

plot LONGUpTargetPrice =if BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12] then getLONGTargetPrice else Double.NaN;
LONGUpTargetPrice.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LONGUpTargetPrice.AssignValueColor(Color.blue);
LONGUpTargetPrice.SetLineWeight(5);

def partialLONGUpTarget = if BuyLONGScanTrending then Round((close + (price - getLONGStopPrice) ), 2) else Double.NaN;;

def getPartLONGTargetPrice = if BuyLONGScanTrending # partial target for the short position
then partialLONGUpTarget
else getPartLONGTargetPrice[1];

plot PartLONGUptargetPrice =if BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12] then getPartLONGTargetPrice else Double.NaN;
PartLONGUptargetPrice.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PartLONGUptargetPrice.AssignValueColor(Color.ORANGE);
PartLONGUptargetPrice.SetLineWeight(5);

#def BuyLONGScanFast = UD and Value2 crosses above Avg2 and Value2 < ZeroLine ; # blue crosses under red above the zero

#def qty = GetQuantity();
#def netLiq = qty * close;
def MaxLongPriceChange = GetLONGEntryPrice - getLONGStopPrice;
#def NumShortShares = MaxDrawdown / MaxLongPriceChange;
#def ShortLoss = (qty * GetLONGEntryPrice) - (qty *LONGStopTarget);

#AddVerticalLine ( BuyLONGScanFast and !BuyLONGScanTrending, "", Color.GREEN, Curve.SHORT_DASH);

def NumLongShares = AbsValue(MaxDrawdown / MaxLongPriceChange);

#AddLabel(yes,"Longs: " +floor(NumLongShares) ,color.green);

#AddLabel(yes,"Long: " +AsDollars(LongEntryPrice),color.green);

AddLabel(yes,"LS: " + AsDollars( getLONGStopPrice),color.green);
AddLabel(yes,"LT1: " + AsDollars(getPartLONGTargetPrice),color.green);
AddLabel(yes,"LT2: " + AsDollars(getLONGTargetPrice),color.green);
#AddChartBubble(BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12],LONGUpTargetPrice, AsDollars(LONGUpTargetPrice) , Color.gray, no);

#AddChartBubble(BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12],PartLONGUptargetPrice, AsDollars(PartLONGUptargetPrice) , Color.gray, no);

AddChartBubble(BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12],EMA2, Floor(NumLongShares) , Color.GREEN, no);

AddChartBubble(BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12],EMA2, AsDollars(LONGENTRYPRICE), Color.GREEN, no);

#AddChartBubble(BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12],getLONGStopPrice, AsDollars(LONGStopTarget) , Color.gray, no);

#AddChartBubble(BuyLONGScanTrending and !BuyLONGScanTrending[1] and !BuyLONGScanTrending[2] and !BuyLONGScanTrending[3] and !BuyLONGScanTrending[4] and !BuyLONGScanTrending[5] and !BuyLONGScanTrending[6] and !BuyLONGScanTrending[7] and !BuyLONGScanTrending[8] and !BuyLONGScanTrending[9] and !BuyLONGScanTrending[10] and !BuyLONGScanTrending[11] and !BuyLONGScanTrending[12], high, "" , Color.GREEN, no);

Alert(BuyLONGScanTrending , "MACD - " + GetSymbol(), Alert.BAR, Sound.Bell);

#Long Loss Calculation Code: ----------------------------------------------------------
def LongLoss = (NumLongShares* LONGENTRYPRICE) - (NumLongShares* LONGStopTarget);
AddLabel(LongLoss > MaxDrawdown, "LongLoss: " + LongLoss, Color.GRAY);

# Auto Order Code: -----------------------------------------------------------------
def TotalLONGSell = close crosses above LONGUpTargetPrice or open crosses above LONGUpTargetPrice or low > LONGUpTargetPrice or high > LONGUpTargetPrice ;
def StopLONGSell = close crosses below getLONGStopPrice or open crosses below getLONGStopPrice or low < getLONGStopPrice or high < getLONGStopPrice;

#AddOrder(OrderType.BUY_AUTO, BuyLONGScanTrending, open, 1, Color.MAGENTA, Color.ORANGE,name = "");

#AddOrder(OrderType.SELL_TO_CLOSE, TotalLONGSell or StopLONGSell, open, 1, Color.ORANGE, Color.MAGENTA,name = "");

AddOrder(OrderType.BUY_AUTO, BuyLONGScanTrending, open[-1], 1, Color.ORANGE, Color.ORANGE,name = "");

AddOrder(OrderType.SELL_TO_CLOSE, TotalLONGSell or StopLONGSell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "");

#Spread Check Code: ------------------------------------------------------------------
#def bid = close(priceType = "Bid");
#def ask = close(priceType = "Ask");
#def spread = ask - bid;
#input ATRspreadMult = .5;
#AddLabel(spread > ATRspreadMult*atr(), " SpATRs: " + ROUND(SPREAD/ATR(),2) + " SPmag: " + spread + " ATRmag " + ATR(), Color.RED);
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
407 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top