I want to set up entry logic that is directly associated with it's associated exit logic. I have used '-nnn' in place of 'PositionScore". Is 'Position Score' used properly or is there a better means?
Day One >> Enter MSFT-200, Enter MSFT-300
Day Two >> Exit MSFT-300, Enter MSFT-500
Day Three >> Exit MSFT-200, Exit MSFT-500
Any mistakes or suggestions about syntax or style, like a Case statement or other more direct or efficient execution is appreciated.
Questions-
- Is the 'AND watchlist' is needed for the exit logic?
- Once implemented, does CBT do all Amibroker processing? In other words, the chart uses CBT logic as well as back testing, optimization, etc.
Thank you any and all.
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Close> $%.2f,", C ) );
P = ParamField( "Price field", 3 );
Plot( P, "Price", ParamColor( "Color", colorDefault ), styleBar | ParamStyle( "Style" ) | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN( "M4" );
M4_L_WL = InWatchListName("M4_L_30");
M4_S_WL = InWatchListName("M4_S_30");
prcM4 = ParamField( "Price field", 3 );
fM4 = Param( "M4 MA Period", 5, 1, 20, 1 );
fstM4 = MA( prcM4, fM4 );
Buy_M4 = prcM4 <= fstM4 AND M4_L_WL;
Short_M4 = prcM4 >= fstM4 AND M4_S_WL;
barM4_L = Param("M4 Long Exit Bars", 3, 0, 10, 1);
barM4_S = Param("M4 Short Exit Bars", 4, 0, 10, 1);
_SECTION_END();
_SECTION_BEGIN( "MV" );
MV_L_WL = InWatchListName("MV_L_30");
MV_S_WL = InWatchListName("MV_S_30");
prcMV = ParamField( "Price field", 3 );
fMV = Param( "MV MA Period", 5, 1, 20, 1 );
fstMV = MA( prcMV, fMV );
Buy_MV = prcMV <= fstMV AND MV_L_WL;
Short_MV = prcMV >= fstMV AND MV_S_WL;
barMV_L = Param("MV Long Exit Bars", 5, 0, 10, 1);
barMV_S = Param("MV Short Exit Bars", 6, 0, 10, 1);
_SECTION_END();
_SECTION_BEGIN( "Trade Logic" ); //%//%//%//%//%//%//%/%//%//%//%//%//%//%//%//%///%//%
// Each symbol is processed by trade specific logic.
Buy = Buy_M4 OR Buy_MV;
Short = Short_M4 OR Short_MV;
PositionScore = IIf( Buy_M4, 200,
IIf( Buy_MV, 300,
IIf( Short_M4, 500,
IIf( Short_MV, 600, 0 ))));
Sell = Cover = 0;
BuyPrice = SellPrice = ShortPrice = CoverPrice = P;
maxpos = Param("Max Open Positions", 1000, 0, 10000, 1);
SetOption( "MaxOpenPositions", maxpos );
SetOption( "InitialEquity", 10000000 );
SetPositionSize( 1, spsPercentOfEquity );
SetTradeDelays( 0, 0, 0, 0 );
SetOption("UseCustomBacktestProc", 1);
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
if( !IsNull(bo) )
{
bo.PreProcess();
exitCount = 0;
for( bar = 1; bar < BarCount; bar++ )
{
bo.UpdateStats( bar, 0 );
bo.HandleStops( bar );
bo.ProcessTradeSignals( bar );
for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() )
{
signalType = pos.Score;
exitPrice = pos.GetPrice( bar, "C" );
BarsInTrade = pos.BarsInTrade;
if( signalType == 200 )
{
if( BarsInTrade >= barM4_L )
{
bo.ExitTrade( bar, pos.Handle, exitPrice, "M4_L Time" );
exitCount++;
}
}
else if( signalType == 300 )
{
if( BarsInTrade >= barMV_L )
{
bo.ExitTrade( bar, pos.Handle, exitPrice, "MV_L Time" );
exitCount++;
}
}
else if( signalType == 500 )
{
if( BarsInTrade >= barM4_S )
{
bo.ExitTrade( bar, pos.Handle, exitPrice, "M4_S Time" );
exitCount++;
}
}
else if( signalType == 600 )
{
if( BarsInTrade >= barMV_S )
{
bo.ExitTrade( bar, pos.Handle, exitPrice, "MV_S Time" );
exitCount++;
}
}
}
bo.UpdateStats( bar, 2 );
}
bo.PostProcess();
}
}
_SECTION_END();