Code to prevent more trades

To add comments, please log in or register
cfabian
11
cfabian  

Hello there,

I'm coding an EA, but have the problem of the EA keep opening orders after a trade is close as long as the conditions are met. (i.e. supose 2 MA lines when cross is the condition to get in the trade. The EA opens a trade, and if it gets closed on its TP or whatever value, the EA will open another trade and so on. There's ALWAYS an open trade).

So, what I want is to limit the EA to open just one trade after a condition is met, until another condition is met. No matter it takes 5 mins or 19 days until it happens again.... just ONE trade.

I guess is something simple, but I'm learning coding. Hope someone can help.

Cheers

23510
492
23510  
cfabian:

Hello there,

I'm coding an EA, but have the problem of the EA keep opening orders after a trade is close as long as the conditions are met. (i.e. supose 2 MA lines when cross is the condition to get in the trade. The EA opens a trade, and if it gets closed on its TP or whatever value, the EA will open another trade and so on. There's ALWAYS an open trade).

So, what I want is to limit the EA to open just one trade after a condition is met, until another condition is met. No matter it takes 5 mins or 19 days until it happens again.... just ONE trade.

I guess is something simple, but I'm learning coding. Hope someone can help.

Cheers

if you are using something like :


if(ma1>ma2)

{

ticket= OrderSend(.....)

}


rather add another 2 ma values that use the index 1 for the last candle...then make the criteria like this :

prevma1=iMA(Symbol(),0,30,0,MODE_EMA,PRICE_CLOSE,1);

prevma2=iMA(Symbol(),0,50,0,MODE_EMA,PRICE_CLOSE,1); -- looks at last candle


now use it like this :


if(prevma1<prevma2&&ma1>ma2)


this will check if the ma's have changed from 1 condition to another.....


also you can add Vloume[0]<5 to the crteria to make sure there are not multiple signals per candle cos of ma's crossing and uncrossing and crossing again.


so :

if(Volume[0]<5&&prevma1<prevma2&&ma1>ma2)

{

ticket = Ordersend(...)

}

cfabian
11
cfabian  
23510 wrote >>

if you are using something like :

if(ma1>ma2)

{

ticket= OrderSend(.....)

}

rather add another 2 ma values that use the index 1 for the last candle...then make the criteria like this :

prevma1=iMA(Symbol(),0,30,0,MODE_EMA,PRICE_CLOSE,1);

prevma2=iMA(Symbol(),0,50,0,MODE_EMA,PRICE_CLOSE,1); -- looks at last candle

now use it like this :

if(prevma1<prevma2&&ma1>ma2)

this will check if the ma's have changed from 1 condition to another.....

also you can add Vloume[0]<5 to the crteria to make sure there are not multiple signals per candle cos of ma's crossing and uncrossing and crossing again.

so :

if(Volume[0]<5&&prevma1<prevma2&&ma1>ma2)

{

ticket = Ordersend(...)

}

Hello 23510

Thanks a lot for your inputs; however, as a newbie, I wonder where in the code this goes, and how to use it if have defined variables as follows?

I really appreciate very much your help.

Here's the code:

//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+

if (Buy1_1 > Buy1_2 && Buy2_1 > Buy2_2 && Buy3_1 > Buy3_2) Order = SIGNAL_BUY;

if (Sell1_1 > Sell1_2 && Sell2_1 < Sell2_2 && Sell3_1 < Sell3_2) Order = SIGNAL_SELL;


//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+

//Buy
if (Order == SIGNAL_BUY && ((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);
}
}

23510
492
23510  
cfabian:

Hello 23510

Thanks a lot for your inputs; however, as a newbie, I wonder where in the code this goes, and how to use it if have defined variables as follows?

I really appreciate very much your help.

Here's the code:

//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+

if (Buy1_1 > Buy1_2 && Buy2_1 > Buy2_2 && Buy3_1 > Buy3_2) Order = SIGNAL_BUY;

if (Sell1_1 > Sell1_2 && Sell2_1 < Sell2_2 && Sell3_1 < Sell3_2) Order = SIGNAL_SELL;


//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+

//Buy
if (Order == SIGNAL_BUY && ((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);
}
}

if (Buy1_1 > Buy1_2 && Buy2_1 > Buy2_2 && Buy3_1 > Buy3_2) Order = SIGNAL_BUY;

if (Sell1_1 > Sell1_2 && Sell2_1 < Sell2_2 && Sell3_1 < Sell3_2) Order = SIGNAL_SELL;

I cant see what constitutes a Buy1_1 and Buy1_2 etc etc...so cant help if i cant see what criteria constitutes those values
To add comments, please log in or register