Draw line on chart in a EA on Buy and Sell

 

How hard is it to code so the EA on buy and sell draws a line like this.


if(trendB<trendA && trendB.old>=trendA.old){
      if(sellFilter1==true){
            signal=-1;
if(trendB>trendA && trendB.old<=trendA.old){
      if(buyFilter1==true){
        signal=1;
 
mattehalen:

How hard is it to code so the EA on buy and sell draws a line like this.



I did finde this but i still wont draw anything on the chart why?

if(trendB>trendA && trendB.old<=trendA.old){
      if(buyFilter1==true){
         signal=1;
            ObjectCreate("Rectangle",OBJ_VLINE,0, 0,0, 0,0);
      }
 

you need to set the position where a line should be drawn...

https://docs.mql4.com/objects/ObjectCreate

 

Here give this a try:

//#################################################################
//#################################################################
int start(){
//#################################################################
//#################################################################
int      Slippage=MarketInfo(Symbol(),MODE_SPREAD);
double   Digit_Info=MarketInfo(Symbol(),MODE_DIGITS);
double   Point_Info=MarketInfo(Symbol(),MODE_POINT);
double   Ask_Norm=NormalizeDouble(Ask,Digit_Info);
double   Bid_Norm=NormalizeDouble(Bid,Digit_Info);
//#################################################################
//#################################################################
if(OrderSend(Symbol(),OP_BUY,0.1,Ask_Norm,Slippage,0,0,
"James_Bond",007,0,Green)>0)
{
   datetime Time_Varable=TimeCurrent();
   ObjectCreate("James_Bond007",OBJ_VLINE,0,Time_Varable,Open[0]);
   ObjectSet("James_Bond007",OBJPROP_COLOR,Green);
   ObjectSet("James_Bond007",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("James_Bond007",OBJPROP_BACK,true);
   ObjectSetText("James_Bond007","James_Bond007",6);
}
//#################################################################
//#################################################################
return(0);}
//#################################################################
//#################################################################
 
ubzen:

Here give this a try:


Can i use different colors on Buy and sell like this?

 if(trendB>trendA && trendB.old<=trendA.old){
      if(buyFilter1==true){
     //    if(rsi>rsi.ma && rsi.old<=rsi.ma.old){
            signal=1;
   // NEW CODE         
   datetime Time_Varable=TimeCurrent();
   ObjectCreate("James_Bond007",OBJ_VLINE,0,Time_Varable,Open[0]);
   ObjectSet("James_Bond007",OBJPROP_COLOR,Blue);
   ObjectSet("James_Bond007",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("James_Bond007",OBJPROP_BACK,true);
   ObjectSetText("James_Bond007","James_Bond007",6);
   // NEW CODE
       //  }
      }
  //    if(buyFilter2==true){
  //       if(rsi>buyFilter2.lowerLimit && rsi<buyFilter2.upperLimit){
  //          signal=1;
  //       }
  //    }      
   }
   if(trendB<trendA && trendB.old>=trendA.old){
     if(sellFilter1==true){
    //     if(rsi<rsi.ma && rsi.old>=rsi.ma.old){
            signal=-1;
   // NEW CODE        
   datetime Time_Varable=TimeCurrent();
   ObjectCreate("James_Bond007",OBJ_VLINE,0,Time_Varable,Open[0]);
   ObjectSet("James_Bond007",OBJPROP_COLOR,Blue);
   ObjectSet("James_Bond007",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("James_Bond007",OBJPROP_BACK,true);
   ObjectSetText("James_Bond007","James_Bond007",6);
   // END NEW CODE      

I know i cant use "datetime Time_Varable=TimeCurrent();" to times but how do I vet it to work on both buy and sell?

 

Yes for Color.

I don't understand your datetime ?

Should work on both buy and sell signals.

It will work on Buy when buy condition is met. 

And work on Sell when that condition is met. 

 
ubzen:

Yes for Color.

I don't understand your datetime ?

Should work on both buy and sell signals.

It will work on Buy when buy condition is met.

And work on Sell when that condition is met.


It works but i only draws one line and then changes that ones color. I would llike to have so it draws each time i make a sell or a buy.

 if(trendB>trendA && trendB.old<=trendA.old){
      if(buyFilter1==true){
     //    if(rsi>rsi.ma && rsi.old<=rsi.ma.old){
            signal=1;
   
       //  }
      }
   // New CODE
   if(OrderSend(Symbol(),OP_BUY,0.1,NormalizeDouble(Ask,MarketInfo(Symbol(),MODE_DIGITS)),Slippage,0,0,"James_Bond",007,0,Green)>0)
{
   datetime Time_Varable=TimeCurrent();
   ObjectCreate("James_Bond007",OBJ_VLINE,0,Time_Varable,Open[0]);
   ObjectSet("James_Bond007",OBJPROP_COLOR,Blue);
   ObjectSet("James_Bond007",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("James_Bond007",OBJPROP_BACK,true);
   ObjectSetText("James_Bond007","James_Bond007",6);
}   
 // END NEW CODE
 
   
  /*    if(buyFilter2==true){
         if(rsi>buyFilter2.lowerLimit && rsi<buyFilter2.upperLimit){
            signal=1;
         }
      } */      
   }
   if(trendB<trendA && trendB.old>=trendA.old){
     if(sellFilter1==true){
    //     if(rsi<rsi.ma && rsi.old>=rsi.ma.old){
            signal=-1;
     //   }
      }
   if(OrderSend(Symbol(),OP_BUY,0.1,NormalizeDouble(Ask,MarketInfo(Symbol(),MODE_DIGITS)),Slippage,0,0,"James_Bond",007,0,Green)>0)
{
   //datetime Time_Varable=TimeCurrent();
   ObjectCreate("James_Bond007",OBJ_VLINE,0,Time_Varable,Open[0]);
   ObjectSet("James_Bond007",OBJPROP_COLOR,Red);
   ObjectSet("James_Bond007",OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet("James_Bond007",OBJPROP_BACK,true);
   ObjectSetText("James_Bond007","James_Bond007",6);
}  
      /*if(sellFilter2==true){
      //   if(rsi>sellFilter2.lowerLimit && rsi<sellFilter2.upperLimit){
      //      signal=-1;
      //   }
      } */   
   } 
   return (signal);
}
 

Right now all your objects are using the same description.

If you want different ones then they need different names. 

Change the line

ObjectCreate("James_Bond007",OBJ_VLINE,0,Time_Varable,Open[0]);

To:

string Ticket_String=DoubleToStr(Ticket,8);
ObjectCreate("James_Bond007"+Ticket_String,OBJ_VLINE,0,Time_Varable,Open[0]);

Get the Ticket by doing

int Ticket=OrderSend(Symbol(),OP_BUY,0.1,NormalizeDouble(Ask,MarketInfo(Symbol(),MODE_DIGITS)),Slippage,0,0,"James_Bond",007,0,Green);

Also Change

if(OrderSend(Symbol(),OP_BUY,0.1,NormalizeDouble(Ask,MarketInfo(Symbol(),MODE_DIGITS)),Slippage,0,0,"James_Bond",007,0,Green)>0)

To 

if(Ticket>0)

 

Should look something like this, I've not tested it but just to give you some idea.

if(trendB>trendA && trendB.old<=trendA.old){
   if(buyFilter1==true){
      signal=1;
   }
   int Ticket=OrderSend(Symbol(),OP_BUY,0.1,
      NormalizeDouble(Ask,MarketInfo(Symbol(),MODE_DIGITS)),
      Slippage,0,0,"James_Bond",007,0,Green);
   if(Ticket>0){
      //----------------------------------------
      string Ticket_String=DoubleToStr(Ticket,8);
      datetime Time_Varable=TimeCurrent();
      ObjectCreate("James_Bond007"+Ticket_String,
      OBJ_VLINE,0,Time_Varable,Open[0]);
      //----------------------------------------
      ObjectSet("James_Bond007",OBJPROP_COLOR,Blue);
      ObjectSet("James_Bond007",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("James_Bond007",OBJPROP_BACK,true);
      ObjectSetText("James_Bond007","James_Bond"+Ticket_String,6);
   }
}
//#################################################################
//#################################################################
if(trendB<trendA && trendB.old>=trendA.old){
   if(sellFilter1==true){
      signal=-1;
   }
   Ticket=OrderSend(Symbol(),OP_BUY,0.1,
      NormalizeDouble(Ask,MarketInfo(Symbol(),MODE_DIGITS)),
      Slippage,0,0,"James_Bond",007,0,Green);
   if(Ticket>0){
      //----------------------------------------
      Ticket_String=DoubleToStr(Ticket,8);
      Time_Varable=TimeCurrent();
      ObjectCreate("James_Bond007"+Ticket_String,
      OBJ_VLINE,0,Time_Varable,Open[0]);
      //----------------------------------------
      ObjectSet("James_Bond007",OBJPROP_COLOR,Red);
      ObjectSet("James_Bond007",OBJPROP_STYLE,STYLE_SOLID);
      ObjectSet("James_Bond007",OBJPROP_BACK,true);
      ObjectSetText("James_Bond007","James_Bond"+Ticket_String,6);
   }  
}
 
ubzen:

Right now all your objects are using the same description.

If you want different ones then they need different names.

Change the line

To:

Get the Ticket by doing

Also Change

To

Should look something like this, I've not tested it but just to give you some idea.

I tryed to change some of the code you gave me just to make it buy and sell by my varibles that allredy vere diffend put when I did that it stopt making LINES and when I put in your code it make lins on buy and sell but not with my rules.

I thought i could change it

to

int Ticket=OrderSend(Symbol(),OP_BUY,LotSize,NormalizeDouble(Ask,MarketInfo(Symbol(),MODE_DIGITS)),
      Slippage,NormalizeDouble(SLFaktor,Digits),NormalizeDouble(TPFaktor,Digits),
   "James_Bond",007,0,Green);
   if(Ticket>0){

instead of

int Ticket=OrderSend(Symbol(),OP_BUY,0.1,
      NormalizeDouble(Ask,MarketInfo(Symbol(),MODE_DIGITS)),
      Slippage,0,0,"James_Bond",007,0,Green);

Where did i think wrong? I just changed your Lot to my LotSize and put in my slippage and Takeproffit and Stoploss.

 

Your code is getting too long within the OrderSend(). 

Best if you define those variable at the beginning of the code. Remember always the computer reads the code from Top to Down and from Left to Right.

Something like this:

int start(){
//#################################################################
//#################################################################
int      Slippage=MarketInfo(Symbol(),MODE_SPREAD);
double   Digit_Info=MarketInfo(Symbol(),MODE_DIGITS);
double   Point_Info=MarketInfo(Symbol(),MODE_POINT);
double   Ask_Norm=NormalizeDouble(Ask,Digit_Info);
double   Bid_Norm=NormalizeDouble(Bid,Digit_Info);

and then when you need Ask use Ask_Norm instead.

Make sure the variable types for your variable matches what the Function(,,,) is expecting. At you level, I'll recommend using allot of Alerts(); statements. Make sure you know what something looks like with Alerts before using it within your code.

Example: create a script like this.

//#################################################################
//#################################################################
int start(){
//#################################################################
//#################################################################
int      Slippage=MarketInfo(Symbol(),MODE_SPREAD);
//#################################################################
//#################################################################
Alert("Slippage= ",Slippage);
//if the value you've been using for slippage was 20 and alert
//returns 20 then you know it'll work. If it returns 20.0 or 0
//then you know you have a problem.
//#################################################################
//#################################################################
return(0);}
//#################################################################
//#################################################################
 
ubzen:

Your code is getting too long within the OrderSend().

Best if you define those variable at the beginning of the code. Remember always the computer reads the code from Top to Down and from Left to Right.

Something like this:

and then when you need Ask use Ask_Norm instead.

Make sure the variable types for your variable matches what the Function(,,,) is expecting. At you level, I'll recommend using allot of Alerts(); statements. Make sure you know what something looks like with Alerts before using it within your code.

Example: create a script like this.

I'm not really sure whats happening in the code at the moment.

When i insert your code it draw the lines on every order buy it makes more orders.

and when I try to change some of the setting in OrderSend() like SL and TP from 0 to anyother numer or callnamn it stops making the drawings.


Iv tryed to find the problem with Aler and have cheeked numbers of openorders and it say:

The max order allowed is 2 but you see that its much more then 2.

Can the code:

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

can this code be making more trades then allowed? I dont think it can but somehow that ordersend makes it all go crazy

Reason: