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

[amibroker] Re: Last Try, Scale out with short and long entry's



PureBytes Links

Trading Reference Links

I GOT IT! So easy to miss. Since I my system is entering at the close of the bar I need to exclude the high/low of the entry bar from my test for profit targets and stop loss.
So very simple:
//check if 1st target hit and short[i] = 0
      if( Short[i] == 0 AND exit == 0 AND
          Low[ i ] <= priceatshort - FirstProfitTarget - TickIncrement )
       {
         // first profit target hit - scale-out
         exit = 1;
         Short[ i ] = sigScaleOut;
          ShortPrice[i] = priceatshort - FirstProfitTarget;
//_TRACE("Exit1: " +exit);
       }

Only the entry bar with show Short[i] == 1, so just make sure Short[i] == 0 before checking for potential exits. Now I need to do the same for the long side of the code.

I am really glad I had to work through this problem. Had someone given me the solution I would not have learned so much about the use of the debugging tool. Now that I have this part working correctly I will debug the rest of the script and once I have a fully operational code I will post it on the AFL library (this will be my first!).

  Pete  :-)

--- In amibroker@xxxxxxxxxxxxxxx, "Pete" <dryheat3@xxx> wrote:
>
> Thanks Grover, I tried this and it did not work. Great suggestion
> though.
> See below for debug output:
>
> I took the time to format the output for maximum clarity.
> 1. The first short on 1/23/09 4:00 pm is not within the range of the
> back test so no trade should take place, note the equity starts at
> 10,000.00
> 2. The second short is within range of the back test and should appear
> in the back test but it does not, note equity is now at POSITIVE
> 9,840.40
> 3. Continuing on you will see every long signal appears on the back test
> while none of the short signals appear until after equity again rises to
> the POSITIVE side, 2,477.90
>
> At this point I am not sure the portfolio equity, in-loop, really has
> anything to do with this.
>
> Larry, thanks for confirming you have tried to get this to work and
> failed as well. It certainly provided some comfort knowing I am not the
> only one failing to get this working.
>
> Unless Tomasz or someone else has a way to get this to work I will
> probably report this as a bug on the Amibroker support site.
> Thanks for all who take the time to try and unravel this one!
>
> Pete :-)
>
>
> ************************************************************
>
> ShortEntry: 1/23/2009 4:00:00 PM <<Not in Selected Range>>
>
> ShortPrice = 825.25 Equity in-loop: 10000
>
> ************************************************************
>
> ShortEntry: 1/26/2009 7:30:00 AM <<Absent From Back Test>>
>
> ShortPrice = 824.5 Equity
> in-loop: 9840.4
>
> ************************************************************
>
> LongEntry: 1/26/2009 8:36:00 AM <<Present In Back Test>>
>
> BuyPrice: 827 Equity in-loop: 7465.4
>
> ************************************************************
>
> ShortEntry: 1/26/2009 11:48:00 AM <<Absent From Back Test>>
>
> ShortPrice = 839.75 Equity in-loop: -4647.1
>
> ************************************************************
>
> LongEntry: 1/26/2009 12:30:00 PM <<Present In Back Test>>
>
> BuyPrice: 844.25 Equity in-loop: -8922.1
>
> ************************************************************
>
> ShortEntry: 1/26/2009 12:33:00 PM <<Absent From Back Test>>
>
> ShortPrice = 840.5 Equity in-loop: -5359.6
>
> ************************************************************
>
> LongEntry: 1/26/2009 3:00:00 PM <<Present In Back Test>>
>
> BuyPrice: 835.25 Equity in-loop: -372.1
>
> ************************************************************
>
> ShortEntry: 1/26/2009 3:42:00 PM <<Present In Back Test>>
>
> ShortPrice = 832.25 Equity in-loop: 2477.9
>
> ************************************************************
>
>
>
> --- In amibroker@xxxxxxxxxxxxxxx, "Grover Yowell" gyowell1@ wrote:
> >
> > Larry,
> >
> > I recall from several years ago someone suggested that priceatshort
> should
> > be initialized as a very large number such as 99999999 instead of zero
> as is
> > in your code. I tried it at the time and it worked. You might try
> that.
> >
> > Grover
> >
> >
> >
> > From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On
> Behalf
> > Of onelkm
> > Sent: Sunday, February 08, 2009 6:34 AM
> > To: amibroker@xxxxxxxxxxxxxxx
> > Subject: [amibroker] Re: Last Try, Scale out with short and long
> entry's
> >
> >
> >
> > Pete
> > I have tried to do the same thing without success. I hope someone
> > will suggest how this can be done.
> > Larry
> >
> > --- In amibroker@xxxxxxxxxxxxxxx <mailto:amibroker%40yahoogroups.com>
> ,
> > "Pete" dryheat3@ wrote:
> > >
> > > Ok, here's the code that I built. As far as my understanding goes,
> > this
> > > code should work perfectly. Yet it does not. Load this into your AB
> > and
> > > before running the back test make sure you have debug app running to
> > > capture the _Trace() output.
> > >
> > > Run this against an intraday chart of ES. I am assuming you already
> > have
> > > the tick size, point value and margin deposit cofigured in your
> > > database. You can use a 3, 5 or 10 minute chart settings for the
> > > backtest. Run the back test in Short only mode and set the report to
> > > detailed log so you can see any scaling of trades. The scaling
> > works,
> > > this is NOT what I am trying to get working. The problem with this
> > code
> > > is it misses entry signals ONLY when including shorts. Longs work
> > just
> > > perfectly.
> > >
> > > Examining the debug output you will see the short signals are
> > driving
> > > portfolio equity into the negatives, even when the back tester may
> > be
> > > reporting a net profit. It's when the portfolio equity is driven
> > into
> > > the negatives that it cuases this system to miss several entry
> > signals.
> > >
> > > You can scan the debug output for a point short entry where the
> > > portfolio equity is negative, then go to the back tester output and
> > see
> > > if you can find that trade in the list. If the equity is negative
> > there
> > > is no entry in the back tester. If the equity is positive, then the
> > > trade will be found in the back tester.
> > >
> > > Some of the statments in this code are a bit long so I'm not sure
> > how
> > > well it will copy/paste from the web into .afl. You may need to
> > remove
> > > some line wrapping after pasting into AB.
> > >
> > > I'm going to figure this out, or go mad trying. So your assistance
> > may
> > > prevent my early retirement to the funny farm. lol!
> > >
> > > SetTradeDelays(0,0,0,0);
> > > BuyPrice = Close;
> > > SellPrice = Close;
> > > SetOption("FuturesMode", True);
> > > SetOption("InitialEquity", 10000);
> > > Buy = Cross(MA(C, 20), MA(C,50));
> > >
> > > Short = Cross(MA(C,50), MA(C,20));
> > >
> > > SystemExitLong = Cross(MA(C,18), C); // This value will be adjusted
> > > according the system's exit rules
> > > SystemExitShort = Cross(C, MA(C,18));
> > >
> > > StopAmt = 1.5; //number of points
> > > ProfitTarget = 3;//number of points
> > >
> > >
> > >
> > > TickIncrement = Param("Tick Increment", 0.25, 0.1, 1, 0.1);//ES =
> > 0.25,
> > > NQ = 0.10, YM = 1
> > > TickIncrement = TickIncrement * 1; //change this value according to
> > the
> > > expected slippage when stops are tiggered
> > >
> > > //set begining value of essential variables
> > > TrailingStop = 0; // This value will be adjusted to
> > FirstProfitTarget
> > > only after SecondProfitTarget is hit
> > > StopLoss = 0;
> > > FirstProfitTarget = 0;
> > > SecondProfitTarget = 0;
> > > //set begining values for long variables
> > > priceatbuy=0;
> > > highsincebuy = 0;
> > > Sell = 0;
> > > TradeDate = DateTime();
> > > //set begining values for short variables
> > > priceatshort=0;
> > > lowsincebuy = 0;
> > > Cover = 0;
> > >
> > > //set exit to zero
> > > exit = 0;
> > > PortEq = Equity();
> > >
> > >
> > > ////////////////////////////////////////////////////////////////////
> > ////\
> > > ///
> > > //////////////Begin code to scale out of
> > positions////////////////////
> > > ////////////////////////////////////////////////////////////////////
> > ////\
> > > ///
> > > for( i = 0; i < BarCount; i++ )
> > > {
> > > if( priceatbuy == 0 AND Buy[ i ] )
> > > {
> > > //initialize required variables
> > > _TRACE("Long Entry = " + DateTimeToStr(TradeDate[i]) +" AND Buy
> > Price =
> > > " +BuyPrice[i] +" AND Equity in-loop: " +PortEq[i]);
> > > priceatbuy = BuyPrice[ i ];
> > > StopLoss = StopAmt[i];
> > > FirstProfitTarget = StopAmt[i];
> > > SecondProfitTarget = ProfitTarget[i];
> > > }
> > >
> > > if( priceatshort == 0 AND Short[ i ] )
> > > {
> > > //initialize required variables
> > > _TRACE("Short Entry = " + DateTimeToStr(TradeDate[i]) +" AND Short
> > Price
> > > = " +ShortPrice[i] +" AND Equity in-loop: " +PortEq[i]);
> > > priceatshort = ShortPrice[ i ];
> > > StopLoss = StopAmt[i];
> > > FirstProfitTarget = StopAmt[i];
> > > SecondProfitTarget = ProfitTarget[i];
> > > }
> > >
> > > if( priceatbuy > 0 )
> > > {
> > > highsincebuy = Max( High[ i ], highsincebuy );
> > >
> > > //check if 1st target hit and long
> > > if( exit == 0 AND
> > > High[ i ] >= FirstProfitTarget + TickIncrement +
> > priceatbuy )
> > > {
> > > // first profit target hit - scale-out
> > > exit = 1;
> > > Buy[ i ] = sigScaleOut;
> > > BuyPrice[i] = FirstProfitTarget + priceatbuy;
> > > }
> > >
> > > //check if 2nd target hit and long
> > > if( exit == 1 AND
> > > High[ i ] >= SecondProfitTarget + TickIncrement +
> > priceatbuy
> > > )
> > > {
> > > // second profit target hit - scale-out
> > > exit = 2;
> > > Buy[ i ] = sigScaleOut;
> > > BuyPrice[i] = SecondProfitTarget + priceatbuy;
> > > TrailingStop = FirstProfitTarget + priceatbuy; //after
> > > hitting SecondProfitTarget, move
> > >
> > > //stop to FirstProfitTarget position
> > > }
> > >
> > > //check if system exit hit and long
> > > if( exit <= 2 AND
> > > SystemExitLong [i]) //need to substitute system exit here
> > > {
> > > // System Exit hit - exit all remaining contracts
> > > exit = 3;
> > > SellPrice[i] = Close[i]; //all three contracts would
> > exit
> > > here
> > > }
> > >
> > > //check if trailing stop hit and long
> > > if( exit == 2 AND
> > > Low[ i ] <= TrailingStop - TickIncrement )
> > > {
> > > // Trailing Stop target hit - exit trade with final
> > contract
> > > exit = 3;
> > > SellPrice[ i ] = TrailingStop -
> > TickIncrement ; //accounting
> > > for one tick slippage
> > > }
> > >
> > > //check if stop loss hit and long
> > > if(Low[ i ] <= priceatbuy - StopLoss - TickIncrement )
> > > {
> > > // Stop Loss hit - exit
> > > exit = 3;
> > > SellPrice[ i ] = Min( Open[ i ], priceatbuy - StopLoss -
> > > TickIncrement ); //assume one tick slippage
> > > }
> > >
> > > //check if exit complete
> > > if( exit >= 3 )
> > > {
> > > Buy[ i ] = 0;
> > > Sell[ i ] = exit + 1; // mark appropriate exit code
> > > exit = 0;
> > > priceatbuy = 0; // reset price
> > > highsincebuy = 0;
> > > ThirdProfitTarget = 0;
> > > TrailingStop = 0;
> > >
> > > }
> > > }
> > >
> > > if( priceatshort > 0 )
> > > {
> > > lowsincebuy = Min( Low[ i ], lowsincebuy );
> > > //check if 1st target hit and short
> > > if( exit == 0 AND
> > > Low[ i ] <= priceatshort - FirstProfitTarget -
> > TickIncrement )
> > > {
> > > // first profit target hit - scale-out
> > > exit = 1;
> > > Short[ i ] = sigScaleOut;
> > > ShortPrice[i] = priceatshort - FirstProfitTarget;
> > > }
> > > //check if 2nd target hit and short
> > > if( exit == 1 AND
> > > Low[ i ] <= priceatshort - SecondProfitTarget -
> > TickIncrement
> > > )
> > > {
> > > // second profit target hit - scale-out
> > > exit = 2;
> > > Short[ i ] = sigScaleOut;
> > > ShortPrice[i] = priceatshort - SecondProfitTarget;
> > > TrailingStop = priceatshort -
> > FirstProfitTarget ; //after
> > > hitting SecondProfitTarget, move
> > >
> > > //stop to FirstProfitTarget position
> > > }
> > > //check if system exit and short
> > > if( exit <= 2 AND
> > > SystemExitShort[i]) //need to substitute system exit here
> > > {
> > > // System Exit hit - exit all remaining contracts
> > > exit = 3;
> > > CoverPrice[i] = Close[i]; //all three contracts would
> > exit
> > > here
> > > }
> > > //check if trailing stop hit and short
> > > if( exit == 2 AND
> > > High[ i ] >= TrailingStop + TickIncrement )
> > > {
> > > // Trailing Stop target hit - exit trade with final
> > contract
> > > exit = 3;
> > > CoverPrice[ i ] = TrailingStop + TickIncrement ;
> > > }
> > > //check if stop loss hit and short
> > > if(High[ i ] >= priceatshort + StopLoss + TickIncrement )
> > > {
> > > // Stop Loss hit - exit
> > > exit = 3;
> > > CoverPrice[ i ] = Max( Open[ i ], priceatshort +
> > StopLoss +
> > > TickIncrement ); //assume one tick slippage
> > > }
> > > //check if exit complete
> > > if( exit >= 3 )
> > > {
> > > Short[ i ] = 0;
> > > Cover[ i ] = exit + 1; // mark appropriate exit code
> > > exit = 0;
> > > priceatshort = 0; // reset price
> > > highsincebuy = 0;
> > > ThirdProfitTarget = 0;
> > > TrailingStop = 0;
> > >
> > > }
> > > }
> > >
> > > }
> > >
> > > SetPositionSize(3,spsShares); //Trade 3 contracts on entry
> > > SetPositionSize( 1, IIf( Short == sigScaleOut OR Buy == sigScaleOut,
> > > spsShares, spsNoChange ) ); //scale out 1 contract at a time until
> > > position closed
> > > ////////////////////////////////////////////////////////////////////
> > ////\
> > > ///
> > > //////////////End of code to scale out of
> > positions////////////////////
> > > ////////////////////////////////////////////////////////////////////
> > ////\
> > > ///
> > >
> >
>


__._,_.___


**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

*********************
TO GET TECHNICAL 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

*********************************




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

__,_._,___