Coding help..How do I get indicator to filter instead of alert? - page 6

 
elihayun:
so far I dont see any problem (code oriented)

But logically, look at this

if(isCrossed == 1 && Ask < upLevel)

You are not checking that they are close, just that the price is lower then the line

That's what I wanted...to filter...I want to use the line as a way to disallow trades. That was the intent. I don't need to make a way to see if it's close to the line if I can change the width of the line itself I can just use the line itself as the limiter. By changing the width of the bands it will be the same as if there were another factor that checked how close it got to the line. By making this a condition of entering it filters or should stop the buy orders from opening unless they are below the top line or the sell orders from opening if they are above the bottom line.

I'm confused why doing this would make it generate order errors?

 

it appears that the error is due to improper lot size having something to do with the MM parameters...

I am changing my focus somewhat...returning to my origianl focus..

I have concluded that the trend bands indicator will not produce the kind of filtering effect I desire. It just won't.

Originally I was looking at the 'ang_AutoCh_HL-v1' channel indicator. I can see that it would give me the filter effect I want however it doesn't have the buffers to call on...so.....

can I MAKE buffers in it? for the line values? of object 1 and object 2? so I can use them as support and resistance in the EA?

here's the indicator...

#property copyright "ANG3110@latchess.com"

//----------------------------------

#property indicator_chart_window

//----------------------------------

extern int Hours=24;

extern color col=SkyBlue;

//------------------

double lr,lr0,lrp;

double sx,sy,sxy,sx2,aa,bb;

int p,sName,fs;

int f,f0,f1;

double dh,dl,dh_1,dl_1,dh_2,dl_2;

int ai_1,ai_2,bi_1,bi_2;

double hai,lai,dhi,dli,dhm,dlm,ha0,hap,la0,lap;

double price_p1,price_p0,price_p2,price_01,price_00,price_02;

int p1,p0,p2,fp;

//*****************************************

int init() {

p=Hours*60/Period();

if (fs==0) {sName=CurTime(); fs=1;}

return(0);}

//*******************************

int deinit() {

ObjectDelete("1"+sName);

ObjectDelete("0"+sName);

ObjectDelete("2"+sName); }

//*******************************

