[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Re: Entry Time Delay



PureBytes Links

Trading Reference Links

gp_sydney,
 
Your logic makes sense but when I added it I still get no trades being initiated. Below is my code.  For the life of me I don't see why your logic won't work.  Could it be something in the preferences?
 

// Simiple MOVING AVERAGE MULTI TIMEFRAME BACKTEST

// Notes: all trades must be triggered on "Close", "0" days delay

// in the backtest setup.

 

// Parameters

LongPeriods=Param("Long Periods",17,5,21,3);

LongPeriodsLTF=Param("Long PeriodsLTF",9,5,21,3);

NumberofPositions=Param("Positions",2,2,6,1);

FirstProfitTarget=Param("FirstProfitTarget",0.004,0.0001,0.0045,0.0001);

SecondProfitTarget = Param("SecondProfitTarget",0.009,0.0001,0.0125,0.0001);

TrailingStop=Param("TrailStop",0.001,0.0001,0.003,0.0001);

 

TimeFrameSet (in5Minute); // switch to 15 minute time frame

// Calculate Moving Averages

MAShort=MA(C,LongPeriods);

MALong=Ref(MAShort,-2);

// Check for Long or Short Signals

LongSignal = (C > MALong) AND (C > MAShort) AND (MAShort > MALong);

ShortSignal = (C < MALong) AND (C < MAShort) AND (MAShort < MALong);

TimeFrameRestore(); // restore time frame to original

 

TimeFrameSet (in15Minute); // switch to 15 minute time frame

// Calculate Moving Averages

MAShortLT=MA(C,LongPeriodsLTF);

MALongLT=Ref(MAShortLT,-1);

LongSignalLT = (C > MALongLT) AND (C > MAShortLT) AND (MAShortLT > MALongLT);

ShortSignalLT = (C < MALongLT) AND (C < MAShortLT) AND (MAShortLT < MALongLT);

SignalLatchLong = Flip(LongSignalLT,ShortSignalLT);

SignalLatchShort = Flip(ShortSignalLT, LongSignalLT);

TimeFrameRestore(); // restore time frame to original

 

// Points only test is equivalent to trading just one contract. This can be

// easily accomplished using Futures mode of the backtester AND adding the following one line to your formula:

MarginDeposit = 1;

PositionSize=NumberofPositions;

 

 

//Entry and Exit

EntryTimeCondition = IIf ( Minute() == 4 + 0*5, True,

IIf ( Minute() == 4 + 1*5, True,

IIf ( Minute() == 4 + 2*5, True,

IIf ( Minute() == 4 + 3*5, True,

IIf ( Minute() == 4 + 4*5, True,

IIf ( Minute() == 4 + 5*5, True,

IIf ( Minute() == 4 + 6*5, True,

IIf ( Minute() == 4 + 7*5, True,

IIf ( Minute() == 4 + 8*5, True,

IIf ( Minute() == 4 + 9*5, True,

IIf ( Minute() == 4 + 10*5, True,

IIf ( Minute() == 4 + 11*5, True,False))))))))))));//Entry only occurs at the end of each 5 minute bar.

//Bars must be setup in preferences for "Time stamp of compressed intraday bars shows:

// START time of interval" as being checked off.

 

Cover = (TimeFrameExpand(LongSignal, in5Minute) AND EntryTimeCondition)

OR (TimeFrameExpand(SignalLatchLong, in15Minute) AND EntryTimeCondition)

OR TimeNum() > 155800;

 

Sell = TimeFrameExpand(ShortSignal, in5Minute) OR TimeFrameExpand(SignalLatchShort, in15Minute)

OR TimeNum() > 155800;

 

Buy = TimeFrameExpand(LongSignal, in5Minute) AND TimeFrameExpand(SignalLatchLong, in15Minute)

AND (TimeNum()>93300) AND (TimeNum() <153000) AND EntryTimeCondition;

 

Short = TimeFrameExpand(ShortSignal, in5Minute) AND TimeFrameExpand(SignalLatchShort, in15Minute)

AND (TimeNum()>93300) AND (TimeNum() <153000) AND EntryTimeCondition;

 

// Trade Management

PriceatShort = 0;

LowSinceShort = 1000000;

PriceatBuy = 0;

HighSinceBuy = 0;

Exit = 0;

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

{

if(PriceAtBuy == 0 AND Buy[i])

{

PriceAtBuy = BuyPrice[i];

BarCountBuy = BarCount;

}

if(PriceAtBuy > 0)

{

HighSinceBuy = Max(High[i-1] , HighSinceBuy);

if(Exit == 0 AND High[i] >= ((1 + FirstProfitTarget)* PriceAtBuy))

{

// first profit target hit - Scale Out

Exit = 1;

Buy[i] = sigScaleOut;

BuyPrice[i] = ((1 + FirstProfitTarget)* PriceAtBuy) ;

}

if(Exit == 0 AND High[i] >= ((1 + SecondProfitTarget)* PriceAtBuy))

{

// Second profit target hit - Exit

Exit = 2;

Sell[i] = 1;

SellPrice[i] = ((1 + SecondProfitTarget)* PriceAtBuy) ;

}

if((Low[i] <= (HighSinceBuy *(1- TrailingStop))) AND (i != BarCountbuy))

{

// trailing stop hit - exit

Exit = 2;

Sell[i] = 1;

SellPrice[i] = (HighSinceBuy *(1- TrailingStop));

}

if(Exit == 2)

{

Exit = 0;

PriceAtBuy = 0;

HighSinceBuy = 0;

}

}

 

if(PriceatShort == 0 AND Short[i])

{

PriceatShort = ShortPrice[i];

BarCountShort = BarCount;

}

if(PriceatShort > 0)

{

LowSinceShort = Min(Low[i-1] , LowSinceShort);

if(Exit == 0 AND Low[i] <= (PriceatShort *(1 - FirstProfitTarget)))

{

// first profit target hit - Scale Out

Exit = 1;

Short[i] = sigScaleOut;

ShortPrice[i]=(PriceatShort *(1 - FirstProfitTarget));

}

if(Exit == 0 AND Low[i] <= (PriceatShort *(1 - SecondProfitTarget)))

{

// first profit target hit - Scale Out

Exit = 2;

Cover[i] = 1;

CoverPrice[i]=(PriceatShort *(1 - SecondProfitTarget));

}

if((High[i] >= (LowSinceShort * (1 + TrailingStop))) AND (i != BarCountShort))

{

// trailing stop hit - exit

Exit = 2;

Cover[i] = 1;

CoverPrice[i] = (LowSinceShort *(1+ TrailingStop));

}

if(Exit == 2)

{

Exit = 0;

PriceatShort = 0;

LowSinceShort = 1000000;

}

}

}

SetPositionSize(50,spsPercentOfPosition*((Buy == sigScaleOut)+(Short

== sigScaleOut)));

//scale out of 50% of position

 

Mask = BarsSince(Sell) >= 5 AND BarsSince(Cover) >= 5;

Mask = IIf(IsNull(Mask), True, Mask); // Convert Nulls to Trues

Buy = Buy AND Mask;

Short = Short AND Mask;

----- Original Message -----
From: gp_sydney
Sent: Tuesday, July 03, 2007 6:58 PM
Subject: [amibroker] Re: Entry Time Delay

Fred,

The problem you're describing is because the BarsSince array returns
Null values before the first True in the array you're testing. If you
convert them back to True values, then your statements should work:

Mask = BarsSince(Sell) >= 5 AND BarsSince(Cover) >= 5;
Mask = IIf(IsNull(Mask), True, Mask); // Convert Nulls to Trues
Buy = Buy AND Mask;
Short = Short AND Mask;

Now the Mask array will start off with all True values before the
first Sell or Cover signal, allowing earlier Buy and Short signals to
remain.

GP

--- In amibroker@xxxxxxxxxps.com, Ed Middleton <jjj_98@xxx> wrote:
>
> Herman,
>
> Actually I think you meant to type:
>
> Buy = Buy AND BarsSince(Sell)>=5 AND BarsSince(Cover) >=5;
>
> similarily,
>
> Short = Short AND BarsSince(Sell)>=5 AND BarsSince(Cover) >=5;
>
> In my head this should only allow a "Buy" (or "Short") 5 bars after
the last trade exit.
>
> Now the problem with this approach is that there cannot be a Sell Or
Cover without a trade (A Buy or Short)first being put on by
definition. You cannot end a trade without first putting on a trade.
In the above, you cannot put on a trade until 5 days after a Sell or
Cover. So when you run this code, no trades are initiated.
>
> If there was a way of initiating the code with a Sell and Cover
signal on Bar "0" then I'd assume that on Bar 5 that the conditions in
the Buy and Short code at the top would be satisfied. I don't know
how to intiate the code so that it starts with a sell and cover signal.
>
> I've tried: Sell = Cover = 1
>
> I've tried in my if loop,
> If (i == 0)
> {
> sell(i) = 1;
> cover(i) = 1;
> }
>
> Neither has worked.
>
> Am I missing something in your logic?
>
> thanks,
>
> Fred
>
> ----- Original Message -----
> From: Herman
> To: Ed Middleton
> Sent: Friday, June 29, 2007 5:31 AM
> Subject: Re: [amibroker] Entry Time Delay
>
>
>
>
>
>
>
> Your system here...
>
>
>
>
> Sell = Sell AND BarsSince(Sell) >= 5;
>
>
>
>
> herman
>
>
>
>
> Thursday, June 28, 2007, 9:36:11 PM, you wrote:
>
>
>
>
> >
>
> Ok now really frustrated. Nothing seems to be working.
>
>
>
> Here's what I want to do:
>
> Wait 5 bars after a sell or cover before buying or shorting
again. You would think this was simple but as you can see
below it is causing me some trouble. Any help would be
appreciated.
>
>
>
> thx,
>
>
>
> Fred
>
>
>
> ----- Original Message -----
>
> From: Ed Middleton
>
> To: amibroker@xxxxxxxxxps.com
>
> Sent: Thursday, June 28, 2007 2:37 PM
>
> Subject: Re: [amibroker] Entry Time Delay
>
>
>
>
> Ok, I've found the problem. BarsSince(Sell) and
BarsSince(Cover) never = anything because there is not Sells or
Covers to get a value from and thus no buys or shorts. Somehow I
need to be able to allow the first buy or Short to occur then
everything will be ok.
>
>
>
> Any suggestions?
>
>
>
> thx,
>
>
>
> ----- Original Message -----
>
> From: Ed Middleton
>
> To: amibroker@xxxxxxxxxps.com
>
> Sent: Thursday, June 28, 2007 11:48 AM
>
> Subject: Re: [amibroker] Entry Time Delay
>
>
>
>
> Also tried:
>
> BarsSinceExit = Min(BarsSince(Sell),BarsSince(Cover));
>
> Added to Buy and Short lines:
>
> AND (BarsSinceExit > 0);
>
> This should have gotten me in on the next 5 minute bar. No
luck with this procedure either. I'm missing something in my
programming logic.
>
>
>
>
>
> ----- Original Message -----
>
> From: jjj_98
>
> To: amibroker@xxxxxxxxxps.com
>
> Sent: Thursday, June 28, 2007 11:04 AM
>
> Subject: [amibroker] Entry Time Delay
>
>
>
>
> After exiting a trade I want to wait 5 minutes before
re-entering a
>
> trade so here's what code I've tried but it does not seem to be
>
> making a difference in the next entry. Do you see any problems?
>
>
>
>
> //Initialize last trade exit times
>
>
>
>
> LastSellTime = LastCoverTime = 000000;
>
>
>
>
> // Calculates Amount of time since last exit
>
>
>
>
> TimeSinceLastTrade = Min((TimeNum() -
LastSellTime),(TimeNum() -
>
> LastCoverTime));
>
>
>
>
> // I added this to my BUY and SHORT instructions so that the
time to
>
> re-enter the next trade must be greater than 5 minutes.
>
>
>
>
> AND (TimeSinceLastTrade > 000500);
>
>
>
>
> //In my logic added the following to get the time of the
Last Sell
>
> and Last Cover
>
>
>
>
> if(Sell[i] == 1)
>
> {
>
> LastSellTime = TimeNum();
>
> }
>
>
>
>
> if(Cover[i] == 1)
>
> {
>
> LastCoverTime = TimeNum();
>
> }
>
>
>
>
> ---------------------------------
>
> Choose the right car based on your needs. Check out Yahoo!
Autos new Car Finder tool.
>
> ---------------------------------
>
> Get the Yahoo! toolbar and be alerted to new email wherever
you're surfing.
>
>
>
>
>
>
> ---------------------------------
>
> Boardwalk for $500? In 2007? Ha!
>
> Play Monopoly Here and Now (it's updated for today's economy) at
Yahoo! Games.
>
>
>
> #ygrp-mlmsg { FONT-SIZE: small; FONT-FAMILY:
arial,helvetica,clean,sans-serif}#ygrp-mlmsg TABLE { }#ygrp-mlmsg
SELECT { FONT: 99% arial,helvetica,clean,sans-serif}INPUT { FONT: 99%
arial,helvetica,clean,sans-serif}TEXTAREA { FONT: 99%
arial,helvetica,clean,sans-serif}#ygrp-mlmsg PRE { FONT: 100%
monospace}CODE { FONT: 100% monospace}#ygrp-mlmsg * { LINE-HEIGHT:
1.22em}#ygrp-text { FONT-FAMILY: Georgia}#ygrp-text P { MARGIN: 0px
0px 1em}#ygrp-tpmsgs { CLEAR: both; FONT-FAMILY: Arial}#ygrp-vitnav {
FONT-SIZE: 77%; MARGIN: 0px; PADDING-TOP: 10px; FONT-FAMILY:
Verdana}#ygrp-vitnav A { PADDING-RIGHT: 1px; PADDING-LEFT: 1px;
PADDING-BOTTOM: 0px; PADDING-TOP: 0px}#ygrp-actbar { CLEAR: both;
MARGIN: 25px 0px; COLOR: #666; WHITE-SPACE: nowrap; TEXT-ALIGN:
right}#ygrp-actbar .left { FLOAT: left; WHITE-SPACE: nowrap}..bld {
FONT-WEIGHT: bold}#ygrp-grft { PADDING-RIGHT: 0px; PADDING-LEFT: 0px;
FONT-SIZE: 77%; PADDING-BOTTOM: 15px; PADDING-TOP: 15px; FONT-FAMILY:
> Verdana}#ygrp-ft { PADDING-RIGHT: 0px; BORDER-TOP: #666 1px solid;
PADDING-LEFT: 0px; FONT-SIZE: 77%; PADDING-BOTTOM: 5px; PADDING-TOP:
5px; FONT-FAMILY: verdana}#ygrp-mlmsg #logo { PADDING-BOTTOM:
10px}#ygrp-vital { PADDING-RIGHT: 0px; PADDING-LEFT: 8px;
MARGIN-BOTTOM: 20px; PADDING-BOTTOM: 8px; PADDING-TOP: 2px;
BACKGROUND-COLOR: #e0ecee}#ygrp-vital #vithd { FONT-WEIGHT: bold;
FONT-SIZE: 77%; TEXT-TRANSFORM: uppercase; COLOR: #333; FONT-FAMILY:
Verdana}#ygrp-vital UL { PADDING-RIGHT: 0px; PADDING-LEFT: 0px;
PADDING-BOTTOM: 0px; MARGIN: 2px 0px; PADDING-TOP: 0px}#ygrp-vital
UL LI { CLEAR: both; BORDER-RIGHT: #e0ecee 1px solid; BORDER-TOP:
#e0ecee 1px solid; BORDER-LEFT: #e0ecee 1px solid; BORDER-BOTTOM:
#e0ecee 1px solid; LIST-STYLE-TYPE: none}#ygrp-vital UL LI .ct {
PADDING-RIGHT: 0.5em; FONT-WEIGHT: bold; FLOAT: right; WIDTH: 2em;
COLOR: #ff7900; TEXT-ALIGN: right}#ygrp-vital UL LI .cat {
FONT-WEIGHT: bold}#ygrp-vital A { TEXT-DECORATION: none}#ygrp-vital
A:hover {
> TEXT-DECORATION: underline}#ygrp-sponsor #hd { FONT-SIZE: 77%;
COLOR: #999}#ygrp-sponsor #ov { PADDING-RIGHT: 13px; PADDING-LEFT:
13px; MARGIN-BOTTOM: 20px; PADDING-BOTTOM: 6px; PADDING-TOP: 6px;
BACKGROUND-COLOR: #e0ecee}#ygrp-sponsor #ov UL { PADDING-RIGHT: 0px;
PADDING-LEFT: 8px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP:
0px}#ygrp-sponsor #ov LI { PADDING-RIGHT: 0px; PADDING-LEFT: 0px;
FONT-SIZE: 77%; PADDING-BOTTOM: 6px; PADDING-TOP: 6px;
LIST-STYLE-TYPE: square}#ygrp-sponsor #ov LI A { FONT-SIZE: 130%;
TEXT-DECORATION: none}#ygrp-sponsor #nc { PADDING-RIGHT: 8px;
PADDING-LEFT: 8px; MARGIN-BOTTOM: 20px; PADDING-BOTTOM: 0px;
PADDING-TOP: 0px; BACKGROUND-COLOR: #eee}#ygrp-sponsor .ad {
PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 8px;
PADDING-TOP: 8px}#ygrp-sponsor .ad #hd1 { FONT-WEIGHT: bold;
FONT-SIZE: 100%; COLOR: #628c2a; LINE-HEIGHT: 122%; FONT-FAMILY:
Arial}#ygrp-sponsor .ad A { TEXT-DECORATION: none}#ygrp-sponsor .ad
A:hover { TEXT-DECORATION:
> underline}#ygrp-sponsor .ad P { MARGIN: 0px}o { FONT-SIZE:
0px}..MsoNormal { MARGIN: 0px}#ygrp-text TT { FONT-SIZE:
120%}BLOCKQUOTE { MARGIN: 0px 0px 0px 4px}..replbq { }
>
> ---------------------------------
> Building a website is a piece of cake.
> Yahoo! Small Business gives you all the tools to get online.
>

__._,_.___

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 NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

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





SPONSORED LINKS
Investment management software Investment property software Investment software
Investment tracking software Return on investment software

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

__,_._,___


Be a better Heartthrob. Get better relationship answers from someone who knows.
Yahoo! Answers - Check it out.