Count the number of times Mark Price crosses Open within a 1 minute bar

JoeBloke

New member
Hi All,

I need help in writing the code to count the number of times the Mark Price has crossed the Open Price within a 1 minute bar. I want to use the result in my Buy Order condition to decide if the order should be placed or not.

Example, I have a Buy order lined up to be submitted at 9:31:00 AM. This order will have a Study Condition attached to it to submit only if the price action of the 9:30 AM bar wasn't too "choppy" i.e. wasn't alternating between red and green many times.

Below is the code I wrote to check for the choppiness of the 9:30 AM 1 minute bar. However, the "crossCounter" value isn't giving me the correct result. Can anyone let me know how to fix this?
Ruby:
def bidPrice = close(priceType = PriceType.BID);
def askPrice = close(priceType = PriceType.ASK);
def mark = (bidPrice + askPrice) / 2;
def crossHappened =  mark crosses  open;
def crossCounter = if IsNaN(crossCounter[1]) and crossHappened then 1 else if crossHappened then crossCounter[1]+1 else crossCounter[1];
def placeOrder = if crossCounter[1] < 3 then Yes else No;
plot _data_ = placeOrder;
The idea is to submit the Buy order at 9:31:00 AM only if the 9:30 AM bar (during its formation) didn't alternate between red and green more than 3 times.

Thanks!
 
Hi All,

I need help in writing the code to count the number of times the Mark Price has crossed the Open Price within a 1 minute bar. I want to use the result in my Buy Order condition to decide if the order should be placed or not.

Example, I have a Buy order lined up to be submitted at 9:31:00 AM. This order will have a Study Condition attached to it to submit only if the price action of the 9:30 AM bar wasn't too "choppy" i.e. wasn't alternating between red and green many times.

Below is the code I wrote to check for the choppiness of the 9:30 AM 1 minute bar. However, the "crossCounter" value isn't giving me the correct result. Can anyone let me know how to fix this?
Ruby:
def bidPrice = close(priceType = PriceType.BID);
def askPrice = close(priceType = PriceType.ASK);
def mark = (bidPrice + askPrice) / 2;
def crossHappened =  mark crosses  open;
def crossCounter = if IsNaN(crossCounter[1]) and crossHappened then 1 else if crossHappened then crossCounter[1]+1 else crossCounter[1];
def placeOrder = if crossCounter[1] < 3 then Yes else No;
plot _data_ = placeOrder;
The idea is to submit the Buy order at 9:31:00 AM only if the 9:30 AM bar (during its formation) didn't alternate between red and green more than 3 times.

Thanks!
can't retain data during a candle. only values from the close.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
230 Online
Create Post

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