Too many sendorders

 

Hi,

My first post, i have an EA that seems to behave differently on different copies of MT4 with the same broker. I have 4 accounts and am running the same EA on all 4, but from different locations. One of the copies sometimes goes into a trade 4 or 5 times when it should only do it once. I am unable to see anyway that the code would allow this. This only happens on forward tests, not back tests.

Within the if statement for entry to a postion i have a global variable "LastTrade", if i entry a short postion this is set to 1 or if a Long set to 2, to enter a long "LastTrade" must be set to 1, in other words it can only entry alternate postions, long then short.

But on one of my copies it goes long or short some times 4 or 5 times all at the same time, there are no loops in the code.

I have read that control is passed from the chart to the code and then when the code completes control is passed back to the chart. So i belive it should not be possible to trigger the start function again before the last start has competed. But this is the only thing i can think of.

Does anyone have any ideas, or had this problem?

Thanks everyone

 
Altogether now.... Post the code please. CB
 
cloudbreaker wrote >>
Altogether now.... Post the code please. CB

As Requested please see code below:

//+------------------------------------------------------------------+
//| Public_Help.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"

int MagicNumberHedgeID0 = 0;
int MagicNumberHedgeID1 = 0;
int MagicNumberHedgeID2 = 0;
int MagicNumberHedgeID3 = 0;
int MagicNumberHedgeID4 = 0;
int MagicNumberHedgeID5 = 0;
int MagicNumberHedgeID6 = 0;
int MagicNumberHedgeID7 = 0;
int MagicNumberHedgeID8 = 0;
int MagicNumberHedgeID9 = 0;


int BaseTicketID0 = 0;
int BaseTicketID1 = 0;
int BaseTicketID2 = 0;
int BaseTicketID3 = 0;
int BaseTicketID4 = 0;
int BaseTicketID5 = 0;
int BaseTicketID6 = 0;
int BaseTicketID7 = 0;
int BaseTicketID8 = 0;
int BaseTicketID9 = 0;

int BaseLongShort0 = 0;
int BaseLongShort1 = 0;
int BaseLongShort2 = 0;
int BaseLongShort3 = 0;
int BaseLongShort4 = 0;
int BaseLongShort5 = 0;
int BaseLongShort6 = 0;
int BaseLongShort7 = 0;
int BaseLongShort8 = 0;
int BaseLongShort9 = 0;

extern double Lots = 0.1;
extern int OverallStop = 1000;
extern int HedgeStart = 300;
extern int Profit = 60;
extern int HedgeStop = 100;
extern double Gap = 0.5;
extern int TrailingStop = 50;
extern int UseChart = 30;
extern int BreakEven = 20;
extern double GapExit = 1.0;
extern int MAGap = 5;
extern int LastTrade = 0;
extern int MaxTrades = 10;
extern double Slipage=3;
extern int StopTradingHour=25;
extern int StartTradingHour=25;


int init()
{
//----

#define MAGICMABase 4567 }

bool TradingTimeOk()
{


if(Hour()>=StartTradingHour && Hour()<StopTradingHour)
{return(false);}
else
{return(true);}

}

int SaveTicketID(int FreeMagicNo, int HedgeTicketID, int BaseTicketID) {
{Alert("This Magic = " + FreeMagicNo + " Base=" +BaseTicketID + " HedgeID=" + HedgeTicketID );}

if (FreeMagicNo == 0) {
MagicNumberHedgeID0 = HedgeTicketID;
BaseTicketID0 = BaseTicketID;
} else {
if (FreeMagicNo == 1) {
MagicNumberHedgeID1 = HedgeTicketID;
BaseTicketID1 = BaseTicketID;
} else {
if (FreeMagicNo == 2) {
MagicNumberHedgeID2 = HedgeTicketID;
BaseTicketID2 = BaseTicketID;
} else {
if (FreeMagicNo == 3) {
MagicNumberHedgeID3 = HedgeTicketID;
BaseTicketID3 = BaseTicketID;
} else {
if (FreeMagicNo == 4) {
MagicNumberHedgeID4 = HedgeTicketID;
BaseTicketID4 = BaseTicketID;
} else {
if (FreeMagicNo == 5) {
MagicNumberHedgeID5 = HedgeTicketID;
BaseTicketID5 = BaseTicketID;
} else {
if (FreeMagicNo == 6) {
MagicNumberHedgeID6 = HedgeTicketID;
BaseTicketID6 = BaseTicketID;
} else {
if (FreeMagicNo == 7) {
MagicNumberHedgeID7 = HedgeTicketID;
BaseTicketID7 = BaseTicketID;
} else {
if (FreeMagicNo == 8) {
MagicNumberHedgeID8 = HedgeTicketID;
BaseTicketID8 = BaseTicketID;
} else {
if (FreeMagicNo == 9) {
MagicNumberHedgeID9 = HedgeTicketID;
BaseTicketID9 = BaseTicketID;
}
}
}
}
}
}
}
}
}
}
return (0);
}

int SetMagicBase(int FreeMagicNo, int LongShort) {
if (FreeMagicNo == 0) BaseLongShort0 = LongShort;
else {
if (FreeMagicNo == 1) BaseLongShort1 = LongShort;
else {
if (FreeMagicNo == 2) BaseLongShort2 = LongShort;
else {
if (FreeMagicNo == 3) BaseLongShort3 = LongShort;
else {
if (FreeMagicNo == 4) BaseLongShort4 = LongShort;
else {
if (FreeMagicNo == 5) BaseLongShort5 = LongShort;
else {
if (FreeMagicNo == 6) BaseLongShort6 = LongShort;
else {
if (FreeMagicNo == 7) BaseLongShort7 = LongShort;
else {
if (FreeMagicNo == 8) BaseLongShort8 = LongShort;
else
if (FreeMagicNo == 9) BaseLongShort9 = LongShort;
}
}
}
}
}
}
}
}
return (0);
}

int FreeMagicNumber() {
int l_count_0 = 0;
int l_count_4 = 0;
for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 0) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (0);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 1) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (1);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 2) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (2);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 3) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (3);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 4) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (4);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 5) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (5);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 6) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (6);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 7) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (7);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 8) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (8);
l_count_0 = 0;
l_count_4 = 0;
for (l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == UseChart + MAGICMABase + 9) {
if (OrderType() == OP_BUY) l_count_0++;
if (OrderType() == OP_SELL) l_count_4++;
}
}
if (l_count_0 == 0 && l_count_4 == 0) return (9);
return (-1);
}

int EnterTrade() {
if (iMACD(NULL, UseChart, 12, 26, 3, PRICE_CLOSE, MODE_SIGNAL, 0) > iMACD(NULL, UseChart, 12, 26, 3, PRICE_CLOSE, MODE_SIGNAL, 1) + Gap * Point)
return (2);
if (iMACD(NULL, UseChart, 12, 26, 3, PRICE_CLOSE, MODE_SIGNAL, 0) < iMACD(NULL, UseChart, 12, 26, 3, PRICE_CLOSE, MODE_SIGNAL, 1) - Gap * Point)
return (1);
return (0);
}

