Help with ATR DRIVEN Trailing stop loss and ATR take profit

sunnybabu

Member
Plus
Could you please help on what is the issue with this script.. i don't get any signals.. How do we ensure ATR value is at the time of entry as opposed being calculated everyr bar.. The below is for ES futuers and at 5 minute

Code:
#Input for ATR Stop Loss and Take Profit
input ATRStopFactor = 4;  # ATRX4 for Stop Loss
input ATRTakeProfitFactor = 8;  # ATRX12 for Take Profit
input ATRPeriod = 7;
input ATRaverageType = AverageType.WILDERS;


# ATR Calculation
def atr = MovingAverage(ATRaverageType, TrueRange(high, close, low), ATRPeriod);


#########################################################
#MACD INPUT VALUES
########################################################
input fastLength = 8;#12;20
input slowLength = 21;#26;40
input macdLength = 5;#15;
input averageType = AverageType.EXPONENTIAL;


def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, macdLength);


def Diff = Value - Avg;


#########################################################
# EMA
#########################################################
input periodS = 21;#8;
input periodL = 50;#21;
input periodML = 50;
input periodSL = 200;
input EMAaverageType = AverageType.weighted;
def emaS = MovingAverage(EMAaverageType, close, periodS);
def emaL = MovingAverage(EMAaverageType, close, periodL);
def emaML = MovingAverage(EMAaverageType, close, periodML);
def emaSL = MovingAverage(EMAaverageType, close, periodSL);




#########################################################
# Trailing_Long_Exit/Trailing_Short_Exit
#########################################################
def entryPrice = entryPrice();
def price = if IsNaN(entryPrice[1]) then entryPrice else if !IsNaN(entryPrice) then Max(CLOSE, price[1]) else entryPrice;


#########################################################
#buy/sell logic
#########################################################


def Buy =  emaS > emaL and close >emasl and Avg > 0 and Value > Avg;
def Sell = emaS < emaL and close <emasl and Avg < 0 and Value < Avg;




# ATR Stop Loss and Take Profit Levels
def longStop =  price - ATRStopFactor;
def longTakeProfit = entryPrice + atr * ATRTakeProfitFactor;
def shortStop = price + ATRStopFactor;
def shortTakeProfit = entryPrice - atr * ATRTakeProfitFactor;


# Place Orders with ATR-based stop loss and take profit logic


AddOrder(OrderType.BUY_to_open, Buy, open[-1], tradeSize = 2,tickColor = Color.GREEN, arrowColor = Color.GREEN, name = "LONG_ENTRY_ORDER");
addOrder(OrderType.BUY_TO_CLOSE, high >= shortStop or low <= shortTakeProfit, open[-1], tradeSize = 2, tickColor = Color.RED, arrowColor = Color.RED,name = "SHORT_EXIT_ORDER");


AddOrder(OrderType.SELL_TO_CLOSE, low <= longStop or high >= longTakeProfit, open[-1], tradeSize = 2,tickColor = Color.RED, arrowColor = Color.RED,name = "LONG_EXIT_ORDER");
AddOrder(OrderType.SELL_to_open, Sell, open[-1], tradeSize = 2,tickColor = Color.GREEN, arrowColor = Color.GREEN, name = "SHORT_ENTRY_ORDER");
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
366 Online
Create Post

Similar threads

Similar threads

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