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

[amibroker] Re: Futures Trading, Scalling out - Backtest



PureBytes Links

Trading Reference Links

Please help.
I have spent another 2 hours trying to get this to work. Below is the
code I have tweaked hoping to get it to scale out 1 contract on first
target, 2nd contract on second target and 100% on trailing stop. In
backtesting all exits are ocurring with all three original contracts. It
is not scaling out. What am I missing?

////////////////////////Code Below///////////////////////////////////
_SECTION_BEGIN("ScaleOut");
//Example of code that exits 1/3 on first profit target,
//1/3 on next profit target AND everything at trailing stop.
//For use in trading futures

Sell = 0;

// the system will exit
// 1 contract if FIRST PROFIT TARGET stop is hit
// 1 contract if SECOND PROFIT TARGET stop is hit
// 100% of position if TRAILING STOP is hit

FirstProfitTarget = Param("Target 1", 4, 2, 6, 1); // profit in points
SecondProfitTarget = Param("Target 2", 8, 6, 12, 1); // in points
TrailingStop = Param("TrailStop", 3, 1, 4, 0.5); // also in points

priceatbuy=0;
highsincebuy = 0;

exit = 0;

for( i = 0; i < BarCount; i++ )
{
    if( priceatbuy == 0 AND Buy[ i ] )
     {
        priceatbuy = BuyPrice[ i ];
     }

    if( priceatbuy > 0 )
     {
        highsincebuy = Max( High[ i ], highsincebuy );

       if( exit == 0 AND
           High[ i ] >= (FirstProfitTarget + priceatbuy ))
        {
          // first profit target hit - scale-out
          exit = 1;
          Buy[ i ] = sigScaleOut;
        }

       if( exit == 1 AND
           High[ i ] >= (SecondProfitTarget + priceatbuy ))
        {
          // second profit target hit - exit
          exit = 2;
          SellPrice[ i ] = Max( Open[ i ], ( SecondProfitTarget +
priceatbuy ));
        }

       if( Low[ i ] <= (highsincebuy - TrailingStop ) )
        {
          // trailing stop hit - exit
          exit = 3;
          SellPrice[ i ] = Min( Open[ i ], (highsincebuy - TrailingStop )
);
        }

       if( exit >= 2 )
        {
          Buy[ i ] = 0;
          Sell[ i ] = exit + 1; // mark appropriate exit code
          exit = 0;
          priceatbuy = 0; // reset price
          highsincebuy = 0;
        }
     }
}

//SetPositionSize( 50, spsPercentOfEquity );
//SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) );
// scale out 1 contract
SetPositionSize( 3, spsShares );
SetPositionSize( 1, spsShares * ( Buy == sigScaleOut ) ); // scale out 1
contract

////////////////////////End Code/////////////////////////////////////




--- In amibroker@xxxxxxxxxxxxxxx, "Pete" <dryheat3@xxx> wrote:
>
> I give up. I tried for a couple hours to get the example in the help
> file of the scale out code to work for futures. I'm just not getting
it.
> I need a section of code to define the scale out strategy based not on
> percent of position size but rather on points. For instance, I would
> like to scale out of a futures position after profit reaches 1 point
> and scale out further when profit reaches 3 points, allowing the 3rd
> and final contract to run out until the sell signal is triggered. I
> sure hope there is some code out there that does this.
>
> The example from the help file:
>
> Buy = Cross( MA( C, 10 ), MA( C, 50 ) );
> Sell = 0;
>
> // the system will exit
> // 50% of position if FIRST PROFIT TARGET stop is hit
> // 50% of position is SECOND PROFIT TARGET stop is hit
> // 100% of position if TRAILING STOP is hit
>
> FirstProfitTarget = 10; // profit
> SecondProfitTarget = 20; // in percent
> TrailingStop = 10; // also in percent
>
> priceatbuy=0;
> highsincebuy = 0;
>
> exit = 0;
>
> for( i = 0; i < BarCount; i++ )
> {
>    if( priceatbuy == 0 AND Buy[ i ] )
>     {
>        priceatbuy = BuyPrice[ i ];
>     }
>
>    if( priceatbuy > 0 )
>     {
>        highsincebuy = Max( High[ i ], highsincebuy );
>
>       if( exit == 0 AND
>           High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy )
>        {
>          // first profit target hit - scale-out
>          exit = 1;
>          Buy[ i ] = sigScaleOut;
>        }
>
>       if( exit == 1 AND
>           High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy
)
>        {
>          // second profit target hit - exit
>          exit = 2;
>          SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget *
> 0.01 ) * priceatbuy );
>        }
>
>       if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy )
>        {
>          // trailing stop hit - exit
>          exit = 3;
>          SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 )
> * highsincebuy );
>        }
>
>       if( exit >= 2 )
>        {
>          Buy[ i ] = 0;
>          Sell[ i ] = exit + 1; // mark appropriate exit code
>          exit = 0;
>          priceatbuy = 0; // reset price
>          highsincebuy = 0;
>        }
>     }
> }
>
> SetPositionSize( 50, spsPercentOfEquity );
> SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) );
> // scale out 50% of position
>



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

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