{"id":2128,"date":"2011-03-04T00:04:46","date_gmt":"2011-03-04T00:04:46","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/?p=2128"},"modified":"2011-04-08T10:27:17","modified_gmt":"2011-04-08T10:27:17","slug":"linking-the-tdash-window-with-the-main-chart","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2011\/03\/04\/linking-the-tdash-window-with-the-main-chart\/","title":{"rendered":"Linking the TDash Window with the Main Chart"},"content":{"rendered":"

The most important requirement for a Trading Dashboard is to be able to place orders by dragging price markers and have these prices reflected on your main chart. To accomplish this relationship, the Main Chart and TDash windows must be bi-directionally linked so that changing prices in one window are being tracked in the other.<\/p>\n

If both the Main chart and Trading Dashboard window had an identical price range and window size, this could simply be done by sharing the price and pixel ranges using Static Variables. However, to make this work in a multi window layout where windows may not be aligned accurately, or when the TDash window is located on another monitor, is a little more complex.<\/p>\n

The problem can be solved by aligning the upper edge of the windows with the upper edge of the AmiBroker window, and use this as the common reference to calculate prices and pixel values.<\/p>\n

The code below demonstrates how the Y-Pixel position and prices, between the Trading Dashboard window and the main chart, can be made to track each other. To apply this technique in an actual application you would want to add graphical price markers that you can drag, start up initialization, and provide a fine-adjustment to set prices exactly. Some of this will be covered in the next post.<\/p>\n

To test the code, create a two-window Layout, arrange them horizontally, apply the TDashLinkDemo code in the right window, and apply the MainChartLinkDemo code in the left window. Since this test code is not initialized at start-up, you need to click in the TDash window to set an initial price and make the price-line appear.<\/p>\n

Click the left mouse button in the TDash window at a new price level to set the price-line to a different value. Click and hold the left mouse button down to drag the price to a new value. Note that the price-line in the main chart window tracks all changes. Price lines would be used to display pending orders and to enable you to set order prices with respect to real-time chart patterns.<\/p>\n

When you release the left mouse button in the TDash window it locks in the current price. The only way to change the price is by clicking or dragging it in the TDash window.<\/p>\n

If you drag the Y-axis in the main chart the corresponding price-line in the TDash window tracks this movement. This is needed to keep price markers in the TDash window in sync when the main chart y-axis changes.<\/p>\n

In a multi-threading environment, where formulas execute asynchronously, we cannot assume that transient conditions saved in a Static Variable in one window will always be detected in another. To ensure reliable mouse-click detection in the Main chart program, the “~LeftButtonRelease” Static Variable, which is set in the control window, is reset in the main chart after it has been detected. This way no triggers will ever be missed.<\/p>\n

The short video below illustrates how this works and what it looks like in AmiBroker.<\/p>\n

\n<\/embed><\/object>\n<\/div>\n
<\/span>\/\/ TDashLinkDemo\r\r<\/span>function <\/span>LinkWithMainChart<\/span>()\r{\r<\/span>MX <\/span>= <\/span>GetCursorXPosition<\/span>( <\/span>1 <\/span>);\r<\/span>MY <\/span>= <\/span>GetCursorYPosition<\/span>( <\/span>1 <\/span>);\r<\/span>OnTDash <\/span>= !<\/span>IsNull<\/span>( <\/span>MX <\/span>) AND !<\/span>IsNull<\/span>( <\/span>MY <\/span>); <\/span>\/\/ Is cursor in TDash?\r<\/span>LeftClick <\/span>= <\/span>GetCursorMouseButtons<\/span>() == <\/span>9 <\/span>AND <\/span>OnTDash<\/span>;\r<\/span>LeftDown <\/span>= ( <\/span>GetCursorMouseButtons<\/span>() == <\/span>1 <\/span>OR <\/span>LeftClick <\/span>) AND <\/span>OnTDash<\/span>;\r<\/span>PrevLeftDownState <\/span>= <\/span>Nz<\/span>( <\/span>StaticVarGet<\/span>( <\/span>"~LeftDown" <\/span>) );\r<\/span>LeftButtonRelease <\/span>= <\/span>LeftDown <\/span>&<\/span>lt<\/span>; <\/span>PrevLeftDownState<\/span>;\r<\/span>StaticVarSet<\/span>( <\/span>"~LeftDown"<\/span>, <\/span>LeftDown <\/span>);\r\rif ( <\/span>LeftClick <\/span>) <\/span>StaticVarSet<\/span>( <\/span>"~NowDragging"<\/span>, <\/span>True <\/span>);\relse if ( <\/span>LeftButtonRelease <\/span>)\r{\r<\/span>StaticVarSet<\/span>( <\/span>"~NowDragging"<\/span>, <\/span>False <\/span>);\r<\/span>StaticVarSet<\/span>( <\/span>"~LeftButtonRelease"<\/span>, <\/span>True <\/span>);\r}\rif ( <\/span>Nz<\/span>( <\/span>StaticVarGet<\/span>( <\/span>"~NowDragging" <\/span>) ) ) <\/span>StaticVarSet<\/span>( <\/span>"~TDashYPixels"<\/span>, <\/span>MY <\/span>);\r}\r\rfunction <\/span>DrawTDashPriceLine<\/span>()\r{\r<\/span>pxWidth <\/span>= <\/span>Status<\/span>( <\/span>"pxWidth" <\/span>);\r<\/span>TDashYPixels <\/span>= <\/span>StaticVarGet<\/span>( <\/span>"~TDashYPixels" <\/span>);\r<\/span>GfxSetBkMode<\/span>( <\/span>1 <\/span>);\r<\/span>GfxSelectPen<\/span>( <\/span>colorRed<\/span>, <\/span>1<\/span>, <\/span>1 <\/span>);\r<\/span>GfxMoveTo<\/span>( <\/span>0<\/span>, <\/span>TDashYPixels <\/span>);\r<\/span>GfxLineTo<\/span>( <\/span>pxwidth<\/span>, <\/span>TDashYPixels <\/span>);\r}\r\rfunction <\/span>TDashDisplayPrice<\/span>()\r{\r<\/span>MY <\/span>= <\/span>GetCursorYPosition<\/span>( <\/span>1 <\/span>);\r<\/span>TDashYPixles <\/span>= <\/span>StaticVarGet<\/span>( <\/span>"~TDashYPixels" <\/span>);\r<\/span>MAinChartPrice <\/span>= <\/span>Nz<\/span>( <\/span>StaticVarGet<\/span>( <\/span>"~MainChartPrice" <\/span>) );\r<\/span>GfxSetTextAlign<\/span>( <\/span>0 <\/span>) ;\r<\/span>GfxSelectFont<\/span>( <\/span>"Lucida Console"<\/span>, <\/span>12<\/span>, <\/span>600 <\/span>);\r<\/span>GfxTextOut<\/span>( <\/span>"TDash price: $" <\/span>+ <\/span>NumToStr<\/span>( <\/span>MAinChartPrice<\/span>, <\/span>1.2<\/span>, <\/span>False <\/span>), <\/span>0<\/span>, <\/span>TDashYPixles <\/span>+ <\/span>5 <\/span>);\r<\/span>GfxTextOut<\/span>( <\/span>"TDash Pixels: " <\/span>+ <\/span>NumToStr<\/span>( <\/span>TDashYPixles<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>), <\/span>0<\/span>, <\/span>TDashYPixles <\/span>+ <\/span>20 <\/span>);\r}\r\r<\/span>RequestTimedRefresh<\/span>( <\/span>0.1 <\/span>);\r<\/span>GfxSetOverlayMode<\/span>( <\/span>2<\/span>);\r<\/span>LinkWithMainChart<\/span>();\r<\/span>DrawTDashPriceLine<\/span>();\r<\/span>TDashDisplayPrice<\/span>(); <\/span>\/\/ Test only\r<\/span><\/pre>\n
\r<\/span>\/\/ MainChartLinkDemo\r\r<\/span>function <\/span>LinkWithTDash<\/span>()\r{\r<\/span>pxWidth <\/span>= <\/span>Status<\/span>( <\/span>"pxWidth" <\/span>);\r<\/span>Miny <\/span>= <\/span>Status<\/span>( <\/span>"axisminy" <\/span>);\r<\/span>Maxy <\/span>= <\/span>Status<\/span>( <\/span>"axismaxy" <\/span>);\r<\/span>Pricerange <\/span>= <\/span>MaxY <\/span>- <\/span>MinY<\/span>;\r<\/span>pxchartbottom <\/span>= <\/span>Status<\/span>( <\/span>"pxchartbottom" <\/span>);\r<\/span>pxcharttop <\/span>= <\/span>Status<\/span>( <\/span>"pxcharttop" <\/span>);\r<\/span>PxChartRange <\/span>= <\/span>Status<\/span>( <\/span>"pxchartheight" <\/span>);\r<\/span>pxheight <\/span>= <\/span>Status<\/span>( <\/span>"pxheight" <\/span>);\r\rif ( <\/span>Nz<\/span>( <\/span>StaticVarGet<\/span>( <\/span>"~LeftButtonRelease" <\/span>) ) )\r{\r<\/span>TDashYPixels <\/span>= <\/span>StaticVarGet<\/span>( <\/span>"~TDashYPixels" <\/span>);\r<\/span>PricePerPixel <\/span>= <\/span>Pricerange <\/span>\/ <\/span>PxChartRange<\/span>;\r<\/span>MAinChartPrice <\/span>= <\/span>Maxy <\/span>- ( <\/span>PricePerPixel <\/span>* ( <\/span>TDashYPixels <\/span>- <\/span>5 <\/span>) );\r<\/span>StaticVarSet<\/span>( <\/span>"~LeftButtonRelease"<\/span>, <\/span>False <\/span>);\r<\/span>StaticVarSet<\/span>( <\/span>"~MainChartPrice"<\/span>, <\/span>MAinChartPrice <\/span>);\r}\relse\rif ( <\/span>Nz<\/span>( <\/span>StaticVarGet<\/span>( <\/span>"~NowDragging" <\/span>) ) == <\/span>0 <\/span>)\r{\r<\/span>MAinChartPrice <\/span>= <\/span>Nz<\/span>( <\/span>StaticVarGet<\/span>( <\/span>"~MainChartPrice" <\/span>) );\r<\/span>PixelPerprice <\/span>= <\/span>PxChartRange <\/span>\/ <\/span>Pricerange<\/span>;\r<\/span>PriceToPixelvalue <\/span>= ( <\/span>MaxY <\/span>- <\/span>MAinChartPrice <\/span>) * <\/span>Pixelperprice <\/span>+ <\/span>5<\/span>;\r<\/span>StaticVarSet<\/span>( <\/span>"~TDashYPixels"<\/span>, <\/span>PriceToPixelvalue <\/span>);\r}\r}\r\rfunction <\/span>DrawTDashPriceLine<\/span>()\r{\r<\/span>pxWidth <\/span>= <\/span>Status<\/span>( <\/span>"pxWidth" <\/span>);\r<\/span>TDashYPixels <\/span>= <\/span>StaticVarGet<\/span>( <\/span>"~TDashYPixels" <\/span>);\r<\/span>GfxSetBkMode<\/span>( <\/span>1 <\/span>);\r<\/span>GfxSelectPen<\/span>( <\/span>colorRed<\/span>, <\/span>1<\/span>, <\/span>1 <\/span>);\r<\/span>GfxMoveTo<\/span>( <\/span>0<\/span>, <\/span>TDashYPixels <\/span>);\r<\/span>GfxLineTo<\/span>( <\/span>pxwidth<\/span>, <\/span>TDashYPixels <\/span>);\r}\r\rfunction <\/span>MainDisplayPrice<\/span>()\r{\r<\/span>pxWidth <\/span>= <\/span>Status<\/span>( <\/span>"pxWidth" <\/span>);\r<\/span>TDashYPixels <\/span>= <\/span>StaticVarGet<\/span>( <\/span>"~TDashYPixels" <\/span>);\r<\/span>MAinChartPrice <\/span>= <\/span>Nz<\/span>( <\/span>StaticVarGet<\/span>( <\/span>"~MainChartPrice" <\/span>) );\r<\/span>GfxSetTextAlign<\/span>( <\/span>2 <\/span>) ;\r<\/span>GfxSelectFont<\/span>( <\/span>"Lucida Console"<\/span>, <\/span>12<\/span>, <\/span>600 <\/span>);\r<\/span>GfxTextOut<\/span>( <\/span>"  Main price: $" <\/span>+ <\/span>NumToStr<\/span>( <\/span>MAinChartPrice<\/span>, <\/span>1.2<\/span>, <\/span>False <\/span>), <\/span>pxwidth<\/span>, <\/span>TDashYPixels <\/span>+ <\/span>5 <\/span>);\r<\/span>GfxTextOut<\/span>( <\/span>"Main Pixels: " <\/span>+ <\/span>NumToStr<\/span>( <\/span>TDashYPixels<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>), <\/span>pxwidth<\/span>, <\/span>TDashYPixels <\/span>+ <\/span>20 <\/span>);\r}\r\r<\/span>RequestTimedRefresh<\/span>( <\/span>0.1 <\/span>);\r<\/span>GfxSetOverlayMode<\/span>( <\/span>0 <\/span>);\r<\/span>LinkWithTDash<\/span>();\r<\/span>DrawTDashPriceLine<\/span>();\r<\/span>MainDisplayPrice<\/span>(); <\/span>\/\/ Test only\r\r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>""<\/span>, <\/span>colorBlack<\/span>, <\/span>styleBar <\/span>);\r<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"

The most important requirement for a Trading Dashboard is to be able to place orders by dragging price markers and have these prices reflected on your main chart. To accomplish this relationship, the Main Chart and TDash windows must be bi-directionally linked so that changing prices in one window are being tracked in the other. […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[141],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/2128"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=2128"}],"version-history":[{"count":105,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/2128\/revisions"}],"predecessor-version":[{"id":2591,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/2128\/revisions\/2591"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=2128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=2128"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=2128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}