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

[amibroker] Re: exit formula help needed



PureBytes Links

Trading Reference Links

Hi,

It sounds like you have the wrong trade delays set in your settings. 
For this particular code, you've already accounted for referring only 
to previous bar values, so you might as well hard code the trade 
delays directly into your code as follows:

SetTradeDelays(0, 0, 0, 0);

That way, if you ever mess around with the settings later, it won't 
effect this script.

To start with, simplify your life and focus just on Buy/Sell. Leave 
Short/Cover until after you have Buy/Sell working.

If it is possible to have redundant Buy signals, you will 
need to remove them before using the ValueWhen on the Buy array when 
calculating Low2:

Buy = NR4DayYest AND InsideDayYest AND HigherHigh;

Low1 = Ref(L, -2);
SellTrigger1 = L < Low1;

Buy = ExRem(Buy, SellTrigger1); // Remove redundant buys
Low2 = ValueWhen(Buy, Ref(L, -1) - 0.01);
SellTrigger2 = L < Low2;

Sell = SellTrigger1 OR SellTrigger2;

I notice a needed adjustment to the BuyPrice/SellPrice to correct for 
the fact that if the current bar opens above/below your calculated 
price, you have to take the Open rather than your calculated price. 
So, you're BuyPrice becomes:

BuyPrice = Ref(High, -1);
BuyPrice = IIF(Open > BuyPrice, Open, BuyPrice);

And your SellPrice becomes:

SellPrice = IIf(L < Low1,
  IIf(L < Low2,
    Max(Low1, Low2),
    Low1),
  IIf(L < Low2,
    Low2,
    0)
);
SellPrice = IIF(Open < SellPrice, Open, SellPrice);

To help see what is going on, you can then add the following code to 
the bottom of your script and show it all on a chart. Regular bars 
will show as dark grey, NR4Days will show as pink, InsideDays will 
show as blue, days that are both NR4Day and InsideDay will show as 
orange, buy actions will show as a green circle at the actual buy 
price, sell actions will show as a red circle at the actual sell 
price:

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

BarColors = IIF(InsideDay,
  IIF(NR4Day,
    colorOrange,
    colorBlue),
  IIF(NR4Day,
    colorPink,
    colorDarkGrey)
);

Plot(Close, "Close", BarColors, styleBar);
PlotShapes(IIf(Buy, shapeSmallCircle, shapeNone), colorBrightGreen, 
0, BuyPrice, 0);
PlotShapes(IIf(Sell, shapeSmallCircle, shapeNone), colorRed, 0, 
SellPrice, 0);

Hope that helps.

Mike

--- In amibroker@xxxxxxxxxxxxxxx, "alta hob" <altahob@xxx> wrote:
>
> I tried changing it as you specified but when I run a backtest it 
still will
> exit a trade on the previous bars high/low (depending on whether im 
short or
> long).  I want AB to exit on either the low/high of the inside day 
or on a
> two bar trailing stop.  Can you see what is causing this problem 
because I
> can't work out why?
> 
> 
> thanks
> -------
> 
> NR4Day = (H - L) < Ref(LLV(H-L,3),-1);
> Column1 =  NR4Day;
> Column1Name = "Nr4Day";
> 
> InsideDay = H < Ref(High,-1) AND Low > Ref(Low,-1);
> Column2 =  InsideDay;
> Column2Name = "Inside Day";
> 
> 
> NR4DayYest = Ref( NR4Day, -1 ); // new array - NR4Day shifted 
forward 1 bar
> 
> InsideDayYest = Ref( InsideDay, -1 ); // new array - InsideDay 
shifted
> forward 1 bar
> 
> HigherHigh = High > Ref( High, -1 );
> 
> LowerLow = Low < Ref( Low, -1 );
> 
> Buy = NR4DayYest AND InsideDayYest AND HigherHigh;
> 
> BuyPrice = Ref ( High, -1 );
> 
> Low1 = Ref(L, -2);
> Low2 = ValueWhen(Buy,Ref(L,-1)-0.01);
> 
> Sell = L < Low1 OR L < Low2;
> 
> SellPrice = IIf(L < Low1,
> IIf(L < Low2,
> Max(Low1, Low2),
> Low1),
> IIf(L < Low2,
> Low2,
> 0)
> );
> 
> Short = NR4DayYest AND InsideDayYest AND LowerLow;
> 
> High1 = Ref(H, -2);
> High2 = ValueWhen(Sell,Ref(H,-1)+0.01);
> 
> Cover = H > High1 OR H > High2;
> 
> CoverPrice = IIf(H > High1,
> IIf(H > High2,
> Max(High1, High2),
> High1),
> IIf(H < High2,
> High2,
> 0)
> );
> 
> On Fri, May 2, 2008 at 11:29 PM, Mike <sfclimbers@xxx> wrote:
> 
> >   Yes, it takes the lower of the two lows preceding the current 
bar. If
> > you want specifically the low of two bars ago, just use:
> >
> > Low1 = Ref(L, -2);
> >
> >
> > Mike
> >
> > --- In amibroker@xxxxxxxxxxxxxxx <amibroker%
40yahoogroups.com>, "alta hob"
> > <altahob@> wrote:
> > >
> > > Thanks for taking the time to help. I will study the code you 
gave
> > me.
> > >
> > > Another question.
> > >
> > > If I use the
> > >
> > > Low1 = Ref(LLV(L,2),-1);
> > >
> > > Does this take the lowest low of the last 2 bars? If it does 
then
> > I have
> > > made a mistake. What I want to do is use the low from two bars
> > ago, not the
> > > lowest of the last two bars as this doesn't give my trade room 
to
> > breathe.
> > >
> > > cheers
> > >
> > > AH
> > >
> > > On Fri, May 2, 2008 at 9:09 PM, Mike <sfclimbers@> wrote:
> > >
> > > > For these types of problems, it often helps to break up the
> > > > conditions for easier understanding.
> > > >
> > > > Using your example, what about something like:
> > > >
> > > > Low1 = Ref(LLV(L,2),-1);
> > > > Low2 = ValueWhen(Buy,Ref(L,-1)-0.01);
> > > >
> > > > Sell = L < Low1 OR L < Low2;
> > > >
> > > > SellPrice = IIF(L < Low1,
> > > > IIF(L < Low2,
> > > > max(Low1, Low2),
> > > > Low1),
> > > > IIF(L < Low2,
> > > > Low2,
> > > > 0)
> > > > );
> > > >
> > > > I'm assuming that if both conditions are met, then the one 
that
> > > > happened first (i.e. the higher price point) would be the 
selling
> > > > price.
> > > >
> > > > Mike
> > > >
> > > >
> > > > --- In amibroker@xxxxxxxxxxxxxxx <amibroker%
40yahoogroups.com><amibroker%
> > 40yahoogroups.com>, "alta hob"
> > > > <altahob@> wrote:
> > > > >
> > > > > Hi
> > > > >
> > > > >
> > > > > I am trying to do something similar but need my sellprice to
> > > > execute at the
> > > > > point the sell criteria below is hit
> > > > >
> > > > > Sell = L < Ref(LLV(L,2),-1) OR L<ValueWhen(Buy,Ref(L,-1)-
0.01);
> > > > >
> > > > > Can someone please show me how to add the sellprice code to
> > this?
> > > > >
> > > > >
> > > > > thanks
> > > > >
> > > > >
> > > > > On Sat, Apr 26, 2008 at 12:03 AM, safetrading <safetrading@>
> > > > wrote:
> > > > >
> > > > > > Thanks Graham;
> > > > > >
> > > > > > That was exactly what I needed. I use Valuewhen all the 
time,
> > for
> > > > > > some reason I just drew a blank on this. I think the "buy"
> > > > variable
> > > > > > threw me, as I have never really done anything with it 
except
> > put
> > > > my
> > > > > > formula there.
> > > > > >
> > > > > > Anyways, thanks very much.
> > > > > >
> > > > > > Safetrading
> > > > > >
> > > > > >
> > > > > > --- In amibroker@xxxxxxxxxxxxxxx <amibroker%
40yahoogroups.com><amibroker%
> > 40yahoogroups.com><amibroker%
> >
> > > > 40yahoogroups.com>,
> > > > Graham
> > > > > > <kavemanperth@> wrote:
> > > > > > >
> > > > > > > Valuewhen is useful for this
> > > > > > > I assume you are entering on same bar as buuy signal. 
This
> > line
> > > > > > gives
> > > > > > > you the low of the bar before buy signal
> > > > > > >
> > > > > > > sell=L<valuewhen(buy,ref(l,-1)-0.01);
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > > Cheers
> > > > > > > Graham Kav
> > > > > > > AFL Writing Service
> > > > > > > http://www.aflwriting.com
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > 2008/4/26 safetrading <safetrading@>:
> > > > > >
> > > > > > > > This is probably easy for most of you, but I just 
can't
> > seem
> > > > to
> > > > > > > > figure this out.
> > > > > > > >
> > > > > > > > I'm using one tick above the high of the previous day 
for
> > an
> > > > > > entry
> > > > > > > > point and trying to create an exit rule where for a 
long
> > > > > > position,
> > > > > > > > the exit would be one tick below the low of the day 
prior
> > to
> > > > the
> > > > > > > > entry day ( the same days where I got my entry point 
from)
> > > > > > until a
> > > > > > > > low exists that is above the high of my entry point, 
at
> > which
> > > > > > time
> > > > > > > > the exit would then become one tick below that low and
> > > > continue
> > > > > > to
> > > > > > > > follow the trade like this; sell=L<ref(l,-1)-0.01;
> > > > > > > >
> > > > > > > > So what I need to do is label and hang on to the "one 
tick
> > > > > > below"
> > > > > > > > the low of the day prior to entering. I believe I 
need to
> > use
> > > > > > the
> > > > > > > > Hold function, but am not sure how to identify the 
price
> > > > point I
> > > > > > > > need.
> > > > > > > >
> > > > > > > > Any suggestions would be appreciated.
> > > > > > > >
> > > > > > > > Thanks,
> > > > > > > >
> > > > > > > > Safetrading
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > ------------------------------------
> > > > > > > >
> > > > > > > > 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
> > > > > > > > Yahoo! Groups Links
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > >
> > > >
> > > >
> > > >
> > >
> >
> >  
> >
>



------------------------------------

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
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/