int start() {

int i,n;

//------------------------------------------------------------------------------

if (f==1) {

p1=iBarShift(Symbol(),Period(),ObjectGet("1"+sName,OBJPROP_TIME1));

p0=iBarShift(Symbol(),Period(),ObjectGet("0"+sName,OBJPROP_TIME1));

p2=iBarShift(Symbol(),Period(),ObjectGet("2"+sName,OBJPROP_TIME1));

if (fp==0 && p!=p1) {p=p1; fp=1;}

if (fp==0 && p!=p0) {p=p0; fp=1;}

if (fp==0 && p!=p2) {p=p2; fp=1;}

}

//====================================================

sx=0; sy=0; sxy=0; sx2=0;

for (n=0; n<=p; n++) {sx+=n; sy+=Close[n]; sxy+=n*Close[n]; sx2+=MathPow(n,2);}

aa=(sx*sy-(p+1)*sxy)/(MathPow(sx,2)-(p+1)*sx2); bb=(sy-aa*sx)/(p+1);

//----------------------------------------------------

for (i=0; i<=p; i++) {

lr=bb+aa*i;

dh=High-lr; dl=Low-lr;

//----------------------------------------------------

if (i<p/2) {if (i==0) {dh_1=0.0; dl_1=0.0; ai_1=i; bi_1=i;}

if (dh>=dh_1) {dh_1=dh; ai_1=i;}

if (dl<=dl_1) {dl_1=dl; bi_1=i;}}

//----------------------------------------------------

if (i>=p/2) {if (i==p/2) {dh_2=0.0; dl_2=0.0; ai_2=i; bi_2=i;}

if (dh>=dh_2) {dh_2=dh; ai_2=i;}

if (dl<=dl_2) {dl_2=dl; bi_2=i;}}}

//-------------------------------------

lr0=bb; lrp=bb+aa*(i+p);

//===================================================

if (MathAbs(ai_1-ai_2)>MathAbs(bi_1-bi_2)) f=1;

if (MathAbs(ai_1-ai_2)<MathAbs(bi_1-bi_2)) f=2;

if (MathAbs(ai_1-ai_2)==MathAbs(bi_1-bi_2)) {if (MathAbs(dh_1-dh_2)=MathAbs(dl_1-dl_2)) f=2;}

//=================================================

if (f==1) {

for (n=0; n<=20; n++) { f1=0;

for (i=0; i<=p; i++) {hai=High[ai_1]*(i-ai_2)/(ai_1-ai_2)+High[ai_2]*(i-ai_1)/(ai_2-ai_1);

if (i==0 || i==p/2) dhm=0.0;

if (High-hai>dhm && i<p/2) {ai_1=i; f1=1;}

if (High-hai>dhm && i>=p/2) {ai_2=i; f1=1;} }

if (f==0) break;}

//----------------------------

for (i=0; i<=p; i++) {hai=High[ai_1]*(i-ai_2)/(ai_1-ai_2)+High[ai_2]*(i-ai_1)/(ai_2-ai_1);

dli=Low-hai;

if (i==0) dlm=0.0; if (dli<dlm) dlm=dli;}

ha0=High[ai_1]*(0-ai_2)/(ai_1-ai_2)+High[ai_2]*(0-ai_1)/(ai_2-ai_1);

hap=High[ai_1]*(p-ai_2)/(ai_1-ai_2)+High[ai_2]*(p-ai_1)/(ai_2-ai_1);

//----------------------------

price_p1=hap;

price_p0=hap+dlm/2;

price_p2=hap+dlm;

price_01=ha0;

price_00=ha0+dlm/2;

price_02=ha0+dlm;

}

//=================================================

if (f==2) {

for (n=0; n<=20; n++) { f1=0;

for (i=0; i<=p; i++) {lai=Low*(i-bi_2)/(bi_1-bi_2)+Low*(i-bi_1)/(bi_2-bi_1);

if (i==0 || i==p/2) dlm=0.0;

if (Low-lai<dlm && i<p/2) {bi_1=i; f1=1;}

if (Low-lai=p/2) {bi_2=i; f1=1;}}

if (f==0) break;}

//----------------------------

for (i=0; i<=p; i++) {lai=Low*(i-bi_2)/(bi_1-bi_2)+Low*(i-bi_1)/(bi_2-bi_1);

dhi=High-lai;

if (i==0) dhm=0.0; if (dhi>dhm) dhm=dhi;}

la0=Low*(0-bi_2)/(bi_1-bi_2)+Low*(0-bi_1)/(bi_2-bi_1);

lap=Low*(p-bi_2)/(bi_1-bi_2)+Low*(p-bi_1)/(bi_2-bi_1);

//----------------------------------------------------------------

price_p1=lap;

price_p0=lap+dhm/2;

price_p2=lap+dhm;

price_01=la0;

price_00=la0+dhm/2;

price_02=la0+dhm;

}

//===================================================================================

ObjectCreate("1"+sName,2, 0,Time[p],price_p1,Time[0],price_01);

ObjectCreate("0"+sName,2, 0,Time[p],price_p0,Time[0],price_00);

ObjectCreate("2"+sName,2, 0,Time[p],price_p2,Time[0],price_02);

//-----------------------------------------------------------------

ObjectSet("1"+sName,OBJPROP_COLOR,col);

ObjectSet("0"+sName,OBJPROP_COLOR,col);

ObjectSet("0"+sName,OBJPROP_STYLE,STYLE_DOT);

ObjectSet("2"+sName,OBJPROP_COLOR,col);

//---------------------------------------------

ObjectSet("1"+sName,OBJPROP_TIME1,Time[p]);

ObjectSet("1"+sName,OBJPROP_PRICE1,price_p1);

ObjectSet("1"+sName,OBJPROP_TIME2,Time[0]);

ObjectSet("1"+sName,OBJPROP_PRICE2,price_01);

ObjectSet("0"+sName,OBJPROP_TIME1,Time[p]);

ObjectSet("0"+sName,OBJPROP_PRICE1,price_p0);

ObjectSet("0"+sName,OBJPROP_TIME2,Time[0]);

ObjectSet("0"+sName,OBJPROP_PRICE2,price_00);

ObjectSet("2"+sName,OBJPROP_TIME1,Time[p]);

ObjectSet("2"+sName,OBJPROP_PRICE1,price_p2);

ObjectSet("2"+sName,OBJPROP_TIME2,Time[0]);

ObjectSet("2"+sName,OBJPROP_PRICE2,price_02);

//==================================================================

f=1; p1=p; p0=p; p2=p; fp=0;

//*************************************************************************************

return(0);}

//=====================================================================================[/PHP]

here is what I'm trying to add a buffer so I can extract the values I need...

#property copyright "ANG3110@latchess.com"

//----------------------------------

#property indicator_chart_window

//----------------------------------

extern int Hours=24;

extern color col=SkyBlue;

//------------------

double ExtBufferResistance[];

double ExtBufferMiddle[];

double ExtBufferSupport[];

double lr,lr0,lrp;

double sx,sy,sxy,sx2,aa,bb;

int p,sName,fs;

int f,f0,f1;

double dh,dl,dh_1,dl_1,dh_2,dl_2;

int ai_1,ai_2,bi_1,bi_2;

double hai,lai,dhi,dli,dhm,dlm,ha0,hap,la0,lap;

double price_p1,price_p0,price_p2,price_01,price_00,price_02;

int p1,p0,p2,fp;

//*****************************************

int init() {

p=Hours*60/Period();

if (fs==0) {sName=CurTime(); fs=1;}

SetIndexBuffer (4, ExtBufferResistance); //--resistance line

SetIndexBuffer (5, ExtBufferMiddle); //--middle line

SetIndexBuffer (6, ExtBufferSupport); //--support line

return(0);}

//*******************************

//===================================================================================

ObjectCreate("1"+sName,2, 0,Time[p],price_p1,Time[0],price_01);

ObjectCreate("0"+sName,2, 0,Time[p],price_p0,Time[0],price_00);

ObjectCreate("2"+sName,2, 0,Time[p],price_p2,Time[0],price_02);

//------------------make indicator buffer for EA-------------------------------------

somehow get object 1 to put it's value into buffer 4

somehow get object 0 to put it's value into buffer 5

somehow get object 2 to put it's value into buffer 6

//------------------------draw chart-------------------------------------------------

[/php]

and then upgrade this EA....

[PHP]#property copyright "Copyright 2005, Chris Battles."

#property link "cbattles@neo.rr.com"

extern double TrailingStop = 5;

extern double TrailingStopTrigger = 19;

extern double StopLoss = 186;

extern double TakeProfit = 250;

extern double Lots = 0.4;

//---- ang_AutoCh_HL-v1 and Filter Parameters

extern int Hours=24;

extern color col=SkyBlue;

extern double TF = 30; //--which bar period for the custom indicator to use

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

int start(){

int cnt, ticket;

if(Bars<100){

Print("bars less than 100");

return(0);

}

double ema1a=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0);

double ema2a=iMA(NULL,0,144,0,MODE_EMA,PRICE_CLOSE,0);

double ema3a=iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0);

double ema1b=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1);

double ema2b=iMA(NULL,0,144,0,MODE_EMA,PRICE_CLOSE,1);

double ema3b=iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,1);

//----------channel filter

double resistance = iCustom(NULL,TF, "ang_AutoCh_HL-v1", Hours, col, 4, 0);

double support = iCustom(NULL,TF, "ang_AutoCh_HL-v1", Hours, col, 6, 0);

//--- end channel filter

and then make the order logic have second condition for entry...

[PHP]if (ema3bema1a && Ask < resistance){

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point, NULL,16384,0,Green);

