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.
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.
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");