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

[amibroker] Yo Graham.. Need help with SetPostionSize, please!



PureBytes Links

Trading Reference Links

Graham,

The help file has got me flummoxed:


SYNTAX 	SetPositionSize( size, method ) 	
RETURNS	 ARRAY 	
FUNCTION 	This function allows to control trade (position) size in
four different ways, depending on 'method' parameter. 


Parameters: 


size (ARRAY) defines desired trade size 


method (ARRAY) defines how 'size' is interpreted 


*	spsValue (=1) - dollar value of size (as in previous versions) 

*	spsPercentOfEquity (=2) - size expressed as percent of
portfolio-level equity (size must be from ..100 (for regular accounts) or
.1000 for margin accounts) 

*	spsShares (=4) - size expressed in shares/contracts (size must be >
0 ) 

*	spsPercentOfPosition (=3) - size expressed as percent of currently
open position (for SCALING IN and SCALING OUT ONLY) 

*	spsNoChange (=0) - don't change previously set size for given bar 

New SetPositionSize function automatically encodes new methods of expressing
position size into old "positionsize" variable as follows: 


*	values below -2000 encode share count, 

*	values between -2000 and -1000 encode % of current position 

*	values between -1000 and 0 encode % of portfolio equity 

*	values above 0 encode dollar value 

Although it is possible to assign these values directly to old-style
PositionSize variable, new code should use SetPositionSize function for
clarity. 

If I'm not using margin and want to use the SetPositionSize( xx,
spsPercentOfEquity )  to take 2% of the current equity what do I set "xx" to
?

the from ..100 part is confoosing me.

In trying this with values of 1, 2, 3, (where these are %'s) as the first
buy, scale in 1, scale in 2,   its not behaving like I think it should.

Here's the current version of my code:

/*
  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. 
 At the end you could have approx 10% of total equity in this 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", 100000);
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");

// Buy and Sell Rules Go Here
Buy  = Cross( EMA( C, 10 ), EMA( C, 50 ) ); 
Sell = Cross( EMA( C, 50 ), EMA( C, 10 ) );

/*==========================================================================
====
 Scale-In Code Starts Here
============================================================================
==*/
if (Status("stocknum") == 0)
 _TRACE("DBGVIEWCLEAR");

// The scale in percents are entered here
TotalPosition  = 10; //Max Pct of Equity of all scaleins
FirstScaleIn   = 10; //Pct of Total Position
SecondScaleIn  = 20;
ThirdScaleIn   = 30;
FourthScaleIn  = 40;

// The percents are used to calculate the total % of equity
// for each scale in and then are put into an array.
// To change the number of scale ins just increase/decrease
// the number of elements in the array. The for loop will 
// continue to scale in as long as there are non 0 entries 
// in the array.
ScaleInPcts = Open - Open;
ScaleInPcts[ 1 ] = ( FirstScaleIn  * TotalPosition ) / 100;
ScaleInPcts[ 2 ] = ( SecondScaleIn * TotalPosition ) / 100;
ScaleInPcts[ 3 ] = ( ThirdScaleIn  * TotalPosition ) / 100;
ScaleInPcts[ 4 ] = ( FourthScaleIn * TotalPosition ) / 100;

ScaleInSize  = Open - Open; 
SIindx = 1; 

// following line is only needed for _Trace
Dte = DateNum();

// Loop thru the bars looking for buys that are set by the
// buy/sell rules. Once one is found scale in based on the 
// scale in rule.

for( i = 1; i < BarCount; i++ ) 
{ 
 // The initial Buy is detected here and the poisiton size is set
 if( Buy[ i ] 
   AND SIindx == 1 ) 
 { 
  ScaleInSize[ i ] = ScaleInPcts[ SIindx ];
  _TRACE(" ");
  _TRACE("Buy " + Name() 
   + " 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 )
   );
   } 
   // Once the initial buy is detected then we apply the scale in rule
   // if a scale in is triggered then the next percentage from the
ScaleInPcts
   // array is used to set the position size
   else If( SIindx > 1
     AND ScaleInPcts[ SIindx ] > 0
 
/*--------------------------------------------------------------------------
----
  Scale in Rule goes here
 
----------------------------------------------------------------------------
--*/
   AND Close[ i ] < Close[ i -1 ]
 
/*--------------------------------------------------------------------------
--*/
   AND Sell[ i ] == 0 )
   { 
  Buy[ i ] = sigScaleIn; 
  ScaleInSize[ i ] = ScaleInPcts[ SIindx ];
  _TRACE("SI" + NumToStr( i, 1.0 )
   + " 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 the last scale in percent was used or a sell signal was generated
 // then reset the index and make sure a scale in wasn't taken
 if( ScaleInPcts[ SIindx ] == 0 
   OR Sell[ i ] ) 
 { 
  Buy[ i ] = 0; 
  SIindx = 1; 
  if ( Sell[ i ] ) 
  {
    _TRACE("Sell  " + Name() + " On Date: " 
    + DateTimeToStr( DateTimeConvert( 2, Dte[ i ] )));
  }
 } 
 // If a Buy or a scale in was set above then increment the index
 else if( Buy[ i ] )
 {
  SIindx = SIindx + 1;
 } 
} 

// The line below is used when the trade delays are set to 1
SetPositionSize( ValueWhen( Buy, ScaleInSize ), spsPercentOfEquity );
/*==========================================================================
====
 Scale-In Code Ends Here
============================================================================
==*/

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