Simple pullback code

Hi all,
can somebody to write the code for this simple pullback, using loops (I understand, it easily even with no loop). Thanks a lot for helping me to study my AFL using your brain :slight_smile: ...

  1. SP500 Stock must make 20 trading days high ( I will be optimize late or change trend filter)
  2. Next 4 days are down days (4 Lower Lows days, I will be optimize late)
  3. 5th day must close above close made on 4th day
  4. STOP loss protection will be 5th day low minus 0.05 ( I will be optimize late)
  5. Buy trigger on 6th day will be 5th high + 0.05 ( I will be optimize late)
  6. Risk level is difference between entry and stop loss
  7. Profit will be entry + 3*risk level ( I will be optimize late, or change profit factor)

Forgotten my coding ...

LongLL = L < Ref(L,-1) AND Ref(L,-1) < Ref(L,-2) AND Ref(L,-2) < Ref(L,-3) AND Ref(L,-3) < Ref(L,-4);

Liquid = Ref(LongLL, -1)
AND Ref(C, -1) > Ref(H, -2)
AND C > Ref(H, -1)
AND Ref(IndFiltUp,-6)
AND NorgateIndexConstituentTimeSeries("$SPX")
AND NOT OnLastTwoBarsOfDelistedSecurity;

Buy = Liquid;

BuyPrice = Buy;
SellPrice1 = BuyPrice - (BuyPrice - Low) - 0.05;
SellPrice2 = BuyPrice + 3*((BuyPrice - Low) - 0.05);

Sell = OnSecondLastBarOfDelistedSecurity
OR C >= SellPrice2
OR C <= SellPrice1;

Q: why it always sell on SellPrice1 and not use SellPrice2?

You have all sorts of mistakes. For example, Buy is a Boolean value (0 or 1). Therefore, setting

BuyPrice = Buy;

Will also set your price to 0 or 1. You also have not set your SellPrice, so you're just getting whatever is specified in your Analysis Settings window.

I suggest you add some exploration code so that you can see the actual values of your variables.

1 Like

First of all please read How to use this site. Mandatory rule for inserting code is using code tags as described in that link.

Secondly below one is all wrong.

BuyPrice = Buy;
SellPrice1 = BuyPrice - (BuyPrice - Low) - 0.05;
SellPrice2 = BuyPrice + 3*((BuyPrice - Low) - 0.05);

Sell = OnSecondLastBarOfDelistedSecurity
OR C >= SellPrice2
OR C <= SellPrice1;
  1. BuyPrice is not getting any actual price assigned. Buy is true/false array.
  2. To apply profit and loss stops you have to either use ApplyStop function or looping. This has been discussed in this forum several times already. Also AmiBroker KB has several examples as well looping example here.
  3. Additional recommended reading here.

BTW this one

LongLL = L < Ref(L,-1) AND Ref(L,-1) < Ref(L,-2) AND Ref(L,-2) < Ref(L,-3) AND Ref(L,-3) < Ref(L,-4);

can be shortened to

LongLL = Sum(L < Ref(L,-1), 4) == 4;
1 Like

Thanks a lot ...
I tried to us your brain, you guys try to force my brain to work ... . it did not working long time ago ...

Hi,
Can anybody tells me what is wrong in this pseudocode ?
Pseudo .. it just for future people, who likes maandaatooryy ...

BuyLim = Close > Ref(High, -1)
BuyPrice = BuyLim   

Liquid =	LongLL            			
			AND NorgateIndexConstituentTimeSeries("$SPX")				// Ticker must be member of S&P500 on the trade date
			AND NOT OnLastTwoBarsOfDelistedSecurity	// Do not enter if on last two bars of an inactive security

Buy = 	Liquid 											
		AND C >= Ref(H, -1)

SellPrice1 = BuyPrice - (BuyPrice - Low) ;
SellPrice2 = BuyPrice + 3*(BuyPrice - Low);
 
Sell = 	0 OR OnSecondLastBarOfDelistedSecurity;

ApplyStop(stopTypeLoss,stopModePoint,Ref(SellPrice1,-1),1,False,0);
ApplyStop(stopTypeProfit,stopModePoint,Ref(SellPrice2,-1),1,False,0); 
	
Buy= ExRem( Buy, Sell); 
Sell= ExRem( Sell, Buy); 

Thanks people ...

Moderator comment: next time really please USE CODE TAGS! I added them to this post

hi,
anybody could explain, why different annual % (few cents) for with/no loop above model? numbers are not stochastics ... . No code, please ...
Maury