enum case / if else

Posted By: danatrader

enum case / if else - 02/25/20 21:25

Again, I am sorry for bothering, but for most of you easy question.


Following works fine in Lite-C:

Code
while(algo(loop("TRND"))) //,"ALGO2","ALGO3","ALGO4")))
		{
			NumComponents = NumLoops1*NumLoops2;
			algoSetup();
			equityCurve();
			switch(Algo) {
				case "TRND": tradeTRND(); break;
				//case "ALGO2": tradeCNTR(); break;
				//case "ALGO3": tradeALGO3(); break;
				//case "ALGO4": tradeALGO4(); break;
// add more algos here
			}


Cause of my lack of knowledge, changing the code I have to C / C++ (dll) I get some errors on compilation in the complete context.
Those I solved, but, the case / switch wants an enumeration.
Don't get it peroper to be accepted by the compiler.


So changed to "if else"

Code
if (0 == strcmp(Algo, "TRND"))
				tradeTRND();
			else if (0 == strcmp(Algo, "CNTR"))
				tradeCNTR();


compiles without errors, just doesn't seem to be correct...

Any advice?
Posted By: AndrewAMD

Re: enum case / if else - 02/25/20 21:31

Your strcmp approach appears to be correct, so perhaps it's some other bug?
Posted By: danatrader

Re: enum case / if else - 02/25/20 21:39

Yes, somewhere something isn't structured properly I think.
But can't grab it frown


Code

#include "stdafx.h"
#include <zorro.h>
#include <Reflex.c>

/* Trading 4 user specified algorithms
for a portfolio of assets
in a separate asset list.
User control by Capital and Panic (profit lock) sliders.
Exit by reversal and trailing stop loss.
Equity curve (phantom) trading.
OptimalF capital allocation.
Reinvestment with square root rule.
WFO training.*/


// User definitions ////////////////////////////////

#define ASSETLIST		"AssetsCurTest"	
#define STARTDATE		2012	// backtest period
#define BARPERIOD		60	
#define LOOKBACKBARS	500
#define ATRPERIOD		30		// for stop distance
#define MAXCAPITAL	10000	// slider limit
#define CAPITALFACTOR 30	// adjust for slider capital ~ required capital
//#define ZERO_COST		// Test with no spread & commission
#define WFOCYCLES		10
#define MAXTRADES		2
#define RETRAINDAYS	100	// retraining period
#define PHANTOMTIME	20		// equity curve trading

/////////////////////////////////////////////////////
//Trading Algos

vars Prices;
var Atr, Panic;
int NumComponents = 1;

// example algo

void tradeTRND()
{
	var TimePeriod = optimize(0.1, 0.025, 0.5, 0.025, 0);
	vars SmoothPrice = series(Laguerre(Prices, TimePeriod));
	int FilterPeriod = optimize(300, 50, 500, 100, 0);

	vars MMI_Raws = series(MMI(Prices, 300));
	vars MMI_Avgs = series(SMA(MMI_Raws, 300));

	if (falling(MMI_Avgs)) {
		if (valley(SmoothPrice)) {
			exitShort(); enterLong();
		}
		else if (peak(SmoothPrice)) {
			exitLong(); enterShort();
		}
	}
}

void tradeCNTR()
{
	// calculate the buy/sell signal with optimized parameters
	vars PriceAvg = series(price(0));
	vars Filtereds = series(BandPass(PriceAvg, optimize(30, 20, 40, 3, 0), 0.5));
	vars Signals = series(FisherN(Filtereds, 500));
	var Threshold = optimize(1, 0.5, 1.5, 0.1, 0);

	// buy and sell
	Stop = optimize(4, 2, 10, 2, 0) * ATR(100);
	Trail = 4 * ATR(100);

	if (crossUnder(Signals, -Threshold))
		enterLong();
	else if (crossOver(Signals, Threshold))
		enterShort();
}


void algoSetup()
{
	MaxLong = MaxShort = MAXTRADES;
	// order comment
	brokerCommand(SET_COMMENT, (DWORD)Algo);
	// Reinvestment with square root rule. 
	var CapitalPerComponent = abs(Capital) / NumComponents;
	Margin = OptimalF * CapitalPerComponent * CAPITALFACTOR * sqrt(max(0.5, 1 + ProfitClosed / CapitalPerComponent));
	// Exit by reversal and trailing stop loss. 
	Hedge = 5;
	Stop = optimize(10, 4, 30, 2, 0) * Atr;
	Trail = Stop;
	TrailLock = Panic;
}

// Equity curve (phantom) trading
void equityCurve()
{
#ifdef PHANTOMTIME
	vars Equities = series(LowPass(series(ProfitClosed + ProfitOpen), 8));
	var EquitySmooth = LowPass(Equities, PHANTOMTIME);
	if (falling(Equities) && Equities[0] < EquitySmooth && LossStreakLong + LossStreakShort > 2) {
		setf(TradeMode, TR_PHANTOM);
	}
	else {
		resf(TradeMode, TR_PHANTOM);
	}
#endif
	return;
}

/////////////////////////////////////////////////////

DLLFUNC void run()
{
	set(LOGFILE, PLOTNOW);
	BarPeriod = BARPERIOD;	// 1 hour bars
	LookBack = LOOKBACKBARS;	// needed for Fisher()
	StartDate = STARTDATE;
	setf(PlotMode, PL_DIFF);

#ifdef WFOCYCLES	
	NumCores = 16;		// use multiple cores (Zorro S only)
	set(PARAMETERS, FACTORS);  // generate and use optimized parameters and factors
	NumWFOCycles = WFOCYCLES; // activate WFO
#endif

// User control by Capital and Panic (profit lock) sliders. 
	Capital = -slider(1, MAXCAPITAL / 4, 0, MAXCAPITAL, "Capital", "Invested capital");
	Panic = slider(2, 0, 0, 100, "Panic", "Profit lock %");
#ifdef ZERO_COST // for testing only
	Spread = Commission = Slippage = RollLong = RollShort = 0;
#endif

#ifdef RETRAINDAYS
	ReTrainDays = RETRAINDAYS;
	if (ReTrain) {
		UpdateDays = -1;	// update price data from the server 
		SelectWFO = -1;	// select the last cycle for re-optimization
		set(FACTORS | OFF);	// don't generate factors when re-training
	}
#endif

	// portfolio loop
	assetList(ASSETLIST);
	while (asset(loop(Assets)))
	{
		Prices = series(priceClose(0));
		Atr = ATR(ATRPERIOD);
		while (algo(loop("TRND", "CNTR"))) //"TRND",,"ALGO2","ALGO3","ALGO4")))
		{

			NumComponents = NumLoops1 * NumLoops2;
			algoSetup();
			equityCurve();

			
			if (0 == strcmp(Algo, "TRND"))
				tradeTRND();
			else if (0 == strcmp(Algo, "CNTR"))
				tradeCNTR();

				//case "AI": tradeAI(); break;
				//case "AI2": tradeAI2(); break;
// add more algos here
		
		}
	}
	if (Init) printf("\n%i components", NumComponents);
}



Posted By: danatrader

Re: enum case / if else - 02/25/20 21:54

I am again thankful for your help, just may you check if you see something obvious?
Posted By: danatrader

Re: enum case / if else - 02/27/20 00:07

Actually the mistake is simple, difference Lite-C to C/C++ --> Do not ommit parameters, although there is one that does not get shown, cause by definition it would be correct.

Tiny but tricky laugh
© 2024 lite-C Forums