WHRoeder:
This makes the EA incompatible with any other including itself on other pairs
I took out the for loop and added this one.
I seem to be getting multiple trades in various directions which seem to close at sl and/or close multiple close results
//+------------------------------------------------------------------+//| Dirty_Rat.mq4 |//| Agent86's Dirty Rat Trade |//| http://www.iclbiz.com/joomla |//+------------------------------------------------------------------+#property copyright "Agent86 N/A"#property link "www.iclbiz.com/joomla"//---- input parametersexterndouble TakeProfit=300.0;
externdouble Lots=0.1;
externdouble StopLoss=20.0;
externint MagicNumber=12345;
//++++ These are adjusted for 5 digit brokers.int pips2points; // slippage 3 pips 3=points 30=pointsdouble pips2dbl; // Stoploss 15 pips 0.0015 0.00150intDigits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips)// OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl//+------------------------------------------------------------------+//| expert initialization function |//+------------------------------------------------------------------+int init()
{
if (Digits == 5 || Digits == 3)
{ // Adjust for five (5) digit brokers.
pips2dbl = Point*10; pips2points = 10; Digits.pips = 1;
}
else
{
pips2dbl = Point; pips2points = 1; Digits.pips = 0;
}
// OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl//---- //----return(0);
}
//+------------------------------------------------------------------+//| expert deinitialization function |//+------------------------------------------------------------------+int deinit()
{
//---- //----return(0);
}
int Crossed (double line1 , double line2)
{
staticint last_direction = 0;
staticint current_direction = 0;
if(line1>line2)current_direction = 1; //up //might need to add here for second macd agreementif(line1<line2)current_direction = 2; //downif(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}
int Crossed_2 (double line1 , double line2) //i'll use this later for macd agreement on 2 timeframes
{
staticint last_direction_2 = 0;
staticint current_direction_2 = 0;
if(line1>line2)current_direction_2 = 1; //upif(line1<line2)current_direction_2 = 2; //downif(current_direction_2 != last_direction_2) //changed
{
last_direction_2 = current_direction_2;
return (last_direction_2);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+//| expert start function |//+------------------------------------------------------------------+int start()
{
//---- int cnt, ticket, total, mrmagic, pos;
double faster, slower, faster_2, slower_2;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1); //MODE_MAIN
slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1); //MODE_MAIN
slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
total = 0;
int isCrossed = Crossed (faster,slower);
int isCrossed_2 = Crossed_2 (faster_2,slower_2); // i'll use this later for macd agreement on separate timeframesfor (pos = OrdersTotal()-1; pos >= 0; pos--)
if(OrderSelect(pos, SELECT_BY_POS)
&& OrderMagicNumber() == mrmagic
&& OrderSymbol() == Symbol()){
total++;
return(0);
}
if(total == 0)
{
if(isCrossed == 1){ // I want to add && isCrossed_2 == 1) but 0 trades occur with the note below
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"Agent86",12345,0,Green);
if(ticket>0){
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
elsePrint("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2){ // I want to add && isCrossed_2 == 2) but 0 trades occur with the note above
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask+StopLoss*pips2dbl,Ask-TakeProfit*pips2dbl,"Agent86",12345,0,Red);
if(ticket>0){
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
elsePrint("Error opening SELL order : ",GetLastError());
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
// should it be closed?
if(isCrossed == 2){
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
// should it be closed?
if(isCrossed == 1){
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
}
}
}
*/
}
return(0);
}
//+------------------------------------------------------------------+
Regardless of using both of the for loops or one or the other this produces many open/close trades which all close with s/l
At the end of the results there are numerous closed trades in positive gains which say: close at stop
I know I have to close these trades somehow and do not appear to understand the concept even though I am using the example from the lessons which I thought I understood what to do and how it works.
Oh well, Back to the Drawing Board again.
Thanks for all the answers I will eventually understand this, I think I need some more lessons and reading.
P.S
I did code the wasLastBuy codes etc. into this code as well, which I have taken out temporarily because it caused the EA to only place 1 trade then never again traded.
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
OrderSelect(pos, SELECT_BY_POS) // Only my orders w/
&& OrderMagicNumber() == magic.number // my magic number
&& OrderSymbol() == Symbol() // and my pair.
){
Just in case the EA is put on other charts and the user doesn't change the magic number.
For the case where the EA is put on the same pair but different timeframe, either the user MUST change the magic number, or you must use a range of magic numbers
externint Magic.Number.Base = 20110202;
int magic.number;
string TF.text
int init(){
/*++++ Adjust for the current chart timeframe */{ staticint TFperiod[] = { PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30,
PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1,
PERIOD_MN1 }; staticstring TFtext[] = { "M1", "M5", "M15", "M30",
"H1", "H4", "D1", "W1",
"MN1" };
for(int iTF=0; TFperiod[iTF] < Period(); iTF++){}
TF.text = TFtext[iTF];
magic.number = Magic.Number.Base + iTF;
/*---- Adjust for the current chart timeframe */}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
This makes the EA incompatible with any other including itself on other pairs
I seem to be getting multiple trades in various directions which seem to close at sl and/or close multiple close results
Regardless of using both of the for loops or one or the other this produces many open/close trades which all close with s/l
At the end of the results there are numerous closed trades in positive gains which say: close at stop
I know I have to close these trades somehow and do not appear to understand the concept even though I am using the example from the lessons which I thought I understood what to do and how it works.
Oh well, Back to the Drawing Board again.
Thanks for all the answers I will eventually understand this, I think I need some more lessons and reading.
P.S
I did code the wasLastBuy codes etc. into this code as well, which I have taken out temporarily because it caused the EA to only place 1 trade then never again traded.
Ok, so it's sort of an EA ID, and would be a different number for each order and associated with a particular EA
What about the same EA on different time frames, would this be handled by the magic number also ? Or a totally different process ?
Just in case the EA is put on other charts and the user doesn't change the magic number.
For the case where the EA is put on the same pair but different timeframe, either the user MUST change the magic number, or you must use a range of magic numbers