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 ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

oz trail short v5b for Amibroker (AFL)

Copy & Paste Friendly
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
_SECTION_BEGIN("Oz Trail Stop Short");
// 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/2013",0);
per1=Param("Length ATR",20,1,150,1); //ATR length
fac1=Param("ATR Multiple (Initial)",1,0.1,10,0.1); //Initial ATR multiple, first bar
fac2=Param("ATR Multiple (Trail)",2,0.1,10,0.1); //Trail ATR multiple
tog1=ParamToggle("Trail value","Close|High&Low",1); //trail calculated with respect to the High or the Close price
tog2=ParamToggle("Display Exit Levels","No|Yes",1); //
nlev=Param("Number of target Levels",2,0,12,1);
levMultiple=Param("Target Level Multiple",2,0,12,1);
entryPrice=Param("Entry Price Trade",0,0,150000,0.01); //entry price, use 0 for Close price
trg1=ParamTrigger("Save Changes", "Click Here"); //press to save changes every time you change parameter settings
trg2=ParamTrigger("Remove Persistent Variable", "Click Here"); //remove persistant trail for active symbol
 
idx1=idx2=0;bi=BarIndex();
 
if(entryPrice==0)
{
    ttt=IIf(DateNum()>=selectDate,1,0);
    ttt=ttt-Ref(ttt,-1);
    entryPrice=LastValue(ValueWhen(ttt,C));
}
function conDate(nDate)
{
    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= mm + "/" + dd + "/" + NumToStr(aa1,1,False);
    return result;
}
function PersistentArraySet(VarName,infoArray)
{
    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)
{
    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;
}
function PersistentVarRemove(VarName)
{
    Fn=PersistentPath+VarName+".pva";
    fh=fdelete(Fn) ;
}
 
infoArraySHORT=Null;
infoArraySHORT[0]=selectdate;
infoArraySHORT[1]=per1;
infoArraySHORT[2]=fac1;
infoArraySHORT[3]=fac2;
infoArraySHORT[4]=tog1;
infoArraySHORT[5]=LastValue(entryPrice);
infoArraySHORT[6]=nlev;
infoArraySHORT[7]=levMultiple;
 
infoSHORT=PersistentArrayGet("SHORT"+Name());
lvspSHORT=LastValue(Cum(infoSHORT));
 
if(trg1)PersistentArraySet("SHORT"+Name(),infoArraySHORT);
if(trg2)PersistentVarRemove("SHORT"+Name());
 
procedure displayTargetLevels(trailArray,entryPriceArr,idx1,idx2,unitStop,nlevArr,levMultipleArr)
{
    validWindow=Flip(idx1==bi,idx2==bi);
    for(i=1;i<=nlevArr;i++)
    {
        ll=IIf(trailArray,entryPriceArr-unitStop*i*levMultipleArr,Null);
        if(LastValue(Cum(ll))>0)
        {
            sig=Cross(ll,L) AND validWindow;sig=ExRem(sig,idx2==bi);
            Plot(ll,"",ColorRGB(60,60,60),styleLine,Null,Null,0,-1,1);
            PlotText(""+ll[idx1],BarCount,ll[idx1],ColorRGB(100,100,100));
            PlotShapes(IIf(sig,shapeSmallCircle,shapeNone),colorLightBlue,0,Min(O,ll),0);
            PlotShapes(IIf(sig,shapeUpArrow,shapeNone),colorLightBlue,0,L,-15);
        }
    }
    // entry signal
    PlotShapes(IIf(idx1==bi,shapeSmallCircle,shapeNone),colorOrange,0,EntryPriceArr,0);
    PlotShapes(IIf(idx1==bi,shapeDownArrow,shapeNone),colorRed,0,H,-15);       
    // exit signal
    PlotShapes(IIf(idx2==bi,shapeSmallCircle,shapeNone),colorLightBlue,0,C,0);
    PlotShapes(IIf(idx2==bi,shapeUpArrow,shapeNone),colorGreen,0,L,-15);   
    // entry level
    th=IIf(trailArray,entryPriceArr,Null);//+unitStop;
    Plot(th,"",ColorRGB(0,0,160),styleLine,Null,Null,0,-1,1);
    PlotText("Entry "+th[idx1],BarCount,th[idx1],ColorRGB(0,0,200));
    // background color trade
    Plot(validWindow,"",ColorRGB(10,10,10),styleArea|styleOwnScale|styleNoLabel,0,1,0,-5);
}
function vstopShort_func(ATRBear,fac1,fac2,tog1,per,idx1,entryPrice)
{
    trailArray=Null;
    if(tog1)
    {
        hh=H;
        ll=L;
    }
    else
    {
        hh=C;
        ll=C;  
    }
 
    trailArray[idx1]=entryPrice+fac1*ATRBear[idx1];
 
    // calculate trail
    if((idx1+1)<BarCount AND idx1>0)
    {
        cnt=0; 
        for(i=idx1+1;i<BarCount;i++)
        {
            prev=trailArray[i-1];cnt=i;
             
            if (C[i]<=prev AND C[i-1]<=prev)//short continuation
            {
                trailArray[i]=Min(prev,ll[i]+fac2*ATRBear[i]);
            }
            else if (C[i]>prev AND C[i-1]<=prev)//long trigger
            {
                idx2=i;
                break;
            }          
        }
        for(i=cnt;i<BarCount;i++)
        {
            trailArray[i]=trailArray[i-1];
        }      
    }
    return trailArray; 
}
 
