[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 160

 
extern double SL=100, TP=200, Tral=0, steps=2, Level=100;
extern int TF1=0, TF2=60, TF3=240, per1=14, per2=14, per3=14, shiftbars=20;
double sl, tp, cci[3], lastbar, lastpos=-1, lot=0.1, x=0.0001, calc, price, lb;
int init(){if( SL< Tral) SL= Tral;if(MarketInfo(Symbol(),MODE_DIGITS)==3) x=0.01; TP*= x; SL*= x; Tral*= x; calc=MathPow(2, steps)*0.1;}

double lot(){int l;double lotiki=0.1;bool isit=false;
for(int i=1; i<= steps; i++){
if(!OrderSelect(OrdersHistoryTotal()- i, SELECT_BY_POS, MODE_HISTORY))break;
if(OrderProfit()>0) lotiki*=2; l++;
if(OrderLots()== calc) { isit=true;break;}
if(OrderProfit()<0) break;}
if( l== steps+1 || isit) lotiki=0.1;
return( lotiki);}

int Trailing()
{double Tip, SL1;
for(int i=OrdersTotal()-1; i>=0; i--){
OrderSelect( i, SELECT_BY_POS);if(OrderMagicNumber()==350000){
Tip=OrderType(); SL1=OrderStopLoss();
if( Tip==0) {if(Bid- SL1> Tral) {OrderModify(OrderTicket(),OrderOpenPrice(),Bid- Tral,OrderTakeProfit(),0);break;}}
if( Tip==1) {if( SL1-Ask> Tral) {OrderModify(OrderTicket(),OrderOpenPrice(),Ask+ Tral,OrderTakeProfit(),0);break;}}
}}}

int Closeorders(){
for(int i=OrdersTotal()-1; i>=0; i--){OrderSelect( i, SELECT_BY_POS);if(OrderMagicNumber()==350000)
{if(OrderType()==0) price=Bid;else price=Ask; OrderClose(OrderTicket(),OrderLots(), price,50);break;}}
}
void start(){
//if(iBarShift(NULL,0,lastbar,false)==0)return;
//lastbar=CurTime();
if(OrdersTotal()!=0 && Tral!=0){ Trailing();}
//cci=iCustom(NULL,0,"i-3CCI-h3",CCI_Period_0,Level_0,TF_1,CCI_Period_1,Level_1,TF_2,CCI_Period_2,Level_2,1000,-1,0,0);
cci[0]=iCCI(NULL, TF1, per1,0,1);
cci[1]=iCCI(NULL, TF2, per2,0,0);
cci[2]=iCCI(NULL, TF3, per3,0,0);
string s1="cci-0 *** "+DoubleToStr( cci[0],3);
string s2="cci-1 *** "+DoubleToStr( cci[1],3);
string s3="cci-2 *** "+DoubleToStr( cci[2],3);
string str[3];
str[0]= s1;
str[1]= s2;
str[2]= s3;
color col=Lime;
     for(int y=0; y<3; y++)
     {ObjectCreate("textSignal"+ y,OBJ_LABEL,0,0,0,0,0);    
      ObjectSet("textSignal"+ y, OBJPROP_CORNER,1);
      ObjectSet("textSignal"+ y, OBJPROP_XDISTANCE,35-25);
      ObjectSet("textSignal"+ y, OBJPROP_YDISTANCE, y*(20)+20+8);
      if( cci[ y]>=0) col=Lime;else col=Red;
      if( cci[ y]> Level) col=Aqua;
      if( cci[ y]<- Level) col=DarkViolet;
      ObjectSetText("textSignal"+ y, str[ y],15,"Tahoma", col);
     }
if(OrdersTotal()!=0)return;
if( cci[0]> Level && cci[1]> Level && cci[2]> Level && (/*iBarShift(NULL,0,lb,false)>shiftbars || */ lastpos!=0))
{ Closeorders(); tp=Ask+ TP; sl=Ask- SL;if( TP==0) tp=0;if( SL==0) sl=0;
OrderSend(Symbol(),0, lot(),Ask,50, sl, tp,"i-3CCI-h3_Buy",350000,0); lastpos=0; lb=Time[0];}
if( cci[0]<- Level && cci[1]<- Level && cci[2]<- Level && (/*iBarShift(NULL,0,lb,false)>shiftbars || */ lastpos!=1))
{ Closeorders(); tp=Bid- TP; sl=Ask+ SL;if( TP==0) tp=0;if( SL==0) sl=0;
OrderSend(Symbol(),1, lot(),Bid,50, sl, tp,"i-3CCI-h3_Sell",350000,0); lastpos=1; lb=Time[0];}
}
Please help.

If during the visual testing of the EA 1.mq4 (attached) I put on a chart the indicator 3-cci-h3 (attached), then, as it would seem, with exactly the same parameters of calculated cci signals of the indicator and the actual cci (object text in the upper right corner) do not coincide.

WHY?

Why is it that alerts don't work in this indicator?

Expert Advisor code:





Indicator code:

//+------------------------------------------------------------------+
//| i-3CCI-h.mq4 |
//| johnfantom & kimIV |
//| http://www.kimiv.ru |
//| |
//| 02.01.2006 CCI with 3 TF in one bottle. |
//+------------------------------------------------------------------+
#property copyright "johnfantom & KimIV"
#property link "http://www.kimiv.ru"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
#property indicator_maximum 1.4
#property indicator_level1 0
#property indicator_minimum -1.2

//------- External indicator parameters -------------------------------
extern int CCI_Period_0 = 14; // CCI period for the current timeframe
extern int Level_0 = 100; // CCI Level for the current TF
extern int TF_1 = 60; // Number of minutes in the first timeframe
extern int CCI_Period_1 = 14; // CCI Period for the first TF
extern int Level_1 = 100; // CCI Level for the first TF
extern int TF_2 = 240; // Number of minutes of the second TF
extern int CCI_Period_2 = 14; // CCI Period for the second TF
extern int Level_2 = 100; // CCI level for the second TF
extern int NumberOfBars = 1000; // Number of bars (0-All)
extern int ExtBars = 3;
int per;


//------- Indicator buffers ------------------------------------------
double buf0[];

//+------------------------------------------------------------------+
//| Custom indicator initialisation function |
//+------------------------------------------------------------------+
void init() {
IndicatorDigits(1);

SetIndexBuffer(0, buf0);
SetIndexLabel(0, "i-3CCI-h");
SetIndexStyle (0, DRAW_HISTOGRAM, STYLE_SOLID, 2);
SetIndexEmptyValue(0, 0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void deinit() {
Comment(");
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
void start() {
double cci0,cci1,cci2;
int nb1, nb2; count;
int LoopBegin, sh;


if (NumberOfBars==0) LoopBegin=Bars-1;
else LoopBegin=NumberOfBars-1;
LoopBegin=MathMin(Bars-1, LoopBegin);
count=IndicatorCounted();
for (sh=count; sh>=0; sh--) {
nb1=iBarShift(NULL, TF_1, Time[sh+1], False);
nb2=iBarShift(NULL, TF_2, Time[sh+1], False);

cci0=iCCI(NULL, 0, CCI_Period_0, PRICE_CLOSE, sh+1);
cci1=iCCI(NULL, TF_1, CCI_Period_1, PRICE_CLOSE, nb1);
cci2=iCCI(NULL, TF_2, CCI_Period_2, PRICE_CLOSE, nb2);
/* double cci[3];
cci[0]=cci0;
cci[1]=cci1;
cci[2]=cci2;
string s1="cci-0 ***"+DoubleToStr(cci[0],3);
string s2="cci-1 ***"+DoubleToStr(cci[1],3);
string s3="cci-2 ***"+DoubleToStr(cci[2],3);
string str[3];
str[0]=s1;
str[1]=s2;
str[2]=s3;
color col=Lime;
for(int y=0;y<3;y++)
{ObjectCreate("textSignal "+y,OBJ_LABEL,0,0,0,0)
ObjectSet("textSignal "+y,OBJPROP_CORNER,1);
ObjectSet("textSignal "+y,OBJPROP_XDISTANCE,35-25);
ObjectSet("textSignal "+y,OBJPROP_YDISTANCE,y*(20)+20+8);
if(cci[y]>=0)col=Lime;else col=Red;
if(cci[y]>Level_0) col=Aqua;
if(cci[y]<-Level_0) col=DarkViolet;
ObjectSetText("textSignal "+y,str[y],15, "Tahoma",col);
}
*/
if (cci0>Level_0 && cci1>Level_1 && cci2>Level_2) {buf0[sh]=1;
if (per != Time[0]) {per = Time[0];
if(sh<=ExtBars && ExtBars!=-1)
Alert(Symbol(),Period()," ",
TimeYear(Time[sh]),",
TimeDay(Time[sh]),",
TimeMonth(Time[sh]),",
TimeHour(Time[sh]),":",
TimeMinute(Time[sh]),",
"Purchase");}}
if (cci0<-Level_0 && cci1<-Level_1 && cci2<-Level_2) {buf0[sh]=-1;
if (per != Time[0]) {per = Time[0];
if(sh<=ExtBars && ExtBars!=-1)
Alert(Symbol(),Period()," ",
TimeYear(Time[sh]),",
TimeDay(Time[sh]),",
TimeMonth(Time[sh]),",
TimeHour(Time[sh]),":",
TimeMinute(Time[sh])," ",
"Sale");}}
}
}
//+------------------------------------------------------------------+
 

Try it on your own. I can't get it to work at all. It's not working at all.

The lines are supposed to line up. But in practice it's a little different.

int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   ObjectCreate("fibowhite",OBJ_TRENDBYANGLE,0,0,0,0,0);
   //ObjectSet("fibowhite",OBJPROP_ANGLE,angle_);
   ObjectSet("fibowhite", OBJPROP_TIME1,Time[5]);
   ObjectSet("fibowhite", OBJPROP_TIME2,Time[1]);
   ObjectSet("fibowhite", OBJPROP_PRICE1,Low[5]);
   ObjectSet("fibowhite", OBJPROP_PRICE2,Low[1]);
   ObjectSet("fibowhite", OBJPROP_COLOR,Red);
   //----------------------------------------------------------
   ObjectCreate("fibowhite1",OBJ_TRENDBYANGLE,0,0,0,0,0);
   ObjectSet("fibowhite1", OBJPROP_TIME1,Time[5]);
   //ObjectSet("fibowhite1",OBJPROP_TIME2,Time[1]);
   ObjectSet("fibowhite1", OBJPROP_PRICE1,Low[5]);
   //ObjectSet("fibowhite1",OBJPROP_PRICE2,Low[1]);
   ObjectSet("fibowhite1", OBJPROP_COLOR,Green);
   WindowRedraw();
   double awd=ObjectGet("fibowhite", OBJPROP_ANGLE);
   Comment("AngleRED= ",ObjectGet("fibowhite", OBJPROP_ANGLE),"\n AngleWHITE= ",ObjectGet("fibowhite1", OBJPROP_ANGLE));
   ObjectSet("fibowhite1", OBJPROP_ANGLE,ObjectGet("fibowhite", OBJPROP_ANGLE));
   
//----
   return(0);
  }
 

Can you please tell me what could be wrong?

I made a recursive function that writes a value to the buffer.

When called from indictor works fine, when I tried to put it in EA, the answer is 0

What may be the problem?

 
Andrej78 писал(а) >>

Can you please tell me what could be wrong?

I made a recursive function that writes a value to the buffer.

When called from indictor works fine, when I tried to put it in EA, the answer is 0

What may be the problem?

maybe the buffer size is not set!?

 

I found where the hitch occurs.

It turns out that IndicatorCounted gives 0 in the indicator and -1 in the Expert Advisor (this function has check for correctness - to be >=0, hence it is not considered in the Expert Advisor)

What is the reason and how to fix the error?

MT4 build 225, eurusd M1.

 
Andrej78 писал(а) >>

I found where the hitch occurs.

It turns out that IndicatorCounted gives 0 in the indicator and -1 in the Expert Advisor (this function has check for correctness - to be >=0, hence it is not considered in the Expert Advisor)

What is the reason and how to fix the error?

MT4 build 225, eurusd M1.

https://docs.mql4.com/ru/customind

 

if (k>k1+7 && s<(High[1]-(High[1]-Low[1])/2) && bay==true)
{
int t=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,NULL,NULL,"My order #",16384,0,Green);
bay=false;
Print (GetLastError());
}
}
if (s>(High[0]-(High[0]-Low[0])/2) && bay==false)
{
OrderSelect(t,SELECT_BY_TICKET,MODE_TRADES);
Print (GetLastError());
OrderClose(t,0.1,Ask,5,Red);
}

What is the error in the logging code 4051 not right ticket when closing an order please advise ?
 
evgenio писал(а) >>

if (k>k1+7 && s<(High[1]-(High[1]-Low[1])/2) && bay==true)
{
int t=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,NULL,NULL,"My order #",16384,0,Green);
bay=false;
Print (GetLastError());
}
}
if (s>(High[0]-(High[0]-Low[0])/2) && bay==false)
{
OrderSelect(t,SELECT_BY_TICKET,MODE_TRADES);
Print (GetLastError());
OrderClose(t,0.1,Ask,5,Red);
}

What is the bug in the logging code 4051 not a valid ticket when closing an order please advise?

The opening and closing takes place on different ticks. And the Expert Advisor no longer remembers what was once assigned to the variable t. Either it should be global or static.

A global variable is defined outside of functions. Or it can be declared as static int

 
Vinin >> :

The opening and closing takes place on different ticks. And the Expert Advisor no longer remembers what was once assigned to the variable t. Either it should be global or static.

A global variable is defined outside of functions. Or it can be declared as static int

thanks a lot, I forgot ))

 

In the Indicator I set (to then compare them with Time[i]):

extern datetime StartRange          = D'1970.12.24';
extern datetime EndRange            = D'2020.12.24';


Then in the loop

   Comment(
      "\n", "Time[i]                = "   , Time[ i],
      "\n", "StrToTime(StartRange)  = "   , StrToTime( StartRange),
      "\n", "StrToTime(EndRange)    = "   , StrToTime( EndRange),
   "\n" );


As a result, StrToTime(StartRange) and StrToTime(EndRange) produce Zeros .

- Why and how to fix it?

Reason: