EA not open positions

 
Files:
deltastop.mq4  3 kb
dea.mq4  5 kb
 

Well I got it to open orders but not in the right way this is what I am looking for

Short

Entry = On the Open of the first bar below the DeltaStop

Exit = On the reverse ie the open of the first bar above the DeltaStop

Long

Entry = On the Open of the first bar above the DeltaStop

Exit = On the reverse ie the open of the first bar below the DeltaStop

The ea is opening mutliple long positions only.

Could some one please point me in the right direction as to where I could be going wrong.

Cheers

Beno

Files:
dea.gif  61 kb
dea.mq4  5 kb
 
 

could some kind soul please have a look at this code and point me in the right direction as to why it opens mutiple positions in the way it does. it should just open one per direction. any help would be great

thanks

extern double Lots = 0.1;

extern double MaximumRisk = 0.05;

extern double DecreaseFactor = 0;

extern string i3="DeltaStop";

extern int Mode=0; //0 - auto, 1 - manual

extern double DeltaPrice=50;

extern int ATRperiod=14;

extern double ATRratio=2.824;

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

//| Calculate open positions |

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

int CalculateCurrentOrders(string DeltaStop)

{

int buys=0,sells=0;

//----

for(int i=0;i<OrdersTotal();i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)

{

if(OrderType()==OP_BUY) buys++;

if(OrderType()==OP_SELL) sells++;

}

}

//---- return orders volume

if(buys>0) return(buys);

else return(-sells);

}

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

//| Calculate optimal lot size |

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

double LotsOptimized()

{

double lot=Lots;

int orders=HistoryTotal(); // history orders total

int losses=0; // number of losses orders without a break

//---- select lot size

lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);

//---- calcuulate number of losses orders without a break

if(DecreaseFactor>0)

{

for(int i=orders-1;i>=0;i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }

if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;

//----

if(OrderProfit()>0) break;

if(OrderProfit()<0) losses++;

}

if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);

}

//---- return lot size

if(lot<0.1) lot=0.1;

return(lot);

}

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

//| Check for open order conditions |

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

void CheckForOpen()

{

double DeltaStop;

int res;

DeltaStop=iCustom(NULL,0,"DeltaStop",Mode,DeltaPrice,ATRperiod,ATRratio,PRICE_CLOSE,0);

//---- sell conditions

if(Close[1]<DeltaStop && Open[0]<DeltaStop)

{

res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGIC,0,Red);

return;

}

//---- buy conditions

if(Close[1]>DeltaStop && Open[0]>DeltaStop)

{

res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGIC,0,Blue);

return;

}

//----

}

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

//| Check for close order conditions |

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

void CheckForClose()

{

double DeltaStop;

//---- go trading only for first tiks of new bar

if(Volume[0]>1) return;

//---- get DeltaStop

DeltaStop=iCustom(NULL,0,"DeltaStop",Mode,DeltaPrice,ATRperiod,ATRratio,PRICE_CLOSE,0);

//----

for(int i=0;i<OrdersTotal();i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;

//---- check order type

if(OrderType()==OP_BUY)

{

if(Open[0]>DeltaStop && Close[1]>DeltaStop) OrderClose(OrderTicket(),OrderLots(),Bid,0,White);

break;

}

if(OrderType()==OP_SELL)

{

if(Open[0]<DeltaStop && Close[1]<DeltaStop) OrderClose(OrderTicket(),OrderLots(),Ask,0,White);

break;

}

}

//----

}

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

//| Start function |

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

void start()

{

//---- check for history and trading

if(Bars<100 || IsTradeAllowed()==false) return;

//---- calculate open orders by current symbol

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();

else CheckForClose();

}

 

I still can't find where I have gone wrong, it opens long positions only and a lot of them.

I am not looking for some one to fix it for me just to high light where I should be looking.

Cheers

Beno

 

Here it is.

For educationnal purposes try to find the error by yourself.

Edit:OOps i didnot see that you already solve the first problem. Forget this post.

AnywayIt opens buys and sells now.

Files:
dea.mq4  5 kb
 
Beno:
I still can't find where I have gone wrong, it opens long positions only and a lot of them.

I am not looking for some one to fix it for me just to high light where I should be looking.

Cheers

Beno

I think you have to look to a TradeSignal () routine. Cos now it opens orders every times the price is above/below delatastop, not only at crosses.

 

Gidday Flytox

I'm not to sure what you mean but I think it's might involve something like

this. ?

Long = PRICE_OPEN>DeltaStop;

Short = PRICE_OPEN<DeltaStop;

buysig = Long;

sellsig = Short;

closebuy=sellsig;

closesell=buysig;

 
Beno:
Gidday Flytox

I'm not to sure what you mean but I think it's might involve something like

this. ?

Long = PRICE_OPEN>DeltaStop;

Short = PRICE_OPEN<DeltaStop;

buysig = Long;

sellsig = Short;

closebuy=sellsig;

closesell=buysig;

Hi Beno,

I did not look inside the code but I think that Flytox is right.

If you choose condition like that:

- price is above DeltaStop for buy

- price is below DeltaStop so sell

So you will get the orders on every bar. Because price may be above DeltaStop during the half a day so you may have 1000 orders in one direction. Because EAs are not understand "above". Because what is last previous bar for EA? It is the same as the word "yesterday": tomorrow the "yesterday will be today and so on.

I think it should be crossing of DeltaStop, or some other condition concerning the crossing.

 

Gidday

What would the code look like for price cross or price open greater than Deltastop

buysig=false;

sellsig=false;

closebuy=false;

closesell=false;

double DeltaStop=iCustom(NULL,0,"DeltaStop",Mode,DeltaPrice,ATRperiod,ATRratio,PRICE_CLOSE,0,shift);

bool Long = ??????????????? DeltaStop;

bool Short = ??????????????? DeltaStop;

buysig = Long;

sellsig = Short;

closebuy=sellsig;

closesell=buysig;

 
Beno:
Gidday

What would the code look like for price cross or price open greater than Deltastop

buysig=false;

sellsig=false;

closebuy=false;

closesell=false;

double DeltaStop=iCustom(NULL,0,"DeltaStop",Mode,DeltaPrice,ATRperiod,ATRratio,PRICE_CLOSE,0,shift);

bool Long = ??????????????? DeltaStop;

bool Short = ??????????????? DeltaStop;

buysig = Long;

sellsig = Short;

closebuy=sellsig;

closesell=buysig;

Hi Beno,

I modified AsctrendBuySellExpert_v1.3 from this thread https://www.mql5.com/en/forum/173249

I created this EA with the help of Igorad (using Gordago tool).

So, I removed Asctrend indicator and inserted DeltaStop indicator.

I checked it by backtesting in visual mode:

So, in default settings this EA is working on the following way:

- open the order if previous close bar is crossing DeltaStop line;

- close on opposite crossing and close on stop loss and take profit/stop loss as well.

With default settings it is closing the orders on previous close bar crossing DeltaStop line.

If you want to use stop loss or take profit so change the settings from 1000 for your value.

Beno,

I am not a coder so ...

But you may use this EA, may change everything and so on.

But, I think we will need to insert some filter to this EA (some additional indicators).

This code will help you to create your own EA.

Reason: