Ask! - page 76

 

EA question

Hi Codersguru,

I'm trying to learn programming and am using your material, let me say it is excellent! step by step and simple to follow, thanks for that.

I have a question, since you use the ema cross over as an example for the EA, I was thinking of using stepMA_7.2 indicator instead of ema. Do you think that the stepMA_7.2 will work? or will the code need to be rearranged or more additonal code may be needed perhaps?

I was thinking just a swap of indicator in the code should surfice.

Thanks in advance

Cheers.

 
Michel:
This is wrong: previous bars are positive counted
MA_EXIT_1=iMA(NULL,PERIOD_H1,MA_Exit,0,MODE_LWMA,PRICE_WEIGHTED,-1);[/PHP] Use this instead:[PHP]MA_EXIT_1=iMA(NULL,PERIOD_H1,MA_Exit,0,MODE_LWMA,PRICE_WEIGHTED,1);

Thank you.

 
 

I'm still having some problems. The buy orders are opened correctly, but all are closed at stoploss, ignoring the exit conditions.

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

//| EA Stripped Down.mq4 |

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

#property copyright ""

#property link ""

extern double StopLoss =13.0;

extern double MA_Open =12.0;

extern double MA_Close =12.0;

extern double MA_Exit =6.0;

extern double Lots =50.0;

#define MAGICMA 20050610

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

int init()

{

return(0);

}

int deinit()

{

return(0);

}

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

int start()

{

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

double MA_CLOSE;

double MA_OPEN;

double MA_EXIT_0;

double MA_EXIT_1;

int res;

if(Volume[0]>1) return;

MA_CLOSE=iMA(NULL,0,MA_Close,1,MODE_SMA,PRICE_CLOSE,0);

MA_OPEN=iMA(NULL,0,MA_Open,1,MODE_SMA,PRICE_OPEN,0);

MA_EXIT_0=iMA(NULL,0,MA_Exit,1,MODE_LWMA,PRICE_WEIGHTED,0);

MA_EXIT_1=iMA(NULL,0,MA_Exit,1,MODE_LWMA,PRICE_WEIGHTED,1);

if((MA_CLOSE-MA_OPEN>=1*Point) && MA_EXIT_0>MA_EXIT_1)

{

res=OrderSend(Symbol(),OP_BUY,Lots,Ask,5,Ask-StopLoss*Point,0,"",MAGICMA,0,Blue);

return;

}

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

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

if(Volume[0]>1) return;

MA_EXIT_0=iMA(NULL,PERIOD_H1,MA_Exit,1,MODE_LWMA,PRICE_WEIGHTED,0);

MA_EXIT_1=iMA(NULL,PERIOD_H1,MA_Exit,1,MODE_LWMA,PRICE_WEIGHTED,1);

if(OrderType()==OP_BUY)

{

if(MA_EXIT_0<=MA_EXIT_1) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);

return;

}

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

}
 
 

Moving average of another indicator

Figured it out.

 

checking for open positions within a range

I'm trying to check for open positions within x pips, if no position is open it will open one. It doesn't work and just opens positions continuously, can anyone help?

if(buyingPosition)

{

weBeBuying = true;

totalorders = OrdersTotal();

for(i=0;i<totalorders;i++)

{

OrderSelect(i,SELECT_BY_POS);

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

{

type = OrderType();

if((type == OP_BUY) || (type == OP_BUYLIMIT))

{

if(((Bid+spread) - OrderOpenPrice()) > -0.0013)

weBeBuying = false;

}

}

}

}

if(weBeBuying==true)

OrderSend(Symbol(),OP_BUY,lots,Bid+spread,slippage,0,0,NULL,magicNumber,0,Green);

 

MA of a iCustom indicator

How do I code the statement in an ea to reference a Moving Average of 7, Smooth, of a iCustom indicator?? Which equates to using a Moving Average using first indicators data. You can do this in displaying a iCustom indicator and having a MA of the indicator (Using first indicators data), but how do you code it in an ea???

Please help me on this statement!

I thank you in advance for your help!

Dave

Can anybody help with this???? Please!!!!

I think I got it!:

LR0=iCustom(NULL,0,"Laguerre RSI LONG & SHORT ENTRY",0,0); //LOWER GAMMA

Still need help on this - Can you help me!

 

Sere you can post your questions related to MQL4, and I'll do my best to answer them

for error code.

I have just returned account to me that on all my expert, I have an error code "OrderModify error 1", even with an expert like that of Coders Guru, below,

why?

Thanks !

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

//| PriceCross.mq4 |

//| Coders Guru |

