How can I use (Open + Close)/2 or "MidBody()" as the price input?

rottentrade

Member
It seems like the price input is limited to reserved terms like close, open, high, low, hl2, hlc3, ohlc4, etc.

But it doesn't allow (Open + Close)/2 or MidBody()...or something like (Open+High+Low)/3.

Is there a way to assign price input to something other than the default.
 
Solution
It seems like the price input is limited to reserved terms like close, open, high, low, hl2, hlc3, ohlc4, etc.

But it doesn't allow (Open + Close)/2 or MidBody()...or something like (Open+High+Low)/3.

Is there a way to assign price input to something other than the default.

You can make your own input for choices. Here is an input pricex which lists multiple price choices. A switch statement then include all of those choices (mandatory). You can add more if you want.

You can use price in your code based upon the choice made. Please note that parameters such as those with math symbols must be in quotes.

Ruby:
input pricex = {default close, open, high, low, hl2, ohlc4, "open+close/2", "open+high+low/3"};

def price;
switch...
It seems like the price input is limited to reserved terms like close, open, high, low, hl2, hlc3, ohlc4, etc.

But it doesn't allow (Open + Close)/2 or MidBody()...or something like (Open+High+Low)/3.

Is there a way to assign price input to something other than the default.

You can make your own input for choices. Here is an input pricex which lists multiple price choices. A switch statement then include all of those choices (mandatory). You can add more if you want.

You can use price in your code based upon the choice made. Please note that parameters such as those with math symbols must be in quotes.

Ruby:
input pricex = {default close, open, high, low, hl2, ohlc4, "open+close/2", "open+high+low/3"};

def price;
switch (pricex) {
case close:
    price = close;
case open:
    price = open;
case high:
    price = high;
case low:
    price = low;
case hl2:
    price = hl2;
case ohlc4:
    price = ohlc4;
case "open+close/2":
    price = (open + close) / 2;
case "open+high+low/3":
    price = (open + high + low) / 3;
}
AddLabel(1, price);
 
Solution
You can make your own input for choices. Here is an input pricex which lists multiple price choices. A switch statement then include all of those choices (mandatory). You can add more if you want.
Excellent! Exactly what I want. Thank you.

One more question please. I want to use a different price input for 2 different indicators, for example, "close" for MovingAverage1 and "o+H+L/2" for MovingAverage2. Can I combine two price inputs into one switch statement or do I need create two switch statements like below? Is there a way to combine the two?

Ruby:
input pricema1 = {default close, open, high, low, hl2, ohlc4, "open+close/2", "open+high+low/3"};
input pricema2 = {default close, open, high, low, hl2, ohlc4, "open+close/2", "open+high+low/3"};

def price1;
switch (pricema1) {
case close:
    price1 = close;
case open:
    price1 = open;
case high:
    price1 = high;
case low:
    price1 = low;
case hl2:
    price1 = hl2;
case ohlc4:
    price1 = ohlc4;
case "open+close/2":
    price1 = (open + close) / 2;
case "open+high+low/3":
    price1 = (open + high + low) / 3;
}

def price2;
switch (pricema2) {
case close:
    price2 = close;
case open:
    price2 = open;
case high:
    price2 = high;
case low:
    price2 = low;
case hl2:
    price2 = hl2;
case ohlc4:
    price2 = ohlc4;
case "open+close/2":
    price2 = (open + close) / 2;
case "open+high+low/3":
    price2 = (open + high + low) / 3;
}


plot ma1 = ExpAverage( price1, 10 );
plot ma2 = ExpAverage( price2, 20 );
 
Excellent! Exactly what I want. Thank you.

One more question please. I want to use a different price input for 2 different indicators, for example, "close" for MovingAverage1 and "o+H+L/2" for MovingAverage2. Can I combine two price inputs into one switch statement or do I need create two switch statements like below? Is there a way to combine the two?

Ruby:
input pricema1 = {default close, open, high, low, hl2, ohlc4, "open+close/2", "open+high+low/3"};
input pricema2 = {default close, open, high, low, hl2, ohlc4, "open+close/2", "open+high+low/3"};

def price1;
switch (pricema1) {
case close:
    price1 = close;
case open:
    price1 = open;
case high:
    price1 = high;
case low:
    price1 = low;
case hl2:
    price1 = hl2;
case ohlc4:
    price1 = ohlc4;
case "open+close/2":
    price1 = (open + close) / 2;
case "open+high+low/3":
    price1 = (open + high + low) / 3;
}

def price2;
switch (pricema2) {
case close:
    price2 = close;
case open:
    price2 = open;
case high:
    price2 = high;
case low:
    price2 = low;
case hl2:
    price2 = hl2;
case ohlc4:
    price2 = ohlc4;
case "open+close/2":
    price2 = (open + close) / 2;
case "open+high+low/3":
    price2 = (open + high + low) / 3;
}


plot ma1 = ExpAverage( price1, 10 );
plot ma2 = ExpAverage( price2, 20 );

I would just use one and use the hybrid price1 from it for the first ema. Then just use close as part of the ema price2.

Another way to do this now that I see how you plan to use this is to make a default template that is part of any chart where you are using different price types. It would be something like this.

def o = open;
def c = close;
def h = high;
def l = low;
def oc2 = (o+c)/2;
def ohl3 = (o+h+l)/3;
etc etc

Or use what you have, but that will be rather cumbersome.
 
Another way to do this now that I see how you plan to use this is to make a default template that is part of any chart where you are using different price types. It would be something like this.
Thanks for the reply. My code has become really bloated lately and it seems to be a drag on the memory. So I would like get rid of any redundant codes.

With that said, I'm constantly testing different parameters, so I think it's more efficient to just create different price inputs. That way, I can just modify those parameters inside the Edit Strategy window.
 

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