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

Re: [amibroker] Scale In problems - need some help



PureBytes Links

Trading Reference Links

Dingo
I think this may be something, if not then obviously it is something else
:-)
You have the trade delays set at 1 bar delay, but the calculations for
position sizing are set on the bar of the signal
Try this to find the size

SetPositionSize( valuewhen(buy,ScaleInSize), spsPercentOfEquity );


-- 
Cheers
Graham
AB-Write >< Professional AFL Writing Service
Yes, I write AFL code to your requirements
http://www.aflwriting.com

On 15/02/07, dingo <dingo@xxxxxxxxxxx> wrote:
>
>  Duudes!
>
> I'm working on a scale in problem and have hit a wall.  I've included the
> afl below.
>
> The problem I'm having is figuring out why the trade list doesn't seem to
> jive with the _Trace output.  The trace seems to be showing that the
> routines are working but the position sizes in the trade list don't seem to
> match. Plus I'm getting some funky exit prices??
>
> I've been running it on a single stock with a date range of a few years
> just to see if I have the mechanics are right.
>
> I'd appreciate any and all comments on this.
>
> Thanks!
>
> d
>
>
>
> /*
>     Here is what we want to try and do.
>
>         On the first buy signal we take 10% of the 10% position, so 1%
>         If it closes down anytime while in position we take 20% of the 10%
> position, so 2%
>         If it closes down again anytime while in position we take 30% of
> the 10% position, so 3%
>         If it closes down again anytime while in position we take 40% of
> the 10% position, so 4%
>
>     So any position can have up to 4 scale ins.
>     You could have up to 10% of total equity in a position.
> */
>
>
> /*==============================================================================
>     Global Settings
>
> ==============================================================================*/
> SetFormulaName("Scale In Test");
>
> SetOption("ActivateStopsImmediately", False);
> SetOption("AllowPositionShrinking", True);
> SetOption("AllowSameBarExit", False);
> SetOption("CommissionAmount", 0);
> SetOption("CommissionMode", 2); //$$ per trade
> SetOption("InitialEquity", 10000);
> SetOption("InterestRate",0.00);
> SetOption("MarginRequirement", 100);
> SetOption("MaxOpenPositions", 10);
> SetOption("MinShares", 0);
> SetOption("NoDefaultColumns", False);
> SetOption("PriceBoundChecking", False);
> SetOption("ReverseSignalForcesExit", False);
> SetOption("UseCustomBacktestProc", False );
> SetOption("UsePrevBarEquityForPosSizing", False);
>
> SetTradeDelays(1, 1, 1, 1);
>
> MaxOpenTrades = GetOption("MaxOpenPositions");
>
> if (Status("stocknum") == 0)
> {
>     _TRACE("DBGVIEWCLEAR");
> }
>
> // Simple buy and sell rules
> Buy  = Cross( EMA( C, 10 ), EMA( C, 50 ) );
> Sell = Cross( EMA( C, 50 ), EMA( C, 10 ) );
>
> TotalPosition  = 10; //Max Pct of Equity of all scaleins
> FirstScaleIn   = 10; //Pct of Total Position
> SecondScaleIn  = 20;
> ThirdScaleIn   = 30;
> FourthScaleIn  = 40;
>
> FirstScaleInPctOfEqty  = ( FirstScaleIn  * TotalPosition ) / 100;
> SecondScaleInPctOfEqty = ( SecondScaleIn * TotalPosition ) / 100;
> ThirdScaleInPctOfEqty  = ( ThirdScaleIn  * TotalPosition ) / 100;
> FourthScaleInPctOfEqty = ( FourthScaleIn * TotalPosition ) / 100;
>
> ScaleInSize  = Open - Open;
> ScaleInCount = 0;
>
> Dte = DateNum();
> PositionScore = 1;
>
> for( i = 0; i < BarCount; i++ )
> {
>  if( Buy[ i ] AND ScaleInCount == 0 )
>  {
>   ScaleInCount = 1;
>        Buy[ i ] = True;
>        ScaleInSize[ i ] = FirstScaleInPctOfEqty;
>        _TRACE(" ");
>        _TRACE("Buy"
>          + " Date: " + DateTimeToStr( DateTimeConvert( 2, Dte[ i ] ) )
>    + " Buy = " + WriteVal( Buy[ i ], 1.0, 0 )
>    + " Size: "  + WriteVal( ScaleInSize[ i ], 1.2, 0 )
>    + " CLose[i] " + Writeval( Close[ i ], 1.2, 0 )
>    + " CLose[i-1] " + Writeval( Close[ i-1 ], 1.2, 0 )
>    );
>    }
>
>    if( ScaleInCount == 2
>       AND ( Close[ i ] < Close[ i -1 ] )
>       AND Sell[ i ] == 0 )
>    {
>        Buy[ i ] = sigScaleIn;
>        ScaleInSize[ i ] = SecondScaleInPctOfEqty;
>        _TRACE("SI2"
>          + " Date: " + DateTimeToStr( DateTimeConvert( 2, Dte[ i ] ) )
>    + " Buy = " + WriteVal( Buy[ i ], 1.0, 0 )
>    + " Size: "  + WriteVal( ScaleInSize[ i ], 1.2, 0 )
>    + " CLose[i] " + Writeval( Close[ i ], 1.2, 0 )
>    + " CLose[i-1] " + Writeval( Close[ i-1 ], 1.2, 0 )
>    );
>    }
>
>    if( ScaleInCount == 3
>       AND ( Close[ i ] < Close[ i -1 ] )
>       AND Sell[ i ] == 0 )
>    {
>      Buy[ i ] = sigScaleIn;
>        ScaleInSize[ i ] = ThirdScaleInPctOfEqty;
>        _TRACE("SI3"
>          + " Date: " + DateTimeToStr( DateTimeConvert( 2, Dte[ i ] ) )
>    + " Buy = " + WriteVal( Buy[ i ], 1.0, 0 )
>    + " Size: "  + WriteVal( ScaleInSize[ i ], 1.2, 0 )
>    + " CLose[i] " + Writeval( Close[ i ], 1.2, 0 )
>    + " CLose[i-1] " + Writeval( Close[ i-1 ], 1.2, 0 )
>    );
>    }
>
>    if( ScaleInCount == 4
>       AND ( Close[ i ] < Close[ i -1 ] )
>       AND Sell[ i ] == 0 )
>    {
>        Buy[ i ] = sigScaleIn;
>        ScaleInSize[ i ] = FourthScaleInPctOfEqty;
>        _TRACE("SI4"
>          + " Date: " + DateTimeToStr( DateTimeConvert( 2, Dte[ i ] ) )
>    + "  Buy = " + WriteVal( Buy[ i ], 1.0, 0 )
>    + "  Size: "  + WriteVal( ScaleInSize[ i ], 1.2, 0 )
>    + " CLose[i] " + Writeval( Close[ i ], 1.2, 0 )
>    + " CLose[i-1] " + Writeval( Close[ i-1 ], 1.2, 0 )
>    );
>    }
>
>    if( ScaleInCount > 4 OR Sell[ i ] )
>    {
>        Buy[ i ] = 0;
>        ScaleInCount = 0;
>    }
>
>  if( Buy[ i ] )
>   ScaleInCount = ScaleInCount + 1;
>
>  If ( Sell[ i ] )
>        _TRACE("Sale On Date: "
>          + DateTimeToStr( DateTimeConvert( 2, Dte[ i ] )));
>
> }
>
> SetPositionSize( ScaleInSize, spsPercentOfEquity );
>
> Filter = 1;
>
> AddColumn(Buy, "Buy", 1.0);
> AddColumn(Sell, "Sell", 1.0);
> AddColumn(Close, "Close", 1.2);
> AddColumn(ScaleInSize, "ScaleInSize", 1.2);
> AddColumn(BuyPrice, "BuyPrice", 1.2);
> AddColumn(PositionSize, "PositionSize", 1.2);
>
>
>
>
>
> 
>

Content-Description: "AVG certification"
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.17.39/687 - Release Date: 2/14/2007 4:17 PM