Draw line on chart in a EA on Buy and Sell - page 2

 
you have to set the MagicNumber in the OrderSend() function
 

I can't read ex4 files. Why don't you attach the mql4 file or paste your entire code. Please don't offer to send it to me via email either. The code I provided as example for drawing vertical line when EA buys or sells. It's not designed to make your EA logic work properly.

 

Your code:

OrderSend(Symbol(),OP_BUY,LotSize,
      Ask,Slippage,0,0,"James_Bond",007,100,Green);

Is different from mine:

if(OrderSend(Symbol(),OP_BUY,0.1,Ask_Norm,Slippage,0,0,
"James_Bond",007,0,Green)

I'll leave the trouble shooting up to you. Also keep in mind it's not a good idea to make the magic# 007. Use just 7 or any other integer without preceding 00. 

 
zzuegg:
you have to set the MagicNumber in the OrderSend() function
Do that MagicNumber need to be the same as the MagicNumber in the top?
extern int     MagicNumber=100000;
Ticket=OrderSend(Symbol(),OP_BUY,LotSize,
      Ask,Slippage,0,0,"James_Bond",7,0,Blue);

I did try to change it to the same but no different result.

And one more question.

Ticket=OrderSend(Symbol(),OP_BUY,LotSize,
      Ask,Slippage,0,0,"James_Bond",7,0,Blue);

I did try to change OP_BUY to OP_BUYSTOP and the other alternative but it to made the draw lines go away but now it made the trades at the right point.

Do "ObjectCreate" need to have any OP_BUYSTOP or the alternative I want it to have?

 
Ticket=OrderSend(Symbol(),OP_BUY,LotSize,
      Ask,Slippage,0,0,"James_Bond",MagicNumber,0,Blue);
use MagicNumber in this way...
 

Try using Zzuegg suggestion for magic#

Ticket=OrderSend(Symbol(),OP_BUY,LotSize,
      Ask,Slippage,0,0,"James_Bond",MagicNumber,0,Blue);

IFRC, we are creating the object based on the fact that Ticket has gone from 0 to greater than 0 based on the fact that when Op_Buy works it returns the Ticket number which is greater than 0. I don't think OP_BUYSTOP returns a Ticket#. You'll want to look at the Documentation and review what OP_BUYSTOP returns and use Arguments against that.

 

To check the Docs, do a search within Meta_Editor. Again without seeing your Logic, I cannot tell where the problem really is.

 
ubzen:

Try using Zzuegg suggestion for magic#

IFRC, we are creating the object based on the fact that Ticket has gone from 0 to greater than 0 based on the fact that when Op_Buy works it returns the Ticket number which is greater than 0. I don't think OP_BUYSTOP returns a Ticket#. You'll want to look at the Documentation and review what OP_BUYSTOP returns and use Arguments against that.

To check the Docs, do a search within Meta_Editor. Again without seeing your Logic, I cannot tell where the problem really is.

hi, of course OP_BUYSTOP returns a ticket number, but i assume he is using the current Ask/Bid as OpenPrice, so he most likely will get a "INVALID STOP" error.

btw, ticket number could also be 0. so use TicketNr>=0


//z

 
//+------------------------------------------------------------------+
//|                                              RSI-MA-ATR_v1.3.mq4 |
//|                                                    Michael Zuegg |
//|                                              whenmoneymakesmoney |
//+------------------------------------------------------------------+
#property copyright "Michael Zuegg & Mathias Halén"
#property link      "when-money-makes-money.com"

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
extern int     TimeperiodSelector=0;
extern int     ROCMA_Period=200;
extern int     ROC_Period=200;
extern int     Hist_ShortPeriod=20;
extern int     Hist_LongPeriod=200;
extern int     delay=4;
extern int     RSIPeriod=20;
       int     MAOfRSIPeriod=10;
extern int     ATRPeriod=10;
extern int     MaxTradesAllowed=1;
extern double  TPFaktor=4;
extern double  SLFaktor=2;
extern bool    UseDelay=false;
extern double  DelayFaktor=0.5;
extern double  LotSize=0.1;
extern bool    CloseOnOpossingCross=true;
extern bool    buyFilter1=true;
extern double  buyFilter1.upperLimit=72;
extern double  buyFilter1.lowerLimit=50;

//extern bool    buyFilter2=true;
//extern double  buyFilter2.upperLimit=28;
//extern double  buyFilter2.lowerLimit=0;

extern bool    sellFilter1=true;
extern double  sellFilter1.upperLimit=50;
extern double  sellFilter1.lowerLimit=28;

//extern bool    sellFilter2=true;
//extern double  sellFilter2.upperLimit=50;
//extern double  sellFilter2.lowerLimit=28;
extern int     MagicNumber=100000;
extern int     Slippage=3;

double Timeperiod[8]={0,1,5,15,30,60,240,1440};
double rsi.buff[];
double rsi.buff.old[];
double rsi=0;
double rsi.old;
double rsi.ma=0;
double rsi.ma.old=0;


double TTrend.old;
double TTrend;
double trendA;
double trendB;
double trendA.old;
double trendB.old;
double atr=0;
int    RealMagic=0;
int init()
{
   RealMagic=MagicNumber+Period();
   ArrayResize(rsi.buff,MAOfRSIPeriod);
   ArrayResize(rsi.buff.old,MAOfRSIPeriod);
   return(0);
}


int updateValues(){
   for(int i=0;i<MAOfRSIPeriod;i++){
      rsi.buff[i]=iRSI(Symbol(),Period(),RSIPeriod,PRICE_CLOSE,i+1);
      rsi.buff.old[i]=iRSI(Symbol(),Period(),RSIPeriod,PRICE_CLOSE,i+2);
   }
   rsi.ma      =iMAOnArray(rsi.buff,0,MAOfRSIPeriod,0,MODE_SMA,0);
   rsi.ma.old  =iMAOnArray(rsi.buff.old,0,MAOfRSIPeriod,0,MODE_SMA,0); 
   rsi         =rsi.buff[0];
   rsi.old     =rsi.buff.old[0];
      
   trendB      =iCustom(NULL, Timeperiod[TimeperiodSelector], "_InsTrend",1,3);
   trendB.old  =iCustom(NULL, Timeperiod[TimeperiodSelector], "_InsTrend",1,delay);
   trendA      =iCustom(NULL, Timeperiod[TimeperiodSelector], "_InsTrend",0,3);
   trendA.old  =iCustom(NULL, Timeperiod[TimeperiodSelector], "_InsTrend",0,delay);
   
   TTrend      =iCustom(NULL,NULL, "30minTrend",ROCMA_Period,ROC_Period,Hist_ShortPeriod,Hist_LongPeriod,0,1);
   TTrend.old  =iCustom(NULL,NULL, "30minTrend",ROCMA_Period,ROC_Period,Hist_ShortPeriod,Hist_LongPeriod,0,2);
 
  
  atr         =iATR(Symbol(),Period(),ATRPeriod,1);
   int signal=0;
 //  if(trendB>trendA && trendB.old<=trendA.old){
   if (trendB>trendA && trendB.old<=trendA.old && TTrend<2){
      if(buyFilter1==true){
        signal=1;
 
}
   int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,
      Ask,Slippage,0,0,"James_Bond",MagicNumber,0,Blue);
   if(Ticket>0){
      //----------------------------------------
      string Ticket_String=DoubleToStr(Ticket,8);
      datetime Time_Varable=TimeCurrent();
      ObjectCreate("James_Bond7"+Ticket_String,
      OBJ_VLINE,0,Time_Varable,Ask);
      //----------------------------------------
      ObjectSet("James_Bond7",OBJPROP_COLOR,Blue);
      ObjectSet("James_Bond7",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("James_Bond7",OBJPROP_BACK,true);
      ObjectSetText("James_Bond7","James_Bond"+Ticket_String,6);
      Alert("Ask On Buy = ",OP_BUYSTOP);
   }
}
  // if(trendB<trendA && trendB.old>=trendA.old){
     if (trendB<trendA && trendB.old>=trendA.old && TTrend.old>-2){
     if(sellFilter1==true){
      signal=-1; 
 //     Alert("TREND DOWN");           
   }
   Ticket=OrderSend(Symbol(),OP_BUY,LotSize,
      Ask,Slippage,0,0,"James_Bond",MagicNumber,0,Red);
   if(Ticket>0){
      //----------------------------------------
      Ticket_String=DoubleToStr(Ticket,8);
      Time_Varable=TimeCurrent();
      ObjectCreate("James_Bond7"+Ticket_String,
      OBJ_VLINE,0,Time_Varable,Ask);
      //----------------------------------------
      ObjectSet("James_Bond7",OBJPROP_COLOR,Red);
      ObjectSet("James_Bond7",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("James_Bond7",OBJPROP_BACK,true);
      ObjectSetText("James_Bond7","James_Bond"+Ticket_String,6);
      Print("Ask on sell= ",Ask,"OrdersTotal= ",+getOpenTradeCount());
   }  
}
   return (signal);
}



 
int deinit()   
{

   return(0);
}

int getOpenTradeCount(){
   int count=0;
   for(int i=OrdersTotal()-1;i>=0;i--){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==RealMagic){
         count++;
      }       
   }
   return (count);
}

void deletePendingOrders(){
   bool done=false;
   int retry=5;
   int openPendings=0;
   while(done==false && retry>0){
      for(int j=OrdersTotal()-1;j>=0;j--){
         OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==RealMagic && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)){
            if(!IsTradeContextBusy()){
               OrderDelete(OrderTicket());
            }
         }         
      }
      
      
      for(int i=OrdersTotal()-1;i>=0;i--){
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==RealMagic && (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)){
            openPendings++;
         }
      }
      if(openPendings==0){
         done=true;
      }
      retry--;
   }
}

