I am using ParamDate to enter the BuyDate for some code which plots a trailing stop. The full code is copied into this message for your use if you find it helpful.  The trailing stop begins on the BuyDate which is entered via the ParamDate function.
 
I now would like to apply this same code to intraday data and when I do, it works except that the start point of the trailing stop (as entered via the ParamDate function, only starts on the beginning bar of each full day.
 
I have read all the help files on Intraday use but am not able to see a way to enter the buy day and time.  I do not want to use Range Markers or the vertical pole to indicate the buy date/time.  The reason for this is that this code can be applied to actual purchases, and would be plotted in numerous panes.  Using the Range markers would change the displayed values for all panes and you want the BuyDate for each pane (as well as its symbol) to remain independent.
 
I want to use this code on a QuoteTracker 1 min database.  I do not wish to mix database times (although I change the display interval at times).
 
Can someone suggest a way to make this work on intraday time intervals?
 
Thanks,
 
Ken
 
Note, there are long lines and line wrap in the message display may cause problems if you just copy/paste.
 

// TRAILINGSTOP_RT.AFL KSC 10/5/2006

// Code contributions by Mark Keitel and Bill Barnard

// Use Parameter Box to set BuyDate, Stop Percent, EMA Length and

// to display candlesticks or line for close price

_SECTION_BEGIN("MW Holding DisplayMA_DW 3");

SetChartOptions(2,chartShowDates);

GraphXSpace = 7;

// next two lines added by Bill Barnard to allow plotting of other than current symbol if a

// symbol is typed into the box

symb = ParamStr("Symbol", "") ;

if (symb != "") SetForeign(symb);

// ============== Use Ctrl-R to change Parameters ======

Drawdown = Param("1) DrawDown Limit",2,0,15,0.1);

EMAFactor = Param("2) EMA Days",20,1,80,1);

BuyDate = ParamDate("BuyDate","08/21/2006",0);

ShowStix = ParamToggle("ShowCandleSticks","No|Yes");

Defaultdate = 1060103; // Set manually to equal date in quotes in above statement

// ============== End Paramters =======================

Buydate = 1060929.1259;

Holding = C;

Hol= FullName();

//Next few lines added by Mark Keitel to show this as CandleSticks and not line

//graph for Mutual Funds

if(NOT Showstix)

{

f_op = IIf(BarIndex() > 0, Ref(C,-1), C); // fund's open price

f_hi = Max(f_op, C);

f_lo = Min(f_op, C);

PlotOHLC(f_op, f_hi, f_lo, C, "", colorRed, styleCandle);

}

else

{

Plot(C,"",colorBlack,styleLine);

}

AllBars = BarIndex();

AllDates = DateNum();

//Calculate the bars that are displayed and to the left of the current date. Seebar=1 for those

SeeBar = Status("barvisible");

NoBuyDate = ValueWhen(Ref(SeeBar,-5)==0 AND Seebar, AllDates,1);

//NoBuyDate = ValueWhen(

LeftRange = BuyDate;//

BuyP = ValueWhen(DateNum()==BuyDate,C,1);

//Now calculate the year, month and day using the fact that

//BuyDate=(BuyYear-1900)*10000 + BuyMonth*100 + BuyDay;

BuyYear = int(BuyDate/10000) + 1900;

BuyMonth = int(((BuyDate) - (BuyYear-1900)*10000)/100);

BuyDay = BuyDate - (BuyYear-1900)*10000 - BuyMonth*100;

//Now make YesNo a vector which =1 after the BuyDate and =0 on or before

YesNo = IIf(DateNum()>Buydate,1,0);

// Iterate through to get the maximum of the holding

Maxt = Holding;

for( i = 1; i < BarCount; i++ )

{

if (YesNo[i]>0.5)

{

if (Holding[i]>Maxt[i-1])

{

Maxt[i]=Holding[i];

}

else

{

Maxt[i]=Maxt[i-1];

}

}

}

Mint = round(100*Maxt * (1 - Drawdown/100))/100;

HolEMA = EMA(Holding,EMAFactor);

//Make the Min the same as the holding before the purchase date (which is BuyDate)

MinT = IIf(DateNum()>BuyDate,Mint,Holding);

// Calculate the Annualized rate of gain based on the last 1, 2 and 3 months (21, 42 and 63 days)

>100 * (Holding - Ref(Holding, -5))/Ref(Holding, -5);

TwOMo = 100 * (Holding - Ref(Holding, -10))/Ref(Holding, -10);

ThrMo = 100 * (Holding - Ref(Holding, -15))/Ref(Holding, -15);

// Calculate distance from current close to the current stop

StopDelP = 100 * (Holding - MinT)/Holding;

// Calculate Average Tru Range for previous ENA Period

ATRange = ATR(EMAFactor);

ATRangeP = 100 * (ATRange / Holding);

// Calc RSI/Stoc

// Trade Ema(100*(|0| - Min(|0|,21))/(Max(|0|,21) - Min(|0|,21)),13)/2 + Rsi(|0|,14)/2

RSIStoc = EMA(100*(Holding - LLV(Holding,21))/(HHV(Holding,21) - LLV(Holding,21)),13);

RSIStoc = RSIStoc + RSIa(Holding,14);

RSIStoc = RSIStoc/2;

RSIColor = IIf(RSIStoc>65,colorBrightGreen,IIf(RSIStoc>50,colorYellow,colorRed));

// Calculate Standard Deviation of fund

Period = 252;

sdf = StDev(ROC(C, 1), Period) * sqrt(21.15);

//Calculate Gain so Far

GainDays = BarsSince(AllDates == BuyDate); //How many days since the pole was set

//NoDisplay = BuyDate==DefaultDate OR (DateNum()<DefaultDate);

if(BuyDate==DefaultDate) //No Start of Range Set or no days since date set

{

// GainSoFar is null

GainSoFar = "";

}

else //A Start of range has been set

{

GainDays = BarsSince(AllDates == BuyDate); //Days since the BuyDate

PriceAtStart = Ref(Holding, -GainDays);

PCGainNow = 100 * (Holding - PriceAtStart)/PriceAtStart;

PCGainAtEMA = 100 * (HolEMA - PriceAtStart)/PriceAtStart;

PCGainAtEMA = IIf(BuyDate==AllDates,0,PCGainAtEMA); //Force gain to zero before date set

PCGainAtSell = 100 * (MinT - PriceAtStart)/PriceAtStart;

GainColor = IIf(PCGainNow>0,colorGreen,colorRed);

GainSoFar = EncodeColor(colorViolet) + "\nGain from --- "

+ NumToStr(BuyMonth,1.0) + "/" + NumToStr(BuyDay,1.0) + "/" + NumToStr(BuyYear,1.0)

+ ":"

+ "\\c42" + " " + "\\c" + Gaincolor + " At current Close = " + NumToStr(PCGainNow,1.1) + "%"

+ EncodeColor(colorLightBlue) + ","

+ EncodeColor(colorRed) + " At Stop = " + NumToStr(PCGainAtSell,1.1) + "%"

+ EncodeColor(colorViolet) + "\nDistance from Current Stop to Close = "

+ EncodeColor(colorRed) + WriteVal(StopDelP,1.1) + "%";

}

BuyDateDisp = NumToStr(BuyMonth,1.0) + "/" + NumToStr(BuyDay,1.0) + "/" + NumToStr(BuyYear,1.0);

Title = Date() + " " +Name() + " -- " + Hol + " -- Close = $" + WriteVal(Holding,1.2)

+ " BuyPrice = $" + WriteVal(BuyP,1.2) + " BuyDate = " + BuyDateDisp

+ "\nGain (last "

+ EncodeColor(colorGreen) + "3"

+ EncodeColor(colorViolet) + ", "

+ EncodeColor(colorBlue) + "2"

+ EncodeColor(colorViolet) + ", "

+ EncodeColor(colorRed) + "1"

+ EncodeColor(colorViolet) + " wk. [from Pole or at last bar) is "

+ EncodeColor(colorGreen) + WriteVal(ThrMo,1.1) + "%"

+ EncodeColor(colorViolet) + ", "

+ EncodeColor(colorBlue) + WriteVal(TwOMo,1.1) + "%"

+ EncodeColor(colorViolet) + ", "

+ EncodeColor(colorRed) + WriteVal(OneMo,1.1) + "%"

+ EncodeColor(colorRed) + "\nSell Point ($" + WriteVal(Mint,1.2) + " - at "

+ WriteVal(Drawdown,1.1) + "% Loss) is in Red"

+ EncodeColor(colorBlue) + "\n" + EMAFactor + " Day EMA is in Blue ($" + WriteVal(HolEMA,1.2) + ")"

+ EncodeColor(colorRed)+ "\nRSI/Stochastic is "

+ "\\c" + RSIcolor +" " + NumToStr(RSIStoc,1.0)

+ EncodeColor(colorRed) + " 3 x Avg Tru Range last " + EMAFactor + " days = " + WriteVal(3 * ATRange,1.2)

+ " (" + WriteVal(3 * ATRangeP,1.2)+")%"

+ GainSoFar;

// + EncodeColor(colorBlue) + "\nBarIndex() = " + WriteVal(allBars,1.0)

// + "\nCum(1) = " + WriteVal(Cum(1),1.0);

//if(ShowStix)

//{Plot(Holding,"Holding",RSIColor,5);

Plot(Holding,"Holding",RSIColor,5);

Plot(Maxt,"Maximum",colorGreen,5);

Plot(Mint,"Drawdown",colorRed,5);

Plot(HolEMA,"MA",colorBlue,5);

Sellsig = Cross(Mint, Holding);

PlotShapes(Sellsig*shapeDownArrow, colorRed, 0, Holding, 50);

On = DateNum()==BuyDate;

PlotShapes(On*shapeUpTriangle,colorBrightGreen,0,Holding,50);

 
 
__._,_.___

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html






SPONSORED LINKS
Software support Small business finance Business finance online
Business finance training Business finance course

Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___

Reply via email to