Switch-Case Function

I performed an Optimization and extracted the optimized parameters.

How can it be code that for each function, it calls the correct Weight1, Weight2...Weight5?

As coded it always defaults to 1. I likely need the Case statement but cannot figure out how to code.

I realize the example is short and i could code manually but will have much larger portfolios in the future.

Thank you,

Mike

weight1=
IIF(AUDCAD_IDEALPRO_CASH_30Val,1  ,
IIF( AUDCHF_IDEALPRO_CASH_30Val,-1  ,
IIF( AUDJPY_IDEALPRO_CASH_30Val,0.5  ,
1)));


weight2=
IIF(AUDCAD_IDEALPRO_CASH_30Val,-0.5  ,
IIF(AUDCHF_IDEALPRO_CASH_30Val,-0.5  ,
IIF(AUDJPY_IDEALPRO_CASH_30Val,1  ,
1)));

weight3=
IIF(AUDCAD_IDEALPRO_CASH_30Val,-1  ,	
IIF(AUDCHF_IDEALPRO_CASH_30Val,1  ,	
IIF(AUDJPY_IDEALPRO_CASH_30Val,-1  ,	
1)));

weight4=
IIF(AUDCAD_IDEALPRO_CASH_30Val,-1  ,
IIF(AUDCHF_IDEALPRO_CASH_30Val,1  ,
IIF(AUDJPY_IDEALPRO_CASH_30Val,0  ,
1)));

weight5=
IIF(AUDCAD_IDEALPRO_CASH_30Val,0  ,
IIF(AUDCHF_IDEALPRO_CASH_30Val,0.5  ,
IIF(AUDJPY_IDEALPRO_CASH_30Val,1  ,
1)));

AUDCAD_IDEALPRO_CASH_30Val	=mROC("EUR.USD-IDEALPRO-CASH","CC1" )*Weight1+mROC("USD.CAD-IDEALPRO-CASH","CC1" )*Weight2+mROC("EUR.NZD-IDEALPRO-CASH","CC1" )*Weight3+mROC("EUR.AUD-IDEALPRO-CASH","CC1" )*Weight4+mROC("EUR.GBP-IDEALPRO-CASH","CC1" )*Weight5;
AUDCHF_IDEALPRO_CASH_30Val	=mROC("EUR.USD-IDEALPRO-CASH","CC1" )*Weight1+mROC("USD.CAD-IDEALPRO-CASH","CC1" )*Weight2+mROC("EUR.NZD-IDEALPRO-CASH","CC1" )*Weight3+mROC("EUR.AUD-IDEALPRO-CASH","CC1" )*Weight4+mROC("EUR.GBP-IDEALPRO-CASH","CC1" )*Weight5;
AUDJPY_IDEALPRO_CASH_30Val	=mROC("EUR.GBP-IDEALPRO-CASH","CC1" )*Weight1+mROC("GBP.CAD-IDEALPRO-CASH","CC1" )*Weight2+mROC("GBP.AUD-IDEALPRO-CASH","CC1" )*Weight3+mROC("GBP.CHF-IDEALPRO-CASH","CC1" )*Weight4+mROC("CHF.JPY-IDEALPRO-CASH","CC1" )*Weight5;

type or paste code here

This doesn't look anything like the switch function I know. Have you read the help section?
http://www.amibroker.com/guide/keyword/switch.html

Yes, I know. I tried coding as a case statement but it would not Verify syntax so I tried this as an alternative but it is not correct. I was at the link you pointed to but cannot get the correct syntax.

Thanks,

Mike

The switch function requires one variable for the condition, and the variable must be a number or string. You're using conditional logic based on three variables, and perhaps your variables are arrays (I don't know your code) so you need a helper variable before the switch. If those three variables are arrays, you cannot use switch.

Assuming the three variables are numbers, I assigned the weights for AUDCAD but not the others; you can figure it out easily.

weight1 = 0;
weight2 = 0;
weight3 = 0;
weight4 = 0;
weight5 = 0;

value = IIF( AUDCAD_IDEALPRO_CASH_30Val, "AUDCAD",
             IIF( AUDCHF_IDEALPRO_CASH_30Val, "AUDCHF",
                  IIF( AUDJPY_IDEALPRO_CASH_30Val, "AUDJPY", "" ) ) );

switch( value )
{
	case "AUDCAD":
		weight1 = 1;
		weight2 = -0.5;
		weight3 = -1;
		weight4 = -1;
		weight5 = 0;
		break;

	case "AUDCHF":
		weight1 = 0; // You can take it from here...
		weight2 = 0;
		weight3 = 0;
		weight4 = 0;
		weight5 = 0;
		break;

	case "AUDJPY":
		weight1 = 0;
		weight2 = 0;
		weight3 = 0;
		weight4 = 0;
		weight5 = 0;
		break;
}


1 Like

You want to return string so you should use WriteIf not IIf.


But actually you do not even need switch statement if you want to do that way.

Here is shorter alternative

