Code to Stop Trading for the day Once a Profit Order is executed


New member
Hi - I was looking to see if anyone can help getting my simple MACD strategy to stop trading for the day once the "Profit" sell_to_close order is completed.

I have tried a few things like tracking FPL, but it seems way over my head to get it to work.

def marketopen = 930;
def marketclose = 1533;

def begin = SecondsFromTime(marketopen);
def end = SecondsTillTime(marketclose);

def tradingday = begin > 0 and end > 0;
def eod = end < 1;

# Define how many shares to purchase in Dollar amount
input positionsize = 500;

# Define the length for the MACD 3
input fastLength3 = 10;
input slowLength3 = 26;
input MACDLength3 = 9;
input averageType3 = AverageType.EXPONENTIAL;
input averagetype = averagetype.EXPONENTIAL;

# Calculate the MACD values for 3
def Value3 = MovingAverage(averageType, close, fastLength3) - MovingAverage(averageType, close, slowLength3);
def Avg3 = MovingAverage(averageType, Value3, MACDLength3);
def Diff3 = Value3 - Avg3;
def ZeroLine = 0;

# Condition MACD 3 value is greater
def zerobid3 = Diff3[3] <= 0;
def bid3 = Diff3 > Diff3[1];
def Value3UP = Value3 > Value3[1];

# Define Stoploss STOPLX and ProfitLX Code
input offsetType = {default percent, value, tick};
input stop = 3.0;
input target = 9.9;

def entryPrice = EntryPrice();
def mult;
switch (offsetType) {
case percent:
    mult = entryPrice / 1000;
case value:
    mult = 1;
case tick:
    mult = TickSize();
def stopPrice = entryPrice - stop * mult;
def targetPrice = entryPrice + target * mult;

# Define the Buy and Sell terms
def buy = tradingday and Value3UP;

# Add a buy order
AddOrder(OrderType.BUY_TO_OPEN, buy, close, 1000 / close, name = "buy", tickcolor = Color.GREEN);

# Add a stop loss order
AddOrder(OrderType.SELL_TO_CLOSE, low <= stopPrice, name = "StopLoss", tickcolor = GetColor(5), arrowcolor = GetColor(5));

# Add a profit target order
AddOrder(OrderType.SELL_TO_CLOSE, high >= targetPrice, name = "Profit", tickcolor = GetColor(6), arrowcolor = GetColor(6));

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
413 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.