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 ....
scaling 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | // AFL code E.M.Pottasch, Aug 2013 x= BarIndex ();Lx= LastValue (x); fvb= Status ( "firstvisiblebarindex" );lvb= Min (Lx, Status ( "lastvisiblebarindex" )); DisplayLineAnnotation= ParamToggle ( "Display Line Annotation" , "Off|On" ,1); DisplaySignalAnnotation= ParamToggle ( "Display Signal Annotation" , "Off|On" ,1); SetTradeDelays (0,0,0,0); SetOption ( "CommissionMode" ,3); SetOption ( "CommissionAmount" ,2.01); SetOption ( "FuturesMode" , True ); SetOption ( "AllowSameBarExit" , True ); SetOption ( "InitialEquity" ,150000); NumContracts=1; //PositionSize=NumContracts*MarginDeposit; beginCalcTime= ParamTime ( "Begin Calc Time" , "09:30:00" ); endCalcTime= ParamTime ( "End Calc Time" , "16:00:00" ); beginTradeTime= ParamTime ( "Begin Trade Time" , "09:30:00" ); endTradeTime= ParamTime ( "End Trade Time" , "15:55:00" ); timeWindowTrade= TimeNum ()>=beginTradeTime AND TimeNum ()<=endTradeTime; timeWindowCalc= TimeNum ()>=beginCalcTime AND TimeNum ()<=endCalcTime; firstBarOfDay= TimeNum ()>=beginCalcTime ;firstBarOfDay=(firstBarOfDay- Ref (firstBarOfDay,-1))==1; lastBarOfDay= TimeNum ()>=endCalcTime;lastBarOfDay=(lastBarOfDay- Ref (lastBarOfDay,-1))==1; firstTradeBarOfDay= TimeNum ()>=beginTradeTime;firstTradeBarOfDay=(firstTradeBarOfDay- Ref (firstTradeBarOfDay,-1))==1; lastTradeBarOfDay= TimeNum ()>=endTradeTime;lastTradeBarOfDay=(lastTradeBarOfDay- Ref (lastTradeBarOfDay,-1))==1; ttt=firstBarOfDay OR lastBarOfDay; inTrade= Flip (firstBarOfDay,LastBarOfDay); procedure entryExit_proc(Buy1,Short1,stopLong,stopShort,BY,BT1,BT2,BT3,SH,ST1,ST2,ST3,timearr,endTradeTime,TickSize) { global BuyAdjusted; global BuyPriceAdjusted; global BuyAdjustedScale1; global BuyAdjustedScale2; global BuyPriceAdjustedScale1; global BuyPriceAdjustedScale2; global ShortAdjusted; global ShortPriceAdjusted; global ShortAdjustedScale1; global ShortAdjustedScale2; global ShortPriceAdjustedScale1; global ShortPriceAdjustedScale2; global SellAdjusted; global SellPriceAdjusted; global CoverAdjusted; global CoverPriceAdjusted; global NContractArray; BuyAdjusted=0; BuyPriceAdjusted=0; BuyAdjustedScale1=0; BuyAdjustedScale2=0; BuyAdjustedScale3=0; BuyPriceAdjustedScale1=0; BuyPriceAdjustedScale2=0; BuyPriceAdjustedScale3=0; ShortAdjusted=0; ShortPriceAdjusted=0; ShortAdjustedScale1=0; ShortAdjustedScale2=0; ShortAdjustedScale3=0; ShortPriceAdjustedScale1=0; ShortPriceAdjustedScale2=0; ShortPriceAdjustedScale3=0; SellAdjusted=0; SellPriceAdjusted=0; CoverAdjusted =0; CoverPriceAdjusted=0; NContractArray=0; delay=0; slip=TickSize*0; for (i=1;i< BarCount ;i++) { exitType=0; if (Buy1[i]) { BuyAdjusted[i]=1; BuyPriceAdjusted[i]= O [i]; NContractArray[i]=3; for (j=i+delay;j< BarCount ;j++) { if (j>i) NContractArray[j]=NContractArray[j-1]; if (timearr[j-1]>=endTradeTime) { SellAdjusted[j]=1; SellPriceAdjusted[j]= O [j]-slip; NContractArray[j]=0; i=j; break ; } else if ( C [j-1]<stopLong[j-1]) { SellAdjusted[j]=1; SellPriceAdjusted[j]= O [j]-slip; NContractArray[j]=0; i=j; break ; } else if ( H [j]>BT3[j] AND exitType==0) //1 { exitType=2; BuyAdjustedScale1[j]=sigScaleOut; BuyPriceAdjustedScale1[j]= Max ( O [j],BT1[j])-slip; NContractArray[j]=NContractArray[j-1]-1; BuyAdjustedScale2[j]=sigScaleOut; BuyPriceAdjustedScale2[j]= Max ( O [j],BT2[j])-slip; NContractArray[j]=NContractArray[j-1]-1; SellAdjusted[j]=1; SellPriceAdjusted[j]= Max ( O [j],BT3[j])-slip; NContractArray[j]=0; i=j; break ; } else if ( H [j]>BT3[j] AND exitType==1) //2 { exitType=2; BuyAdjustedScale2[j]=sigScaleOut; BuyPriceAdjustedScale2[j]= Max ( O [j],BT2[j])-slip; NContractArray[j]=NContractArray[j-1]-1; SellAdjusted[j]=1; SellPriceAdjusted[j]= Max ( O [j],BT3[j])-slip; NContractArray[j]=0; i=j; break ; } else if ( H [j]>BT2[j] AND H [j]<=BT3[j] AND exitType==0) //3 { exitType=2; BuyAdjustedScale1[j]=sigScaleOut; BuyPriceAdjustedScale1[j]= Max ( O [j],BT1[j])-slip; NContractArray[j]=NContractArray[j-1]-1; BuyAdjustedScale2[j]=sigScaleOut; BuyPriceAdjustedScale2[j]= Max ( O [j],BT2[j])-slip; NContractArray[j]=NContractArray[j-1]-1; } else if ( H [j]>BT1[j] AND H [j]<=BT2[j] AND exitType==0) //4 { exitType=1; BuyAdjustedScale1[j]=sigScaleOut; BuyPriceAdjustedScale1[j]= Max ( O [j],BT1[j])-slip; NContractArray[j]=NContractArray[j-1]-1; } else if ( H [j]>BT2[j] AND H [j]<=BT3[j] AND exitType==1) //5 { exitType=2; BuyAdjustedScale2[j]=sigScaleOut; BuyPriceAdjustedScale2[j]= Max ( O [j],BT2[j])-slip; NContractArray[j]=NContractArray[j-1]-1; } else if ( H [j]>BT3[j] AND exitType==2) //6 { SellAdjusted[j]=1; SellPriceAdjusted[j]= Max ( O [j],BT3[j])-slip; NContractArray[j]=0; i=j; break ; } else if (j== BarCount -1) { i= BarCount ; break ; } } } else if (Short1[i]) { ShortAdjusted[i]=1; ShortPriceAdjusted[i]= O [i]; NContractArray[i]=-3; for (j=i+delay;j< BarCount ;j++) { if (j>i) NContractArray[j]=NContractArray[j-1]; if (timearr[j-1]>=endTradeTime) { CoverAdjusted[j]=1; CoverPriceAdjusted[j]= O [j]+slip; NContractArray[j]=0; i=j; break ; } else if ( C [j-1]>stopShort[j-1]) { CoverAdjusted[j]=1; CoverPriceAdjusted[j]= O [j]+slip; NContractArray[j]=0; i=j; break ; } else if ( L [j]<ST3[j] AND exitType==0) //1 { ShortAdjustedScale1[j]=sigScaleOut; ShortPriceAdjustedScale1[j]= Min ( O [j],ST1[j])+slip; NContractArray[j]=NContractArray[j-1]+1; ShortAdjustedScale2[j]=sigScaleOut; ShortPriceAdjustedScale2[j]= Min ( O [j],ST2[j])+slip; NContractArray[j]=NContractArray[j-1]+1; CoverAdjusted[j]=1; CoverPriceAdjusted[j]= Min ( O [j],ST3[j])+slip; NContractArray[j]=0; i=j; break ; } else if ( L [j]<ST3[j] AND exitType==1) //2 { ShortAdjustedScale2[j]=sigScaleOut; ShortPriceAdjustedScale2[j]= Min ( O [j],ST2[j])+slip; NContractArray[j]=NContractArray[j-1]+1; CoverAdjusted[j]=1; CoverPriceAdjusted[j]= Min ( O [j],ST3[j])+slip; NContractArray[j]=0; i=j; break ; } else if ( L [j]<ST2[j] AND L [j]>=ST3[j] AND exitType==0) //3 { exitType=2; ShortAdjustedScale1[j]=sigScaleOut; ShortPriceAdjustedScale1[j]= Min ( O [j],ST1[j])+slip; NContractArray[j]=NContractArray[j-1]+1; ShortAdjustedScale2[j]=sigScaleOut; ShortPriceAdjustedScale2[j]= Min ( O [j],ST2[j])+slip; NContractArray[j]=NContractArray[j-1]+1; } else if ( L [j]<ST1[j] AND L [j]>=ST2[j] AND exitType==0) //4 { exitType=1; ShortAdjustedScale1[j]=sigScaleOut; ShortPriceAdjustedScale1[j]= Min ( O [j],ST1[j])+slip; NContractArray[j]=NContractArray[j-1]+1; } else if ( L [j]<ST2[j] AND L [j]>=ST3[j] AND exitType==1) //5 { exitType=2; ShortAdjustedScale2[j]=sigScaleOut; ShortPriceAdjustedScale2[j]= Min ( O [j],ST2[j])+slip; NContractArray[j]=NContractArray[j-1]+1; } else if ( L [j]<ST3[j] AND exitType==2) //6 { CoverAdjusted[j]=1; CoverPriceAdjusted[j]= Min ( O [j],ST3[j])+slip; NContractArray[j]=0; i=j; break ; } else if (j== BarCount -1) { i= BarCount ; break ; } } } } } myHigh= ValueWhen (timeWindowCalc, HighestSince (firstBarOfDay, High )); myLow= ValueWhen (timeWindowCalc, LowestSince (firstBarOfDay, Low )); myClose= ValueWhen (lastBarOfDay, C ); DH= TimeFrameCompress (myHigh, inDaily ,compressLast); DL= TimeFrameCompress (myLow, inDaily ,compressLast); DC= TimeFrameCompress (myClose, inDaily ,compressLast); DH= Ref (DH,-1);DL= Ref (DL,-1);DC= Ref (DC,-1); YHigh= TimeFrameExpand (DH, inDaily ,expandFirst); YLow= TimeFrameExpand (DL, inDaily ,expandFirst); YClose= TimeFrameExpand (DC, inDaily ,expandFirst); fact=1.1; R3=YClose+((YHigh-Ylow)*fact)/4; R4=YClose+((YHigh-Ylow)*fact)/2; R5=((YHigh-Ylow)*0.82)+YClose; R6=((YHigh-Ylow)*1.09)+YClose; R7=((YHigh-Ylow)*1.35)+YClose; R8=((YHigh-Ylow)*1.62)+YClose; S3=YClose-((YHigh-Ylow)*fact)/4; S4=YClose-((YHigh-Ylow)*fact)/2; S5=YClose-((YHigh-Ylow)*0.82); S6=YClose-((YHigh-Ylow)*1.09); S7=YClose-((YHigh-Ylow)*1.35); S8=YClose-((YHigh-Ylow)*1.62); BY=R4;BT1=R6;BT2=R7;BT3=R8; SH=S4;ST1=S6;ST2=S7;ST3=S8; Buy1= Cross ( C ,BY) AND C <BT1;Buy1= Ref (Buy1,-1) AND timeWindowTrade; Short1= Cross (SH, C ) AND C >ST1;Short1= Ref (Short1,-1) AND timeWindowTrade; stopLong=(R3+R4)/2;stopShort=(S3+S4)/2; entryExit_proc(Buy1,Short1,stopLong,stopShort,BY,BT1,BT2,BT3,SH,ST1,ST2,ST3, TimeNum (),endTradeTime,TickSize); SetChartOptions (0, chartShowArrows | chartShowDates ); SetChartBkColor ( ColorRGB (0,0,0)); SetBarFillColor ( IIf ( C > O , ColorRGB (0,75,0), IIf ( C <= O , ColorRGB (75,0,0), colorLightGrey ))); Plot ( C , "\nPrice" , IIf ( C > O , ColorRGB (0,255,0), IIf ( C <= O , ColorRGB (255,0,0), colorLightGrey )),64,0,0,0,0); if ( Interval ()< inDaily ) { PlotShapes ( IIf (BuyAdjusted, shapeUpArrow , shapeNone ), colorGreen ,0, L ,-15); PlotShapes ( IIf (BuyAdjusted, shapeSmallCircle , shapeNone ), ColorRGB (255,20,147),0,BuyPriceAdjusted,0); PlotShapes ( IIf (BuyAdjusted, shapeHollowSmallCircle , shapeNone ), ColorRGB (255,255,255),0,BuyPriceAdjusted,0); PlotShapes ( IIf (SellAdjusted, shapeDownArrow , shapeNone ), colorRed ,0, H ,-15); PlotShapes ( IIf (SellAdjusted, shapeSmallCircle , shapeNone ), ColorRGB (255,20,147),0,SellPriceAdjusted,0); PlotShapes ( IIf (SellAdjusted, shapeHollowSmallCircle , shapeNone ), ColorRGB (255,255,255),0,SellPriceAdjusted,0); PlotShapes ( IIf (ShortAdjusted, shapeSmallDownTriangle , shapeNone ), colorRed ,0, H , IIf (ShortAdjusted AND SellAdjusted,-30,-15)); PlotShapes ( IIf (ShortAdjusted, shapeSmallCircle , shapeNone ), ColorRGB (255,20,147),0,ShortPriceAdjusted,0); PlotShapes ( IIf (ShortAdjusted, shapeHollowSmallCircle , shapeNone ), ColorRGB (255,255,255),0,ShortPriceAdjusted,0); PlotShapes ( IIf (CoverAdjusted, shapeSmallUpTriangle , shapeNone ), colorGreen ,0, L , IIf (CoverAdjusted AND BuyAdjusted,-30,-15)); PlotShapes ( IIf (CoverAdjusted, shapeSmallCircle , shapeNone ), ColorRGB (255,20,147),0,CoverPriceAdjusted,0); PlotShapes ( IIf (CoverAdjusted, shapeHollowSmallCircle , shapeNone ), ColorRGB (255,255,255),0,CoverPriceAdjusted,0); PlotShapes ( IIf (BuyAdjustedScale1, shapeSmallDownTriangle , shapeNone ), colorOrange ,0, H ,-15); PlotShapes ( IIf (BuyAdjustedScale1, shapeSmallCircle , shapeNone ), ColorRGB (255,20,147),0,BuyPriceAdjustedScale1,0); PlotShapes ( IIf (BuyAdjustedScale1, shapeHollowSmallCircle , shapeNone ), ColorRGB (255,255,255),0,BuyPriceAdjustedScale1,0); PlotShapes ( IIf (BuyAdjustedScale2, shapeSmallDownTriangle , shapeNone ), colorOrange ,0, H ,-15); PlotShapes ( IIf (BuyAdjustedScale2, shapeSmallCircle , shapeNone ), ColorRGB (255,20,147),0,BuyPriceAdjustedScale2,0); PlotShapes ( IIf (BuyAdjustedScale2, shapeHollowSmallCircle , shapeNone ), ColorRGB (255,255,255),0,BuyPriceAdjustedScale2,0); PlotShapes ( IIf (ShortAdjustedScale1, shapeSmallUpTriangle , shapeNone ), colorBlue ,0, L ,-15); PlotShapes ( IIf (ShortAdjustedScale1, shapeSmallCircle , shapeNone ), ColorRGB (255,20,147),0,ShortPriceAdjustedScale1,0); PlotShapes ( IIf (ShortAdjustedScale1, shapeHollowSmallCircle , shapeNone ), ColorRGB (255,255,255),0,ShortPriceAdjustedScale1,0); PlotShapes ( IIf (ShortAdjustedScale2, shapeSmallUpTriangle , shapeNone ), colorBlue ,0, L ,-15); PlotShapes ( IIf (ShortAdjustedScale2, shapeSmallCircle , shapeNone ), ColorRGB (255,20,147),0,ShortPriceAdjustedScale2,0); PlotShapes ( IIf (ShortAdjustedScale2, shapeHollowSmallCircle , shapeNone ), ColorRGB (255,255,255),0,ShortPriceAdjustedScale2,0); Plot ( IIf (timeWindowTrade,BY, Null ), "BY" , colorLime , styleLine | styleNoRescale | styleNoTitle ); Plot ( IIf (timeWindowTrade,BT1, Null ), "BT1" , colorGreen , styleLine | styleNoRescale | styleNoTitle ); Plot ( IIf (timeWindowTrade,BT2, Null ), "BT2" , colorGreen , styleLine | styleNoRescale | styleNoTitle ); Plot ( IIf (timeWindowTrade,BT3, Null ), "BT3" , colorGreen , styleLine | styleNoRescale | styleNoTitle ); Plot ( IIf (timeWindowTrade,SH, Null ), "SH" , colorRed , styleLine | styleNoRescale | styleNoTitle ); Plot ( IIf (timeWindowTrade,ST1, Null ), "ST1" , colorBrown , styleLine | styleNoRescale | styleNoTitle ); Plot ( IIf (timeWindowTrade,ST2, Null ), "ST2" , colorBrown , styleLine | styleNoRescale | styleNoTitle ); Plot ( IIf (timeWindowTrade,ST3, Null ), "ST3" , colorBrown , styleLine | styleNoRescale | styleNoTitle ); Plot ( IIf (timeWindowTrade,stopLong, Null ), "" , colorYellow , styleDashed ); Plot ( IIf (timeWindowTrade,stopShort, Null ), "" , colorYellow , styleDashed ); Plot (firstTradeBarOfDay, "" , ColorRGB (0,0,255), styleHistogram | styleThick | styleOwnScale | styleNoLabel ,0,1,0,0); Plot (lastTradeBarOfDay, "" , ColorRGB (200,200,200), styleHistogram | styleThick | styleOwnScale | styleNoLabel ,0,1,0,0); for (i=fvb;i<=lvb;i++) { if (displaySignalAnnotation) { if (BuyAdjusted[i]==1) PlotText ( "Buy" ,i+2, L [i], colorWhite , colorGreen ); if (BuyAdjustedScale1[i]==sigScaleOut) PlotText ( "Scale Out" ,i+2, H [i], colorBlack , colorYellow ); if (BuyAdjustedScale2[i]==sigScaleOut) PlotText ( "Scale Out" ,i+2, H [i], colorBlack , colorYellow ); if (SellAdjusted[i]==1) PlotText ( "Sell" ,i+2, H [i], colorWhite , colorRed ); if (ShortAdjusted[i]==1) PlotText ( "Short" ,i+2, H [i], colorWhite , colorRed ); if (ShortAdjustedScale1[i]==sigScaleOut) PlotText ( "Scale Out" ,i+2, L [i], colorBlack , colorYellow ); if (ShortAdjustedScale2[i]==sigScaleOut) PlotText ( "Scale Out" ,i+2, L [i], colorBlack , colorYellow ); if (CoverAdjusted[i]==1) PlotText ( "Cover" ,i+2, L [i], colorWhite , colorGreen ); } if (displayLineAnnotation) { if (lastTradeBarOfDay[i]) { PlotText ( "ENTRY LONG" ,i+3,BY[i], colorWhite ); PlotText ( "TARGET 1" ,i+3,BT1[i], colorWhite ); PlotText ( "TARGET 2" ,i+3,BT2[i], colorWhite ); PlotText ( "TARGET 3" ,i+3,BT3[i], colorWhite ); PlotText ( "ENTRY SHORT" ,i+3,SH[i], colorWhite ); PlotText ( "TARGET 1" ,i+3,ST1[i], colorWhite ); PlotText ( "TARGET 2" ,i+3,ST2[i], colorWhite ); PlotText ( "TARGET 3" ,i+3,ST3[i], colorWhite ); PlotText ( "LONG STOP" ,i+3,stoplong[i], colorRed ); PlotText ( "SHORT STOP" ,i+3,stopshort[i], colorRed ); } } } } |