Calculating Diagonal Trend Lines Using Fractals and Trigonometry

Forums ProRealTime English forum ProOrder support Calculating Diagonal Trend Lines Using Fractals and Trigonometry

Viewing 15 posts - 1 through 15 (of 29 total)
  • #45716

    Good Day Everyone

    I am working on a trend following breakout strategy that relies on me being able to determine when a diagonal trend line has been broken based on a couple of conditions. As soon as I am finished I will share the strategy here but for now, I need some help determining a crucial ingredient;

    How can I determine the width of a candle stick in relation to height? Currently, the width of a candle stick only represent a time-unit, but I need to assign an actual point (or pip) value for the width of a candle stick to using the trigonometry formula. Here are 2 viewpoints I currently consider:

    1. It does not matter as long as they are all the same
    2. Use a percentage of an ATR[x] value calculated back from the first bar since the trade was opened.

    I would appreciate any suggestions. @Nicolas I believe having a piece of code in the library that can be used to determine when a diagonal trendline is violated will be very helpful.

    The basic idea is to calculate the diagonal trend line projecting it from the last fractal onto the next high/low that produces the widest trend line angle. The trend line is then updated using the next high/low producing a wider angle than the last, but only as long as the high/low has a close in the direction of the trend or if the close following the new high/low is higher/lower than close[1]. The trend line is considered broken when a close is outside of the calculated trend line.

    #45729

    Okay, so I just realised it probably won’t matter as the width will be the number of bars since the last fractal. I just have to calculate the height and then use the formula Tan(bars/height) to get the angle.

    #45731

    This was what I was thinking. (y)

    #45732

    Okay so here is the code, unfortunately, it is not opening trades which means I must be missing something? @Nicolas, basically here is where I will need help with some debugging.

     

    #45794

    Okay, so I finally figured out the problem.

    Had to rewrite big parts of the code, but the main issue was basically the fact that I did not properly identify the active trend.

    The strategy performs surprisingly well. I tested it on the EUR/USD 1Hr (see attached screenshot) but it should work on other markets and time frames as well.

    The strategy has been submitted to the PRT library. Will post the link here as soon as it is posted.

    Regards,

    1 user thanked author for this post.
    #45805

    Sorry, I’m a bit late here. Very fine and clever @juanj, well done!  I reviewed your post and your strategy is now available in the library for everyone to test and improve: trend lines breakout strategy

    #45806

    We seem to be working on the same projects: https://www.prorealcode.com/topic/wings-trend-lines-indicator/

    #45906
    Huw

    Hi Juan,

    Would it be possible to code this to trade channels instead but reverse if the price goes beyond a trend line to also chase the breakout?

    I note the CAC 40 has been in a nice down channel but may break to the up side at some point for example.

    #45920

    Hi Huw, a channel is but an area of support (for long positions) or resistance (for short positions) that is respected by the price. The strategy in effect actually aims to do exactly as you are requesting. The problem, however, is that the way in which the diagonal line is positioned in the strategy is possibly not as optimal as we I initially thought. I will attempt to improve on it to better trade with the trend. Maybe @Nicolas or @Wing or @Francesco78 can assist in graphing each revision of the lines as to make it clear how they are actually plotted.

     

    #45951

    Okay, so I have been going over the code carefully and made some changes that I believe is more consistent with what I have been trying to achieve. However, in doing so I think I broke it as it stops opening trades a few trades in.

     

    #45956

    Okay, I have been crawling deeper and deeper into the rabbit hole and found more oversights in my code.

    Amongst other things I didn’t reset the Trendbreak indicator to zero if the breakout was invalid and also I didn’t cater for close = open scenarios.

    However, even after fixing the above the strategy is still only opening a few trades and then stopping. Results based on the few opened trades looks very promising though (attached)!


    @Nicolas
    I really need some expert help fixing this as I believe there are major potential waiting to be unlocked here.

     

    #45959

    Haha okay, I think I found the problem. The stop order level is constantly being updated. Talk about moving the goalposts!

    Will be back with the fix soon…

    #45969

    Finally done, realised I also didn’t re-instate the previous trend if the buy/sell order were never met and the trend continued on in the initial direction. But I believe all logical issues have now been addressed. I also added some heuristics to optimize the ATR period used to calculate the buy/sell order level. So here is my final version, which for some frustrating reason doesn’t perform as well as the original version (I spent way too much time on this):

     

    2 users thanked author for this post.
    #46150

    Well done, been quite busy here and there and also a lot IRL.. Sometimes buggy codes lead to better results than final and neat ones .. that’s programmer’s life 🙁

    Did you think of a clean way to draw your trend lines or even an oscillator of their difference between price and them? Should help a lot visualize how the things goes with the strategy.

    #46158

    I’m not too well versed with coding indicators as I try to not spend too much time in front of a screen trading. My focus is mainly on automation.

    I have spent allot of time lately designing and debugging this code and sharing it with the PRC community, so it would be nice if someone else can add to this by designing an indicator based on this concept.

Viewing 15 posts - 1 through 15 (of 29 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login