{"id":2842,"date":"2011-03-29T19:26:34","date_gmt":"2011-03-29T19:26:34","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/?p=2842"},"modified":"2011-07-14T09:17:19","modified_gmt":"2011-07-14T09:17:19","slug":"using-a-trade-composite","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2011\/03\/29\/using-a-trade-composite\/","title":{"rendered":"Logging and displaying trades"},"content":{"rendered":"

In real trading it is desirable to use a persistent trade record so that trades for multiple sessions be analyzed using the Backtester. The easiest way to do this is to save the trades in a composite. The code shown below is a simplified version of what will be used in the TDash system, however, it can be used with any trading system. This code requires you set Preferences -> Intraday -> “Time of first tick inside bar” or “START time of interval”. Apply the code to an empty pane, start Bar-Replay, and open the Parameter window. After placing some trades you can send the code to the Backtester for analysis. You can perform the following actions:<\/p>\n

\n\"\"<\/a>\n<\/p>\n

I use small solid triangles for Buy and Sell signals, and hollow triangles for Short and Cover signals. This allows me to overlay the Buy and Cover signals, and the Sell and Short signals, to display trade reversals. The triangles are placed at the actual trading price (the Close in this example). This is what the arrows look like:<\/p>\n

\n\"\"<\/a>\n<\/p>\n

_SECTION_BEGIN<\/span>( <\/span>"USING A TRADE COMPOSITE" <\/span>);\rfunction <\/span>DeleteComposite<\/span>( <\/span>CompositeName <\/span>)\r{\r    global <\/span>Ticker<\/span>;\r    <\/span>oAB <\/span>= <\/span>CreateObject<\/span>( <\/span>"Broker.Application" <\/span>);\r    <\/span>oStocks <\/span>= <\/span>oAB<\/span>.<\/span>Stocks<\/span>();\r    <\/span>oStocks<\/span>.<\/span>Remove<\/span>( <\/span>CompositeName <\/span>);\r    <\/span>oAB<\/span>.<\/span>RefreshAll<\/span>();\r}\r\rfunction  <\/span>AddTradeToComposite<\/span>( <\/span>Ticker<\/span>, <\/span>Action<\/span>, <\/span>LMTPrice <\/span>)\r{\r    global <\/span>Ticker<\/span>;\r    <\/span>BI  <\/span>= <\/span>BarIndex<\/span>();\r    <\/span>LBI <\/span>= <\/span>LastValue<\/span>( <\/span>BI <\/span>);\r\r    if ( <\/span>Action <\/span>!= <\/span>"" <\/span>)\r    {\r        <\/span>SignalArray <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"V"<\/span>, <\/span>False <\/span>) );\r        <\/span>BuyPriceArray <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"O"<\/span>, <\/span>False <\/span>) );\r        <\/span>SellPriceArray <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"H"<\/span>, <\/span>False <\/span>) );\r        <\/span>ShortPriceArray <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"L"<\/span>, <\/span>False <\/span>) );\r        <\/span>CoverPriceArray <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"C"<\/span>, <\/span>False <\/span>) );\r\r        switch ( <\/span>Action <\/span>)\r        {\r\r            case <\/span>"BUY"<\/span>:\r                <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] = <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] | <\/span>1<\/span>;\r                <\/span>BuyPriceArray<\/span>[<\/span>LBI<\/span>] = <\/span>LMTPrice<\/span>;\r                break;\r\r            case <\/span>"SELL"<\/span>:\r                <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] = <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] | <\/span>2<\/span>;\r                <\/span>SellPriceArray<\/span>[<\/span>LBI<\/span>] = <\/span>LMTPrice<\/span>;\r                break;\r\r            case <\/span>"SHORT"<\/span>:\r                <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] = <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] | <\/span>4<\/span>;\r                <\/span>ShortPriceArray<\/span>[<\/span>LBI<\/span>] = <\/span>LMTPrice<\/span>;\r                break;\r\r            case <\/span>"COVER"<\/span>:\r                <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] = <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] | <\/span>8<\/span>;\r                <\/span>CoverPriceArray<\/span>[<\/span>LBI<\/span>] = <\/span>LMTPrice<\/span>;\r                break;\r\r            case <\/span>"REVLONG"<\/span>:\r                <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] = <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] | <\/span>8 <\/span>| <\/span>1<\/span>;\r                <\/span>CoverPriceArray<\/span>[<\/span>LBI<\/span>] = <\/span>BuyPriceArray<\/span>[<\/span>LBI<\/span>] = <\/span>LMTPrice<\/span>;\r                break;\r\r            case <\/span>"REVSHORT"<\/span>:\r                <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] = <\/span>SignalArray<\/span>[<\/span>LBI<\/span>] | <\/span>2 <\/span>| <\/span>4<\/span>;\r                <\/span>SellPriceArray<\/span>[<\/span>LBI<\/span>] = <\/span>ShortPriceArray<\/span>[<\/span>LBI<\/span>] = <\/span>LMTPrice<\/span>;\r                break;\r        }\r\r        <\/span>AddToComposite<\/span>( <\/span>SignalArray<\/span>, <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"V"<\/span>, <\/span>7 <\/span>| <\/span>atcFlagEnableInIndicator <\/span>);\r        <\/span>AddToComposite<\/span>( <\/span>BuyPriceArray<\/span>, <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"O"<\/span>, <\/span>7 <\/span>| <\/span>atcFlagEnableInIndicator <\/span>);\r        <\/span>AddToComposite<\/span>( <\/span>SellPriceArray<\/span>, <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"H"<\/span>, <\/span>7 <\/span>| <\/span>atcFlagEnableInIndicator <\/span>);\r        <\/span>AddToComposite<\/span>( <\/span>ShortPriceArray<\/span>, <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"L"<\/span>, <\/span>7 <\/span>| <\/span>atcFlagEnableInIndicator <\/span>);\r        <\/span>AddToComposite<\/span>( <\/span>CoverPriceArray<\/span>, <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"C"<\/span>, <\/span>7 <\/span>| <\/span>atcFlagEnableInIndicator <\/span>);\r    }\r}\r\r<\/span>Ticker <\/span>= <\/span>Name<\/span>();\r<\/span>Action <\/span>= <\/span>""<\/span>;\r\rif ( <\/span>ParamTrigger<\/span>( <\/span>"Buy"<\/span>, <\/span>"BUY" <\/span>) ) <\/span>Action <\/span>= <\/span>"BUY"<\/span>;\rif ( <\/span>ParamTrigger<\/span>( <\/span>"Sell"<\/span>, <\/span>"SELL" <\/span>) ) <\/span>Action <\/span>= <\/span>"SELL"<\/span>;\rif ( <\/span>ParamTrigger<\/span>( <\/span>"Short"<\/span>, <\/span>"SHORT" <\/span>) ) <\/span>Action <\/span>= <\/span>"SHORT"<\/span>;\rif ( <\/span>ParamTrigger<\/span>( <\/span>"Cover"<\/span>, <\/span>"COVER" <\/span>) ) <\/span>Action <\/span>= <\/span>"COVER"<\/span>;\rif ( <\/span>ParamTrigger<\/span>( <\/span>"Reverse to Long"<\/span>, <\/span>"REVLONG" <\/span>) ) <\/span>Action <\/span>= <\/span>"REVLONG"<\/span>;\rif ( <\/span>ParamTrigger<\/span>( <\/span>"Reverse to Short"<\/span>, <\/span>"REVSHORT" <\/span>) ) <\/span>Action <\/span>= <\/span>"REVSHORT"<\/span>;\rif ( <\/span>ParamTrigger<\/span>( <\/span>"Delete Signal Compoiste"<\/span>, <\/span>"DELETE" <\/span>) ) <\/span>DeleteComposite<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays" <\/span>);\r<\/span>AddTradeToComposite<\/span>( <\/span>Ticker<\/span>, <\/span>Action<\/span>, <\/span>LastValue<\/span>( <\/span>Close <\/span>) );\r\r<\/span>RequestTimedRefresh<\/span>( <\/span>0.1 <\/span>);\rif ( <\/span>SetForeign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays" <\/span>) )\r{\r    <\/span>SignalArray    <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"V"<\/span>, <\/span>False <\/span>) );\r    <\/span>Buy         <\/span>= <\/span>IIf<\/span>( <\/span>SignalArray <\/span>& <\/span>1<\/span>, <\/span>1<\/span>, <\/span>0 <\/span>);\r    <\/span>Sell         <\/span>= <\/span>IIf<\/span>( <\/span>SignalArray <\/span>& <\/span>2<\/span>, <\/span>1<\/span>, <\/span>0 <\/span>);\r    <\/span>Short         <\/span>= <\/span>IIf<\/span>( <\/span>SignalArray <\/span>& <\/span>4<\/span>, <\/span>1<\/span>, <\/span>0 <\/span>);\r    <\/span>Cover         <\/span>= <\/span>IIf<\/span>( <\/span>SignalArray <\/span>& <\/span>8<\/span>, <\/span>1<\/span>, <\/span>0 <\/span>);\r\r    <\/span>BuyPrice     <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"O" <\/span>) );\r    <\/span>SellPrice     <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"H" <\/span>) );\r    <\/span>ShortPrice    <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"L" <\/span>) );\r    <\/span>CoverPrice     <\/span>= <\/span>Nz<\/span>( <\/span>Foreign<\/span>( <\/span>Ticker <\/span>+ <\/span>"~SignalArrays"<\/span>, <\/span>"C" <\/span>) );\r\r    <\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Buy<\/span>, <\/span>shapeSmallUpTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>5<\/span>, <\/span>0<\/span>, <\/span>BuyPrice<\/span>, <\/span>0 <\/span>);\r    <\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Sell<\/span>, <\/span>shapeSmallDownTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>4<\/span>, <\/span>0<\/span>, <\/span>SellPrice<\/span>, <\/span>0 <\/span>);\r    <\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Short<\/span>, <\/span>shapeHollowDownTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>4<\/span>, <\/span>0<\/span>, <\/span>ShortPrice<\/span>, <\/span>0 <\/span>);\r    <\/span>PlotShapes<\/span>( <\/span>IIf<\/span>( <\/span>Cover<\/span>, <\/span>shapeHollowUpTriangle<\/span>, <\/span>shapeNone <\/span>), <\/span>5<\/span>, <\/span>0<\/span>, <\/span>CoverPrice<\/span>, <\/span>0 <\/span>);\r\r}\r<\/span>RestorePriceArrays<\/span>();\r\r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>""<\/span>, <\/span>1<\/span>, <\/span>128 <\/span>);\r<\/span>_SECTION_END<\/span>();<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"

In real trading it is desirable to use a persistent trade record so that trades for multiple sessions be analyzed using the Backtester. The easiest way to do this is to save the trades in a composite. The code shown below is a simplified version of what will be used in the TDash system, however, […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[141],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/2842"}],"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=2842"}],"version-history":[{"count":22,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/2842\/revisions"}],"predecessor-version":[{"id":2878,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/2842\/revisions\/2878"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=2842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=2842"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=2842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}