Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 35
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Here's what came up.
Seems to determine the right way apart
#property strict
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots 6
//--- plot UpBar
#property indicator_label1 "UpBar"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrDeepSkyBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot DnBar
#property indicator_label2 "DnBar"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrDeepPink
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- plot UpFr
#property indicator_label3 "UpFr"
#property indicator_type3 DRAW_ARROW
#property indicator_color3 clrBlue
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- plot DnFr
#property indicator_label4 "DnFr"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 clrRed
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//--- plot UpBarFr
#property indicator_label5 "UpBarFr"
#property indicator_type5 DRAW_ARROW
#property indicator_color5 clrBlue
#property indicator_style5 STYLE_SOLID
#property indicator_width5 1
//--- plot DnBarFr
#property indicator_label6 "DnBarFr"
#property indicator_type6 DRAW_ARROW
#property indicator_color6 clrRed
#property indicator_style6 STYLE_SOLID
#property indicator_width6 1
//--- input parameters
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;
input string cSymbol = "AUDCAD";
input int CountBar = 5; // Баров подряд
//--- indicator buffers
double BufferUPb[];
double BufferDNb[];
double BufferUPf[];
double BufferDNf[];
double BufferUPbf[];
double BufferDNbf[];
//--- global variables
double open1=0,close1=0,open2=0,close2=0;
double frDn=0,frUp=0,frUp1=0,frUp2=0,frUp3=0,frDn1=0,frDn2=0,frDn3=0;
string dSymbol;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
dSymbol=cSymbol==""?_Symbol:cSymbol;
//--- indicator buffers mapping
SetIndexBuffer(0,BufferUPb);
SetIndexBuffer(1,BufferDNb);
SetIndexBuffer(2,BufferUPf);
SetIndexBuffer(3,BufferDNf);
SetIndexBuffer(4,BufferUPbf);
SetIndexBuffer(5,BufferDNbf);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
SetIndexArrow(0,233);
SetIndexArrow(1,234);
SetIndexArrow(2,221);
SetIndexArrow(3,222);
SetIndexArrow(4,108);
SetIndexArrow(5,108);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
int b=1,s=1;
if(rates_total<2) return(0);
int limit=rates_total-prev_calculated;
if(limit>1) {
limit=rates_total-2;
ArrayInitialize(BufferUPb,EMPTY_VALUE);
ArrayInitialize(BufferDNb,EMPTY_VALUE);
ArrayInitialize(BufferUPf,EMPTY_VALUE);
ArrayInitialize(BufferDNf,EMPTY_VALUE);
ArrayInitialize(BufferUPbf,EMPTY_VALUE);
ArrayInitialize(BufferDNbf,EMPTY_VALUE);
}
for(int i=limit; i>=0; i--) {
//--------------------------------------------------------------
open1=iOpen(dSymbol, TimeFrame, i);
close1=iClose(dSymbol, TimeFrame, i);
open2= iOpen(dSymbol, TimeFrame, i+1);
close2= iClose(dSymbol, TimeFrame, i+1);
if(open2<close2 && open1<close1 && open2<open1) {
b++;
if(b==CountBar) {
BufferUPb[i]=Low[i]-200*_Point;
b=1;
}
} else { b=1;}
//---
if(open2>close2 && open1>close1 && open2>open1) {
s++;
if(s==CountBar) {
BufferDNb[i]=High[i]+200*_Point;
s=1;
}
} else { s=1;}
//--------------------------------------------------------------
frDn=iFractals(dSymbol, TimeFrame, MODE_LOWER, i);
if(frDn!=0) {
frDn1=frDn;
}
//-
if(frDn3 > frDn2 && frDn2 < frDn1) {
BufferUPf[i]=Low[i]-200*_Point;
}
if(frDn2!=frDn3 && frDn2!=frDn1) { frDn3=frDn2;} frDn2=frDn1;
//---
frUp=iFractals(dSymbol, TimeFrame, MODE_UPPER, i);
if(frUp!=0) {
frUp1=frUp;
}
//-
if(frUp3 < frUp2 && frUp2 > frUp1) {
BufferDNf[i]=High[i]+200*_Point;
}
if(frUp2!=frUp3 && frUp2!=frUp1) { frUp3=frUp2;} frUp2=frUp1;
//--------------------------------------------------------------
//-
if(/*что сюда*/) {
BufferUPbf[i]=Low[i]-200*_Point;
}
//-
if(/*что сюда*/) {
BufferDNbf[i]=High[i]+200*_Point;
}
//------------
} // end for
//--- return value of prev_calculated for next call
return(rates_total);
}
//--------------------------------------------------------------
The question remains: how to combine all this and output to buffer 5 and 6.
Thank you!
Greetings.
I changed the order selection type in the written function of certain order counting from selection by order to selection by ticket.
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}}return(count);}
int count=0;
if(OrderSelect(ticketUP, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}return(count);}
After that, when running a few dozen candles, everything works correctly and after that there are repeated errors in the log:
2016.12.17 17:44:31.609 2016.12.07 00:27 test3 EURUSD,M1: unknown ticket 2 for OrderModify function
2016.12.17 17:44:31.608 2016.12.07 00:25 test3 EURUSD,M1: OrderModify error 4108 // Invalid ticket number.
Here is the full text, I changed it in all four functions:
double slB, tpB, slS, tpS; double x=0, z=0; int ticketUP, ticketD;
void OnTick()
{
double maxpr1=-9999; double minpr1=9999;
for(int shift1=0; shift1<bars; shift1++)
{double i=iHigh(Symbol(), PERIOD_CURRENT, shift1);
if (i>maxpr1){maxpr1=i;}}
for(int shiftA1=0; shiftA1<bars; shiftA1++)
{double y=iLow(Symbol(), PERIOD_CURRENT, shiftA1);
if (y<minpr1) {minpr1=y;}}
if (BuyLimitCount()==0 && BuyCount()==0){
slB=NormalizeDouble(minpr1-pointsl*Point,5);
tpB=NormalizeDouble(minpr1+pointtp*Point,5);
ticketUP=OrderSend(Symbol(), OP_BUYLIMIT, lotB, minpr1, 3, slB, tpB, "", MagicB, 0, Red);
if (ticketUP==-1) Print("ERROR OP_BUY"); else Print("OP_BUY OK");}
if (SellLimitCount()==0 && SellCount() ==0){
slS=NormalizeDouble(maxpr1+pointsl*Point,5);
tpS=NormalizeDouble(maxpr1-pointtp*Point,5);
ticketD=OrderSend(Symbol(), OP_SELLLIMIT, lotS, maxpr1, 3, slS, tpS, "", MagicS, 0, Blue);
if (ticketD==-1) Print("ERROR OP_SELL"); else Print("OP_SELL OK");}
if (x!=maxpr1){x=maxpr1;
slS=NormalizeDouble(maxpr1+pointsl*Point,5);
tpS=NormalizeDouble(maxpr1-pointtp*Point,5);
OrderModify(ticketD, maxpr1, slS, tpS, 0, Blue);}
if (z!=minpr1){z=minpr1;
slB=NormalizeDouble(minpr1-pointsl*Point,5);
tpB=NormalizeDouble(minpr1+pointtp*Point,5);
OrderModify(ticketUP, minpr1, slB, tpB, 0, Red);}
double maxpr=-9999; double minpr=9999;
for(int shift=0; shift<bars; shift++)
{double e=iHigh(Symbol(), PERIOD_CURRENT, shift);
if (e>maxpr){maxpr=e;}}
for(int shiftA=0; shiftA<bars; shiftA++)
{double r=iLow(Symbol(), PERIOD_CURRENT, shiftA);
if (r<minpr) {minpr=r;}}
string a;
if(bars==1)a="bar: ";
else a= IntegerToString(bars,1) + " bar's: ";
Comment("Last ", a, "max ", DoubleToStr(maxpr, 5), ", min ", DoubleToStr(minpr, 5),".");
}
int BuyLimitCount(){
int count=0;
if(OrderSelect(ticketUP, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}return(count);}
int BuyCount(){
int count=0;
if(OrderSelect(ticketUP, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUY)
count++;}}return(count);}
int SellLimitCount(){
int count=0;
if(OrderSelect(ticketD, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELLLIMIT)
count++;}}return(count);}
int SellCount(){
int count=0;
if(OrderSelect(ticketD, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELL)
count++;}}return(count);}
Why does this happen and how to deal with it?
Greetings.
In the written function for counting certain orders in the order selection function, I changed the selection type from order selection to ticket selection.
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}}return(count);}
And where?
Finished. Thank you for your promptness.
And where? Where's the choice on the ticket?
Right there, in the post above. Or is there something wrong?
int count=0;
if(OrderSelect(ticketUP, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}return(count);}
int BuyCount(){
int count=0;
if(OrderSelect(ticketUP, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUY)
count++;}}return(count);}
int SellLimitCount(){
int count=0;
if(OrderSelect(ticketD, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELLLIMIT)
count++;}}return(count);}
int SellCount(){
int count=0;
if(OrderSelect(ticketD, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELL)
count++;}}return(count);}
Right there, in the post above. Or is there something wrong?
int count=0;
if(OrderSelect(ticketUP, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}return(count);}
int BuyCount(){
int count=0;
if(OrderSelect(ticketUP, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUY)
count++;}}return(count);}
int SellLimitCount(){
int count=0;
if(OrderSelect(ticketD, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELLLIMIT)
count++;}}return(count);}
int SellCount(){
int count=0;
if(OrderSelect(ticketD, SELECT_BY_TICKET, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELL)
count++;}}return(count);}
I advise you to read how to select by ticket. It would be better if you understand what you are doing. I cannot tell you anything else from my mobile.
Been reading, couldn't find any errors (need help from the hall).
And there is a point here - after changing the select function, the order modification function started giving an error and only some time after running the test.
Been reading, couldn't find any errors (need help from the hall).
And there is one point here - after changing the selection function, the order modification function started to generate an error and only some time after the test was started.
Where do you get the order ticket from?
ticketD=OrderSend(Symbol(), OP_SELLLIMIT, lotS, maxpr1, 3, slS, tpS, "", MagicS, 0, Blue);