AUDCAD_IDEALPRO_CASH_30Val = 1;
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,0,0,0}}";
weights = MxTranspose(MxFromString(weights_string));

printf(MxToString(weights));
printf("\nSymbol: %s\n", StrExtract("AUDCAD,AUDCHF,AUDJPY", value));

weights_block = MxGetBlock(weights, 0, MxGetSize(weights,0)-1, value, value);

weigths1 = weights_block[0][0];
weigths2 = weights_block[1][0];
weigths3 = weights_block[2][0];
weigths4 = weights_block[3][0];
weigths5 = weights_block[4][0];

Or even shorter

AUDCAD_IDEALPRO_CASH_30Val = 1;
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,0,0,0}}";
weights = MxTranspose(MxFromString(weights_string));

printf(MxToString(weights));
printf("\nSymbol: %s\n", StrExtract("AUDCAD,AUDCHF,AUDJPY", value));

weights_block = MxGetBlock(weights, 0, MxGetSize(weights,0)-1, value, value);

for( i = 0; i < 5; i++ )
	VarSet("weights"+(i+1), weights_block[i][0]);
3 Likes

PeterD & fxshrat,

Thank you both for your detailed replies. I will implement each and report back.

Best,

Mike

That's a great catch on WriteIf... I didn't check that code enough before posting.

I love your shorter alternates. It's like working towards an advanced degree in coding. :sunglasses:

Again, that you both for your time & expertise!

fxshrat, as always, your ability to see multiple solutions to a problem and turn those solutions into code is genius.

Thank you,

Mike

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 );

You did not mention that you want to use all weights at same time, did you?

EnableTextOutput(0);
symbols = "AUDCAD,AUDCHF,AUDJPY";
weights_string = "{{-1,-1,-1,-1,-1},"+	// AUDCAD
				"{0,0,0,0,0},"+			// AUDCHF
				"{-0.5,-1,-1,-1,1}}";	// AUDJPY

sym_count = StrCount(symbols, ",")+1;
weights = MxTranspose(MxFromString(weights_string));
printf(MxToString(weights));

for( i = 0; i < 5; i++ )
	for( j = 0; j < sym_count; j++ )
		VarSet("weight"+(i+1)+"_"+StrExtract(symbols, j), weights[i][j]);
	
printf("\n\nAUDCAD\n%g, %g, %g, %g, %g", weight1_AUDCAD,weight2_AUDCAD,weight3_AUDCAD,weight4_AUDCAD,weight5_AUDCAD);
printf("\nAUDCHF\n%g, %g, %g, %g, %g", weight1_AUDCHF,weight2_AUDCHF,weight3_AUDCHF,weight4_AUDCHF,weight5_AUDCHF);
printf("\nAUDJPY\n%g, %g, %g, %g, %g", weight1_AUDJPY,weight2_AUDJPY,weight3_AUDJPY,weight4_AUDJPY,weight5_AUDJPY);

24

fxshrat,

If use the below snippet of code I do not get any results. If I manually enter each weight, replacing weight1, weight2...with the weights, I get the correct values. Something is preventing the weights from being passed to the formula. I appreciate your time, especially on the weejkend!

Mike

weight1=0;
weight2=0;
weight3=0;
weight4=0;
weight5=0;

EnableTextOutput(0);
symbols = "AUDCAD,AUDCHF,AUDJPY";
weight_string = "{{-1,-1,-1,-1,-1},"+	// AUDCAD
				"{0,0,0,0,0},"+			// AUDCHF
				"{-0.5,-1,-1,-1,1}}";	// AUDJPY

sym_count = StrCount(symbols, ",")+1;
weight = MxTranspose(MxFromString(weight_string));
printf(MxToString(weight));

for( i = 0; i < 5; i++ )
	for( j = 0; j < sym_count; j++ )
		VarSet("weight"+(i+1)+"_"+StrExtract(symbols, j), weight[i][j]);
	
printf("\n\nAUDCAD\n%g, %g, %g, %g, %g", weight1_AUDCAD,weight2_AUDCAD,weight3_AUDCAD,weight4_AUDCAD,weight5_AUDCAD);
printf("\nAUDCHF\n%g, %g, %g, %g, %g", weight1_AUDCHF,weight2_AUDCHF,weight3_AUDCHF,weight4_AUDCHF,weight5_AUDCHF);
printf("\nAUDJPY\n%g, %g, %g, %g, %g", weight1_AUDJPY,weight2_AUDJPY,weight3_AUDJPY,weight4_AUDJPY,weight5_AUDJPY);


//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}  {1,-1,1,1,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)));

Please take a look at the printf arguments!
Not just copy & pasting.

weight1_AUDCAD and so on...
weight1_AUDCHF and so on...
weight1_AUDJPY and so on...

So needless to say what you have to add to

AUDCAD_IDEALPRO_CASH_30Val		=mROC("NZDUSD.FXCM","CC1")*Weight1_AUDCAD*...

And so on...

1 Like

fxshrat,

That did it!

Thank you very much for your expertise, patience, and time!

Best,

Mike

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.