void OpenNewOrder(int signal){
   int type=0;
   double op=0;
   double sl=0;
   double tp=0;
   bool done=false;
   int retry=10;
   while(done==false && retry>0){
      while(IsTradeContextBusy()){
         Sleep(1000);
         RefreshRates();
      }
      RefreshRates();
      if(UseDelay==true){
         if(signal==1){
            op=Ask+(atr*DelayFaktor);
            sl=op-(atr*SLFaktor);
            tp=op+(atr*TPFaktor);
            type=OP_BUYSTOP;
         }else{
            if(signal==(-1)){
               op=Bid-(atr*DelayFaktor);
               sl=op+(atr*SLFaktor);
               tp=op-(atr*TPFaktor);
               type=OP_SELLSTOP;         
            }
         }
      }else{
         if(signal==1){
            op=Ask;
            sl=op-(atr*SLFaktor);
            tp=op+(atr*TPFaktor);
            type=OP_BUY;
         }else{
            if(signal==(-1)){
               op=Bid;
               sl=op+(atr*SLFaktor);
               tp=op-(atr*TPFaktor);
               type=OP_SELL;         
            }
         }   
      }    
      int err=OrderSend(Symbol(),type,LotSize,NormalizeDouble(op,Digits),Slippage,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"RSI-MA-ATR "+Period(),RealMagic,0,CLR_NONE);
      if(err>=0){
         done=true;
      }else{
         err=GetLastError();
         if(err==130){
            Print("RSI-MA-ATR "+Period()+" WRONG STOPLOSS/TACKPROFIT");
            retry=0;
         }
         if(err==131){
            Print("RSI-MA-ATR "+Period()+" INVALID LOTSIZE");
            retry=0;
         }
         if(err==149){
            Print("RSI-MA-ATR "+Period()+" HEDGING IS NOT ALLOWED");
            retry=0;
         }         
         if(err==137){
            Print("RSI-MA-ATR "+Period()+" BROKER IS BUSY, WAITING 5 SECOND");
            Sleep(5000);
         }   
         if(err==138){
            Print("RSI-MA-ATR "+Period()+" REQUOTE- Retrying");
            retry++;
         }          
      }
      retry--;
   }
}

void WatchDogClose(int type){
   for(int i=OrdersTotal()-1;i>=0;i--){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol()){
         if(OrderMagicNumber()==RealMagic){
            if(OrderType()==type){
               if(!IsTradeContextBusy()){
                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,CLR_NONE);
               }
            }
         }
      }
   }
}

int start()
{
   
   static   datetime lastCandle=0;
            datetime currCandle=Time[0];
   if(lastCandle==0){
      lastCandle=currCandle;
      updateValues();
   }else{
      
      if(lastCandle!=currCandle){
         lastCandle=currCandle;
         if(UseDelay==true){
            deletePendingOrders();
         }
         int signal=updateValues();
         if(signal!=0 && getOpenTradeCount()<MaxTradesAllowed){
            OpenNewOrder(signal);
         }  
      }
   }
   if(CloseOnOpossingCross==true){
    if(TTrend>0){
      WatchDogClose(OP_SELL);
  //    Alert("Close Trend UP");
      }
   if(TTrend<0){
      WatchDogClose(OP_BUY);   
  //    Alert("Close Trend Down");   
      }
   }


   string c="";
   c=c+"RSI_MA_ATR_EA: \n";
   c=c+"Currently Open Trades: "+getOpenTradeCount()+" of "+MaxTradesAllowed+"\n";
   c=c+"RSI(1)="+rsi+" MAofRSI(1)="+rsi.ma+" Up/Down "+(rsi>rsi.ma)+"\n";
   c=c+"RSI(2)="+rsi.old+" MAofRSI(2)="+rsi.ma.old+" Up/Down "+(rsi.old>rsi.ma.old)+"\n";
   c=c+"ATR="+atr+"\n";
   c=c+"TREND="+iCustom(NULL,NULL, "30minTrend",ROCMA_Period,ROC_Period,Hist_ShortPeriod,Hist_LongPeriod,0,1)+"\n";
   c=c+"TREND.old="+iCustom(NULL,NULL, "30minTrend",ROCMA_Period,ROC_Period,Hist_ShortPeriod,Hist_LongPeriod,0,2)+"\n";//c=c+"trend.ind.B BLUE="+iCustom(NULL, Timeperiod[TimeperiodSelector], "_InsTrend",1,0)+"\n";
   //c=c+"trend.ind.A RED="+iCustom(NULL, Timeperiod[TimeperiodSelector], "_InsTrend",0,0)+"\n";
   Comment(c);
   return(0);
}
 

what where you trying to do?

OrderSend() has nothing to do in the updateValue() function.

i assume you want only drawn lines when your EA opens a new order, send me the EA via pm or email and i will add this.

Buy you have realy messed up the code ;)

but good that you are trying

Reason: