How to return the lowest or highest price based on the last 3 bars?

Hi friends,

Please if any kind souls could help or guide the following questions in bold?

# Using engulfing for Long and Short
input length = 20;
input trendSetup = 3;
def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax > BodyMax[1] and BodyMin < BodyMin[1];
def IsWhite = open < close;
def IsBlack = open > close;
def IsPrevDoji = IsDoji(length)[1];

plot longSignal = if IsDescending(close, trendSetup)[1] and (IsBlack[1] or IsPrevDoji) and
IsWhite and IsEngulfing then 1 else 0;

plot shortSignal = if IsAscending(close, trendSetup)[1] and (IsWhite[1] or IsPrevDoji) and IsBlack and IsEngulfing then 1 else 0;

def longStopLoss = lowest( entryPrice(),3 ) == 0; # Stoploss will be triggered based on lowest price of the last 3 bars from the entry price, am I coding this correctly?

def longProfit = longStopLoss*3; # How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.


def longExitSignal = if longStopLoss or longProfit then 1 else 0; # the condition will trigger whichever one come first

def longExitPrice = close;

def shortStopLoss = highest( entryPrice(),3 ) == 0; # Stoploss will be triggered based on highest price of the last 3 bars from the entry price, am I coding this correctly?

def shortProfit = shortStopLoss*3; # How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.


def shortExitSignal = if shortStopLoss or shortProfit then 1 else 0; # the condition will trigger whichever one come first

def shortExitPrice = close;

Many thanks in advance.

Cheers, Lui.
 
Solution
Hi friends,

Please if any kind souls could help or guide the following questions in bold?

see if this is close to what you want.
when there is a buy signal, it finds a new lowest and keeps it until the next buy signal.

Ruby:
# lowest3atbuy

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

# Using engulfing for Long and Short
input length = 20;
input trendSetup = 3;
def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax > BodyMax[1] and BodyMin < BodyMin[1];
def IsWhite = open < close;
def IsBlack = open > close;
def IsPrevDoji = IsDoji(length)[1];

plot longSignal = if IsDescending(close, trendSetup)[1] and (IsBlack[1] or IsPrevDoji) and
IsWhite and IsEngulfing then 1 else 0;

plot shortSignal = if...
Hi friends,

Please if any kind souls could help or guide the following questions in bold?

see if this is close to what you want.
when there is a buy signal, it finds a new lowest and keeps it until the next buy signal.

Ruby:
# lowest3atbuy

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

# Using engulfing for Long and Short
input length = 20;
input trendSetup = 3;
def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax > BodyMax[1] and BodyMin < BodyMin[1];
def IsWhite = open < close;
def IsBlack = open > close;
def IsPrevDoji = IsDoji(length)[1];

plot longSignal = if IsDescending(close, trendSetup)[1] and (IsBlack[1] or IsPrevDoji) and
IsWhite and IsEngulfing then 1 else 0;

plot shortSignal = if IsAscending(close, trendSetup)[1] and (IsWhite[1] or IsPrevDoji) and IsBlack and IsEngulfing then 1 else 0;



#def longStopLoss = lowest( entryPrice(),3 ) == 0;
# Stoploss will be triggered based on lowest price of the last 3 bars from the entry price, am I coding this correctly?
def longStopLoss = if bn == 1 then na
else if longsignal then lowest( low, 3 )
 else longStopLoss[1];


def longProfit = longStopLoss*3;
# How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.

def longExitSignal = if longStopLoss or longProfit then 1 else 0; # the condition will trigger whichever one come first

def longExitPrice = close;




#def shortStopLoss = highest( entryPrice(),3 ) == 0;
 # Stoploss will be triggered based on highest price of the last 3 bars from the entry price, am I coding this correctly?
def shortStopLoss = if bn == 1 then na
else if shortsignal then highest( high, 3 )
 else shortStopLoss[1];



def shortProfit = shortStopLoss*3; # How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.

def shortExitSignal = if shortStopLoss or shortProfit then 1 else 0; # the condition will trigger whichever one come first

def shortExitPrice = close;


plot zl = longStopLoss;
plot zs = shortStopLoss;

#
 
Solution
see if this is close to what you want.
when there is a buy signal, it finds a new lowest and keeps it until the next buy signal.

Ruby:
# lowest3atbuy

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

# Using engulfing for Long and Short
input length = 20;
input trendSetup = 3;
def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax > BodyMax[1] and BodyMin < BodyMin[1];
def IsWhite = open < close;
def IsBlack = open > close;
def IsPrevDoji = IsDoji(length)[1];

plot longSignal = if IsDescending(close, trendSetup)[1] and (IsBlack[1] or IsPrevDoji) and
IsWhite and IsEngulfing then 1 else 0;

plot shortSignal = if IsAscending(close, trendSetup)[1] and (IsWhite[1] or IsPrevDoji) and IsBlack and IsEngulfing then 1 else 0;



#def longStopLoss = lowest( entryPrice(),3 ) == 0;
# Stoploss will be triggered based on lowest price of the last 3 bars from the entry price, am I coding this correctly?
def longStopLoss = if bn == 1 then na
else if longsignal then lowest( low, 3 )
 else longStopLoss[1];


def longProfit = longStopLoss*3;
# How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.

def longExitSignal = if longStopLoss or longProfit then 1 else 0; # the condition will trigger whichever one come first

def longExitPrice = close;




#def shortStopLoss = highest( entryPrice(),3 ) == 0;
 # Stoploss will be triggered based on highest price of the last 3 bars from the entry price, am I coding this correctly?
def shortStopLoss = if bn == 1 then na
else if shortsignal then highest( high, 3 )
 else shortStopLoss[1];



def shortProfit = shortStopLoss*3; # How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.

def shortExitSignal = if shortStopLoss or shortProfit then 1 else 0; # the condition will trigger whichever one come first

def shortExitPrice = close;


plot zl = longStopLoss;
plot zs = shortStopLoss;

#
 
Thanks bro. But some how it does not open the trades?! Bro, please if you don't mind have a check as below:

# lowest3atbuy

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

# Using engulfing for Long and Short
input length = 20;
input trendSetup = 3;
def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax > BodyMax[1] and BodyMin < BodyMin[1];
def IsWhite = open < close;
def IsBlack = open > close;
def IsPrevDoji = IsDoji(length)[1];

plot longSignal = if IsDescending(close, trendSetup)[1] and (IsBlack[1] or IsPrevDoji) and
IsWhite and IsEngulfing then 1 else 0;

plot shortSignal = if IsAscending(close, trendSetup)[1] and (IsWhite[1] or IsPrevDoji) and IsBlack and IsEngulfing then 1 else 0;



#def longStopLoss = lowest( entryPrice(),3 ) == 0;
# Stoploss will be triggered based on lowest price of the last 3 bars from the entry price, am I coding this correctly?
def longStopLoss = if bn == 1 then na
else if longsignal then lowest( low, 3 )
else longStopLoss[1];


def longProfit = longStopLoss*3;
# How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.

def longExitSignal = if longStopLoss or longProfit then 1 else 0; # the condition will trigger whichever one come first

def longExitPrice = close;



#def shortStopLoss = highest( entryPrice(),3 ) == 0;
# Stoploss will be triggered based on highest price of the last 3 bars from the entry price, am I coding this correctly?
def shortStopLoss = if bn == 1 then na
else if shortsignal then highest( high, 3 )
else shortStopLoss[1];



def shortProfit = shortStopLoss*3; # How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.

def shortExitSignal = if shortStopLoss or shortProfit then 1 else 0; # the condition will trigger whichever one come first

def shortExitPrice = close;


plot zl = longStopLoss;
plot zs = shortStopLoss;

longSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
shortSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
longSignal.SetLineWeight(5);
shortSignal.SetLineWeight(5);
longSignal.SetDefaultColor(Color.GREEN);
shortSignal.SetDefaultColor(Color.RED);

AddOrder(OrderType.BUY_TO_OPEN, yes and longSignal, close, 100);
AddOrder(OrderType.SELL_TO_CLOSE, longExitSignal, longExitPrice, 100);

AddOrder(OrderType.SELL_TO_OPEN, no and shortSignal, close, 100);
AddOrder(OrderType.BUY_TO_CLOSE, shortExitSignal, shortExitPrice, 100);
 
Thanks bro. But some how it does not open the trades?! Bro, please if you don't mind have a check as below:

# lowest3atbuy

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

# Using engulfing for Long and Short
input length = 20;
input trendSetup = 3;
def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax > BodyMax[1] and BodyMin < BodyMin[1];
def IsWhite = open < close;
def IsBlack = open > close;
def IsPrevDoji = IsDoji(length)[1];

plot longSignal = if IsDescending(close, trendSetup)[1] and (IsBlack[1] or IsPrevDoji) and
IsWhite and IsEngulfing then 1 else 0;

plot shortSignal = if IsAscending(close, trendSetup)[1] and (IsWhite[1] or IsPrevDoji) and IsBlack and IsEngulfing then 1 else 0;



#def longStopLoss = lowest( entryPrice(),3 ) == 0;
# Stoploss will be triggered based on lowest price of the last 3 bars from the entry price, am I coding this correctly?
def longStopLoss = if bn == 1 then na
else if longsignal then lowest( low, 3 )
else longStopLoss[1];


def longProfit = longStopLoss*3;
# How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.

def longExitSignal = if longStopLoss or longProfit then 1 else 0; # the condition will trigger whichever one come first

def longExitPrice = close;



#def shortStopLoss = highest( entryPrice(),3 ) == 0;
# Stoploss will be triggered based on highest price of the last 3 bars from the entry price, am I coding this correctly?
def shortStopLoss = if bn == 1 then na
else if shortsignal then highest( high, 3 )
else shortStopLoss[1];



def shortProfit = shortStopLoss*3; # How can I record the stop loss value(Even though it is not triggered) and return the profit target price? This allows the risk/reward ratio of 1:3.

def shortExitSignal = if shortStopLoss or shortProfit then 1 else 0; # the condition will trigger whichever one come first

def shortExitPrice = close;


plot zl = longStopLoss;
plot zs = shortStopLoss;

longSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
shortSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
longSignal.SetLineWeight(5);
shortSignal.SetLineWeight(5);
longSignal.SetDefaultColor(Color.GREEN);
shortSignal.SetDefaultColor(Color.RED);

AddOrder(OrderType.BUY_TO_OPEN, yes and longSignal, close, 100);
AddOrder(OrderType.SELL_TO_CLOSE, longExitSignal, longExitPrice, 100);

AddOrder(OrderType.SELL_TO_OPEN, no and shortSignal, close, 100);
AddOrder(OrderType.BUY_TO_CLOSE, shortExitSignal, shortExitPrice, 100);
 

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