Stock Portfolio Organizer
The ultimate porfolio management solution.
Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
WiseTrader Toolbox
#1 Selling Amibroker Plugin featuring:
Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Scale Out for Amibroker (AFL)
Copy & Paste Friendly
Back
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | _SECTION_BEGIN ( "Scale Out: Futures" ); SetTradeDelays (0,0,0,0); BuyPrice = Close ; ShortPrice = Close ; SetOption ( "FuturesMode" , True ); SetOption ( "InitialEquity" , 10000000); Buy = Cross ( MA ( C , 20), MA ( C ,50)); Short = Cross ( MA ( C ,50), MA ( C ,20)); SystemExitLong = Cross ( MA ( C ,18), C ); // This value will be adjusted according the system's exit rules SystemExitShort = Cross ( C , MA ( C ,18)); StopAmt = 1.5; //number of points ProfitTarget = 3; //number of points TickIncrement = Param ( "Tick Increment" , 0.25, 0.1, 1, 0.1); //ES = 0.25, NQ = 0.10, YM = 1 TickIncrement = TickIncrement * 1; //change this value according to the expected slippage when stops are tiggered //set begining value of essential variables TrailingStop = 0; // This value will be adjusted to FirstProfitTarget only after SecondProfitTarget is hit StopLoss = 0; FirstProfitTarget = 0; SecondProfitTarget = 0; //set begining values for long variables priceatbuy=0; highsincebuy = 0; Sell = 0; TradeDate = DateTime (); //set begining values for short variables priceatshort= 0; lowsincebuy = 0; Cover = 0; //set exit to zero exit = 0; PortEq = Equity (); /////////////////////////////////////////////////////////////////////////// //////////////Begin code to scale out of positions///////////////////////// /////////////////////////////////////////////////////////////////////////// for ( i = 0; i < BarCount ; i++ ) { if ( priceatbuy == 0 AND Buy [ i ] ) { //initialize required variables priceatbuy = BuyPrice [ i ]; StopLoss = StopAmt[i]; FirstProfitTarget = StopAmt[i]; SecondProfitTarget = ProfitTarget[i]; } if ( priceatshort == 0 AND Short [ i ] ) { //initialize required variables priceatshort = ShortPrice [ i ]; StopLoss = StopAmt[i]; FirstProfitTarget = StopAmt[i]; SecondProfitTarget = ProfitTarget[i]; } if ( priceatbuy > 0 ) { highsincebuy = Max ( High [ i ], highsincebuy ); //un-comment statement below for debuging //_TRACE("LongEntry: " + DateTimeToStr(TradeDate[i]) +"/ BuyPrice: " +BuyPrice[i] +"/ Equity in-loop: " +PortEq[i]); //check if 1st target hit and Buy not = 1 if ( Buy [i] != 1 AND exit == 0 AND High [ i ] >= FirstProfitTarget + TickIncrement + priceatbuy ) { // first profit target hit - scale-out exit = 1; Buy [ i ] = sigScaleOut; BuyPrice [i] = FirstProfitTarget + priceatbuy; } //check if 2nd target hit and Buy not = 1 if ( Buy [i] != 1 AND exit == 1 AND High [ i ] >= SecondProfitTarget + TickIncrement + priceatbuy ) { // second profit target hit - scale-out exit = 2; Buy [ i ] = sigScaleOut; BuyPrice [i] = SecondProfitTarget + priceatbuy; //if close of bar that sets trailing stop is higher than target 1 assume //trailing stop is not triggered on that bar. SetTrail = IIf ( Close [i] > priceatbuy + FirstProfitTarget, 1, 0); //after hitting SecondProfitTarget, move //stop to FirstProfitTarget position TrailingStop = FirstProfitTarget + priceatbuy; } //check if trailing stop hit and Buy not = 1 //make sure SetTrail is not = 1 to ensure trail stop is not hit //unless close of bar where trail stop is set is lower than //trail stop if ( Buy [i] != 1 AND exit == 2 AND SetTrail == 0 AND Low [ i ] <= TrailingStop - TickIncrement ) { // Trailing Stop target hit - exit trade with final contract exit = 3; SellPrice [ i ] = TrailingStop - TickIncrement ; //accounting for one tick slippage } //check if system exit hit and Buy not = 1 if ( Buy [i] != 1 AND exit <= 2 AND SystemExitLong [i]) //need to substitute system exit here { // System Exit hit - exit all remaining contracts exit = 3; SellPrice [i] = Close [i]; //all three contracts would exit here } //check if stop loss hit and Buy not = 1 if ( Buy [i] != 1 AND Low [ i ] <= priceatbuy - StopLoss - TickIncrement ) { // Stop Loss hit - exit exit = 3; SellPrice [ i ] = Min ( Open [ i ], priceatbuy - StopLoss - TickIncrement ); //assume one tick slippage } //un-comment statement below for debuging //_TRACE("Buy = " + Buy[i] +"/ Exit: " +exit); //Reset the SetTrail variable back to zero before processing next bar SetTrail = 0; //check if exit complete if ( exit >= 3 ) { Buy [ i ] = 0; Sell [ i ] = exit + 1; // mark appropriate exit code exit = 0; priceatbuy = 0; // reset price highsincebuy = 0; ThirdProfitTarget = 0; TrailingStop = 0; } } //exit: Check exits for longs if ( priceatshort > 0 ) { lowsincebuy = Min ( Low [ i ], lowsincebuy ); //un-comment statement below for debuging //_TRACE("ShortEntry: " + DateTimeToStr(TradeDate[i]) +"/ ShortPrice = " +priceatshort +"/ Equity in-loop: " +PortEq[i]); //check if 1st target hit and short not = 1 if ( Short [i] != 1 AND exit == 0 AND Low [ i ] <= priceatshort - FirstProfitTarget - TickIncrement ) { // first profit target hit - scale-out exit = 1; Short [ i ] = sigScaleOut; ShortPrice [i] = priceatshort - FirstProfitTarget; } //check if 2nd target hit and short not = 1 if ( Short [i] != 1 AND exit == 1 AND Low [ i ] <= priceatshort - SecondProfitTarget - TickIncrement ) { // second profit target hit - scale-out exit = 2; Short [ i ] = sigScaleOut; ShortPrice [i] = priceatshort - SecondProfitTarget; //if close of bar that sets trailing stop is lower than target 1 assume //trailing stop is not triggered on that bar. SetTrail = IIf ( Close [i] < priceatshort - FirstProfitTarget, 1, 0); //after hitting SecondProfitTarget, move //stop to FirstProfitTarget position TrailingStop = priceatshort - FirstProfitTarget ; } //check if trailing stop hit and short not = 1 //make sure SetTrail is not = 1 to ensure trail stop is not hit //unless close of bar where trail stop is set is higher than //trail stop if ( Short [i] != 1 AND exit == 2 AND SetTrail == 0 AND High [ i ] >= TrailingStop + TickIncrement ) { // Trailing Stop target hit - exit trade with final contract exit = 3; CoverPrice [ i ] = TrailingStop + TickIncrement ; } //check if system exit and short not = 1 if ( Short [i] != 1 AND exit <= 2 AND SystemExitShort[i]) //need to substitute system exit here { // System Exit hit - exit all remaining contracts exit = 3; CoverPrice [i] = Close [i]; //all three contracts would exit here } //check if stop loss hit and short not = 1 if ( Short [i] != 1 AND High [ i ] >= priceatshort + StopLoss + TickIncrement ) { // Stop Loss hit - exit exit = 3; CoverPrice [ i ] = Max ( Open [ i ], priceatshort + StopLoss + TickIncrement ); //assume one tick slippage } //un-comment statement below for debuging //_TRACE("Short = " + Short[i] +"/ Exit: " +exit); //Reset the SetTrail variable back to zero before processing next bar SetTrail = 0; //check if exit complete if ( exit >= 3 ) { Short [ i ] = 0; Cover [ i ] = exit + 1; // mark appropriate exit code exit = 0; priceatshort = 0; // reset price highsincebuy = 0; ThirdProfitTarget = 0; TrailingStop = 0; } } //exit: check exits for shorts } //exit: loop //trade three contracts with every entry signal SetPositionSize (3, spsShares ); //scale out one contract at a time SetPositionSize ( 1, IIf ( Short == sigScaleOut OR Buy == sigScaleOut, spsShares , spsNoChange ) ); /////////////////////////////////////////////////////////////////////////// //////////////End of code to scale out of positions//////////////////////// /////////////////////////////////////////////////////////////////////////// _SECTION_END (); |