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 ....
oz trail v2 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 | _SECTION_BEGIN ( "Oz Trail Stop Long" ); // E.M.Pottasch, Feb 2014 // based on http://www.tradernexus.com/advancedstop/advancedstop.html _N (PersistentPath= "C:\\Program Files\\AmiBroker64\\PersistentVariables\\" ); // **** You need to create a directory to store the persistent variable **** selectDate= ParamDate ( "Start date" , "08/01/2011" ,0); per1= Param ( "Length ATR" ,20,1,150,1); // ATR length fac1= Param ( "Chandelier Factor" ,2,1,10,0.1); // chandelier factor tog1= ParamToggle ( "Trail value" , "Close|High&Low" ,1); entryPrice= Param ( "Entry Price Trade" ,0,0,150000,0.01); //entry price trg1= ParamTrigger ( "Save Changes" , "Click Here" ); if (entryPrice==0) { ttt= IIf ( DateNum ()>=selectDate,1,0); ttt=ttt- Ref (ttt,-1); entryPrice= LastValue ( ValueWhen (ttt, C )); } function conDate(nDate) { result= "" ; string= StrFormat ( "%0.9g" ,nDate); aa= StrLeft (string,3); mm= StrMid (string,3,2); dd= StrRight (string,2); aa1= StrToNum (aa)+1900; // ONLY CORRECT AFTER 2000 result= dd + "/" + mm + "/" + NumToStr (aa1,1, False ); return result; } function PersistentArraySet(VarName,infoArray) { global PersistentPath; fh= fopen ( PersistentPath+VarName+ ".pva" , "w" ); if (fh) { cnt=0; while (! IsEmpty (infoArray[cnt])) { String= NumToStr (infoArray[cnt])+ "\n" ; fputs ( String,fh); cnt=cnt+1; } fclose (fh); } } function PersistentArrayGet(VarName) { global PersistentPath; infoArray=0; fh = fopen ( PersistentPath+VarName+ ".pva" , "r" ); if (fh) { cnt=0; while (! feof (fh)) { infoArray[cnt]= StrToNum ( fgets ( fh )); cnt=cnt+1; } fclose (fh); } return infoArray; } infoArray= Null ; infoArray[0]=selectdate; infoArray[1]=per1; infoArray[2]=fac1; infoArray[3]=tog1; infoArray[4]= LastValue (entryPrice); sdatep=PersistentArrayGet( "ss" + Name ()); lvsp= LastValue ( Cum (sdatep)); if (lvsp==0)PersistentArraySet( "ss" + Name (),infoArray); if (trg1)PersistentArraySet( "ss" + Name (),infoArray); function vstopLong_func(trBull,tog1,per,idx,entryPrice) { trailArray=0; if (tog1) { hh= H ; ll= L ; } else { hh= C ; ll= C ; } trailArray[idx]=entryPrice-trBull[idx]; // calculate trail if ((idx+1)< BarCount AND idx>0) { cnt=0; for (i=idx+1;i< BarCount ;i++) { prev=trailArray[i-1];cnt=i; if ( C [i]>=prev AND C [i-1]>=prev) //long continuation { trailArray[i]= Max (prev,hh[i]-trBull[i]); } else if ( C [i]<prev AND C [i-1]>=prev) //short trigger { break ; } } for (i=cnt;i< BarCount ;i++) { trailArray[i]=trailArray[i-1]; } } return trailArray; } sdatep=PersistentArrayGet( "ss" + Name ()); selectDateArr=sdatep[0]; per1Arr=sdatep[1]; fac1Arr=sdatep[2]; tog1Arr=sdatep[3]; entryPriceArr=sdatep[4]; bi= BarIndex ();tt= IIf ( DateNum ()>=selectDateArr,1,0); tt=tt- Ref (tt,-1);idx= LastValue ( ValueWhen (tt,bi)); trBull=fac1Arr* ATR (per1Arr); trailArray=vstopLong_func(trBull,tog1Arr,per1Arr,idx,entryPriceArr);trailArray= IIf (trailArray==0, Null ,trailArray); if (tog1Arr==0) trailRef= "Close" ; else trailRef= "High" ; Title = "Entry Date: " + conDate(selectDateArr) + "\n" + "ATR Period: " + per1Arr + "\n" + "ATR Multiple: " + fac1Arr + "\n" + "Trail reference: " + trailRef + "\n" + "Entry Price: " + entryPriceArr + "\n" ; GraphXSpace =5; SetChartBkColor ( colorBlack ); SetChartOptions (0, chartShowDates ); 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); Plot (trailArray, "\ntrailLong" , ColorRGB (0,255,0), styleStaircase ); _SECTION_END (); |