Eric,
You need to use SetOption. See code
below I use as an include file to setup many of the SETTINGS available in the
backtester. This insures I don't forget and test the wrong settings AND gives
me some easy control via Parameters.
IMPORTANT: Also note the two lines of code
for PositionSize toward the bottom of my code. You must double position size to
get 50% margin to work.
Feel free to modify this code to suit
your preferences. Save as SetOptions.afl and then #include this code at the top
of your own AFLs.
/*************************************** Settings for Operation
***************************************/
when = ParamToggle("Trade Day:","Next Day,Same
Day",0);
if (when) SetTradeDelays(0,0,0,0); else SetTradeDelays(1,1,1,1);
OC = ParamToggle("Trade Time:","Open,Close",0);
if (OC) BuyPrice = SellPrice = ShortPrice = CoverPrice
= C; else BuyPrice
= SellPrice = ShortPrice = CoverPrice = O;
WriteIf(OC,"Close","Open");
//Set trading mode and commissions
_N(mode = ParamList("Trading Mode","Stocks 1:1,No Commission Funds 1:1,Futures xx:1",0));
if (StrLeft(mode,1) == "S") {fm = False;
cm = 3; margin = 100; myCommission =
.005;} //$0.005 per
share
if (StrLeft(mode,1) == "N") {fm = False;
cm = 2; margin = 100; myCommission = 0;} //No commission
ProFunds or Rydex
if (StrLeft(mode,1) == "F") {fm = True;
cm = 3; margin = 100; myCommission = 1.65;} //$1.65 per
contract
SetOption("FuturesMode",fm); //Requires True or False
SetOption("CommissionMode", cm); /* Modes explained next...
0 - use portfolio
manager commission table
1 - percent of trade
2 - $ per trade
3 - $ per
share/contract */
SetOption("CommissionAmount", myCommission); //Used to allow for slippage and commissions.
SetOption("MaxOpenPositions",1); //Allows for simultaneous Long and Short positions
or multiple tickers
SetOption("NoDefaultColumns",False);
SetOption("InitialEquity", Param("Initial Equity",10000,10000,1000000,5000));
SetOption("AllowSameBarExit",False);
SetOption("ActivateStopsImmediately",False);
SetOption("AllowPositionShrinking",True);
SetOption("InterestRate",0);
SetOption("MinShares",1);
SetOption("PriceBoundChecking",True)
;
SetOption("MarginRequirement",margin);
SetOption("ReverseSignalForcesExit",True);
//Normally True
if not trading Long and Short simultaneously
SetOption("UsePrevBarEquityForPosSizing",False);
PositionSize = Param("Position Size - see notes in W_Options.afl",-100,-100,1000000,1);
/* How to use PositionSize...
Positive Values invest that amount of money in
each trade.
Negative values invest a percentage of your
portfolio value in each trade.
So, to invest $25,000 in each trade enter
25000, profits will accumulate (hopefully).
To invest half of all your Equity in each trade
enter -50, profits compound.
The "normal" mode is to invest all
your money in each trade AND let it compound. Enter -100 to do this.*/
leverage = 100/margin; //defines leverage for stop loss and gain calcs
based on selected margin.
PositionSize = IIf(PositionSize
> 0,PositionSize,PositionSize * leverage);
RoundLotSize = 1; //Overrides the Settings page.
SetChartOptions(0, chartShowDates);
//The following 4 lines for ApplyStop are here to prevent AA
Settings from over-riding this code.
ApplyStop(stopTypeLoss,stopModeDisable,100,1,False,0);
ApplyStop(stopTypeTrailing,stopModeDisable,100,1,False,0);
ApplyStop(stopTypeNBar,stopModeDisable,100,1,False,0);
ApplyStop(stopTypeProfit,stopModeDisable,100,1,False,0);
//END OF CODE
--
Terry
-----Original Message-----
From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of
eric paradis
Sent: Thursday, October 27, 2005 15:03
To: amibroker@xxxxxxxxxxxxxxx
Subject: [amibroker] Modifying margin inside AFL Code
hi,
Is it possible to use MarginRequirement to modify my
margin amount inside an Iif Statement using AFL? This
example seems to work, but never seems to execute-
MarginRequirement = IIf (Equity() * 1.1 <
Ref(Equity(),-50),100 ,50);
MarginRequirement = IIf (Equity() * .95 >
Ref(Equity(),-30),25,50);
Any ideas why it may not be executing?
Thanks,
Eric
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
------------------------ Yahoo! Groups Sponsor
--------------------~-->
Put more honey in your pocket. (money matters made easy).
http://us.click.yahoo.com/r7D80C/dlQLAA/cosFAA/GHeqlB/TM
--------------------------------------------------------------------~->
Please note that this group is for discussion between users only.
To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
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/
<*> 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/