PureBytes Links
Trading Reference Links
|
Hello,
i have finished my CBI code almost, however i still need some help.
The problem can be best described by viewing the attached snapshot
from the Debug Viewer.
At bar 1 the Trade Risk is higher than 1%, therefore a scale out
signal is generated to scale out to get the risk below 1%.
At bar 1 it generates the amount of 26.607 contracts (rounding will be
implemented later) for Symbol SWI. Than, at the next bar, it scales
out 26.607 contracts at symbol SWI.
So far it would be correct, but unfortunately it scales out also
26.607 contracts at symbol BP.
The risk at symbol BP at bar 1 is 0.44% of equity, so there is no need
to scale out at the next bar.
I think that i need anywhere trade.Symbol...
Note: the detailed log shows 21.57 contracts to be scaled out at bar 3
while the Debug Viewer shows 26.607 contracts.
I really appreciate every help. Thanks in advance !
--
Regards
function FindValueAtDateTime( input, dt, Value )
{
found = -1;
for( i = 0; i < BarCount AND found == -1; i++ )
{
if( dt[ i ] == Value ) found = i;
}
return IIf( found != -1, input[ found ], Null );
}
if( Status("action") == actionPortfolio )
{
AddRMultipleColumn = StaticVarGet("AddRMultipleColumn");
bo = GetBacktesterObject();
bo.PreProcess();
dt = DateTime();
EquityArray = 0;
CurrentContracts = 0;
for(bar = 0; bar < BarCount; bar++)
{
bo.ProcessTradeSignals( bar ); // newly added
CurEquity[bar] = bo.Equity;
// iterate through open trades
for( trade = bo.GetFirstopenPos(); trade; trade = bo.GetNextopenPos() )
{
// Store entry values in dynamic variables
if(trade.BarsInTrade == 1)
{
VarSet("ContractsAtEntry" + trade.Symbol, trade.Shares);
VarSet("FxRateAtEntry" + trade.Symbol, trade.EntryFxRate);
}
// Get values from entry bar
EquityAtEntry = FindValueAtDateTime( EquityArray , dt, trade.EntryDateTime );
ContractsAtEntry = VarGet("ContractsAtEntry" + trade.Symbol);
FxRateAtEntry = VarGet("FxRateAtEntry" + trade.Symbol);
// Store current values in dynamic variables
VarSet("CurrentContracts" + trade.Symbol, trade.Shares);
// Get current values from dynamic variables
CurrentContracts = VarGet("CurrentContracts" + trade.Symbol);
if(bar > 0 AND DiffToMaxRisk[bar-1] > 0 )
CurrentContracts = CurrentContracts - ScaleAmountInContracts[bar-1];
MaxTradeRisk = StaticVarGet("MaxRiskPerTrade");
MaxTradeRiskPercent = MaxTradeRisk * 0.01;
Slippage = StaticVarGet("Slippage");
if(trade.IsLong)
{
// Get values from phase 1 -> composite indicators
RiskAtClose = Foreign("~AddValues_2" + trade.Symbol, "Open");
ScalePrice = trade.GetPrice( bar, "O" ) - Slippage ;
}
else
{
RiskAtClose = Foreign("~AddValues_2" + trade.Symbol, "High");
ScalePrice = trade.GetPrice( bar, "O" ) + Slippage ;
}
// Calculate current trade risk
RiskAtCloseCash[bar] = RiskAtClose[bar] * CurrentContracts[bar] *
trade.PointValue * FxRateAtEntry;
RiskAtClosePercent[bar] = RiskAtCloseCash[bar] / (0.01 * CurEquity[bar]);
DiffToMaxRisk[bar] = RiskAtCloseCash[bar] - MaxTradeRiskPercent *
CurEquity[bar];
RiskPerContract[bar] = RiskAtClose[bar] * trade.PointValue * FxRateAtEntry;
ScaleAmountInContracts[bar] = DiffToMaxRisk[bar] / RiskPerContract[bar];
ScaleAmountCashWithMargin[bar] = ScaleAmountInContracts[bar] *
trade.MarginDeposit * FxRateAtEntry +0.01;//0.01 necessary (rounding)
_TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ", Bars In
Trade = " + trade.BarsInTrade + ", Symbol " + trade.Symbol );
_TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ", Contracts
@ Entry = " + ContractsAtEntry + ", Current " + CurrentContracts[bar]
);
_TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ",
EquityAtEntry = " + EquityAtEntry + ", Current Equity = " +
bo.Equity);
_TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ",
MaxTradeRisk = " + MaxTradeRiskPercent * bo.Equity + "€"+ ", " +
MaxTradeRisk[bar] + "%" + ", DiffToMaxRisk = " + DiffToMaxRisk[bar] +
" € ");
// _TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ",
trade.GetEntryValue() = " + trade.GetEntryValue() +" € ");
// _TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ",
trade.GetPositionValue() = " + trade.GetPositionValue() +" € \n\n");
// _TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ", Risk = "
+ RiskAtClose[bar] +" Ticks, " + RiskAtCloseCash[bar] +" €, " +
RiskAtClosePercent[bar] +" % ");
_TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ", Current
Risk = " + RiskAtCloseCash[bar] +" €, " + RiskAtClosePercent[bar] +" %
");
// _TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ", FxRate @
Entry = " + FxRateAtEntry );
if(bar >= 0 AND DiffToMaxRisk[bar] > 0 )
_TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ", Scale
Out, Amount = " + DiffToMaxRisk[bar] + " €, Contracts = " +
ScaleAmountInContracts[bar] );
if(bar > 0 AND DiffToMaxRisk[bar-1] > 0 )
{
bo.ScaleTrade( bar, trade.Symbol, False, ScalePrice,
ScaleAmountCashWithMargin[bar-1], trade.MarginDeposit );
_TRACE("bar " + bar + ", " + DateTimeToStr(dt[bar]) + ", " +
ScaleAmountInContracts[bar-1] + " Contracts scaled out ");
}
_TRACE("\n");
}
EquityArray[bar] = bo.Equity;
}
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 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/
Attachment:
snap.png
Description: PNG image
Attachment:
Description: Binary data
|