ROC Strategy - need to use the closing value of each bar

DudeTrader

New member
Plus
I've been trying to use Rate of Change (ROC) to trigger a buy/sell condition, in a trend following way. I ONLY want to pass through the CLOSE value of the bar. I cannot get my script to act as I intend.

I've attached a chart - the Buy/Sell orders hit at the beginning of the new bar. The "old" bar is where the condition happens. I have highlighted two orders that worked correctly with a WHITE vertical line. The ROC was positive and a buy order was produced. There are three orders that are incorrect with PURPLE lines. The easiest one to spot is the long downslope in ROC from late Tuesday. A sell order should have triggered many bars earlier.

My suspicion is that once a bar opens, and the trend is not perfect for that hour (in this example the ROC goes up for a while before resuming downward during the bar), TOS treats that as an upward ROC. Therefore I would like to have the code ONLY use the closing value of a bar.

Also, it would be super cool if someone could help me code it so that you can select how the script works (so that my first line of code actually does something to the ROC values when you select other options). And also so that one could change the ROC values via Edit Properties rather than having to edit the script.

ROC problem1.jpg


Code:
input price = close;

def ROCup;
def ROCdown;
ROCup = RateOfChange("length" = 6) is greater than RateOfChange()[1];
ROCdown = RateOfChange("length" = 6) is less than RateOfChange()[1];

def buy;
def sell;

buy = ROCup;
sell = ROCdown;

AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(2), arrowcolor = GetColor(6), name = "ROCup");
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(1), arrowcolor = GetColor(5), name = "ROCdown");
 
OK, I figured out one problem - I needed the "length=6" inside the second ROC parentheses:

ROCup = RateOfChange("length" = 6) is greater than RateOfChange("length" = 6)[1];
ROCdown = RateOfChange("length" = 6) is less than RateOfChange("length" = 6))[1];

But I would still love it if I could input that length value, and again, drop down choices for handling the Type of ROC value (Open, Close, etc).
 
From your description, it seems you want customizable inputs. If that's the case, here is the method to implement it.

Python:
input rocPeriod = 6; # Default ROC period
input triggerMethod = {default "Simple Trend", "Enhanced Trend"}; # Selection for how to interpret ROC

Python:
switch (triggerMethod) {
    case "Simple Trend":
        # Basic condition for Simple Trend: Buy if ROC is positive, Sell if negative
        buySignal = rocValue > 0;
        sellSignal = rocValue < 0;

    case "Enhanced Trend":
        # Enhanced logic for trend filtering (optional example)
        # Buy if ROC is positive and moving up; Sell if ROC is negative and moving down
        buySignal = rocValue > 0 and rocValue > rocValue[1];
        sellSignal = rocValue < 0 and rocValue < rocValue[1];
}
 
Thanks! This is exactly what I needed to select the ROC length - here is my updated Strategy code:
Code:
input rocPeriod = 6; # Default ROC period

def ROCup;
def ROCdown;
ROCup = RateOfChange(rocPeriod) is greater than RateOfChange(rocPeriod)[1];
ROCdown = RateOfChange(rocPeriod) is less than RateOfChange(rocPeriod)[1];

def buy;
def sell;

buy = ROCup;
sell = ROCdown;

AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(2), arrowcolor = GetColor(6), name = "ROCup");
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(1), arrowcolor = GetColor(5), name = "ROCdown");
 
Thanks! This is exactly what I needed to select the ROC length - here is my updated Strategy code:
Code:
input rocPeriod = 6; # Default ROC period

def ROCup;
def ROCdown;
ROCup = RateOfChange(rocPeriod) is greater than RateOfChange(rocPeriod)[1];
ROCdown = RateOfChange(rocPeriod) is less than RateOfChange(rocPeriod)[1];

def buy;
def sell;

buy = ROCup;
sell = ROCdown;

AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(2), arrowcolor = GetColor(6), name = "ROCup");
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(1), arrowcolor = GetColor(5), name = "ROCdown");
Hi @DudeTrader , good concept - What is the best time frame for this study ? how is your success so far?
 
Hi @DudeTrader , good concept - What is the best time frame for this study ? how is your success so far?
Nothing stands out so far, it was mostly an exercise in coding, as I'd only done some modifications on code up until now. It seems like with any oscillator, that it works well when the market moves, but chops you to pieces when there isn't much movement...
 

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