selectDateArr=infoSHORT[0];
per1Arr=infoSHORT[1];
fac1Arr=infoSHORT[2];
fac2Arr=infoSHORT[3];
tog1Arr=infoSHORT[4];
entryPriceArr=infoSHORT[5];
nlevArr=infoSHORT[6];
levMultipleArr=infoSHORT[7];
 
if(lvspSHORT>0)
{
    tt=IIf(DateNum()>=selectDateArr,1,0);
    tt=tt-Ref(tt,-1);idx1=LastValue(ValueWhen(tt,bi));
    ATRBear=ATR(per1Arr);
    trailArray=vstopShort_func(ATRBear,fac1Arr,fac2Arr,tog1Arr,per1Arr,idx1,entryPriceArr);trailArray=IIf(trailArray==0,Null,trailArray);
}
else
{
    trailArray=Null;
}
 
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,Null,Null,0,0,1);
Plot(trailArray,"\ntrailShort",ColorRGB(255,5,0),styleStaircase,Null,Null,0,0,1);
 
if(lvspSHORT>0)
{  
    if(tog1Arr==0)trailRef="Close";
    else trailRef="High";
 
    unitStop=(trailArray[idx1]-entryPriceArr);
    barsInTrade=0;
    if(idx1>0 AND idx2==0)
    {
        barsInTrade=BarsSince(idx1==bi)+1;
    }
    else if(idx1>0 AND idx2>0)
    {
        barsIntrade=(idx2-idx1)+1;
    }
 
    Title= EncodeColor(colorRed) +
    "POSITION SHORT\n" +
    EncodeColor(colorWhite) +
    "Entry Date: " + conDate(selectDateArr) +"\n"+
    "ATR Period: " + per1Arr +"\n"+
    "ATR Multiple (Initial): " + fac1Arr +"\n"+
    "ATR Multiple (Trail): " + fac2Arr +"\n"+
    "Trail reference: " + trailRef +"\n"+
    "Entry Price: " + entryPriceArr +"\n"+
    "Number of Levels: " + nlevArr +"\n"+
    "Multiple of Unitstop: " + levMultiple +"\n"+  
    EncodeColor(colorYellow) +
    "UnitStop: " + unitStop +"\n"+
    "Bars in Trade: " + barsInTrade;   
     
    if(tog2 AND nlevArr>0 AND levMultipleArr>0)
    {
        displayTargetLevels(trailArray,entryPriceArr,idx1,idx2,unitStop,nlevArr,levMultipleArr);
    }
}
_SECTION_END();
Back