Little question about EA's

 

Hello All, I have this simple EA that just trades on the VQ indicator and exits with an Exit indicator which mostly works but......

Two questions;

1) The EA only trades/opens ONE order at a time for the currency it's on until the order is closed. What do I need to change so that the EA will open another trade if VQ signals another trade even if a trade on the same currency is already open? So to allow two, three, ... same currency trades open at the same time.

2) The EA doesn't seem to be checking the MagicNumber before closing a trade. It will close the trade according to the correct currency but if I'm running the EA on two charts using the same currency and different settings and MagicNumbers the EA will close a trade that was opened by the other chart also from the same currency even though it does have a different MagicNumber. How do I make the EA also obey the MagicNumber when going to close a trade if it's running on two charts of the same currency?

Thank you

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

//| #VQExit_EA.mq4 |

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

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

extern int MagicNumber = 121212;

extern bool SignalMail = False;

extern bool EachTickMode = False;

extern double Lots = 0.1;

extern int Slippage = 3;

extern bool UseStopLoss = True;

extern int StopLoss = 40;

extern bool UseTakeProfit = True;

extern int TakeProfit = 40;

extern bool UseTrailingStop = False;

extern int TrailingStop = 25;

int BarCount;

int Current;

bool TickCheck = False;

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

//| expert initialization function |

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

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

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

//| expert deinitialization function |

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

int deinit() {

return(0);

}

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

//| expert start function |

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

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

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

//| Variable Begin |

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

double Entry1 = iCustom(NULL, 0, "VQ", false,0,5,3,1,5,true,false,true,true,false,true,1485, 0, 1);

double Entry2 = iCustom(NULL, 0, "VQ", false,0,5,3,1,5,true,false,true,true,false,true,1485, 0, 2);

double Up2 = iCustom(NULL, 0, "VQ", false,0,5,3,1,5,true,false,true,true,false,true,1485, 1, 2);

double Down2 = iCustom(NULL, 0, "VQ", false,0,5,3,1,5,true,false,true,true,false,true,1485, 2, 2);

double Entry3 = iCustom(NULL, 0, "Exit", 5,3,5, 0, 1);

double Entry4 = iCustom(NULL, 0, "Exit", 5,3,5, 0, 2);

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

//| 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 Exit Buy |

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

if (Entry3 > Entry4 && ((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(UseTrailingStop && 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 Exit Sell |

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

if (Entry3 < Entry4 && ((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(UseTrailingStop && 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;

}

}

}

}

}

}

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

//| Signal Entry |

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

//Buy

if (Up2==Down2 && Entry1>Entry2 && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

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

} else {

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

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Up2==Down2 && Entry1<Entry2 && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

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

} else {

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

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

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

Files:
vqexit_ea.mq4  9 kb
 

Do you know any code at all? I don't know how much I need to tell you to make you understand.

if(!IsTrade) {[/PHP]

is on some lines, this makes so it doesn't open more trades if one is open alreadty.

The line that checks open orders if they are correct currency:

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

you need to add

[PHP] && OrderMagicNumber()==MagicNumber
 

Ok, thank you for responding. Don't really code at all. Just learning some basic stuff. So to kill two birds with one stone, could I do?;

//Check position

bool IsTrade = False;

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

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

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

// IsTrade = True;

IsTrade = False;

if(OrderType() == OP_BUY) {

//Close

or just delete this line if(!IsTrade) { from the Buy and Sell entry commands

but still add && OrderMagicNumber()==MagicNumber

EDIT: if I remove the if(!IsTrade) { and the EA trades more times that will screw up my close command because I wont have away of tracking what trade I want to close If I have two E/J trades open how can I decipher between the two ?? Ok, maybe I just leave that part alone and have one order at a time

Reason: