How can we get a variable out of a function?

For instance, if wanna set up the ml - sl > 0 from this function as buy condition. Has somebody any idea?

// Technical Analysis of Stocks and Commodites Jan 2018,
// The CAM Indicator For Trends And Countertrends by Barbara Star
// "CAM" which stands for Coordinated ADX and MACD indicator

CAMup = ADX( 10 ) >= Ref( ADX( 10 ), -1 ) AND MACD() > Ref( MACD(), -1 ); // upward market
CAMpb = ADX( 10 ) <= Ref( ADX( 10 ), -1 ) AND MACD() < Ref( MACD(), -1 ); // pullback
CAMdn = ADX( 10 ) >= Ref( ADX( 10 ), -1 ) AND MACD() < Ref( MACD(), -1 ); // down market
CAMct = ADX( 10 ) <= Ref( ADX( 10 ), -1 ) AND MACD() > Ref( MACD(), -1 ); // counter-trend

barCOLOR = IIf( CAMup, colorGreen, IIf( CAMpb, colorGold, IIf( CAMdn, colorRed, colorBlue ) ) );

ChartType = ParamList( "Chart Type", "Price Chart|ADX|MACD" ); //

if( ChartType == "Price Chart" ) // Price Chart
{
    SetChartOptions( 0, chartShowArrows | chartShowDates );
    _N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%)", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );

    PlotOHLC( Open, High, Low, Close, "", barCOLOR, styleBar | ParamStyle( "Style" ) | GetPriceStyle() , Null, Null, 0, 0, 3 ); // thick bars
    emaPeriods = Param( "emaPeriods", 15, 2, 200, 1 );
    Plot( EMA( C, emaPeriods ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle( "Style" ) );
}


if( ChartType == "ADX" ) // ADX
{
    range = Param( "ADXPeriods", 10, 2, 200, 1 );
    Plot( ADX( range ), "ADX(" + range + ")", ParamColor( "ADX color", colorAqua ), ParamStyle( "ADX style", styleThick ) );
}

if( ChartType == "MACD" ) // MACD
{
    r1 = Param( "Fast avg", 12, 2, 200, 1 );
    r2 = Param( "Slow avg", 26, 2, 200, 1 );
    r3 = Param( "Signal avg", 9, 2, 200, 1 );
    Plot( ml = MACD( r1, r2 ), StrFormat( _SECTION_NAME() + "(%g,%g)", r1, r2 ), ParamColor( "MACD color", colorRed ), ParamStyle( "MACD style" ) );
    Plot( sl = Signal( r1, r2, r3 ), "Signal" + _PARAM_VALUES(), ParamColor( "Signal color", colorBlue ), ParamStyle( "Signal style" ) );
    Plot( ml - sl, "MACD Histogram", IIf( ml - sl > 0, colorGreen, colorRed ), styleHistogram | styleThick, Null, Null, 0, 0, 5 ); // ExtraThick

}

Buy = ml - sl > 0;
Sell = ......


Hi

You can take the hole MACD() afl code out of the If ParamList()

or just like that

if( ChartType == "MACD" ) // MACD
{
    r1 = Param( "Fast avg", 12, 2, 200, 1 );
    r2 = Param( "Slow avg", 26, 2, 200, 1 );
    r3 = Param( "Signal avg", 9, 2, 200, 1 );

    ml = MACD( r1, r2 );
    sl = Signal( r1, r2, r3 );
    cond1= IIf( ml - sl > 0, True, false ); //  or cont1= IIf( ml - sl > 0, Buy, Sell );
    
    Buy =  cond1 ; 		// we can say Buy and some other condition buy = cont1 and cont2; 
    // Buy= Cross(Buy,0);  // or we can say buy only on the first bar signal

    BuyPrice= Open;    
    // Buy = Ref(Buy,-1) ;	// or we can say buy on next bar OPEN

    
    Plot( ml, StrFormat( _SECTION_NAME() + "(%g,%g)", r1, r2 ), ParamColor( "MACD color", colorRed ), ParamStyle( "MACD style" ) );
    Plot( sl, "Signal" + _PARAM_VALUES(), ParamColor( "Signal color", colorBlue ), ParamStyle( "Signal style" ) );
    Plot( ml - sl, "MACD Histogram", IIf( ml - sl > 0, colorGreen, colorRed ), styleHistogram | styleThick, Null, Null, 0, 0, 5 ); // ExtraThick

	PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), 5,0,ml, -20); 

}
2 Likes

Very interesting, thank you. As I embed your code with the Buy = cond1 ; in the function, the backtester displays the "Error 701. Missing buy/sell variable assignments.". For the case, that we work with different functions and combine the signals, it could be useful to get them out of the function. How would it look like in this case, if we would get it out?

Hi
when you asking " out of the function " i was thinking you try to tell us the ParamList() function
and i already answer

if( ChartType == "MACD" ) // MACD    // << delete this line 
{   // << delete this line 
here is your macd code

}   // << delete this line 

Now about the "Error 701. Missing buy/sell variable assignments."
it is because you use parameter in your code. Before you run exploration or the backtest ALWAYS you have to open and set the correct parameters in the AAnalysis window
for the this particular code look the picture below

i hope i cover your question

image

1 Like

Thank you very much, it works now :wink:

1 Like

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