TrendSurfer,
I cannot figure out why myMargin22 and myMargin22_foreign yield different results. I tried in a very simple script, no CBT, and the results with either myMargin22 variables is identical. However, when I try with the intended script, posted below, I get different results with myMargin22 defaulting to always using "1" and myMargin22_foreign dynamically changing from 1 to 2 correctly.
Thanks in advance for your help!
Mike
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
ApplyStop( stopTypeNBar, stopModeBars, 5 );
_SECTION_BEGIN("Entry / Exit");
UseShort = ParamToggle("Allow Short", "No|Yes", 0);
EntryOption = ParamList("Entry Price", "Close of Current Bar|Open of Next Bar|Close of Next Bar|Average OHLC of Next Bar");
UseSL = ParamToggle("Use Stop Loss", "No|Yes", 1);
UseTP = ParamToggle("Use Profit Target", "No|Yes", 0);
SLOnLostOption = ParamList("SL On Lost Option", "Keep SL|Reset SL", 1);
SLAdjustmentFactor = Param("SL Adjustment Factor", 1.0, 0.1, 100, 0.1);
MaxHoldingPeriod = Param("Max Holding Period", 100, 1, 100);
ProfitTargetPct = Param("Profit Target %", 40, 0.1, 100, 0.1)/100;
StopLossPct = Param("Stop Loss %", 1.5, 0.1, 100, 0.1)/100;
SlippagePct = Param("Slippage %", 0.0, -100, 100, 0.1)/100;
SLCount = Param("SL Count", 0, 0, 100, 1);
AllocMethod = ParamList("Allocation Method", "Equal|Maximum", 1);
MinPosCount = Param("Min # of Positions", 1, 1, 100, 1);
SymbolCount = Param("# of Symbols", 1, 1, 1000, 1);
MyMargin = Param("Margin ##:1", 2.0, 0, 10, 1);
_SECTION_BEGIN("MA Cross Strategy");
function Trace(tt, str) {
_TRACE(DateTimeToStr(tt)+" "+str);
}
function XMA(period) {
res = IIf(AvgType == "SMA", MA(O, period), EMA(O, period));
return res;
}
_SECTION_END();
//Expression 1
SPY_close = Foreign("SPY", "C");
SPY_trendup_foreign = SPY_close > MA(SPY_close, 2);
myMargin22_foreign = IIf(SPY_trendup_foreign, 2, 1);
//Expression 2
SPY_trendup = close > MA(close, 2);//clicking on SPY in watchlist & have set to "Current"
myMargin22 = IIf(SPY_trendup, 2, 1);
//assume timeframe = Daily
symbol = Name();
dt = DateTime();
dn = DateNum();
dow = DayOfWeek();
mn = Month();
newMonth = mn != Ref(mn, -1);
SetCustomBacktestProc("");
SetOption("AccountMargin",100/MyMargin);
if( Status( "action" ) == actionPortfolio ) {
/*
if ( GetOption( "ApplyTo" ) == 2 ) {
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
}
else if ( GetOption( "ApplyTo" ) == 0 ) {
List = CategoryGetSymbols( categoryAll, 0 );
}
else {
List = Name();
}
symbolCount = StrCount(List, ",")+1;
*/
_TRACE("Symbol Count "+symbolCount);
bo = GetBacktesterObject();
bo.PreProcess();
for( i = 0; i < BarCount; i++ ) {
bo.HandleStops(i);
//MR
LongList = "";
ShortList = "";
//_TRACE( "[AB] " +DateTimeToStr( dt[ i ] ) );
//bo.RawTextOutput( DateTimeToStr( dt[ i ] ) );
if (GetOption("PortfolioReportMode") != 0) {
bo.RawTextOutput( DateTimeToStr( dt[ i ] ) );
}
bo.UpdateStats(i, 0);
posCount = 0;
for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i)) {
if (sig.IsEntry()) {
posCount++;
}
else if (sig.IsExit()) {
bo.ExitTrade(i, sig.Symbol, sig.Price);
}
}
bo.UpdateStats(i, 1);
if (AllocMethod == "Equal") {
posSize = (bo.Cash *MyMargin22[i]) / symbolCount;
}
else {
posSize = (bo.Cash *MyMargin22[i]) / Max(MinPosCount, posCount);
}
for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i)) {
if (sig.IsEntry()) {
bo.EnterTrade(i, sig.Symbol, sig.IsLong(), sig.Price, posSize);
}
}
bo.UpdateStats(i, 2);
}
bo.PostProcess();
}
Startperiod= dow==5;
ShortTerm=Close;
LongTerm = Ref(Close,-20);
IsUp = ShortTerm > LongTerm;
IsDown = ShortTerm < LongTerm;
Buy1 = (ShortTerm-LongTerm)/Longterm *100 > 0.50 && StartPeriod && ShortTerm >MA(Close,200); //ShortTerm > LongTerm && StartPeriod;//ShortTerm > LongTerm && StartPeriod;
Short1 = ShortTerm < LongTerm && StartPeriod;
Buy1 = IIf(EntryOption == "Close of Current Bar", Buy1, Ref(Buy1, -1));
Short1 = IIf(EntryOption == "Close of Current Bar", Short1, Ref(Short1, -1));
BuyPrice = IIf(EntryOption == "Open of Next Bar", O,
IIf(EntryOption == "Average OHLC of Next Bar", (O+H+L+C)/4, C));
BuySL = BuyPrice*(1 - StopLossPct);
BuyTP = BuyPrice*(1 + ProfitTargetPct);
ShortPrice = BuyPrice;
ShortSL = ShortPrice*(1 + StopLossPct);
ShortTP = ShortPrice*(1 - ProfitTargetPct);
Buy = Sell = Short = Cover = 0;
inTrade = 0; //Buy = 1, Short = 2
SL=TP=0;
TradePeriod=0;
BuyCount = ShortCount = 0;
ExitCode = 1; //1 = By PT/SL, 2 = By End of Holding Period
LastProfit = 0;
FirstTrade = true;
EntryPrice = 0;
SLBuffer = TPBuffer = Null;
NextTradePeriod=0;
for (i=0; i<BarCount; i++) {
if (StartPeriod[i]) {
TradePeriod++;
}
if (inTrade != 0) {
TPBuffer[i] = TP;
SLBuffer[i] = SL;
if (inTrade == 1) { //Long
if (L[i] <= SL && UseSL) {
Sell[i] = 1;
inTrade = 0;
SellPrice[i] = IIf(O[i] <= SL, O[i], SL);
ExitCode = 1;
NextTradePeriod = TradePeriod+SLCount;
}
else if (H[i] >= TP && UseTP) {
Sell[i] = 1;
inTrade = 0;
SellPrice[i] = IIf(O[i] >= TP, O[i], TP);
ExitCode = 1;
}
else if (StartPeriod[i]) { //reached start of next period / end of current period
//else if (newWeek[i]) { //reached start of next period / end of current period
Sell[i] = 1;
inTrade = 0;
SellPrice[i] = C[i];
ExitCode = 2;
}
else if (BuyCount > MaxHoldingPeriod) { //3d. Exit at opposite signal
if (IsDown[i]) {
Sell[i] = 1;
inTrade = 0;
SellPrice[i] = C[i];
BuyCount = 0;
}
}
if (inTrade == 0) {
SellPrice[i] = (1+SlippagePct)*SellPrice[i];
LastProfit = SellPrice[i] - EntryPrice;
}
}
else { //Short
if (H[i] >= SL && UseSL) {
Cover[i] = 1;
inTrade = 0;
CoverPrice[i] = IIf(O[i] >= SL, O[i], SL);
ExitCode = 1;
NextTradePeriod = TradePeriod+SLCount;
}
else if (L[i] <= TP && UseTP) {
Cover[i] = 1;
inTrade = 0;
CoverPrice[i] = IIf(O[i] <= TP, O[i], TP);
ExitCode = 1;
}
else if (StartPeriod[i]) {
Cover[i] = 1;
inTrade = 0;
CoverPrice[i] = C[i];
ExitCode = 2;
}
else if (ShortCount > MaxHoldingPeriod) { //3d. Exit at opposite signal
if (IsUp[i]) {
Cover[i] = 1;
inTrade = 0;
CoverPrice[i] = C[i];
ShortCount = 0;
}
}
if (inTrade == 0) {
CoverPrice[i] = (1-SlippagePct)*CoverPrice[i];
LastProfit = EntryPrice[i] - CoverPrice[i];
}
}
}
if (inTrade == 0 && TradePeriod > NextTradePeriod) {
//check buy
if (Buy1[i]) {
Buy[i] = 1;
inTrade = 1;
TP = BuyTP[i];
//SLOnLostOption = ParamList("SL On Lost Option", "Keep SL|Reset SL", 0);
SL = IIf(ExitCode == 2 && LastProfit < 0 && SLOnLostOption == "Keep SL", SL = SL + SL*(SLAdjustmentFactor - 1), BuySL[i]);
TPBuffer[i] = TP;
SLBuffer[i] = SL;
BuyCount++;
ShortCount=0;
FirstTrade = False;
EntryPrice = BuyPrice[i];
}
else if (Short1[i] && UseShort) {
Short[i] = 1;
inTrade = 2;
TP = ShortTP[i];
SL = IIf(ExitCode == 2 && LastProfit < 0 && SLOnLostOption == "Keep SL", SL = SL - SL*(SLAdjustmentFactor - 1), ShortSL[i]);
TPBuffer[i] = TP;
SLBuffer[i] = SL;
ShortCount++;
BuyCount=0;
FirstTrade = False;
EntryPrice = ShortPrice[i];
}
else {
BuyCount=0;
ShortCount=0;
}
}
}
bgColor = IIf(StartPeriod, colorLime, colorDefault);
PlotShapes(IIf(StartPeriod, shapeSquare, shapeNone), colorWhite, 0, H, 0);
PlotShapes(IIf(StartPeriod, shapeSquare, shapeNone), colorWhite, 0, L, 0);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorLime, 0, L, -24);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone), colorRed, 0, H, -24);
PlotShapes(IIf(Sell, shapeCircle, shapeNone), colorLime, 0, SellPrice, 0);
PlotShapes(IIf(Cover, shapeCircle, shapeNone), colorRed, 0, CoverPrice, 0);
Plot(ShortTerm, "Short Term", colorYellow);
Plot(LongTerm, "Long Term", colorLightBlue);
if (UseSL) {
Plot(SLBuffer, "Stop Loss", colorRed, styleDashed);
}
if (UseTP) {
Plot(TPBuffer, "Profit Target", colorRed, styleDashed);
}
Filter = 1;
AddColumn(StartPeriod, "Period", 1.0);
AddColumn(Buy, "Buy", 1.0);
AddColumn(Short, "Short", 1.0);
AddColumn(Close, "Close", 1.0);
AddColumn(MA(Close, 2), "MA(Close, 2)", 1.0);
AddColumn(SPY_trendup_foreign, "SPY_trendup_foreign", 1.0);
AddColumn(myMargin22_foreign, "myMargin22_foreign", 1.0);
AddColumn(SPY_trendup, "SPY_trendup", 1.0);
AddColumn(myMargin22, "myMargin22", 1.0);
_SECTION_END();
type or paste code here