AFL help needed to plot lines

mechtrader

Well-Known Member
hi MT,

I agree that you want to limit the number of trades. But the problem is how to decide which trades to take and which to ignore. The code I posted also trades pre-market and after-hours when the volume is very low so one thing that can be done is to remove these trades. Then still, if you want to trade automatically, you need some method to filter out the trades that have little potential to become a winner trade.

If you have suggestions I could build them in and test it.
Hi Edward,
One thing that I think will filter out most false trades (also some good ones) is that the PH on which we enter should be greater than the prev pivot high that was made.

PFA image.



Thanks,
MT
 

mastermind007

Well-Known Member
hi MT,

I agree that you want to limit the number of trades. But the problem is how to decide which trades to take and which to ignore. The code I posted also trades pre-market and after-hours when the volume is very low so one thing that can be done is to remove these trades. Then still, if you want to trade automatically, you need some method to filter out the trades that have little potential to become a winner trade.

If you have suggestions I could build them in and test it.
I have implemented a filter, that is called "Count to 10", in C++, and its origins are from the "Anger Management" books

After AB identify the signal and invokes a C++ layer, it count to 10 (Secs, ticks) and after that signal is still valid, a market order will be placed at the exchange.

Once the market order is placed, again count to 10 (Secs, ticks) and if the order is not filled, a cancelling order is sent to the exchange.

Got no idea how that can be done in AmiBroker and as far as signalling in AB is concerned, a trade was taken.
 
Hi Edward,
One thing that I think will filter out most false trades (also some good ones) is that the PH on which we enter should be greater than the prev pivot high that was made.

PFA image.



Thanks,
MT
hi MT,

yes that can be easily implemented. Basically you are saying you only want to take a long trade after a breakout of a HH (Higher High) and a short trade after a breakout of a LL (lower Low). Will add a switch to the code that does this.
 
I have implemented a filter, that is called "Count to 10", in C++, and its origins are from the "Anger Management" books

After AB identify the signal and invokes a C++ layer, it count to 10 (Secs, ticks) and after that signal is still valid, a market order will be placed at the exchange.

Once the market order is placed, again count to 10 (Secs, ticks) and if the order is not filled, a cancelling order is sent to the exchange.

Got no idea how that can be done in AmiBroker and as far as signalling in AB is concerned, a trade was taken.
hi MM, yes that is an idea but it is hard to test in a backtest using 1-minute data for instance. You will need tick data to check if this works in a backtest. I implemented something similar in an automated system I ran. I have sent the order as a limit order and wait for 10 or 20 seconds and if I didn't get it I would send the order again using a limit order and again wait 10sec etc. I used the modulus operator fro this in Amibroker
 

rmike

Well-Known Member
mastermind007 said:
Once the market order is placed, again count to 10 (Secs, ticks) and if the order is not filled, a cancelling order is sent to the exchange
A market order would always be filled unless a circuit breaker has been hit or trading has ceased for the day.

Slippage is a fact of trading process. However two methods can reduce it to a reasonable extent.

1. Trading highly liquid instruments (implement a volume, as well as a bid-ask spread filter for selection).

2. Make cyclic volatility work for you. Enter a trend when volatility is near minimum. For e.g after range expansion at trend commencement, market generally tends to enter a short period of low volatility (as well as volume). Entry during this short pause on a lower timeframe is beneficial for slippage reduction.

P.S - In runaway markets you'll have to take your chances!!! Slippage be damned :)
 
Hi Edward,
One thing that I think will filter out most false trades (also some good ones) is that the PH on which we enter should be greater than the prev pivot high that was made.

PFA image.

Thanks,
MT
here is an example where I use the pivot lines defined by hh and ll

PHP:
// code by E.M.Pottasch, Jan 2015
SetBarsRequired(sbrAll,sbrAll);
xx=BarIndex();Lx=LastValue(xx); 
rightStrength=Param("Right Strength (Major Pivot)",5,1,50,1);
leftStrength=Param("Left Strength (Major Pivot)",5,1,50,1);
rightStrength_mini=Param("Right Strength (Mini Pivot)",3,1,50,1);
leftStrength_mini=Param("Left Strength (Mini Pivot)",3,1,50,1);
tf=Param("Time Frame (Minutes)",5,1,1440,1);tfrm=in1Minute*tf;
slippageFactor=Param("Slippage Factor",0,0,10,1);
useMiniStop=ParamToggle("Exit Type","Major Pivot Stop and Reverse|Mini Pivot Stop",1);
fvb=0;lvb=BarCount;

SetTradeDelays(0,0,0,0);
SetOption("CommissionMode",3);
SetOption("CommissionAmount",2.01);
SetOption("FuturesMode",True);
NumContracts=1;//StaticVarGet("NumContractsSV");
PositionSize=NumContracts*MarginDeposit;
SetOption("MaxOpenPositions",4);

slip=TickSize*slippageFactor;

function handleSignals(Buy,BuyPrice,Sell,SellPrice,Short,ShortPrice,Cover,CoverPrice)
{
global BuyAdjusted;
global BuyPriceAdjusted;
global ShortAdjusted;
global ShortPriceAdjusted;
global SellAdjusted;
global SellPriceAdjusted;
global CoverAdjusted;
global CoverPriceAdjusted;
global longTarget;
global shortTarget;

BuyAdjusted=0;
BuyPriceAdjusted=0;
ShortAdjusted=0;
ShortPriceAdjusted=0;
SellAdjusted=0;
SellPriceAdjusted=0;
CoverAdjusted=0;
CoverPriceAdjusted=0;

for(i=fvb;i<lvb;i++) 
{
	if(Buy[i])
	{
		BuyAdjusted[i]=1;
		BuypriceAdjusted[i]=Min(BuyPrice[i]+slip,H[i]);
		
		for(j=i+1;j<BarCount;j++) 
		{					
			if(Sell[j])
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=Max(SellPrice[j]-slip,L[j]);
				i=j-1;
				break;				
			}
			else if(Short[j])
			{
				SellAdjusted[j]=1;
				SellPriceAdjusted[j]=Max(ShortPrice[j]-slip,L[j]);
				i=j-1;
				break;				
			}
			else if(j==BarCount-1)
			{
				i=j;
				break;
			}						
		}
	}
	else if(Short[i])
	{
		ShortAdjusted[i]=1;
		ShortPriceAdjusted[i]=Max(ShortPrice[i]-slip,L[i]);
		
		for(j=i+1;j<BarCount;j++) 
		{					
			if(Cover[j])
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=Min(CoverPrice[j]+slip,H[j]);
				i=j-1;
				break;				
			}
			else if(Buy[j])
			{
				CoverAdjusted[j]=1;
				CoverPriceAdjusted[j]=Min(BuyPrice[j]+slip,H[j]);
				i=j-1;
				break;				
			}
			else if(j==BarCount-1)
			{
				i=j;
				break;
			}										
		}
	}

}
}
function pkID(rightStrength,leftStrength)
{
	pk=H>Ref(HHV(H,leftStrength),-1) AND H>=Ref(HHV(H,rightStrength),rightStrength);
	return pk;
}
function trID(rightStrength,leftStrength)
{
	tr=L<Ref(LLV(L,leftStrength),-1) AND L<=Ref(LLV(L,rightStrength),rightStrength);
	return tr;
}

TimeFrameSet(tfrm); 
	pk=pkID(rightStrength,leftStrength);
	tr=trID(rightStrength,leftStrength);
	pkh=IIf(pk,H,Null);
	trl=IIf(tr,L,Null);
TimeFrameRestore();
fact=Nz(Max(tfrm/60,Interval()/60)/(Interval()/60));
if(fact==0)fact=1;
Lkbk=Nz(tfrm/Interval());
if(Lkbk>1)
{
	pk=TimeFrameExpand(pk,tfrm,expandFirst);
	pkh=TimeFrameExpand(pkh,tfrm,expandFirst);
	pkhs=IIf(!IsEmpty(pkh),1,0);pkhs=pkhs-Ref(pkhs,-1);
	pk=pk AND H==pkh;
	cond1=Sum(pk,BarsSince(pkhs==1)+1)==1 AND pk;
	pk=pk AND cond1;
	
	tr=TimeFrameExpand(tr,tfrm,expandFirst);	
	trl=TimeFrameExpand(trl,tfrm,expandFirst);
	trls=IIf(!IsEmpty(trl),1,0);trls=trls-Ref(trls,-1);
	tr=tr AND L==trl;
	cond1=Sum(tr,BarsSince(trls==1)+1)==1 AND tr;
	tr=tr AND cond1;
}

px0=ValueWhen(pk,xx,0); tx0=ValueWhen(tr,xx,0);
px1=ValueWhen(pk,xx,1); tx1=ValueWhen(tr,xx,1);
px2=ValueWhen(pk,xx,2); tx2=ValueWhen(tr,xx,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
 
ll=tr AND tl1<tl2;
hl=tr AND tl1>tl2;
hh=pk AND ph1>ph2;
lh=pk AND ph1<ph2;
dt=pk AND ph1==ph2;
db=tr AND tl1==tl2;

pkm=pkID(rightStrength_mini,leftStrength_mini);
trm=trID(rightStrength_mini,leftStrength_mini);

pkHigh1=Ref(ValueWhen(hh,H,1),-(rightStrength*fact+fact));
trLow1=Ref(ValueWhen(ll,L,1),-(rightStrength*fact+fact));
pkmHigh1=Ref(ValueWhen(pkm,H,1),-(rightStrength_mini+1));
trmLow1=Ref(ValueWhen(trm,L,1),-(rightStrength_mini+1));
		
SetChartBkColor(ColorRGB(0,0,0));SetChartOptions(0,chartShowDates);
SetBarFillColor(IIf(C>O,colorGreen,IIf(C<=O,colorRed,colorLightGrey)));
Plot(C,"Price",IIf(C>O,colorDarkGreen,IIf(C<=O,colorDarkRed,colorLightGrey)),64,null,null,0,0,1);
PlotShapes(shapeCircle*tr,IIf(Lx-ValueWhen(tr,xx)>rightStrength*fact+fact,ColorRGB(0,255,0),colorWhite),0,L,-10);
PlotShapes(shapeCircle*pk,IIf(Lx-ValueWhen(pk,xx)>rightStrength*fact+fact,ColorRGB(255,0,0),colorWhite),0,H,10);
PlotShapes(shapeSmallCircle*trm,IIf(Lx-ValueWhen(trm,xx)>rightStrength_mini,ColorRGB(0,70,0),colorWhite),0,L,-10);
PlotShapes(shapeSmallCircle*pkm,IIf(Lx-ValueWhen(pkm,xx)>rightStrength_mini,ColorRGB(70,0,0),colorWhite),0,H,10);

Plot(pkHigh1,"",colorBlue,1,Null,Null,0,0,1);
Plot(trLow1,"",colorRed,1,Null,Null,0,0,1);

Buy=(Cross(H,pkHigh1) OR (Ref(C,-1)<pkHigh1 AND H>pkHigh1)) AND pkHigh1>trmLow1;BuyPrice=Max(O,pkHigh1);
Short=(Cross(trLow1,L) OR (Ref(C,-1)>trLow1 AND L<trLow1)) AND trLow1<pkmHigh1;ShortPrice=Min(O,trLow1);

if(useMiniStop)
{
	Sell=Cross(trmLow1,L);SellPrice=Min(O,trmLow1);
	Cover=Cross(H,pkmHigh1);CoverPrice=Max(O,pkmHigh1);
	Plot(pkmHigh1,"",colorBlue,styledashed,Null,Null,0,0,1);
	Plot(trmLow1,"",colorRed,styledashed,Null,Null,0,0,1);
}
else
{
	Sell=Cross(trLow1,L);SellPrice=Min(O,trLow1);
	Cover=Cross(H,pkHigh1);CoverPrice=Max(O,pkHigh1);
}

handleSignals(Buy,BuyPrice,Sell,SellPrice,Short,ShortPrice,Cover,CoverPrice);
Buy=BuyAdjusted;
BuyPrice=BuyPriceAdjusted;
Short=ShortAdjusted;
ShortPrice=ShortPriceAdjusted;
Sell=SellAdjusted;
SellPrice=SellPriceAdjusted;
Cover=CoverAdjusted;
CoverPrice=CoverPriceAdjusted;

PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorDarkGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Sell,shapeSmallCircle,shapeNone),colorWhite,0,SellPrice,0);
PlotShapes(IIf(Short,shapeSmallDownTriangle,shapeNone),colorRed,0,H,IIf(Short AND Sell,-30,-15));
PlotShapes(IIf(Short,shapeSmallCircle,shapeNone),colorWhite,0,ShortPrice,0);
PlotShapes(IIf(Cover,shapeSmallUpTriangle,shapeNone),colorDarkGreen,0,L,IIf(Cover AND Buy,-30,-15));
Plotshapes(IIf(Cover,shapeSmallCircle,shapeNone),colorWhite,0,CoverPrice,0);

fvb=Status("firstvisiblebarindex");
lvb=Min(Lx,Status("lastvisiblebarindex"));

for(i=fvb;i<lvb;i++) 
{
	if(ll[i]) PlotTextSetFont("LL","Arial Black",8,i,L[i],colorGreen,colorDefault,-30); 
	if(hh[i]) PlotTextSetFont("HH","Arial Black",8,i,H[i],colorRed,colorDefault,20); 
}
 

casoni

Well-Known Member
hi MM, yes that is an idea but it is hard to test in a backtest using 1-minute data for instance. You will need tick data to check if this works in a backtest. I implemented something similar in an automated system I ran. I have sent the order as a limit order and wait for 10 or 20 seconds and if I didn't get it I would send the order again using a limit order and again wait 10sec etc. I used the modulus operator fro this in Amibroker
Hello Edward sir and everyone,
for 1 min database , How about using Time Left !!
There is code [ Probably coded by Herman ], which changes color when 10 sec left for bar to complete.
we can alter that code , to find time passed since new bar
Thank you
 
Hello Edward sir and everyone,
for 1 min database , How about using Time Left !!
There is code [ Probably coded by Herman ], which changes color when 10 sec left for bar to complete.
we can alter that code , to find time passed since new bar
Thank you
hi Cas, yes all this is possible but for backtesting this is not useful unless you use tick data. Because when you use 1-minute data you can not know when 10 seconds has passed within a 1-minute bar when using historical data.
 

Similar threads