Simple indicator problem

Hi everyone, I'm new to AFL... I have a simple problem, I'm trying to create a simple code ( buy after a bullish candle is created ) and sell when candle closes above the high + atr or below low - atr...

Buy = Ref(C,-1) > Ref(O,-1);
outlevelup = ValueWhen(Buy,High+ATR(14),1);
outleveldown = ValueWhen(buy,Low-ATR(14),1);
Sell = Open > outlevelup OR Open < outleveldown ;

PlotShapes(IIf(Buy,shapeUpArrow,Null),colorWhite,0,O,-10);
PlotShapes(IIf(Sell,shapeDownArrow,Null),colorYellow,0,O,-10);
PlotShapes(IIf(outlevelup,shapeSmallCircle,Null),colorGreen,0,outlevelup);
PlotShapes(IIf(outleveldown,shapeSmallCircle,Null),colorRed,0,outleveldown);

the results actually make sense, what I can't figure out how to do tho, is that I don't want another buy signal ( and I don't want amibroker to calculate new "outlevelup" or "outleveldown" until the previous buy has been closed...

thank you ! :slight_smile:
asl

@Francisco,

See ExRem.

You need to get "Verified Badge" before asking anymore questions.

Also see,

1 Like

I can't make the exrem work... as it will only eliminate the signals but the up and down levels will still be calculated, and so the sell signal won't happen when it should...could you please give me a simple fix example? I spent the whole day trying.. so trust me is not a matter of lazines...

Once you are verified then you will receive further assistance.

sorry didn't know I had to verify my account... I think its done, can you help me now? :3

Yep! All good. :+1:

Are you sure you used ExRem correctly?

If you are still stuck then post your complete code attempt using ExRem for modifaction.

Buy =  Ref(C,-1) > Ref(O,-1);


outlevelup = ValueWhen(Buy,High+ATR(14),1);
outleveldown = ValueWhen(buy,Low-ATR(14),1);


Sell = Open > outlevelup OR Open < outleveldown ;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);


PlotShapes(IIf(Buy,shapeUpArrow,Null),colorWhite,0,O,-10);
PlotShapes(IIf(Sell,shapeDownArrow,Null),colorYellow,0,O,-10);
PlotShapes(IIf(outlevelup,shapeSmallCircle,Null),colorGreen,0,outlevelup);
PlotShapes(IIf(outleveldown,shapeSmallCircle,Null),colorRed,0,outleveldown);

remember, the rules are simple:
Bulish candle = Buy
if the market opens above or below the outlevelup or outleveldown = sell;

the problem is that the outlevels are re-calculated after every bulish candle ( which is the signal ) sss

Has been discussed multiple times in his forum already.
For proper stops working on every case you either have use ApplyStop or looping code.

So as for your request

/// @link https://www.amibroker.com/guide/afl/applystop.html
/// @link http://www.amibroker.com/kb/index.php?s=applystop
/// @link https://forum.amibroker.com/t/simple-indicator-problem/19005/8
myATR = 3*ATR(14);

SetOption("ActivateStopsImmediately", True);

BuyPrice = Open;

Buy = Ref(C>O,-1);
Sell = 0;

ApplyStop( stopTypeLoss, stopModePoint, myATR, exitatstop = 1, 0 );
ApplyStop( stopTypeProfit, stopModePoint, myATR, exitatstop, 0 );

if ( Status("action") == actionIndicator ) {
	eq = Equity(1, 0);
	
	intrade = Flip(Buy, Sell);
	
	outlevelup = ValueWhen(Buy, High+myATR);
	outleveldown = ValueWhen(Buy,Low-myATR);

	Plot( C, "Price", colorDefault, styleBar );	

	PlotShapes(Buy*shapeUpArrow,colorWhite,0, L);
	PlotShapes((Sell>0)*shapeDownArrow,colorYellow,0,H);
	Plot(IIf(intrade, outlevelup, Null), "outlevelup", colorGreen );
	Plot(IIf(intrade, outleveldown, Null), "outleveldown", colorRed );
}

15

2 Likes

wow thanks for the code... I kind of understand what you did... I'll have to analyze it line per line...
for what I understand the code its devided in two... the order sending... and the plot part ( inside the if statement ) altho I don't really understand what the eq = ( equity ) has to do in there... T_T, guess there is alot to learn... thank you for the help...

It's required when using ApplyStop if you want to plot your stops.

thank you :), I found a little mistake in that code the apply stop its calculated buyprice + or - myATR
and the upper and lower level are calculated high or low + - myATR... I'm understanding the logic of that code, altho I still don't understand the importance of that if statement... whats thats the reason of status("action") == actionIndicator ??? what is that for?, I read the help manual, but I don't get the idea behind it... even if I delete that statement the code still works...

It's so that section of the code only runs when you are on a chart.

thanks mate... amibroker is so damn powerful... I'm creating a simple temple using the code posted by fxshrat, so whenever I want to create a system where the stop loss and entry profit are done by applystop function I could just refere to this template... Im also ploting the equity curve which is actually really interesting to do...

{// SET TEST OPTIONS
SetOption("ActivateStopsImmediately", True);
SetPositionSize(1,spsShares);
}

{// SET ENTRY RULES
BuyPrice = Open; 
Buy = Ref(C>O,-1) AND Ref(C>O,-2) AND Ref(C>O,-3) AND Ref(C>O,-4) AND Ref(C>O,-5);
}


{// SET STOP LOSS AND TAKE PROFIT
Sell = 0;
myATR = Ref(ATR(14),-1)*2;
stoplevel = ValueWhen(Buy, BuyPrice - (Ref(Low,-1)-myATR),1) ; // calculate stop loss distance
profitlevel = ValueWhen(Buy, (Ref(High,-1)+myATR)-BuyPrice,1); // calculate take profit distance
ApplyStop( stopTypeLoss, stopModePoint, stoplevel, 1, 0 );
ApplyStop( stopTypeProfit, stopModePoint, profitlevel, 1, 0 );
}





if ( Status("action") == actionIndicator ) {// PLOT CHART, ENTRY SIGNALS AND EQUITY CURVE
ChartType = ParamList("Chart Type", "Price Chart|EquityLine"); // to switch from candle chart to equity curve in the "chart parameters window"
if (ChartType == "Price Chart") // Price Chart
{
	eq = Equity(1, 0);
	
	intrade = Flip(Buy, Sell);
	
	outlevelup = ValueWhen(Buy, BuyPrice+profitlevel); // calculate take profit price
	outleveldown = ValueWhen(Buy,BuyPrice-stoplevel);  // calculate stop loss price

	Plot( C, "Price", colorDefault, styleCandle );	

	PlotShapes(Buy*shapeUpArrow,colorWhite,0, BuyPrice);
	PlotShapes((Sell>0)*shapeDownArrow,colorYellow,0,H);
	Plot(IIf(intrade, outlevelup, Null), "outlevelup", colorGreen );
	Plot(IIf(intrade, outleveldown, Null), "outleveldown", colorRed ); 
}	

	
if (ChartType == "EquityLine") // equity curve
{
	eq = Equity(1, -1);
	Plot(eq,"eq",colorWhite,styleLine);	
}

}

image

@Francisco,
THAT is an understatement! :sunglasses:

1 Like