# MACD Strategy For ThinkOrSwim

##### 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];

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];

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];

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];

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);

def NumLongShares = AbsValue(MaxDrawdown / MaxLongPriceChange);

#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.SELL_TO_CLOSE, TotalLONGSell or StopLONGSell, open, 1, Color.ORANGE, Color.MAGENTA,name = "");

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

#def bid = close(priceType = "Bid");

Last edited by a moderator:

87k+ Posts
407 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?