//| https://www.mql5.com/en/forum |

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

#property copyright "Coders Guru"

#property link "https://www.forex-tsd.com"

//---- Includes

#include

//---- Trades limits

extern double TakeProfit = 200;

extern double TrailingStop = 50;

extern double StopLoss = 50;

extern double Lots = 0.1;

extern int Slippage = 5;

//--- External options

extern int CurrentBar = 1;

extern int HedgeLevel = 6;

extern double Expiration = 7200;

extern int Size = 4;

extern int Step = 1;

extern bool UseClose = true;

//--- Indicators settings

extern int MaMode = 3;

extern int MaPeriod = 24;

//--- Global variables

int MagicNumber = 101090;

string ExpertComment = "PriceCross";

bool LimitPairs = true;

bool LimitFrame = true;

int TimeFrame = 60;

string LP[] = {"EURUSD","USDCHF","GBPUSD","USDJPY"}; // add/remove the paris you want to limit.

bool Optimize = true;

int NumberOfTries = 5;

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

int init()

{

return(0);

}

int deinit()

{

return(0);

}

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

bool isNewSymbol(string current_symbol)

{

//loop through all the opened order and compare the symbols

int total = OrdersTotal();

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

string selected_symbol = OrderSymbol();

if (current_symbol == selected_symbol && OrderMagicNumber()==MagicNumber)

return (False);

}

return (True);

}

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

int start()

{

int cnt, ticket, total,n;

double trend ;

if(Bars<100) {Print("bars less than 100"); return(0);}

if(LimitFrame)

{

if(Period()!=TimeFrame) {Print("This EA is not working with this TimeFrame!"); return(0);}

}

if(LimitPairs)

{

if(AllowedPair(Symbol()) == false) {Print("This EA is not working with this Currency!"); return(0);}

}

trend = iMA(NULL,0,MaPeriod,0,MaMode,PRICE_CLOSE,CurrentBar);

//--- Trading conditions

bool BuyCondition = false , SellCondition = false , CloseBuyCondition = false , CloseSellCondition = false ;

if (Open[1]trend)

BuyCondition = true;

if (Open[1]>trend && Close[1]<trend)

SellCondition = true;

if (Open[1]>trend && Close[1]<trend)

CloseBuyCondition = true;

if (Open[1]trend)

CloseSellCondition = true;

total = OrdersTotal();

if(total < 1 || isNewSymbol(Symbol()))

{

if(BuyCondition) //<-- BUY condition

{

ticket = OpenOrder(OP_BUY); //<-- Open BUY order

CheckError(ticket,"BUY");

for(n=0 ; n< Size ; n++)

{

ticket = OpenPendingOrder(OP_BUYSTOP,Lots,HedgeLevel+(n*Step+1),Slippage,StopLoss,TakeProfit,ExpertComment,MagicNumber,CurTime() + Expiration);

}

return(0);

}

if(SellCondition) //<-- SELL condition

{

ticket = OpenOrder(OP_SELL); //<-- Open SELL order

CheckError(ticket,"SELL");

for(n=0 ; n < Size ; n++)

{

ticket = OpenPendingOrder(OP_SELLSTOP,Lots,HedgeLevel+(n*Step+1),Slippage,StopLoss,TakeProfit,ExpertComment,MagicNumber,CurTime() + Expiration);

}

return(0);

}

return(0);

}

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if(OrderType()==OP_BUY) //<-- Long position is opened

{

if(UseClose)

{

if(CloseBuyCondition) //<-- Close the order and exit!

{

CloseOrder(OrderType()); return(0);

}

}

TrailOrder(OrderType()); return(0); //<-- Trailling the order

}

if(OrderType()==OP_SELL) //<-- Go to short position

{

if(UseClose)

{

if(CloseSellCondition) //<-- Close the order and exit!

{

CloseOrder(OP_SELL); return(0);

}

}

TrailOrder(OrderType()); return(0); //<-- Trailling the order

}

}

}

return(0);

}

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

....

 

Hi,

Is there anyway we can know that an order is already closed?

The illustration is like this :

I open 2 pending orders.

open #182 buy limit 0.17 EURUSD at 1.3005 sl: 1.2970 tp: 1.3013

open #183 buy limit 0.17 EURUSD at 1.3000 sl: 1.2968 tp: 1.3011

then

order #182, buy 0.17 EURUSD is opened at 1.3005

take profit #182 at 1.3013 (1.3013 / 1.3015)

i want to delete order #183 after order #182 is closed. The trigger is if order #182 closed then order #183 will be deleted.

Cheers

Reason: