How to Submit Sell_To_Close based Buy_To_Open order price?

shondahector_2023

New member
VIP
Is there a way to Submit Sell_To_Close when the price reaches 5 points after whatever the price was at the Buy_To_Open order?

My Code:

Code:
# Evaluate 2 min over 5 day period
# S.Hector edited previous code 08-15-2024
input price = close;
input length = 15;
input wide = 10;
input narrow = 2;
input averageType = AverageType.SIMPLE;
input mode = {default "Trend Following", "Reversal"};


plot Avg = MovingAverage(averageType, price, length);
plot Avg20 = MovingAverage(averageType, price, 20);
plot Avg200 = MovingAverage(averageType, price, 200);

#input past_bars = 1;
#def hix = Highest(high[1], past_bars);
#def lox = lowest(low[1], past_bars);
#def rngx = hix - lox;

# Big Candles


input bigbarHeight = 5;


def bearBar = close < open and (open - close) >= bigbarHeight;
def bullBar = close > open and (close - open) >= bigbarHeight;


plot upCandle= bullBar;
upCandle.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upCandle.SetLineWeight(2);
upCandle.AssignValueColor(Color.GREEN);

plot downCandle = bearBar;
downCandle.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downCandle.SetLineWeight(2);
downCandle.AssignValueColor(Color.RED);

Avg.SetDefaultColor(GetColor(0));

#def crossAbove = price crosses above Avg20;
def crossBelow = price crosses below Avg20;

def buy;
def sell;

def wide_state = absvalue(Avg200 - Avg20) > wide; #Note this doesn't work!
def wide_state2 = (MovingAverage(averageType, price, 20) - MovingAverage(averageType, price, 200))> wide;


#def MktHours = SecondsFromTime(930) > 0 and SecondsFromTime(2300) <= 3600;

switch (mode) {
case  "Trend Following":
    buy = price > MovingAverage(averageType, price, 20) and wide_state2 and bullBar;
    sell =price >=MovingAverage(averageType, price, 20) + 5 + bigbarHeight ; #This doesn't give me what I was hoping for

case  "Reversal":
    sell = wide_state and price < Avg20;
    buy = price<Avg20 and wide_state;
}

    AddOrder(OrderType.BUY_TO_OPEN, buy, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "MAS_BuyOpen");
    AddOrder(OrderType.SELL_TO_CLOSE, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "MAS_SellClose");



#These alerts are working 8-28-2023
Alert(buy, "Buy Order", Alert.ONCE, Sound.Bell);
Alert(sell, "Sell Order", Alert.ONCE, Sound.Bell);
 
Last edited by a moderator:
Solution
Is there a way to Submit Sell_To_Close when the price reaches 5 points after whatever the price was at the Buy_To_Open order?

My Code:

Code:
# Evaluate 2 min over 5 day period
# S.Hector edited previous code 08-15-2024
input price = close;
input length = 15;
input wide = 10;
input narrow = 2;
input averageType = AverageType.SIMPLE;
input mode = {default "Trend Following", "Reversal"};


plot Avg = MovingAverage(averageType, price, length);
plot Avg20 = MovingAverage(averageType, price, 20);
plot Avg200 = MovingAverage(averageType, price, 200);

#input past_bars = 1;
#def hix = Highest(high[1], past_bars);
#def lox = lowest(low[1], past_bars);
#def rngx = hix - lox;

# Big Candles


input bigbarHeight = 5;


def bearBar = close <...
Is there a way to Submit Sell_To_Close when the price reaches 5 points after whatever the price was at the Buy_To_Open order?

My Code:

Code:
# Evaluate 2 min over 5 day period
# S.Hector edited previous code 08-15-2024
input price = close;
input length = 15;
input wide = 10;
input narrow = 2;
input averageType = AverageType.SIMPLE;
input mode = {default "Trend Following", "Reversal"};


plot Avg = MovingAverage(averageType, price, length);
plot Avg20 = MovingAverage(averageType, price, 20);
plot Avg200 = MovingAverage(averageType, price, 200);

#input past_bars = 1;
#def hix = Highest(high[1], past_bars);
#def lox = lowest(low[1], past_bars);
#def rngx = hix - lox;

# Big Candles


input bigbarHeight = 5;


def bearBar = close < open and (open - close) >= bigbarHeight;
def bullBar = close > open and (close - open) >= bigbarHeight;


plot upCandle= bullBar;
upCandle.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upCandle.SetLineWeight(2);
upCandle.AssignValueColor(Color.GREEN);

plot downCandle = bearBar;
downCandle.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downCandle.SetLineWeight(2);
downCandle.AssignValueColor(Color.RED);

Avg.SetDefaultColor(GetColor(0));

#def crossAbove = price crosses above Avg20;
def crossBelow = price crosses below Avg20;

def buy;
def sell;

