Trendlinie als Support für EA

 

Hallo Zusammen,

ich bin ein Anfänger in Programmieren und brauche eure Hilfe.

In meinen EA möchte ich eine Trendlinie einfügen und stehe vor dem Problem, dass die Trendlinie nach jedem Tick gelöscht und neu erzeugt wird.

Ich würde mich über Eure Hilfe sehr freuen und bedanke mich für Eure Verbesserungsvorschläge im Voraus!


Vereinfacht sieht mein EA folgender maßen aus, anders kriege ich meine Trendlinie nicht angezeigt:


input string _______With_Trendline_______ ;

input string Up = "Long"; /* Trade long/ Trendline name "Long"*/

input string Down = "Short"; /* Trade short/ Trendline name "Short"*/

int OnInit(){

return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

void OnDeinit(const int reason){

}

//+------------------------------------------------------------------+

void OnTick(){

//Trend Line

//--- number of visible bars in the chart window

int bars=(int)ChartGetInteger(0,CHART_VISIBLE_BARS);

//--- price array size

int accuracy=1000;

//--- arrays for storing the date and price values to be used

//--- for setting and changing line anchor points' coordinates

datetime date[];

double Price[];

//--- memory allocation

ArrayResize(date,bars);

ArrayResize(Price,accuracy);

//--- fill the array of dates

ResetLastError();

if(CopyTime(Symbol(),Period(),0,bars,date)==-1)

{

Print("Failed to copy time values! Error code = ",GetLastError());

return;

}

//--- fill the array of prices

//--- find the highest and lowest values of the chart

double min_price=ChartGetDouble(0,CHART_PRICE_MIN);

double max_price=ChartGetDouble(0,CHART_PRICE_MAX);

//--- define a change step of a price and fill the arraydouble step=(max_price-min_price)/accuracy;

for(int i=0;i<accuracy;i++)

Price[i]=min_price+i*step;

//--- define points for drawing the line

int d1=35*(bars-1)/100;

int d2=65*(bars-1)/100;

int p1=60*(accuracy-1)/100;

int p2=40*(accuracy-1)/100;

//--- define points for drawing the line

int d3=40*(bars-1)/100;

int d4=70*(bars-1)/100;

int p3=65*(accuracy-1)/100;

int p4=45*(accuracy-1)/100;

if(Up == "Long"){

TrendCreate(0,"Long",0,date[d1],Price[p1],date[d2],Price[p2],clrGreen,STYLE_SOLID,1,false,true,tr

ue,true,0);

}

if(Down == "Short"){

TrendCreate(0,"Short",0,date[d3],Price[p3],date[d4],Price[p4],clrRed,STYLE_SOLID,1,false,true,tru

e,true,0);

}

}

bool TrendCreate(const long chart_ID=0, // chart's ID

const string name="TrendLine", // line name

const int sub_window=0, // subwindow index

datetime time1=0, // first point time

double price1=0, // first point price

datetime time2=0, // second point time

double price2=0, // second point price

const color clr=clrGreen, // line color

const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

const int width=1, // line width

const bool back=false, // in the background

const bool selection=true, // highlight to move

const bool ray_right=true, // line's continuation to the right

const bool hidden=true, // hidden in the object list

const long z_order=0) // priority for mouse click

{

//--- create a trend line by the given coordinates

ObjectCreate(chart_ID,name,OBJ_TREND,sub_window,time1,price1,time2,price2);

//--- set line color

ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set line display style

ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set line width

ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- display in the foreground (false) or background (true)ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the line by mouse

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- enable (true) or disable (false) the mode of continuation of the line's display to the right

ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);

//--- hide (true) or display (false) graphical object name in the object list

ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution

return(true);

}

 
  1. Proramm-Code bitte nur als Code eintragen: entweder mit Alt+S oder
  2. Was willst Du programmieren Indikator oder EA oder Skript? (Kennst Du den Unterschied?
  3. Lies vielleicht erst einmal:
    https://www.mql5.com/en/forum/277762
    https://www.mql5.com/de/articles/496
    https://www.mql5.com/de/articles/100
  4. Stell den Kursor auf ObjectCreate und drück F1
    und dann klick nacheinander auf ENUM_OBJECT und OBJ_TREND und
    dann schau Dir das Beispiel der Referenz an. Es ist ein Skript dort kein EA oder Indikator!
  5. Bedenke, es gibt fast nichts, was nicht schon programmiert wurde => Suchen & Kopieren ist schneller für Anfänger!!


Use tips in the Freelance service! - How to fix your most frequent customer issues in the Freelance Forum
Use tips in the Freelance service! - How to fix your most frequent customer issues in the Freelance Forum
  • 2018.09.05
  • MetaQuotes
  • www.mql5.com
To improve the convenience of our services, we have added tips on the most frequent customer issues in the freelance. ", the developer or customer can access the list of the most frequent questions and search by keywords
 
Carl Schreiber #:
  1. Proramm-Code bitte nur als Code eintragen: entweder mit Alt+S oder
  2. Was willst Du programmieren Indikator oder EA oder Skript? (Kennst Du den Unterschied?
  3. Lies vielleicht erst einmal:
    https://www.mql5.com/en/forum/277762
    https://www.mql5.com/de/articles/496
    https://www.mql5.com/de/articles/100
  4. Stell den Kursor auf ObjectCreate und drück F1
    und dann klick nacheinander auf ENUM_OBJECT und OBJ_TREND und
    dann schau Dir das Beispiel der Referenz an. Es ist ein Skript dort kein EA oder Indikator!
  5. Bedenke, es gibt fast nichts, was nicht schon programmiert wurde => Suchen & Kopieren ist schneller für Anfänger!!


Hi Carl,

vielen Dank für deine Rückmeldung und deine Tipps! Der eingefügte Code sieht auch schon besser aus... Danke!!! 👍😉

Mit Suchen & Kopieren kommt man tatsächlich weiter, so bin ich, unter anderem, an den geposteten EA gekommen.

Die Unterschiede zwischen EA, Indikator und Skript kenne ich. Es soll ein EA werden, den ich in einen bereis erarbeiteten EA hinzufügen möchte. Da es um die Trendlinie geht, habe ich den EA auf das nötigste reduziert...

Mein EA funktioniert, die Trendlinien werden angezeigt und können auch bewegt/ eingestellt werden. Nach jedem Tick springt die Trendlinie jedoch zurück.

An dem Code muss etwas geändert werden, sodass es nicht nach jedem Tick alle Schritte neu durchläuft. Und hier habe ich einen Hänger und kann nicht dazu finden... 


Vielen Dank für Eure Hilfe!!!


Der Code von meinem Trendline-EA ist wie folgt:

//+------------------------------------------------------------------+
input string _______With_Trendline_______ ;
input string Up = "Long"; /* Trade long/ Trendline name "Long"*/
input string Down = "Short"; /* Trade short/ Trendline name "Short"*/
int OnInit(){
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
}
//+------------------------------------------------------------------+
void OnTick(){
//Trend Line
//--- number of visible bars in the chart window
int bars=(int)ChartGetInteger(0,CHART_VISIBLE_BARS);
//--- price array size
int accuracy=1000;
//--- arrays for storing the date and price values to be used
//--- for setting and changing line anchor points' coordinates
datetime date[];
double Price[];
//--- memory allocation
ArrayResize(date,bars);
ArrayResize(Price,accuracy);
//--- fill the array of dates
ResetLastError();
if(CopyTime(Symbol(),Period(),0,bars,date)==-1)
{
Print("Failed to copy time values! Error code = ",GetLastError());
return;
}
//--- fill the array of prices
//--- find the highest and lowest values of the chart
double min_price=ChartGetDouble(0,CHART_PRICE_MIN);
double max_price=ChartGetDouble(0,CHART_PRICE_MAX);
//--- define a change step of a price and fill the array
double step=(max_price-min_price)/accuracy;
for(int i=0;i<accuracy;i++)
Price[i]=min_price+i*step;
//--- define points for drawing the line
int d1=35*(bars-1)/100;
int d2=65*(bars-1)/100;
int p1=60*(accuracy-1)/100;
int p2=40*(accuracy-1)/100;
//--- define points for drawing the line
int d3=40*(bars-1)/100;
int d4=70*(bars-1)/100;
int p3=65*(accuracy-1)/100;
int p4=45*(accuracy-1)/100;
if(Up == "Long"){
TrendCreate(0,"Long",0,date[d1],Price[p1],date[d2],Price[p2],clrGreen,STYLE_SOLID,1,false,true,true,true,0);
}
if(Down == "Short"){
TrendCreate(0,"Short",0,date[d3],Price[p3],date[d4],Price[p4],clrRed,STYLE_SOLID,1,false,true,true,true,0);
}
}
bool TrendCreate(const long chart_ID=0, // chart's ID
const string name="TrendLine", // line name
const int sub_window=0, // subwindow index
datetime time1=0, // first point time
double price1=0, // first point price
datetime time2=0, // second point time
double price2=0, // second point price
const color clr=clrGreen, // line color
const ENUM_LINE_STYLE style=STYLE_SOLID, // line style
const int width=1, // line width
const bool back=false, // in the background
const bool selection=true, // highlight to move
const bool ray_right=true, // line's continuation to the right
const bool hidden=true, // hidden in the object list
const long z_order=0) // priority for mouse click
{
//--- create a trend line by the given coordinates
ObjectCreate(chart_ID,name,OBJ_TREND,sub_window,time1,price1,time2,price2);
//--- set line color
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style
ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width
ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- enable (true) or disable (false) the mode of continuation of the line's display to the right
ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);
//--- hide (true) or display (false) graphical object name in the object list
ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
return(true);
}
 
Du must wohl abfangen, wann zB. eine neue Kerze entsteht, oder einen anderen Grund für's Neuzeichnen definieren, und nur dann Deine Linie neu zeichnen. Aber, wenn sich die Linie immer dann stark ändert, könnte sich das einem Repaint nähern?