OCO SELL Orders Not Working Out

rubixcube49

New member
Plus
I am trying to create 3 OCO SELL orders independent of an initial BUY order for the same stock. All 3 SELL orders are conditional, driven by an underlying STUDY. Once any one of the sell orders is filled, I want to automatically cancel the other two. I tried OCO brackets, but it's trying to force me into a BUY / SELL LIMIT / SELL STOP configuration, which is not what I want. Is this Post the sad answer to my question:

https://usethinkscript.com/threads/...-target-profit-limit-orders.17196/post-134839

If not, what am I missing here?
 
Solution
Well, first, you should probably change all of the unnecessary inputs to def, leaving only important inputs like Price that you plan on changing often. Then you can just use a single conditional order, and combine all three scripts in the condition. Once it fills due to any of the three reasons, there's nothing left to cancel.

Plot Conditional = Study1(price = $$$, ... ) Or Study2() Or Study3();
Tell me more about the actual orders once submitted. Are they a mix of different types, like market and limit orders, or at different quantities?
 

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

Tell me more about the actual orders once submitted. Are they a mix of different types, like market and limit orders, or at different quantities?
to be more specific, here are the 3 scripts I want to use to trigger 3 OCO SELL orders:

# TARGET PRICE EXIT
################################################################
########### Variables ############
################################################################
input buyPrice = 100000.0;
input targetMultiplier = 3;
input length = 7;
#input averageType = AverageType.WILDERS;
input averageType = AverageType.SIMPLE;
input agg = aggregationPeriod.DAY;

################################################################
############ Defs ################
################################################################
def ATR = MovingAverage(averageType, trueRange(high(period = agg),close(period = agg),low(period = agg)), length);
def profitTarget = ATR * targetMultiplier;
def targetPrice = buyPrice + profitTarget;
plot Signal = if targetPrice >= close[1] then 1 else 0;
#Signal.Hide();
-------------------------------------------------------------------------------------------------------
Note that I have to enter the stock purchase price manually with "input buyPrice =" because I can't pick it up automatically from ToS.
-------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
# STOP LOSS EXIT
################################################################
########### Variables ############
################################################################
input buyPrice = 100000.0;
input stopLossMultiplier = 1.5;
input length = 7;
#input averageType = AverageType.WILDERS;
input averageType = AverageType.SIMPLE;
input agg = aggregationPeriod.DAY;

################################################################
############ Defs ################
################################################################
def ATR = MovingAverage(averageType, trueRange(high(period = agg),close(period = agg),low(period = agg)), length);
def stopLoss = ATR * stopLossMultiplier;
def targetPrice = buyPrice - stopLoss;
################################################################
############ Plot ################
################################################################
plot Signal = if targetPrice <= close[1] then 1 else 0;
#Signal.Hide();
-------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
# BLACK BAR EXIT
#
#REQUIREMENTS (NEW) - RSI Set to 7, WILDERS
# 5-25-24 Stoch Slow 14 and 3 SIMPLE
# MACD 12,26,9 EXPONENTIAL
#
#REQUIREMENTS (OLD) - RSI Set to 7, EXPONENTIAL
# Stoch Slow 14 and 3 WILDERS
# MACD 12,26,9 WEIGHTED

declare upper;

################################################################
########## RSI #########
################################################################
input lengthRSI = 7;
input price = close;
#input averageTypeRSI = AverageType.EXPONENTIAL;
input averageTypeRSI = AverageType.WILDERS;
def NetChgAvg = MovingAverage(averageTypeRSI, close - close[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(close - close[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

################################################################
########## Stochastic Slow #########
################################################################
input KPeriod = 14;
input DPeriod = 3;
#input averageTypeStoch = AverageType.WILDERS;
input averageTypeStoch = AverageType.SIMPLE;
#def SlowK = reference StochasticFull(80,20, KPeriod, DPeriod, high, low, close, 3, averageTypeStoch).FullK;
def SlowD = reference StochasticFull(80,20, KPeriod, DPeriod, high, low, close, 3, averageTypeStoch).FullD;

################################################################
########## MACD #########
################################################################
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
#input averageTypeMACD = AverageType.WEIGHTED;
input averageTypeMACD = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageTypeMACD, close, fastLength) - MovingAverage(averageTypeMACD, close, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

#################################################################
##### No Trend - Moving Sideways #########
#################################################################
def UpTrend = if RSI > 50 and SlowD > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowD < 50 and Value < Avg then 1 else 0;
#
plot NoTrend = if !UpTrend and !DownTrend then 1 else 0;
#NoTrend.Hide();
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
Here is a representative Edit Condition:

6xMLATu.jpeg


Here is a representative Order Rule:

5mlZWaS.jpeg


Here is my 1st order attempt:

6fiWGrA.jpeg


OOPS! Can't use GTC with market orders.

Here is my 2nd order attempt:

SMkmEmg.jpeg


Hmmm...can't use a MARKET order in an OCO group

That's when I posted to this forum.
 
Well, first, you should probably change all of the unnecessary inputs to def, leaving only important inputs like Price that you plan on changing often. Then you can just use a single conditional order, and combine all three scripts in the condition. Once it fills due to any of the three reasons, there's nothing left to cancel.

Plot Conditional = Study1(price = $$$, ... ) Or Study2() Or Study3();
 
Solution
Well, first, you should probably change all of the unnecessary inputs to def, leaving only important inputs like Price that you plan on changing often. Then you can just use a single conditional order, and combine all three scripts in the condition. Once it fills due to any of the three reasons, there's nothing left to cancel.

Plot Conditional = Study1(price = $$$, ... ) Or Study2() Or Study3();
Thanks so much for your input, Joshua. Here's how I implemented your suggestion:
-----------------------------------------------------------------------------------------------------------
script targetPrice {
input buyPrice = 0.0;
def targetMultiplier = 3;
def length = 7;
def averageType = AverageType.SIMPLE;
def agg = aggregationPeriod.DAY;
def ATR = MovingAverage(averageType, trueRange(high(period = agg),close(period = agg),low(period = agg)), length);
def profitTarget = ATR * targetMultiplier;
def targetPrice = buyPrice + profitTarget;
plot Signal = if targetPrice >= close[1] then 1 else 0;
Signal.Hide();
}

script stopLoss {
input buyPrice = 0.0;
def stopLossMultiplier = 1.5;
def length = 7;
def averageType = AverageType.SIMPLE;
def agg = aggregationPeriod.DAY;
def ATR = MovingAverage(averageType, trueRange(high(period = agg),close(period = agg),low(period = agg)), length);
def stopLoss = ATR * stopLossMultiplier;
def targetPrice = buyPrice - stopLoss;
plot Signal = if targetPrice <= close[1] then 1 else 0;
Signal.Hide();
}

script blackBar {
################################################################
########## RSI #########
################################################################
def lengthRSI = 7;
def price = close;
def averageTypeRSI = AverageType.WILDERS;
def NetChgAvg = MovingAverage(averageTypeRSI, close - close[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(close - close[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

################################################################
########## Stochastic Slow #########
################################################################
def KPeriod = 14;
def DPeriod = 3;
input averageTypeStoch = AverageType.SIMPLE;
def SlowD = reference StochasticFull(80,20, KPeriod, DPeriod, high, low, close, 3, averageTypeStoch).FullD;

################################################################
########## MACD #########
################################################################
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageTypeMACD, close, fastLength) - MovingAverage(averageTypeMACD, close, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

#################################################################
##### No Trend - Moving Sideways #########
#################################################################
def UpTrend = if RSI > 50 and SlowD > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowD < 50 and Value < Avg then 1 else 0;
#
plot Signal = if !UpTrend and !DownTrend then 1 else 0;
Signal.Hide();
}
plot Trigger = targetPrice(buyPrice = 0.0).Signal or stopLoss(buyPrice = 0.0).Signal or blackBar().Signal;

----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
If this looks ok, my only question is, where do I actually update the buyPrice variable before submitting this script?

Thanks again.
 
Thanks so much for your input, Joshua. Here's how I implemented your suggestion:
-----------------------------------------------------------------------------------------------------------
script targetPrice {
input buyPrice = 0.0;
def targetMultiplier = 3;
def length = 7;
def averageType = AverageType.SIMPLE;
def agg = aggregationPeriod.DAY;
def ATR = MovingAverage(averageType, trueRange(high(period = agg),close(period = agg),low(period = agg)), length);
def profitTarget = ATR * targetMultiplier;
def targetPrice = buyPrice + profitTarget;
plot Signal = if targetPrice >= close[1] then 1 else 0;
Signal.Hide();
}

script stopLoss {
input buyPrice = 0.0;
def stopLossMultiplier = 1.5;
def length = 7;
def averageType = AverageType.SIMPLE;
def agg = aggregationPeriod.DAY;
def ATR = MovingAverage(averageType, trueRange(high(period = agg),close(period = agg),low(period = agg)), length);
def stopLoss = ATR * stopLossMultiplier;
def targetPrice = buyPrice - stopLoss;
plot Signal = if targetPrice <= close[1] then 1 else 0;
Signal.Hide();
}

script blackBar {
################################################################
########## RSI #########
################################################################
def lengthRSI = 7;
def price = close;
def averageTypeRSI = AverageType.WILDERS;
def NetChgAvg = MovingAverage(averageTypeRSI, close - close[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(close - close[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

################################################################
########## Stochastic Slow #########
################################################################
def KPeriod = 14;
def DPeriod = 3;
input averageTypeStoch = AverageType.SIMPLE;
def SlowD = reference StochasticFull(80,20, KPeriod, DPeriod, high, low, close, 3, averageTypeStoch).FullD;

################################################################
########## MACD #########
################################################################
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageTypeMACD, close, fastLength) - MovingAverage(averageTypeMACD, close, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

#################################################################
##### No Trend - Moving Sideways #########
#################################################################
def UpTrend = if RSI > 50 and SlowD > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowD < 50 and Value < Avg then 1 else 0;
#
plot Signal = if !UpTrend and !DownTrend then 1 else 0;
Signal.Hide();
}
plot Trigger = targetPrice(buyPrice = 0.0).Signal or stopLoss(buyPrice = 0.0).Signal or blackBar().Signal;

----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
If this looks ok, my only question is, where do I actually update the buyPrice variable before submitting this script?

Thanks again.
Yes, original code was sloppy! Let's try this:

script TopBottomExits {
input buyPrice = 0.0;
def stopLossMultiplier = 1.5;
def targetMultiplier = 3;
def length = 7;
def averageType = AverageType.SIMPLE;
def agg = aggregationPeriod.DAY;
def ATR = MovingAverage(averageType, trueRange(high(period = agg),close(period = agg),low(period = agg)), length);
def profitTarget = ATR * targetMultiplier;
def stopLoss = ATR * stopLossMultiplier;
def targetPrice = buyPrice + profitTarget;
def stopLossPrice = buyPrice - stopLoss;
plot TakeProfit = if targetPrice >= close[1] then 1 else 0;
plot StopLossPlot = if stopLossPrice <= close[1] then 1 else 0;
StopLossPlot.Hide();
TakeProfit.Hide();
}

script blackBar {
################################################################
########## RSI #########
################################################################
def lengthRSI = 7;
def price = close;
def averageTypeRSI = AverageType.WILDERS;
def NetChgAvg = MovingAverage(averageTypeRSI, close - close[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(close - close[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

################################################################
########## Stochastic Slow #########
################################################################
def KPeriod = 14;
def DPeriod = 3;
input averageTypeStoch = AverageType.SIMPLE;
def SlowD = reference StochasticFull(80,20, KPeriod, DPeriod, high, low, close, 3, averageTypeStoch).FullD;

################################################################
########## MACD #########
################################################################
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.EXPONENTIAL;
def Value = MovingAverage(averageTypeMACD, close, fastLength) - MovingAverage(averageTypeMACD, close, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

#################################################################
##### No Trend - Moving Sideways #########
#################################################################
def UpTrend = if RSI > 50 and SlowD > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowD < 50 and Value < Avg then 1 else 0;
#
plot Signal = if !UpTrend and !DownTrend then 1 else 0;
Signal.Hide();
}
plot Trigger = TopBottomExits(buyPrice = 0.0).TakeProfit or TopBottomExits(buyPrice = 0.0).StopLossPlot or blackBar().Signal;
-------------------------------------------------------------------------------------------------------------------
 
With it all as Script{}s within a single study like that, you just add another input for the price, above all of the scripts. Then at the bottom, where you plot the trigger, you use that input's value as the BuyPrice parameter.

Then in the conditional order setup, you can actually reference the entire study as if it were a function, and put whatever price you want in as that parameter. It would go in here:

KzGgxhj.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
555 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