Hi fxshrat,
I am trying to implement your shorted solution. However, I cannot get the mROC formula to use the weight variables per the weight_string. In each unique case, I would like the mROC to replace the Weight1, Weight2...with the corresponding number in the "use this" above comment?
Please advise when you have the chance.
Thanks you,
Mike
_SECTION_BEGIN("Mean Reversion Trading System");
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 ) ) ));
SetOption("MaxOpenPositions", 14);
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
Leverage=10.00;
SetOption("AccountMargin",100);
SetPositionSize (((100/14)*Leverage),spsPercentofEquity);
SetOption( "AllowPositionShrinking", True );
//LT MA
ST_MA=ATR(51);//MA(Close,50);
LT_MA=ATR(201); //MA(Close,200);
// Tested on DIA.US - 5 minutes
skipNTDays = ParamToggle("Skip NON TRADING DAYS", "No|Yes", 1); //"1201223,1201224,1201225,1201231,1210101,1210102,1210104,1210105,1210106")
// some random dates to test it
diaNTDn = ParamStr("Comma separated list of day numbers",
"1100101,1100102," +
"1101224,1101225,1101231,1110101,1110102," +
"1111224,1111225,1111231,1120101,1120102," +
"1121224,1121225,1121231,1130101,1130102," +
"1131224,1131225,1131231,1140101,1140102," +
"1141224,1141225,1141231,1150101,1150102," +
"1151224,1151225,1151231,1160101,1160102," +
"1161224,1161225,1161231,1170101,1170102," +
"11701224,1171225,1171231,1180101,1180102,"+
"1181224,1181225,1181231,1190101,1190102," +
"1191224,1191225,1191231,1200101,1200102," +
"1201224,1201225,1201231,1210101,1210102");
dn = DateNum();
ntDays = 0; // NON TRADING DATES - will be an array
if (skipNTdays) {
for( i = 0; ( ntdstr = StrExtract( diaNTDn, i ) ) != ""; i++ )
{
dayNum = StrToNum( ntdstr );
// _TRACE("Day # " + dayNum);
// populate the array of "non trading days"
ntDays = IIf( dn == dayNum OR ntDays, 1, 0 ); // Need to apply OR to keep any previous TRUE (1) values
}
}
Trading_Hours = ParamList("Trading Hours","US_Hours|24_Hours|At_US_Close|FIXI|NYClose",1); //
switch (Trading_Hours)
{
case Trading_Hours = "US_Hours"://20%
FirstTradeTime= 050000;//05 hourly enter next bar open/limit
SecondTradeTime= 110000;
SquareOffTime = 120000;//14 06
AMSessionEndTime = 080000;//14 06
DaySessionEndTime = 150000;//13 14
NO_TRADE = 170000;
break;
case Trading_Hours = "24_Hours"://40%
FirstTradeTime= 0 ;
SquareOffTime = 240000;
DaySessionEndTime = 150000;
NO_TRADE = 170000;
break;
case Trading_Hours = "At_US_Close"://40%
FirstTradeTime= 080000;
SquareOffTime = 150000;
NO_TRADE = 170000;
break;
case Trading_Hours = "FIXI"://40%
FirstTradeTime= 110000 ;//100000 hour brfore & hour after =09 & 11
SquareOffTime = 120000;//11
FIXITIME = 110000;
DaySessionEndTime = 150000;
NO_TRADE = 170000;
break;
case Trading_Hours = "NYClose"://40%
FirstTradeTime= 160000 ;//080000 hour brfore & hour after =09 & 11
SquareOffTime = 170000;//130000
FIXITIME = 170000;
DaySessionEndTime = 150000;
NO_TRADE = 170000;
break;
default:
FirstTradeTime= 090000;
SquareOffTime = 160000;
break;
}
LookBack =24; //24*4;// optimize("LookBack",12,96,96*15,96);//24*4;// 8=2 days back on 15-min chart, 1=6hours,0.5=3hours,etc.. (24hours*multiple)/4(15-minute bars)
function mROC( symbol, mode )
{
fO = Foreign( symbol, "O");
fC = Foreign( symbol, "C");
fH = Foreign( symbol, "H");
fL = Foreign( symbol, "L");
switch ( mode )
{
case "CC1":
returnvalue = ROC( fC, LookBack );
break;
case "OC0":
returnvalue = (fC- fO)/fO*100;
break;
case "HC0": //HCO
returnvalue = (fC - fH)/fH*100;
break;
case "LC0": //LC0
returnvalue = (fC - fL)/fL*100; // returnvalue = (fC - fL)/fL*100;
break;
default:
returnvalue = Null;
break;
}
return returnvalue;
}
n = Name();
AUDCAD_IDEALPRO_CASH_30Val=0;
AUDCHF_IDEALPRO_CASH_30Val=0;
AUDJPY_IDEALPRO_CASH_30Val=0;
VALUE=IIF(AUDCAD_IDEALPRO_CASH_30Val,0,
IIF(AUDCHF_IDEALPRO_CASH_30Val ,1,
IIF(AUDJPY_IDEALPRO_CASH_30Val ,2,
0)));
//weights_string = "{{1,-0.5,-1,-1,0},{0,0,0,0,0},{0,0
weight_string = "{{-1,-1,-1,-1,-1},{0,0,0,0,0},{-0.5,-1,-1,-1,1}}";
weight = MxTranspose(MxFromString(weight_string));
printf(MxToString(weight));
printf("\nSymbol: %s\n", StrExtract("AUDCAD,AUDCHF,AUDJPY", value));
weight_block = MxGetBlock(weight, 0, MxGetSize(weight,0)-1, value, value);
for( i = 0; i < 5; i++ )
VarSet("weight"+(i+1), weight_block[i][0]);
//use this{-1,-1,-1,-1,-1}
AUDCAD_IDEALPRO_CASH_30Val =mROC("NZDUSD.FXCM","CC1")*Weight1*-1.4886217941978+mROC("EURUSD.FXCM","CC1")*Weight2*-0.427856540232707+mROC("USDJPY.FXCM","CC1")*Weight3*1.5+mROC("USDCHF.FXCM","CC1")*Weight4*1.5+mROC("GBPNZD.FXCM","CC1")*Weight5*0.302876309259443;
//use this {0,0,0,0,0}
AUDCHF_IDEALPRO_CASH_30Val =mROC("EURUSD.FXCM","CC1")*Weight1*-1.5+mROC("USDCAD.FXCM","CC1")*Weight2*1.5+mROC("EURNZD.FXCM","CC1")*Weight3*-0.388652520989642+mROC("EURAUD.FXCM","CC1")*Weight4*-1.48437375451469+mROC("EURGBP.FXCM","CC1")*Weight5*0.273375858683219;
//use this {-0.5,-1,-1,-1,1}
AUDJPY_IDEALPRO_CASH_30Val =mROC("EURGBP.FXCM","CC1")*Weight1*-1.5+mROC("GBPCAD.FXCM","CC1")*Weight2*1.5+mROC("GBPAUD.FXCM","CC1")*Weight3*1.5+mROC("GBPCHF.FXCM","CC1")*Weight4*1.5+mROC("CHFJPY.FXCM","CC1")*Weight5*1.5;
Val=IIF(n=="AUDCAD.FXCM", AUDCAD_IDEALPRO_CASH_30Val,
IIF(n=="AUDCHF.FXCM", AUDCHF_IDEALPRO_CASH_30Val ,
IIF(n=="AUDJPY.FXCM", AUDJPY_IDEALPRO_CASH_30Val ,
0)));
timeMultiple=0.5;//3
SlippageMultiplier=IIF(n=="GBPJPY.FXCM", 1000*timeMultiple,
IIF(n=="AUDJPY.FXCM", 1000*timeMultiple ,
IIF(n=="CADJPY.FXCM", 1000*timeMultiple ,
IIF(n=="CHFJPY.FXCM", 1000*timeMultiple ,
IIF(n=="EURJPY.FXCM", 1000*timeMultiple ,
IIF(n=="NZDJPY.FXCM", 1000*timeMultiple ,
IIF(n=="USDJPY.FXCM", 1000*timeMultiple ,
10*timeMultiple)))))));
// buy on the next bar
Buy = MA(Ref( Val, -1),1) >1.00 ;//&& Ref( Val, -2) > 0;
Short = MA(Ref( Val, -1),1) <-1.00;// && Ref( Val, -2) < -0;
BuyLimitPrice = Open-(0.0001*slippagemultiplier);
ShortLimitPrice = Open+(0.0001*slippagemultiplier);
// now we check if limit was hit
//Buy = Hold( Buy, 1 ) AND L < BuyLimitPrice;
//Short = Hold( Short, 1 ) AND H > ShortLimitPrice;
//if Open price is below the limit, then we use Open for entry
BuyPrice = BuyLimitPrice; //Min( Open, BuyLimitPrice );
ShortPrice =ShortLimitPrice;// Max( Open, ShortLimitPrice );
Buy = Buy AND L < BuyLimitPrice;
Short = Short AND H > ShortLimitPrice;
SellPrice=Open;
CoverPrice=Open;
Buy= Buy AND TimeNum()>= FirstTradeTime AND TimeNum()<=SquareOffTime AND ntDays == 0 ;
Sell = MA(Val,1) < 1.0 or TimeNum()>=DaySessionEndTime or TimeNum()==AMSessionEndTime;
Short= Short AND TimeNum()>= FirstTradeTime AND TimeNum()<=SquareOffTime AND ntDays == 0 ;
Cover=MA(Val,1)>-1.0 or TimeNum()>=DaySessionEndTime or TimeNum()==AMSessionEndTime;
StopDelay=1;
TargetDelay=1;
StopLoss=0.50;
Target=0.55;
SetOption("ActivateStopsImmediately", False) ;//set tio True for other than FX
ApplyStop(stopTypeProfit,stopModePercent,Target,1,False,ReEntryDelay =10,0);
ApplyStop(stopTypeLoss, stopModePercent, IIf(TimeNum()== No_Trade, 50, StopLoss), 1, False, ReEntryDelay = 10, 0);//matt
Filter= Buy OR Sell OR Short OR Cover;
filter = close > 0;
AddColumn( Close, "Close" );
AddColumn( Val, "Rule Val" );
AddColumn( BuyPrice, "Buy Price" );
AddColumn( IIf( Buy, 'B', 'S' ), "Signal", formatChar );
AddColumn( IIf( Short, 't', 'c' ), "Signal", formatChar );