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

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



PureBytes Links

Trading Reference Links

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

*********************************
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/