Results 1 to 20 of 34

Thread: Technical indicators over trading curves

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2012
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Technical indicators over trading curves

    I am converting a Forex trading application from KDChart to Qwt and am wondering:
    2012-11-21_14h08_18.jpg
    1. How can I hide the horizontal gaps in a candlestick/bar chart (I've marked this with 'Weekend' in the screenshot provided)?
    2. Which 'Qwt*Data' classes are best suited to storing calculations made from the candlesticks samples that are to be plotted over or below the chart the calculation formula need to look back (i.e a Simple Moving Average, or MACD)?
    3. What is the proper way to handle the last candle in the plot, the last candle changes when the markets are open. At the moment I'm using two trading curves, one for finished bars one for open bar. I had to prepend a fake candle to plot the open bar as Qwt wasn't handling a data-set with only one sample properly.


    Details about screenshot:
    Left chart is KDChart using QIdentityProxyModels to calculate 5 proxy models from the one data-set.
    Right chart is Qwt SVN.
    They both contain the same samples and time frame.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Technical indicators over trading curves

    Quote Originally Posted by Seamus View Post
    How can I hide the horizontal gaps in a candlestick/bar chart (I've marked this with 'Weekend' in the screenshot provided)?
    You can subclass QwtPlotCurve and draw whatever you want (and not draw whatever you want) by reimplementing draw* methods.

    Which 'Qwt*Data' classes are best suited to storing calculations made from the candlesticks samples that are to be plotted over or below the chart the calculation formula need to look back (i.e a Simple Moving Average, or MACD)?
    I did it this way that I had a QAbstractItemModel storing all the data and I had a subclass of QwtSeriesData<QPointF> that calculated and cached desired MA samples from the main data store.

    What is the proper way to handle the last candle in the plot, the last candle changes when the markets are open. At the moment I'm using two trading curves, one for finished bars one for open bar. I had to prepend a fake candle to plot the open bar as Qwt wasn't handling a data-set with only one sample properly.
    I don't really understand what you mean here. But you can again override draw* methods from QwtPlotCurve and draw what you want. I was reading open/close/min/max data from the abstract item model associated with my series data but you can also store the data directly in QwtSeriesData if you use a structure containing x and those four y values. Then if the open price is lower than 0 then just don't draw it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    When using Qwt from SVN: what type of plot items are your questions about ?

    The screenshots show regular curves ( QwtPlotCurve ) but your questions are about candlestick or bar charts ( QwtPlotTradingCurve/QwtPlotBarChart/QwtPlotMultiBarChart ). F.e. you are asking about how to hide horizontal gaps, what doesn't make much sense for the latter type of items.

    Uwe

  4. #4
    Join Date
    Nov 2012
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    Quote Originally Posted by wysota View Post
    I don't really understand what you mean here. But you can again override draw* methods from QwtPlotCurve and draw what you want. I was reading open/close/min/max data from the abstract item model associated with my series data but you can also store the data directly in QwtSeriesData if you use a structure containing x and those four y values. Then if the open price is lower than 0 then just don't draw it.
    Currently my app pulls the full (closed) candles from a price server (open candles are unavailable) to mimic an open candle I create a synthetic one from the streaming price data stored in another QVector (with a size() of 1, for one candle), on a candle stick chart the open candle can change an unlimited amount of time during the charts time frame and a closed candle never changes, at the moment I call replot() when I update the last candle and replot() again when I new candle is created. The first replot() I mention only needs to redraw one candle (the last/open one), the second replot() I mention only needs to shift the chart left one candle. Am I missing a more efficient way of of doing this?

    Quote Originally Posted by Uwe View Post
    When using Qwt from SVN: what type of plot items are your questions about ?

    The screenshots show regular curves ( QwtPlotCurve ) but your questions are about candlestick or bar charts ( QwtPlotTradingCurve/QwtPlotBarChart/QwtPlotMultiBarChart ). F.e. you are asking about how to hide horizontal gaps, what doesn't make much sense for the latter type of items.

    Uwe
    The screenshot is of QwtPlotTradingCurve (really small candles). It's actually more common to hide the horizontal gaps in a trading packages, the gaps are created when a market is closed, the screenshot provided is EURUSD which is only closed over the weekend. When the gaps are included line studies (i.e. Trend lines and channels) can't be drawn accurately. Currently when plotting a candlestick chart showing daily candles over one year the QwtPlotTradingCurve plots space for around 365 candles when it should be closer to 200 (business days in a year) for most financial instruments.

    Thanks for your replies guys

  5. #5
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    Am I missing a more efficient way of of doing this?
    It is possible to avoid replotting the complete curve in the situation, when you update the last candle only, but as long as you don't have a performance issue I would recommend to keep the code simple.

    Currently when plotting a candlestick chart showing daily candles over one year the QwtPlotTradingCurve plots space for around 365 candles when it should be closer to 200 (business days in a year) for most financial instruments.
    It's not the curve - your samples ( the time values ) decide where to put the candle sticks.

    F.e when you return samples with an time coordinate that represents the index of the day in the year without closed days you wouldn't have any gap. Of course you would also need to adjust the tick labels by adding the closed days to represent the real day on the scale.

    Uwe

  6. #6
    Join Date
    Nov 2012
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    Quote Originally Posted by Uwe View Post
    It's not the curve - your samples ( the time values ) decide where to put the candle sticks.

    F.e when you return samples with an time coordinate that represents the index of the day in the year without closed days you wouldn't have any gap. Of course you would also need to adjust the tick labels by adding the closed days to represent the real day on the scale.

    Uwe
    Okay I changed the QwtOHLCSample::time to use int/index (2012-12-03_21h05_57.jpg) instead or time_t/double (2012-12-03_21h07_44.jpg). That works fine, but all the annotating tools (hLine, plot markers. shapes..etc) get attached to xBottom which doesn't work as expected now that xBottom is not a date (x axis position have to be calculated when time frame on chart is changed).

    Is their any other possibilities to hide the gaps and keep the time_t values in 'sample.time'. Could I mess-around with the transform function in the scale map or scale draw to collapse/squash the empty space. Or should I look into replacing QwtOHLCSample with something that will hold both an index and datetime?

    [I think] I seen somewhere in the qwt source a mention of vertical invert (of the plotting), is this also available for a horizontal flip/invert, so index zero would be last on the right instead of first on the left (opposite to time scale), it's not really a problem it would just mean less brain work when writing technical indicators, I did write helper functions to mimic the behavior, just wondering if its built-in somewhere?

    Thanks

  7. #7
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    Quote Originally Posted by Seamus View Post
    Could I mess-around with the transform function in the scale map or scale draw to collapse/squash the empty space.
    Deriving from QwtTransform is an option. I wouldn't expect it to be too difficult to implement transform/invTransform with gaps: have a look at the playground/scaleengine.

    While the time scaleengine from the playground examples might work when you are in ranges of months or weeks you probably have to reimplement divideScale for intervals where you are on days.

    [I think] I seen somewhere in the qwt source a mention of vertical invert (of the plotting), is this also available for a horizontal flip/invert, so index zero would be last on the right instead of first on the left (opposite to time scale), it's not really a problem it would just mean less brain work when writing technical indicators, I did write helper functions to mimic the behavior, just wondering if its built-in somewhere?
    setAxisScale( 0, 10 ) shows an increasing, setAxisScale( 10, 0 ) a decreasing axis. When you use autoscaling you have to enable the QwtScaleEngine::Inverted flag.

    Uwe

  8. #8
    Join Date
    Nov 2012
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    Quote Originally Posted by Uwe View Post
    Deriving from QwtTransform is an option. I wouldn't expect it to be too difficult to implement transform/invTransform with gaps: have a look at the playground/scaleengine.

    While the time scaleengine from the playground examples might work when you are in ranges of months or weeks you probably have to reimplement divideScale for intervals where you are on days.
    I've decided to go with a reimplemented QwtOHLCSample that includes another variable to store the real time and I'll use QwtOHLCSample::time to store the index, I figure it would be more useful as I can re-use the annotating tools on candlestick charts that are n*tick based (instead of time).


    Quote Originally Posted by Uwe View Post
    setAxisScale( 0, 10 ) shows an increasing, setAxisScale( 10, 0 ) a decreasing axis. When you use autoscaling you have to enable the QwtScaleEngine::Inverted flag.
    Setting QwtScaleEngine::Inverted and setAxisScale( 10, 0 ) works good for me, but QwtPlotRescaler doesn't respect the invert.

  9. #9
    Join Date
    Nov 2012
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    Quote Originally Posted by Seamus View Post
    Setting QwtScaleEngine::Inverted and setAxisScale( 10, 0 ) works good for me, but QwtPlotRescaler doesn't respect the invert.
    I got QwtPlotRescaler working with an my inverted plot:
    Qt Code:
    1. m_rescaler = new QwtPlotRescaler( canvas() );
    2. m_rescaler->setReferenceAxis( QwtPlot::xBottom );
    3. m_rescaler->setRescalePolicy( QwtPlotRescaler::Expanding );
    4. m_rescaler->setEnabled(true);
    5. m_rescaler->setExpandingDirection( QwtPlot::yRight, QwtPlotRescaler::ExpandBoth );
    6. m_rescaler->setAspectRatio( QwtPlot::yRight, 0.0 );
    To copy to clipboard, switch view to plain text mode 

    I only had to change
    Qt Code:
    1. m_rescaler->setExpandingDirection( QwtPlot::xBottom, QwtPlotRescaler::ExpandDown );
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. m_rescaler->setExpandingDirection( QwtPlot::yRight, QwtPlotRescaler::ExpandBoth );
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Nov 2012
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    So my candlestick plot now uses QwtPlotRescaler and QwtPlotPanner, when panning on a rescaled chart the y axis doesn't update to visible samples only (like this 2012-12-06_19h30_32.jpg) but I would like it to rescale on every pan (like this 2012-12-06_19h31_26.jpg). This is my current working code
    Qt Code:
    1. if( !axisAutoScale(yRight) )
    2. {
    3. double min, max, begin, end;
    4.  
    5. begin = qMin( axisScaleDiv(xBottom).lowerBound(), axisScaleDiv(xBottom).upperBound() );
    6. end = qMax( axisScaleDiv(xBottom).lowerBound(), axisScaleDiv(xBottom).upperBound() );
    7.  
    8. if( begin < 0 )
    9. begin = 0;
    10. if( end < 0 )
    11. end = 0;
    12.  
    13. if( m_samples.size() < end )
    14. end = m_samples.size() - 1;
    15.  
    16. if(!includeWeekends())
    17. {
    18. min = m_samples.at(begin).low;
    19. max = m_samples.at(begin).high;
    20. }
    21. else
    22. {
    23. min = m_openCandle.at(begin).low;
    24. max = m_openCandle.at(begin).high;
    25. }
    26.  
    27. for ( int i = begin + 1; i <= end; i++ )
    28. {
    29. min = qMin( min, m_samples.at(i).low );
    30. max = qMax( max, m_samples.at(i).high );
    31. }
    32. setAxisScale(yRight, min, max);
    33. }
    To copy to clipboard, switch view to plain text mode 

    This code seems to work okay, but I'm thinking this requirement is common and may be build in to Qwt already? I though it was QwtScaleEngine::Floating but that didn't have any effect.

  11. #11
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Technical indicators over trading curves

    Autoscaling and a fixed aspect ratio ( = QwtPlotRescaler ) are contradictory requirements !

    What would you expect to happen after panning: shrinking the y axis to the boundaries of the visible area and adjusting the range of the x axis somehow according to the aspect ratio ?

    Uwe

Similar Threads

  1. Replies: 0
    Last Post: 17th January 2012, 19:24
  2. Replies: 0
    Last Post: 4th April 2011, 16:17
  3. Replies: 0
    Last Post: 1st July 2010, 21:55
  4. Qwt - Qt Widgets for Technical Applications
    By gandalf in forum Newbie
    Replies: 6
    Last Post: 5th May 2010, 15:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.