= is assignment, == is equality check, was: ParamToggle turns off whole trading system

Hello,
In the code below see "marketup = MA(allords,15) > Ref(MA(allords,15),-10);"
I have added a paramtoggle in bold further down (under entrysetup) to turn the marketup code on and off. However, when it is enabled all the entry conditions are affected...i.e. toggled. I guess that is because of the AND statements between the entry conditions. Is this right? Is there a way to toggle one condition without affecting the others?
Thanks in advance...note: not all the code of the system is included...hope its enought o answer my question...cheers

Allords 				        = Foreign( "$xao", "C" );
Marketup 				= MA(allords,15) > Ref(MA(allords,15),-10); 
						;
// Entry Filtering
ShortTermMATickUp		= ShortTermMA > Ref(ShortTermMA, -1);
GreenCandleAboveSMA		= GreenCandle AND C > ShortTermMA;
GreenCandleAllAboveSMA	= GreenCandle AND C > ShortTermMA AND O > ShortTermMA;

EntryFilterMet			= 	IIf(EntryFilter == 1, ShortTermMATickUp,
							IIf(EntryFilter == 2, GreenCandleAboveSMA,
							IIf(EntryFilter == 3, GreenCandleAllAboveSMA,
							IIf(EntryFilter == 4, GreenCandle,
							True))));

// Entry  Criteria
EntrySetup 				=	AboveMinTurnover
						AND AboveAvgVolume
						AND AboveMinPrice
						AND LongTermUpTrend
						AND PriceAboveLongTerm
						AND c > Ref(MA(c,200), -1 * 100)
					AND marketup= paramtoggle("marketup", "enable|disable",0)		
						;

					
EntryCondition			= 	EntrySetup 
						AND RedCandle
						AND CandleNotTooBig
						AND BelowShortTermMA
						AND RSIBelowEntryLevel
						
						;

EntryPeriod				= Flip(EntryCondition, ExRem(ExitTrigger, EntryCondition));

Hi Steve,

ParamToggle is used to record user's choice externally.

Example 1:

Ch = ParamToggle( "Choose MA Period", "20|50", 0 );
PerMa = IIf( Ch, 50, 20 );
/*
//You could alternately use if..else since 'Ch' is not an Array
if( Ch ) PerMa = 50;
else PerMa = 20;
*/
Plot( MA( C, PerMA ), "Simple MA", colorWhite );

1

Example 2:

Ch = ParamToggle( "Choose EMA Type", "MA|EMA", 1 );
PerMA = Param( "MA Period", 20, 1, 100, 1 );

switch( Ch ) {
	 case 0:
         myMA = MA( C, PerMA );
     break;

	 case 1:
         myMA = EMA( C, PerMA );
	 break;
}

Plot( myMA, "myMA", colorWhite );
Title = WriteIf( Ch, "EMA", "MA" ) + "(C, " + PerMA + ") " + NumToStr( myMA, 1.2 );

2

Example 3:

Ch = ParamToggle( "Plot MA?", "No|Yes", 1 );
PerMA = Param( "MA Period", 20, 1, 100, 1 );

myMA = MA( C, PerMA );
styleDrawMA = IIf( Ch, styleThick, styleNoDraw );

Plot( C, "Price", colorDefault, styleBar | styleThick );
Plot( myMA, "myMA", colorDefault, styleDrawMA );

Title =
StrFormat( "{{DATE}}\nO %1.2f H %1.2f L %1.2f C %1.2f", O, H, L, C ) +
WriteIf( Ch, "\nMA( C, " + PerMA + ") " + NumToStr( myMA, 1.2 ), "" );

3

And if you happen to Toggle the conditions before running your Analysis, you can use the Parameters tab from the Analysis Window.

4

In your case since you want to Toggle Conditions dynamically (i.e. internally), then use the State of StaticVars. Something like this:

State = Nz( StaticVarGet( "VariableName" + Name() ), 0 ); //Name() is used to make the varname Unique. Sometimes Name() + Interval() OR simply a GetChartID()

//Condition that makes the State as True
Condition = /*Your Condition*/;

//IF (or IIF) Condition is Met or Holds True
//setting StaticVar as True
if( Condition ) {
	 //StaticVarSet has a Persist argument too, use wisely as per your requirements
	 StaticVarSet( "VariableName" + Name(), 1 ); //State is Set to True
}

if( State ) { //If State is True
	 /*
		 Do Something
	 */
	 
	 //State is reset to False such that until
	 //the "Condition" is Met again, it won't "Do Something".
	 //In other words, "if( State )" won't get executed until the "Condition" holds True again
	 StaticVarSet( "VariableName" + Name(), 0 );
}

A simple example of Toggling the State of a StaticVar that comes to mind is in this KB. Do play with the MoveinProgress. :slight_smile:

If you grasp the power of StaticVars in AFL, you will be able to create Wonders.

@stevecar144

Your code has one problem (quite basic and mentioned in Common mistakes).

In your code you wrote:

AND marketup= paramtoggle("marketup", "enable|disable",0)	

You have used assignment operator (=) instead of comparison - equality check (==)

What = (assignment) does is to assign the value, so:

marketup= paramtoggle("marketup", "enable|disable",0)	

assigns new value to marketup variable. If you toggle off the parameter, it will assign FALSE (0) to marketup variable and then this FALSE will become:

..your code..
AND FALSE;

Which means that entire condition is NEVER met.

You should be using equality check (==) instead

AND marketup == paramtoggle("marketup", "enable|disable",0);

Read about all common mistakes: http://www.amibroker.com/guide/a_mistakes.html

1 Like

As usual the forum is enormously helpful for a newbie like me....thank you all. Amibroker is awesome!!