def wide_state = absvalue(Avg200 - Avg20) > wide; #Note this doesn't work!
def wide_state2 = (MovingAverage(averageType, price, 20) - MovingAverage(averageType, price, 200))> wide;


#def MktHours = SecondsFromTime(930) > 0 and SecondsFromTime(2300) <= 3600;

switch (mode) {
case  "Trend Following":
    buy = price > MovingAverage(averageType, price, 20) and wide_state2 and bullBar;
    sell =price >=MovingAverage(averageType, price, 20) + 5 + bigbarHeight ; #This doesn't give me what I was hoping for

case  "Reversal":
    sell = wide_state and price < Avg20;
    buy = price<Avg20 and wide_state;
}

    AddOrder(OrderType.BUY_TO_OPEN, buy, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "MAS_BuyOpen");
    AddOrder(OrderType.SELL_TO_CLOSE, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "MAS_SellClose");



#These alerts are working 8-28-2023
Alert(buy, "Buy Order", Alert.ONCE, Sound.Bell);
Alert(sell, "Sell Order", Alert.ONCE, Sound.Bell);

this is a strategy

here is something to experiment with. i didn't change your logic. its a little messy
i changed some values , so i could use SPY and have buy signals. ( your numbers were huge and barley had signals even with /ES)

added sell level offsets,
input sell_gain = 4.0;
input sell_loss = -3.0; notice the negative sign


Code:
#strat_sell_at_buyoffset

#https://usethinkscript.com/threads/how-to-submit-sell_to_close-based-buy_to_open-order-price.19446/
#How to Submit Sell_To_Close based Buy_To_Open order price?

# change values to work with SPY

def na = double.nan;
def bn = barnumber();

input sell_gain = 4.0;
input sell_loss = -3.0;

input price = close;
input length = 15;
#input wide = 10;
input wide = 0.7;
input narrow = 2;
input averageType = AverageType.SIMPLE;
input mode = {default "Trend Following", "Reversal"};


plot Avg = MovingAverage(averageType, price, length);
plot Avg20 = MovingAverage(averageType, price, 20);
plot Avg200 = MovingAverage(averageType, price, 200);

#input past_bars = 1;
#def hix = Highest(high[1], past_bars);
#def lox = lowest(low[1], past_bars);
#def rngx = hix - lox;

# Big Candles
#input bigbarHeight = 5;
# chg and use SPY for testing
input bigbarHeight = 1.5;

def bearBar = close < open and (open - close) >= bigbarHeight;
def bullBar = close > open and (close - open) >= bigbarHeight;


plot upCandle = bullBar;
upCandle.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
upCandle.SetLineWeight(2);
upCandle.AssignValueColor(Color.GREEN);

plot downCandle = bearBar;
downCandle.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
downCandle.SetLineWeight(2);
downCandle.AssignValueColor(Color.RED);

Avg.SetDefaultColor(GetColor(0));

#def crossAbove = price crosses above Avg20;
def crossBelow = price crosses below Avg20;


def wide_state = absvalue(Avg200 - Avg20) > wide; #Note this doesn't work!
def wide_state2 = (Avg20 - Avg200) > wide;

def buy;
#def sell;
#def MktHours = SecondsFromTime(930) > 0 and SecondsFromTime(2300) <= 3600;
switch (mode) {
case  "Trend Following":
    buy = price > Avg20 and wide_state2 and bullBar;
#    sell = price >= (Avg20 + 5 + bigbarHeight) ; #This doesn't give me what I was hoping for
case  "Reversal":
#    sell = wide_state and price < Avg20;
    buy = wide_state and price < Avg20;
}


def buyprice = if bn == 1 then 0
 else if buy then close
 else buyprice[1];

def sell = if bn == 1 then 0
 else if close > (buyprice + sell_gain) then 1
 else if close < (buyprice + sell_loss) then -1
 else 0;


plot z = if buyprice > 0 then buyprice else na;
z.setdefaultcolor(color.yellow);


AddOrder(OrderType.BUY_TO_OPEN, buy, tickcolor = GetColor(2), arrowcolor = color.green, name = "MAS_BuyOpen");

# sell at a profit
AddOrder(OrderType.SELL_TO_CLOSE, sell == 1, tickcolor = GetColor(1), arrowcolor = color.green, name = "MAS_SellClose");
# sell at a loss
AddOrder(OrderType.SELL_TO_CLOSE, sell == -1, tickcolor = GetColor(1), arrowcolor = color.red, name = "MAS_SellClose");


#These alerts are working 8-28-2023
Alert(buy, "Buy Order", Alert.ONCE, Sound.Bell);
Alert(sell, "Sell Order", Alert.ONCE, Sound.Bell);


input test1_buy = no;
addverticalline(test1_buy and buy, "buy", color.green);
#


here is something similar, that draws a stopline
https://usethinkscript.com/threads/trailing-stop-loss-help-needed.16492/#post-133724
 
Solution

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
444 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