Stock Portfolio Organizer
The ultimate porfolio management solution.
WiseTrader Toolbox
#1 Selling Amibroker Plugin featuring:
Stops Implementation in AFS for Amibroker (AFL)
Please find attached afl script with stops implementation directly as a afl code without using ApplyStop formula. This implementation gives possibility to get arrows on the chart. Also please note this stops have been split into two parts – for long and short trades. Also activation level is defined as a level when stop should be activated. By multiplying stop code you can get (a) stop loss, (b) static save profit stop (means: static stop activated when you start getting profit and suddenly market turns around – this stop is used to at least have money for brokerage fee) and © trailing stop.
By Marek Chlopek – mchlopek [at] post.pl
Indicator / Formula
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | // TEST.AFL v 0.03 28/11/2001 // Testing Stops // Developed by Marek Chlopek // Coded Marek Chlopek, November 2001 // Support from Tomasz Janeczko and Amibroker Mailing List members - THANKS!!! //**************************************************************************************** // STATIC EXPLORATION IN AMIBROKER //**************************************************************************************** Filter = 1; NumColumns = 5; Column0 = O ; Column0Name = "O" ; Column0Format = 1.2; Column1 = H ; Column1Name = "H" ; Column1Format = 1.2; Column2 = L ; Column2Name = "L" ; Column2Format = 1.2; Column3 = C ; Column3Name = "C" ; Column3Format = 1.2; Column4 = V ; Column4Name = "V" ; Column4Format = 1.0; // END OF "STATIC EXPLORATION IN AMIBROKER" SECTION //**************************************************************************************** // TRADING SYSTEM GLOBAL ENTRY FORMULA //**************************************************************************************** Buy = Cross ( Close , 115); Short = Cross (150, Close ); // END OF "TRADING SYSTEM GLOBAL ENTRY FORMULA" SECTION //**************************************************************************************** // TRADING SYSTEM LONG STOP FORMULA //**************************************************************************************** BuyStopLoss = 0; // 0 - not a stop loss formula, stop must be activated // 1 - stop is activated without checking ActiveLevel // How many bars ago was the last Buy signal? BuyBarsSince = BarsSince ( Buy == 1); // Setting stop level // As an example BuyStopLevel depends directly on BuyPrice and a constant // Could be also: BuyPrice + ATR or any other dynamic formula BuyStopLevel = Ref ( BuyPrice , -BuyBarsSince) + 3; // Setting the level to activate stop // BuyActivateLevel depends directly on BuyPrice and a constant // and I think this is how it should be - BuyActiveLevel should not bedynamically alculated // NOTE: When BuyStopLoss = 1, BuyActiveLevel is not important BuyActiveLevel = Ref ( BuyPrice , -BuyBarsSince) + 22; // Active is triggered when the highest price since Buy is higher then BuyActiveLevel OR // is always 1 (True) when BuyStopLoss = 1 // NOTE: Cross() returns 0 when HHV = BuyActiveLevel BuyActive = Cross (BuyActiveLevel, HHV ( High , -BuyBarsSince)) OR BuyStopLoss; // BuyStop is triggered when Low of the session is lower than BuyStopLevel // NOTE: Cross() returns 0 when BuyStopLevel = Low BuyStop = Cross ( Low , BuyStopLevel); // BuyActive will be 1 (True) till BuyStop is triggered // therefore we will know if BuyStop is activated and should be executed BuyActive = Flip (BuyActive, BuyStop); // When gap on open - use Open otherwise use BuyStopLevel as a SellPrice SellPrice = Min ( Open , BuyStopLevel); // Exploration in Amibroker AddColumn (BuyStopLoss, "BuyStopLoss" , format=1.0); AddColumn (BuyBarsSince, "BuyBarsSince" , format=1.0); AddColumn (BuyStopLevel, "BuyStopLevel" , format=1.2); AddColumn (BuyActiveLevel, "BuyActiveLevel" , format=1.2); AddColumn (BuyStop, "BuyStop" , format=1.0); AddColumn (BuyActive, "BuyActive" , format=1.0); // END OF "TRADING SYSTEM LONG STOP FORMULA" SECTION //**************************************************************************************** // TRADING SYSTEM SHORT STOP FORMULA //**************************************************************************************** ShortStopLoss = 0; // 0 - not a stop loss formula, stop must be activated // 1 - stop is activated without checking ActiveLevel // How many bars ago was the last Short signal? ShortBarsSince = BarsSince ( Short == 1); // Setting stop level // As an example ShortStopLevel depends directly on ShortPrice and a constant // Could be also: ShortPrice - ATR or any other dynamic formula ShortStopLevel = Ref ( ShortPrice , -ShortBarsSince) - 3; // Setting the level to activate stop // ShortActivateLevel depends directly on ShortPrice and a constant // and I think this is how it should be - ShortActiveLevel should not be dynamically calculated // NOTE: When ShortStopLoss = 1, ShortActiveLevel is not important ShortActiveLevel = Ref ( ShortPrice , -ShortBarsSince) - 22; // Active is triggered when the lowest price since Short is lower then ShortActiveLevel OR // is always 1 (True) when ShortStopLoss = 1 // NOTE: Cross() returns 0 when LLV = ShortActiveLevel ShortActive = Cross (ShortActiveLevel, LLV ( Low , -ShortBarsSince)) OR ShortStopLoss; // ShortStop is triggered when High of the session is higher than ShortStopLevel // NOTE: Cross() returns 0 when ShortStopLevel = High ShortStop = Cross ( High , ShortStopLevel); // ShortActive will be 1 (True) till ShortStop is triggered // therefore we will know if ShortStop is activated and should be executed ShortActive = Flip (ShortActive, ShortStop); // When gap on open - use Open otherwise use ShortStopLevel as a CoverPrice CoverPrice = Max ( Open , ShortStopLevel); // Exploration in Amibroker AddColumn (ShortStopLoss, "ShortStopLoss" , format=1.0); AddColumn (ShortBarsSince, "ShortBarsSince" , format=1.0); AddColumn (ShortStopLevel, "ShortStopLevel" , format=1.2); AddColumn (ShortActiveLevel, "ShortActiveLevel" , format=1.2); AddColumn (ShortStop, "ShortStop" , format=1.0); AddColumn (ShortActive, "ShortActive" , format=1.0); // END OF "TRADING SYSTEM SHORT STOP FORMULA" SECTION //**************************************************************************************** // TRADING SYSTEM GLOBAL EXIT FORMULA //**************************************************************************************** Sell = BuyStop OR Short ; Cover = ShortStop OR Buy ; // Exploration in Amibroker AddColumn ( Buy , "Buy" , format=1.0); AddColumn ( Short , "Short" , format=1.0); AddColumn ( Sell , "Sell" , format=1.0); AddColumn ( Cover , "Cover" , format=1.0); AddColumn ( BuyPrice , "BuyPrice" , format=1.2); AddColumn ( ShortPrice , "ShortPrice" , format=1.2); AddColumn ( SellPrice , "SellPrice" , format=1.2); AddColumn ( CoverPrice , "CoverPrice" , format=1.2); // END OF "TRADING SYSTEM GLOBAL EXIT FORMULA" SECTION //**************************************************************************************** // GRAPHIC PRESENTATION IN AMIBROKER //**************************************************************************************** // [code for graphic presentation] // END OF "GRAPHIC PRESENTATION IN AMIBROKER" SECTION //**************************************************************************************** // END OF CODE (TEST.AFL) //**************************************************************************************** /* old style comment - to avoid parser bug */ |
0 comments
Leave Comment
Please login here to leave a comment.
Back