void OpenTrade()
{
int FreeMagic = FreeMagicNumber();
int HedgeTicketID = 0;
int BaseTicketID = 0;
double HedgeTakeProfit=0;
double BaseStop=0;
double BuyStopOpenPrice = Ask + (HedgeStart * Point);
double BuyStopStop = BuyStopOpenPrice - (HedgeStop * Point);

double SellStopOpenPrice = Bid - (HedgeStart * Point);
double SellStopStop = SellStopOpenPrice + (HedgeStop * Point);

if (FreeMagic > -1 && FreeMagic < MaxTrades && TradingTimeOk()==true)
{
if (EnterTrade() == 1 && LastTrade == 2 )
{


BaseStop = Bid + (OverallStop * Point);
HedgeTakeProfit = BaseStop;

BaseTicketID = OrderSend(Symbol(), OP_SELL, Lots, Bid,Slipage, Bid + OverallStop * Point, Bid - Profit * Point, "follow =" + UseChart + " TradeNo=" + FreeMagic + " Base", UseChart + MAGICMABase + FreeMagic, 0, Red);
HedgeTicketID = OrderSend(Symbol(), OP_BUYSTOP, Lots, BuyStopOpenPrice,Slipage, BuyStopStop, HedgeTakeProfit, "follow =" + UseChart + " TradeNo=" + FreeMagic + " Hedge", UseChart + MAGICMABase + FreeMagic, 0, Blue);

SaveTicketID(FreeMagic, HedgeTicketID, BaseTicketID);
SetMagicBase(FreeMagic, 1);
LastTrade = 1;
}
else if (EnterTrade() == 2 && LastTrade == 1)
{

BaseStop = Ask - (OverallStop * Point);
HedgeTakeProfit = BaseStop;

BaseTicketID = OrderSend(Symbol(), OP_BUY, Lots, Ask,Slipage, BaseStop, Ask + Profit * Point, "follow =" + UseChart + " TradeNo=" + FreeMagic + " Base", UseChart + MAGICMABase + FreeMagic, 0, Blue);
HedgeTicketID = OrderSend(Symbol(), OP_SELLSTOP, Lots, SellStopOpenPrice,Slipage, SellStopStop, HedgeTakeProfit, "follow =" + UseChart + " TradeNo=" + FreeMagic + " Hedge", UseChart + MAGICMABase + FreeMagic, 0, Red);

SaveTicketID(FreeMagic, HedgeTicketID, BaseTicketID);
SetMagicBase(FreeMagic, 2);
LastTrade = 2;
}//end Buy code

} //End Free Magic and Trading Time
} //End Function


void start()
{

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

if (MaxTrades > 10)
{
Alert("Max trades allowed is 10, Please reset");
return;
}
else
{
OpenTrade();

}
}

 
Bremsford:

Hi,

My first post, i have an EA that seems to behave differently on different copies of MT4 with the same broker. I have 4 accounts and am running the same EA on all 4, but from different locations. One of the copies sometimes goes into a trade 4 or 5 times when it should only do it once. I am unable to see anyway that the code would allow this. This only happens on forward tests, not back tests.

Within the if statement for entry to a postion i have a global variable "LastTrade", if i entry a short postion this is set to 1 or if a Long set to 2, to enter a long "LastTrade" must be set to 1, in other words it can only entry alternate postions, long then short.

But on one of my copies it goes long or short some times 4 or 5 times all at the same time, there are no loops in the code.

I have read that control is passed from the chart to the code and then when the code completes control is passed back to the chart. So i belive it should not be possible to trigger the start function again before the last start has competed. But this is the only thing i can think of.

Does anyone have any ideas, or had this problem?

Thanks everyone

Hi,

I Have been midifying PipMaker, a very fast acting Martingale sca;per system that placed orders way to fast and thus ende up not getting placed at all. I found some code on here that uses 'TradeContext' taht CB helped me with as well that checks and waits till the server is open and tehn places the trade. I have also applies it to closing orders, but so far this has not worked and keeps timing out at 30 seconds. You can check this out for yourself at : can't find the topic now )< 8( The search engine here sucks. I'll upload teh files for you:

Files:
 
DougRH4x:

Hi,

I Have been midifying PipMaker, a very fast acting Martingale sca;per system that placed orders way to fast and thus ende up not getting placed at all. I found some code on here that uses 'TradeContext' taht CB helped me with as well that checks and waits till the server is open and tehn places the trade. I have also applies it to closing orders, but so far this has not worked and keeps timing out at 30 seconds. You can check this out for yourself at : can't find the topic now )< 8( The search engine here sucks. I'll upload teh files for you:

Files:
 
upload
Files:
 
upload
Files:
 
DougRH4x:
upload

Thanks once more to the highly proficient CloudBreaker (< 8)

(watch out for Icing of the rotors in this weather CB, we souldn't watn to lose you!)


Seasons grettings to all

 
DougRH4x wrote >>

Thanks once more to the highly proficient CloudBreaker (< 8)

(watch out for Icing of the rotors in this weather CB, we souldn't watn to lose you!)

Seasons grettings to all

Thank you very much for your help