if(ticket>0){

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if (ema3b>ema1b && ema3a support){

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point, NULL,16384,0,Red);

if(ticket>0) {

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

really my sticking point is getting the values out of the indicator so the EA can use them...I think I can manage the rest ok if you could just show me how to get the values out FROM indicator INTO the EA.

 

I really don't know what I'm doing with buffers. I go throwing setbuffer lines in and i don't even know if it's necessary or what

 
raff1410:
Hi...

Indycator with buffers,

You can call res && sup like below:

double resistance = iCustom(NULL,TF,"Channel",24,col,0,0);

double support = iCustom(NULL,TF,"Channel",24,col,2,0);

Raff

oh man see!! that's what I'm talking bout....I can hardly wait to try this to see if it will work.

now do I have to put this stuff in the top of the ea? to tell the indicator what settings it use?

//---- Channel and Filter Parameters

extern int Hours=24;

extern color col=SkyBlue;

extern double TF = 30; //--which bar period for the custom indicator to use

and then...

double resistance = iCustom(NULL,TF,"Channel",Hours,col,0,0);

double support = iCustom(NULL,TF,"Channel",Hours,col,2,0);

 

ok i'm getting this error 130 again what is that again?

oh yea stop loss too close..ok...

 

Hi...

Indycator with buffers,

channel.mq4

You can call res && sup like below:

double resistance = iCustom(NULL,TF,"Channel",24,col,0,0);

double support = iCustom(NULL,TF,"Channel",24,col,2,0);

Raff

Files:
channel.mq4  6 kb
 

New Problem...

I have several versions of stops operating on this...The drawdowns are coming from a failure of the trailing stop trigger to engage. When it doesn't engage then the fall back 'equity stop' is what closes the position and THAT is what's hurting...

I can see from the many small draw downs that when the trailing stop is triggered it doesn't do nearly as much equity damage to the account. It can sustain alot of those kinds of losses and make them up but it can't overcome the larger losses of having the equity stop close.

so....

Is there a way to close the position if the bar following the open doesn't move enough to engage the trailing stop?

I have started on this by now calling for the current period as well as the three previous periods from the moving average lines. Basically my thought was that the signal and the one bar confirm could come off the three periods previous to the current and the current period could be used to see if it was now moved enough to trigger the trailing stop. I thought that this condition could be added as an || or condition to the equity stop which closes all open orders.

What has me a baffled is how to tell if it's moved enough to trip the trailing stop trigger depends on if it's going long it will have to move up but if it's going short it will have to move down. So the trailing stop trigger detection has to pay attention to if it's a long or a short position to know which way to move to trigger. I'm not sure how to have it detect that.

I can imagine this report with the big draw downs looking no bigger than the small ones and if that were so then it would be profitable.

Files:
whatever2.htm  292 kb
whatever2.gif  6 kb
 

one more tweak needed...

this IS closing losing positions before they get big...it is also closing WINNING positions before they get big too

the thing it needs to do it close the position only if it's not moved enough in the first ten minutes to trigger the trailing stop...it only has to move 1 pip to do that I think...so I'm not asking for alot in the first ten minutes. Really I only want to make sure that it doesn't turn tail and go back on the position immediately after it's opened. Those are the ones that lose....

so....if it can be made to detect if the trailing stop HAS been triggered and if so to let it run!! let the trailing stop close it rather than close as a function of time. The time closing parameter is not meant to be the primary closing strategy it's just to handle the close until the trailing stop takes over.

Files:
whatever3.htm  304 kb
whatever3.gif  6 kb
 

This is not doing what I intended...I can't make sense of these results..

https://c.mql5.com/forextsd/forum/9/whatever3.htm

just looking over it most everything is only open 10 mins or less ( I set it to close in ten mins. if criteria not met) with only a very few exceptions like order #52 which is open 19 mins and #48 which is open 15 mins.

this part isn't working... OrderType()==OP_BUY && Bid-Profit<OrderOpenPrice()

nor is this working... if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Bid+Profit>OrderOpenPrice())

what we need to check is if the trailing stop has triggered. I don't want this to close based on price. I want this to close based on the trailing stop not being triggered. I realize that I originally said that we needed to check to see if it had gone up enough to trigger the trailing stop..but I don't fully understand the trailing stop trigger code or how far that is...I don't know if this keeps repeating to check even after the specified interval and would close it at 15 or 19 minutes if/when the criteria of the bid+Profit being the OrderOpenPrice? What this is doing is making itself the dominant close criteria instead of the stop gap criteria until the trailing stop is triggered...that's what it seems like...

//+------------------------------------------------------------------+

void CloseOrder()

{

double Profit=ThresholdMove*Point;

int total = OrdersTotal();

for (int cnt = 0 ; cnt < total ; cnt++)

{

OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if ((CurTime()-OrderOpenTime())>MonitorInMinutes*60*MinsMultiplier)

{

if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && Bid-Profit<OrderOpenPrice() )

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);

}

if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Bid+Profit>OrderOpenPrice())

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);

}

}

}

}

//+------------------------------------------------------------------+[/PHP]

Somewhere in this closing strategy there must be a way to detect if it's been triggered or not...

[PHP]//+------------------------------------------------------------------+

//| Variable End |

//+------------------------------------------------------------------+

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+

if (CloseBuy1_1 == CloseBuy1_2) Order = SIGNAL_CLOSEBUY;

//+------------------------------------------------------------------+

//| Signal End(Exit Buy) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

if(Bid - OrderOpenPrice() > Point * TrailingStop) {

if(OrderStopLoss() < Bid - Point * TrailingStop) {

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Sell) |

//+------------------------------------------------------------------+

if (CloseSell1_1 == CloseSell1_2) Order = SIGNAL_CLOSESELL;

//+------------------------------------------------------------------+

//| Signal End(Exit Sell) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(TrailingStopMode && TrailingStop > 0) {

if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

bottom line the timeclosing parameter is far too aggressive unless it can be modified to perform as intended.

 

Sorry, I don't have time to examine the entire code, but let us just examine this snippet.

if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && Bid+Profit>OrderOpenPrice())

Let us say that the OrderOpenPrice = 1.2100

and the profit is 0.0010

So, you want to close the short trade if the bid is less than or equal to 1.2100-0.0010 = 1.2090.

Let us assume that there was a gap down and the prices skipped 1.2090 and the bid price now is 1.2088. According to your formula,

Bid + Profit = 1.2088 + 0.0010 = 1.2098. It is NOT greater than OrderOpenPrice even though your system has exceeded the profit threshold. Thus, the order will not be closed. The logic of the closing condition need to be reevaulated and rescripted, in my opinion. Also, you should deal with Ask prices when dealing with short trades as you can only close the trade at the ask price.

Another bit of advice,

The count loop of for (int cnt = 0 ; cnt = 0; cnt--) or something similar.

Good luck.

Reason: