OPEN-SOURCE SCRIPT

Reversal closing price

Cập nhật
The reversal closing price (RCP) is a candlestick pattern which follows two simples rules:

  • the low price of current candle needs to be lower than the low price of the last 2 candles
  • the closing price of current candle needes to be higher than the closing price of the last candle


This generates a signal for a long position. For a short position, the conditions are inverted:

  • the high price of current candle needs to be higher than the high price of the last 2 candles
  • the closing price of current candle needes to be lower than the closing price of the last candle


Since RCP is a trend follower indicator, the strategy is programmed in such a way that long positions are only placed if the short period EMA is above the long period EMA, and short positions are only placed if the short EMA is bellow the long EMA. Both periods are configurable, and should be ajusted for each asset.

This strategy uses a fixed stop loss and take profit, and the it's ratio is configurable. The stop price is one tick lower than the lowest price of X candles prior to the order execution for long positions, while in short positions it's one tick higher than the higher price. The amount of candles to lookback (X) is configurable. Both stop and take profit prices are displayed, the first as a red line, and the second as a green line.

This is the setting that I've found to work best with SPX, but you may find a better setting. While the RCP is universal, it's placement depends on the trend and it's strenght, something that is very heterogeneous among assets.

I really wish that I was able to place images, but I don't have PRO, so text will have to do.

This strategy was designed by Alexandre Wolwacz, a.k.a. Stormer.
Phát hành các Ghi chú
This is an update to include different types of moving averages. You can choose if you want an EMA, a SMA, a HMA or a WMA. If you want another type of MA, please comment and I can add it.

I've also added an option for the orders to take place regardless of the trend. It is disabled by default because in 99% of the time you will want to trade in favor of the trend, not against it.

There are also two settings I forgot to mention on the original post:

If an RCP is generated, but the trade is not triggered, the order price will be retraced to the new candle price. This is done until the trade is taken. So if the RCP gives a signal at 10.61, but the high of the next candle is 10.58, the order's buy price will be retraced to 10.59 (remember, the high of the candle + 1 tick). If the max of the next candle is 10.56, the price will be retraced to 10.57. And so on, until the order is triggered. This can be disabled in the "Allow price retracing" check. Note that the same applies for short positions, but inverted.

If you are in a long position, but the moving average crosses downwards, signalling the beggining of a down trend, you can exit the current position if the "Close if EMA crosses in oposite direction" is checked. The same applies for short positions, but inverted.
Phát hành các Ghi chú
This is a very big update.

Firstly the strategy now calculates a fixed amount of risk. To get it working I had to do what we in Brazil call a "gambiarra". The first order is to get how many money are you willing to lose, and that money is going to be risk for every single order in the future. In other words, you will always gain the same amount, and always lose the same amount, the strategy will calculate the size of the order for that to happen, thus making a positive mathmatical expectation actually possible.

The stop loss orders are working better, since the calc_on_order_fill was enabled, thus allowing them to end on the same candle they opened. But as I said on the original post, you need to mind the gap. The stop loss won't save you if the gap is big.

Unfortunatelly, I couldn't get comments in the exit positions working, because Pine Script uses the last candle to calculate prices, not the one that actually closes the order. Maybe there is a way to do it, but I spent so many time trying to fix the stop loss orders that it just isn't worth it simply for comments. Luckly it isn't a big problem, since you can see the stop and take profit prices. Maybe in a future update the Trading View team is going to implement functions that should be basic. Really, you can't even place two exit orders at the same time, nor place one order with a fixed trigger price, a stop loss price and a take profit price. Sorry for the rant, but it just boggles me that Pine Script is in version 4 and it doesn't have a function that Backtr*der has, which is both free and open source.
Phát hành các Ghi chú
Fixed negative take profit values for short positions, something that happened in a stock like OIBR3. Also added an option to filter the trend with the stochastic oscillator, which is disabled by default.
Phát hành các Ghi chú
Fixed a price retracing bug.
Phát hành các Ghi chú
Added an option to place the stop loss at a bottom or a top. A bottom happens when the low of a candle is lower than the adjacent lows, so it makes a V shape. Similarly, a top is an ^ shape, with the high of the candle being higher than the it's adjacent highs. This is affected by the stop lookback parameter, it will take the lowest or highest price of the lookback period.

While this option is not necessary in a daily or weekly graph, it may be interesting in an intraday graph, since they have a low liquidity and the chance of false stops being higher.

Also fixed a silly bug that caused orders to be canceled for no reason because of price retracing.
Phát hành các Ghi chú
Now the bottom/top stoploss is actually working. I tought that by using a simple [] operator I could get the older values, but the [] operator is for bars, so I had to do another gambiarra and use arrays to get the prices instead.

It's kinda ironic that this is strategy is so simple yet the code demands so much to get it working.
Phát hành các Ghi chú
Added KAMA as an option for the MAs and now you can retrace the stop price if a new signal is given in the same direction. Orders are also canceled if you disabled price retracing, something that should have happened for a long time.
Phát hành các Ghi chú
Included different types of MA trends, as well as oscillators.
Phát hành các Ghi chú
Made some fixes to the take profit price.
Phát hành các Ghi chú
I made use of the new group and inline features in the input functions, hopefully it is less of a mess now.

I also changed how the stop and take profit works, so now it won't close the position at the high or low of a candle in the same candle it was opened, making the results more accurate. I wish I could use the := operator on a variable list, but PineScript doesn't allow me to do that (yet), which made the code WET in some parts. At least it still works.
Phát hành các Ghi chú
I made the strategy more accurate and faster.
Phát hành các Ghi chú
Added the ability of having two targets.
Candlestick Analysiscandlestickpatternobjectiverewardrisksimplestormerstrategytrendfollowing

Mã nguồn mở

Theo tinh thần TradingView thực sự, tác giả của tập lệnh này đã xuất bản dưới dạng nguồn mở để các nhà giao dịch có thể hiểu và xác minh. Chúc mừng tác giả! Bạn có thể sử dụng miễn phí. Tuy nhiên, bạn cần sử dụng lại mã này theo Quy tắc nội bộ. Bạn có thể yêu thích nó để sử dụng nó trên biểu đồ.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?

Thông báo miễn trừ trách nhiệm