Difficulty to code sigScalein...please help

i am trying to write the code when price cross previous day high go long and scale in when in subsequent day price also fulfill the same criteria of crossing the previous day high. When price cross previous day low exit all. Chart display showing all the signal but backtest only considering first entry, subsequent is not ignored in back test. cant find the mistake i have made. please help. thank you.

_SECTION_BEGIN("Breakout_Day H/L & Pivot");

psz =Param("PositionSize",75,1,500000);
pick = Param("TICK",1,1,100,.05);
tick = .05*pick;
SetPositionSize( psz, spsShares );

isEndofDay = Nz(TimeFrameExpand(1,inDaily,expandPoint));
newday = Day() != Ref(Day(),-1);

PDC= TimeFrameGetPrice("C",inDaily,-1);
PDH = TimeFrameGetPrice("H",inDaily,-1);
PDL = TimeFrameGetPrice("L",inDaily,-1);

R1= 2*PIVOT - PDL;
S1 = 2*PIVOT - PDH;
R3 = PDH + (2*(PIVOT-PDL));
S3 = PDL - (2*(PDH-PIVOT));

BUYLEVEL= (Max(PDH,R1)+tick);
SELLLEVEL = (Min(PDL,S1)-tick);

Buy1 = Cross(H,BUYLEVEL);  
Sell1 = Cross(SELLLEVEL,L);
buy2 = newday AND buy1;

InitialBuy = buy1 ;
InitialBuy = ExRem(InitialBuy,isEndofDay);

SubsequentBuy = buy2;
SubsequentBuy = ExRem(SubsequentBuy,isEndofDay);

Buy = IIf(InitialBuy,1,
         IIf(SubsequentBuy, sigScaleIn,0));
Sell = sell1;

Sell = ExRem(Sell,Buy);

BuyPrice = buylevel;
SellPrice = SELLLEVEL;

Plot(R1,"R1",ParamColor("R1", colorAqua));

Plot(PDH,"Previous Day High",ParamColor("pDayHigh", colorGreen));
Plot(PDL,"Previous Day Low",ParamColor("pDayLow", colorRed));




I'm not sure exactly what your goal is, but you may have multiple problems here. Your existing code will allow you to have one InitialBuy each day, and possibly one SubsequentBuy each day. A SubsequentBuy may only occur on the first trading bar of the day due to this logic:

buy2 = newday AND buy1;

More importantly, EVERY SubsequentBuy will also be an InitialBuy (probably not what you intended). So perhaps you want something more like this:

InitialBuy = ExRem(Buy1,Sell1);
SubsequentBuy = ExRem(buy2,Sell1);
Buy = IIf(InitialBuy,1,
        IIf(SubsequentBuy, sigScaleIn,0));

basically i am getting confused here. what would be my subsequent buy condition or level ,as it is nothing but the same condition cross(h,ref(h,-1) but on the next day.

sir i have tried this code but does not works. basically that subsequent buy is wrong i think. so what to do?


when i am selecting below code
then it plots the buy shape accurately but during backtest only initial entry is taken.

InitialBuy = buy1 ;
InitialBuy = ExRem(InitialBuy,isEndofDay);
SubsequentBuy = buy1;
SubsequentBuy = ExRem(SubsequentBuy,isEndofDay);
//InitialBuy = ExRem(Buy1,Sell1);
//SubsequentBuy = ExRem(buy1,Sell1);

Buy = IIf(InitialBuy,1,
         IIf(SubsequentBuy, sigScaleIn,0));

The code that I gave you performs scale-ins as you requested. If you are not seeing any scale-ins, then there is probably something else wrong. As you hopefully already know, scale-ins do not appear as a separate row in the trade list produced by the back test, but rather are indicated by the Scale In/Out column in the trade list.

As I mentioned in my initial post, your logic dictates that scale-ins can only occur on the first bar of the day, which is likely not what you wanted. Rather than trying to debug your code using a chart, I suggest using an Exploration to help you better understand your variables. You really only need two things:

Filter = True;   // Or other logic that determines which bars to output in the Exploration
AddColumn(<your variable>);
AddColumn(<your variable>);

Let us know what you discover.

Sir, sorry for the late response. Yes with your code exploration showing the subsequent entry and as you mentioned its always in the first bar. But sir back test still the same only initial entry.

Try using some of the other debugging techniques described in the Help files or elsewhere in this forum: How do I debug my formula?. Specifically, you might want to turn on the Detailed Log to see if there's a reason AB is not taking your scale-in signals, for example because you have insufficient capital.

no sir i have add 10 lot fund .

sir how to avoid those 1st bar entry?

I'm not saying that is definitely your problem, I'm saying that's one possibility. To be successful with AmiBroker, you need to learn how to debug your own issues, and the Detailed Log is a good next step for you.

I pointed this out to you in an earlier post. Find that logic in your code and remove it if you don't want SubsequentBuy to be restricted to the first bar. If you don't know where/why it's happening, then expand your Exploration to show more of your variables so you can see which one(s) are causing you problems.

sir thank you , the first bar problem is solved.

i just changed

newday = Day() != Ref(Day(),-1);

to this..

newday = day() > ref( day(), -1);

but scale in is still not working... trying to debug using exploration as you mentioned. thank you again.

sir can give me a hint how to define that subsequent entry signal. as the entry condition is same , todays price cross previous day high only difference with initial entry is that newday.

sorry sir i was worng 1st bar entry was not solved. althoug i have solved the scale in problem...

now scale in is working only when the signal comes in first bar. sir then how tofine the next day?

Sir everything solved. Thanks a lot. I am just overburdening my code with that "newday". It was unnecessary.
Thanks a lot sir.

isEndofDay = Nz(TimeFrameExpand(1,inDaily,expandPoint));
InitialBuy = buy1  ;
InitialBuy = ExRem(Buy1,Sell1);
SubsequentBuy = buy1 ;
SubsequentBuy = ExRem(SubsequentBuy,isEndofDay);

Glad that you were able to figure it out. Your code will allow a scale-in on each day that meets the buy1 conditions, i.e. you can have multiple scale-ins on a single trade. Is that what you want?

1 Like

exactly sir. scale in with 1lot everyday if previous day high cross.