This is an indicator I am working on. The problem I am having is it doesn't match the same output as my excel file, and I am pretty sure my excel file is correct. If you would like to help me solve this issue just email me at thetfordjw#gmail.com Subject: Excel mql4 please and I will send you the excel file. Otherwise if you have something to add on this web page as to something I am doing wrong would be happy to hear from you. Thanks !
//+------------------------------------------------------------------+
//| JT-Statistics Close GT LT Terminated.mq4 |
//| Copyright 2021,Jon W. Thetford |
//| Email: thetfordjw#gmail.com |
//+------------------------------------------------------------------+
#property version "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int i;
float a,b,c,d,e,f,g,h,j,k;
float aa,bb,cc,dd,ee,ff,gg,hh,jj,kk,ll,mm,nn,oo;
//input float BarsBack = "1000";
input bool FrequencyNumber = True;
input bool FrequencyPercentage = False;
input bool CumulativePercentageOfAllBars = False;
//input bool PercentageOfCustomBarsBack = False;
int OnInit()
{
//--- indicator buffers mapping
//---
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[])
{
//---
for(i=0;i<Bars; i++)
{
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]<Close[i+4])
{
a=a+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]<Close[i+5])
{
b=b+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]>Close[i+5]&&Close[i+5]<Close[i+6])
{
c=c+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]>Close[i+5]&&Close[i+5]>Close[i+6]&&Close[i+6]<Close[i+7])
{
d=d+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]>Close[i+5]&&Close[i+5]>Close[i+6]&&Close[i+6]>Close[i+7]&&Close[i+7]<Close[i+8])
{
e=e+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]>Close[i+5]&&Close[i+5]>Close[i+6]&&Close[i+6]>Close[i+7]&&Close[i+7]>Close[i+8]&&Close[i+8]<Close[i+9])
{
f=f+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]>Close[i+5]&&Close[i+5]>Close[i+6]&&Close[i+6]>Close[i+7]&&Close[i+7]>Close[i+8]&&Close[i+8]>Close[i+9]&&Close[i+9]<Close[i+10])
{
g=g+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]>Close[i+5]&&Close[i+5]>Close[i+6]&&Close[i+6]>Close[i+7]&&Close[i+7]>Close[i+8]&&Close[i+8]>Close[i+9]&&Close[i+9]>Close[i+10]&&Close[i+10]<Close[i+11])
{
h=h+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]>Close[i+5]&&Close[i+5]>Close[i+6]&&Close[i+6]>Close[i+7]&&Close[i+7]>Close[i+8]&&Close[i+8]>Close[i+9]&&Close[i+9]>Close[i+10]&&Close[i+10]>Close[i+11]&&Close[i+11]<Close[i+12])
{
j=j+1;
}
if(Close[i+1]<Close[i+2]&&Close[i+2]>Close[i+3]&&Close[i+3]>Close[i+4]&&Close[i+4]>Close[i+5]&&Close[i+5]>Close[i+6]&&Close[i+6]>Close[i+7]&&Close[i+7]>Close[i+8]&&Close[i+8]>Close[i+9]&&Close[i+9]>Close[i+10]&&Close[i+10]>Close[i+11]&&Close[i+11]>Close[i+12]&&Close[i+12]<Close[i+13])
{
k=k+1;
}
//LESS THAN Close[1]<Close[2]
//1
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]>Close[i+4])
{
aa=aa+1;
}
//2
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]>Close[i+5])
{
bb=bb+1;
}
//3
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]>Close[i+6])
{
cc=cc+1;
}
//4
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]>Close[i+7])
{
dd=dd+1;
}
//5
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]>Close[i+8])
{
ee=ee+1;
}
//6
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]<Close[i+8]&&Close[i+8]>Close[i+9])
{
ff=ff+1;
}
//7
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]<Close[i+8]&&Close[i+8]<Close[i+9]&&Close[i+9]>Close[i+10])
{
gg=gg+1;
}
//8
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]<Close[i+8]&&Close[i+8]<Close[i+9]&&Close[i+9]<Close[i+10]&&Close[i+10]>Close[i+11])
{
hh=hh+1;
}
//9
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]<Close[i+8]&&Close[i+8]<Close[i+9]&&Close[i+9]<Close[i+10]&&Close[i+10]<Close[i+11]&&Close[i+11]>Close[i+12])
{
jj=jj+1;
}
//10
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]<Close[i+8]&&Close[i+8]<Close[i+9]&&Close[i+9]<Close[i+10]&&Close[i+10]<Close[i+11]&&Close[i+11]<Close[i+12]&&Close[i+12]>Close[i+13])
{
kk=kk+1;
}
//11
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]<Close[i+8]&&Close[i+8]<Close[i+9]&&Close[i+9]<Close[i+10]&&Close[i+10]<Close[i+11]&&Close[i+11]<Close[i+12]&&Close[i+12]<Close[i+13]&&Close[i+13]>Close[i+14])
{
ll=ll+1;
}
//12
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]<Close[i+8]&&Close[i+8]<Close[i+9]&&Close[i+9]<Close[i+10]&&Close[i+10]<Close[i+11]&&Close[i+11]<Close[i+12]&&Close[i+12]<Close[i+13]&&Close[i+13]<Close[i+14]&&Close[i+14]>Close[i+15])
{
mm=mm+1;
}
//13
if(Close[i+1]>Close[i+2]&&Close[i+2]<Close[i+3]&&Close[i+3]<Close[i+4]&&Close[i+4]<Close[i+5]&&Close[i+5]<Close[i+6]&&Close[i+6]<Close[i+7]&&Close[i+7]<Close[i+8]&&Close[i+8]<Close[i+9]&&Close[i+9]<Close[i+10]&&Close[i+10]<Close[i+11]&&Close[i+11]<Close[i+12]&&Close[i+12]<Close[i+13]&&Close[i+13]<Close[i+14]&&Close[i+14]<Close[i+15]&&Close[i+15]>Close[i+16])
{
nn=nn+1;
}
//=
if(Close[i+1]==Close[i+2])
{
oo=oo+1;
}
//OUTPUT > <
//Frequency NUMBER of consecutive closes greater than and less than the previous.
if(FrequencyNumber) Comment("FrequencyNumber\n# Close[1]>Close[2]\n\n1: "+a+"\n2: "+b+"\n3: "+c+"\n4: "+d+"\n5: "+e+"\n6: "+f+"\n7: "+g+"\n8: "+h+"\n9: "+j+"\n10: "+k
+"\n\n# Close[1]<Close[2]\n\n1: "+aa+"\n2: "+bb+"\n3: "+cc+"\n4: "+dd+"\n5: "+ee+"\n6: "+ff+"\n7: "+gg+"\n8: "+hh+"\n9: "+jj+"\n10: "+kk+"\n11: "+ll+"\n12: "+mm+"\n13: "+nn+"\n==: "+oo+"\n\n Total bars: "+Bars);
}
Related
I just moved from mql4 to mql5 and now i am trying to read buffers values from an indicator , but it only shows me 0 instead of the actual buffer values
Here is my code :
#property version "1.00"
int handle1 = 0;
double valu1,valu2,valu3,valu4;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(60);
handle1 = iCustom(NULL, NULL, "LTD by KDMfx");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
IndicatorRelease(handle1);
}
//+------------------------------------------------------------------+
//| Get indicator value |
//+------------------------------------------------------------------+
double GetIndicator(int handle, int buffer_num, int index)
{
double arr[];
if(CopyBuffer(handle, buffer_num, 0, index+1, arr) <= 0)
{
Sleep(200);
for(int i=0; i<100; i++)
{
if(BarsCalculated(handle) > 0)
break;
Sleep(50);
}
int copied = CopyBuffer(handle, buffer_num, 0, index+1, arr);
if(copied <= 0)
{
Print("CopyBuffer failed. Maybe history has not download yet? Error = ", GetLastError());
return -1;
}
else
return (arr[index]);
}
return 0;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
//---
valu1 = GetIndicator(handle1,0,4);
valu2 = GetIndicator(handle1,1,4);
valu3 = GetIndicator(handle1,2,4);
valu4 = GetIndicator(handle1,3,4);
Comment("b1: ", valu1,
"\nb2: ", valu2,
"\nb3: ", valu3,
"\nb4: ", valu4
);
}
what am i doing wrong here ?
the indicator does not appear on the current or the last closed candle , it appears on the current+4 candle , meaning on the 5th candle in past . so i am using "4" as the candle id but still no use , no matter what i try i can get it to work
Try simplifying things a bit. I don't have a copy of the indicator you are trying to read, but the following code should work
#property version "1.10"
#property strict
int handle;
//+------------------------------------------------------------------+
//| Initialization function of the expert |
//+------------------------------------------------------------------+
int OnInit()
{
handle=iCustom(_Symbol, 0, "LTD by KDMfx");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Deinitialization function of the expert |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(handle);
}
//+------------------------------------------------------------------+
//| "Tick" event handler function |
//+------------------------------------------------------------------+
void OnTick()
{
double LTDindicator[];
ArraySetAsSeries(LTDindicator, true);
CopyBuffer(handle, 0, 0, 25, LTDindicator);
Comment(LTDindicator[0]);
return;
}
Everything seems fine. But EA usually opens multiple trades in the same second... The way I built it is very linear and i can't seem to spot the logical mistake. It is basically a random martingale EA to test stuff out. Any indicator (that's why I called it random, haven't decided myself) can be put in there.
Basic idea is that it has an upper and lower threshold which determines when it is in buy zone and when at sell zone. Once it is in either zone, if trend goes against it (determined by indicator's value, not symbol's price) it opens another trade with the same SL/TP of the initial order. Also it checks whether initial trade still runs so it does not open other ones and once the initial trade is open. After that the criterias about the rest of the trades (that go against the trade are different).
The problem is that it opens multiple trades at times that it shouldn't, or like 3-4 trades within the same second or two. Any idea why this happens?
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
input int stepValue = 5;
input double lotsize = 0.01;
input int stoploss = 2000;
input int takeprofit = 140;
input int slippage = 10;
input double upper_border = 60.0;
input double lower_border = 40.0;
const string EAComment = "Xind";
string mode = "";
bool first_trade = false;
int InitTicket = 1;
double X = 0.0;
double X_Last = 0.0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
first_trade = false;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
SearchSignal();
if (mode == "Buy")
{
if (first_trade == false)
{
Buy();
}
if (first_trade == true)
{
MartinCheck();
CloseCheck();
}
}
if (mode == "Sell")
{
if (first_trade == false)
{
Sell();
}
if (first_trade == true)
{
MartinCheck();
CloseCheck();
}
}
}
//+------------------------------------------------------------------+
void Buy()
{
X_Last = X;
first_trade = true;
InitTicket = OrderSend(NULL,OP_BUY,lotsize,Ask,slippage,Ask-stoploss*Point,Ask+takeprofit*Point,EAComment,1,0,clrDarkBlue);
}
//---
void Sell()
{
X_Last = X;
first_trade = true;
InitTicket = OrderSend(NULL,OP_SELL,lotsize,Bid,slippage,Bid+stoploss*Point,Bid-takeprofit*Point,EAComment,1,0,clrDarkRed);
}
//---
void MartinBuy()
{
if (OrderSelect(InitTicket, SELECT_BY_TICKET) == true)
{
double new_SL = OrderStopLoss();
double new_TP = OrderTakeProfit();
int dont_care = OrderSend(NULL,OP_BUY,lotsize,Ask,slippage,new_SL,new_TP,EAComment+" martin",1,0,clrDarkBlue);
}
}
//---
void MartinSell()
{
if (OrderSelect(InitTicket, SELECT_BY_TICKET) == true)
{
double new_SL = OrderStopLoss();
double new_TP = OrderTakeProfit();
int dont_care = OrderSend(NULL,OP_SELL,lotsize,Bid,slippage,new_SL,new_TP,EAComment+" martin",1,0,clrDarkRed);
}
}
//---
void SearchSignal()
{
X = 0.0; //where 0.0, put here the iCustom for external indicators, or some built-in indicator
if (X >= upper_border)
{
mode = "Sell";
}
else if (X <= lower_border)
{
mode = "Buy";
}
else
{
mode = "";
first_trade = false;
InitTicket = 1;
X_Last = 0.0;
}
}
//---
void CloseCheck()
{
if (OrderSelect(InitTicket, SELECT_BY_TICKET))
{
if (OrderCloseTime() == 0)
{
first_trade = true;
}
else if (OrderCloseTime() != 0)
{
first_trade = false;
}
else
{
return;
}
}
}
//---
void MartinCheck()
{
if (mode == "Buy")
{
if ((X_Last - stepValue) >= X)
{
X_Last = X;
MartinBuy();
}
}
if (mode == "Sell")
{
if ((X_Last + stepValue) <= X)
{
X_Last = X;
MartinSell();
}
}
}
The layout of your code makes it possible for several processes to happen in sequence all on the same tick which I assume you do not want. Try changing your code initially to this and work from there:
void OnTick()
{
SearchSignal();
if(mode=="Buy")
{
if(!first_trade) Buy();
else
{
MartinCheck();
CloseCheck();
}
}
else if(mode=="Sell")
{
if(!first_trade) Sell();
else
{
MartinCheck();
CloseCheck();
}
}
}
Remember to use if(...) else to stop executing all functions when it should only be an either/or situation.
I'm making a line of on-chart buttons that will launch specific functions and indicators. So far so good the buttons load an indicator when pressed and delete it when the button is unpressed.
But I would like to add two more functions to the button but I'm doing something wrong and it doesn't work properly and I need some help.
1st - I would like the button do be able to detect indicator on chart an automatically set the button to "ON" state in case I will drag and drop the indicator from indicator list.
2nd - when I press the button it will launch an indicator popup window and we have 2 options;
1 - "OK" which will launch the indicator on the chart and the button will stay in "ON" state.
2 - "CANCEL" which will abort lunching the indicator but it will leave the button in "ON" state. I would like the button to go back to the "OFF" state just after I press "CANCEL".
Here is a peace of the code for testing.
#property indicator_chart_window
string sButtonName1 ="B1";
string sButtonName2 ="B2";
const string IndiName1 ="X";
const string IndiName2 ="Y";
//--------------------------------------------------------------
color UnloadedColor=clrGray;
color LoadedColor=clrBlue;
color currentColor;
//--------------------------------------------------------------
#import "user32.dll"
void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);
#define VK_L 0x4C
#define VK_M 0x4D
#define VK_MENU 0x12
#define KEYEVENTF_KEYUP 0x0002
#import
//+------------------------------------------------------------------+
int OnInit()
{
int buttonwindow=ObjectFind(sButtonName1);
int buttonwindow1=ObjectFind(sButtonName2);
{
if(buttonwindow<0)
CreateButton1(sButtonName1,UnloadedColor,"Arial",0);
if(buttonwindow1<0)
CreateButton2(sButtonName2,UnloadedColor,"Arial",0);
}
return(INIT_SUCCEEDED);
}
//+----------------------------------------------------------------------------------+
//|------------------------CreateButton 1--------------------------------------------|
//+----------------------------------------------------------------------------------+
void CreateButton1(string sName1,color sColor,string sFont="Arial",string sText="")
{
ObjectCreate(0,sName1,OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,sName1,OBJPROP_XDISTANCE,725);
ObjectSetInteger(0,sName1,OBJPROP_YDISTANCE,0);
ObjectSetInteger(0,sName1,OBJPROP_XSIZE,50);
ObjectSetInteger(0,sName1,OBJPROP_YSIZE,50);
ObjectSetInteger(0,sName1,OBJPROP_CORNER,4);
ObjectSetString(0,sName1,OBJPROP_TEXT,"X");
ObjectSetInteger(0,sName1,OBJPROP_COLOR,sColor);
ObjectSetString(0,sName1,OBJPROP_FONT,sFont);
ObjectSetInteger(0,sName1,OBJPROP_FONTSIZE,26);
ObjectSetInteger(0,sName1,OBJPROP_STATE,false);
}
//+----------------------------------------------------------------------------------+
//|------------------------CreateButton 2--------------------------------------------|
//+----------------------------------------------------------------------------------+
void CreateButton2(string sName2,color sColor,string sFont="Arial",string sText="")
{
ObjectCreate(0,sName2,OBJ_BUTTON,0,0,0);
ObjectSetInteger(0,sName2,OBJPROP_XDISTANCE,775);
ObjectSetInteger(0,sName2,OBJPROP_YDISTANCE,0);
ObjectSetInteger(0,sName2,OBJPROP_XSIZE,50);
ObjectSetInteger(0,sName2,OBJPROP_YSIZE,50);
ObjectSetInteger(0,sName2,OBJPROP_CORNER,4);
ObjectSetString(0,sName2,OBJPROP_TEXT,"Y");
ObjectSetInteger(0,sName2,OBJPROP_COLOR,sColor);
ObjectSetString(0,sName2,OBJPROP_FONT,sFont);
ObjectSetInteger(0,sName2,OBJPROP_FONTSIZE,26);
ObjectSetInteger(0,sName2,OBJPROP_STATE,false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//+----------------------------------------------------------------------------------+
//+--------------------------------------- BUTTON 1 ---------------------------------+
//+----------------------------------------------------------------------------------+
{
{
if(id==CHARTEVENT_OBJECT_CLICK && sparam==sButtonName1)
{
currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR);
if(currentColor==UnloadedColor)
{
keybd_event(VK_MENU,0,0,0);
keybd_event(VK_L,0,0,0);
keybd_event(VK_L,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor);
return;
}
else
if(currentColor==LoadedColor)
{
ChartIndicatorDelete(0,0,IndiName1);
ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor);
}
}
}
//+----------------------------------------------------------------------------------+
//+---------------------------------------BUTTON 2-----------------------------------+
//+----------------------------------------------------------------------------------+
{
if(id==CHARTEVENT_OBJECT_CLICK && sparam==sButtonName2)
{
currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR);
if(currentColor==UnloadedColor)
{
keybd_event(VK_MENU,0,0,0);
keybd_event(VK_M,0,0,0);
keybd_event(VK_M,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,LoadedColor);
return;
}
else
if(currentColor==LoadedColor)
{
ChartIndicatorDelete(0,0,IndiName2);
ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,UnloadedColor);
}
}
}
}
/*
//+-------------------------------------NOT WORKING-----------------------------------+
//------------------------------ DETECT INDICATOR ON THE CHART-------------------------------
{
int indicators_total = ChartIndicatorsTotal(0,0);
for(int i = 0; indicators_total > i; i++)
//+------------------------- INDICATOR 1 DETECTED -----------------------------------
{
if(ChartIndicatorName(0,0,i)==IndiName1)
{
currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR);
if(currentColor==UnloadedColor)
{
ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,true);
ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,LoadedColor);
}
}
//+------------------------- INDICATOR 1 NOT DETECTED -----------------------------------
if(!(ChartIndicatorName(0,0,i)==IndiName1))
{
currentColor=ObjectGetInteger(0,sButtonName1,OBJPROP_COLOR);
if(currentColor==LoadedColor)
{
ObjectSetInteger(0,sButtonName1,OBJPROP_STATE,false);
ObjectSetInteger(0,sButtonName1,OBJPROP_COLOR,UnloadedColor);
}
}
//+------------------------- INDICATOR 2 DETECTED-----------------------------------
if(ChartIndicatorName(0,0,i)==IndiName2)
{
currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR);
if(currentColor==UnloadedColor)
{
ObjectSetInteger(0,sButtonName2,OBJPROP_STATE,true);
ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,LoadedColor);
}
}
//+------------------------- INDICATOR 2 NOT DETECTED -----------------------------------
if(!(ChartIndicatorName(0,0,i)==IndiName2))
{
currentColor=ObjectGetInteger(0,sButtonName2,OBJPROP_COLOR);
if(currentColor==LoadedColor)
{
ObjectSetInteger(0,sButtonName2,OBJPROP_STATE,false);
ObjectSetInteger(0,sButtonName2,OBJPROP_COLOR,UnloadedColor);
}
}
}
}
//+-------------------------------------------------------------------------------------
*/
}
//+------------------------------------------------------------------+
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[])
{
return(rates_total);
}
//+------------------------------------------------------------------+
I have a angle checking mql4 source but it have array out of range error.
I can't figure out what is the problem.
I can guess the problem comes from a loop but can't fix the error.
I did several modifications like i+1 or so where possibly can fix the error but to no success.
I'm guessing the error comes from here: MALast= varMA[i+nb_0];
anyone enlighten me much appreciate
#property indicator_chart_window
#property indicator_color1 CLR_NONE
#property indicator_buffers 3
#property indicator_color2 CLR_NONE
extern int ma_per = 10;
extern int ma_met = 3;
extern color col_ang = clrYellow;
extern int ExtDepth = 21;
extern int ext = 0;
extern int Complect = 0;
double varMA[], value[];
//-------------------------------------------------------------------------
int init()
{
IndicatorBuffers(3);
IndicatorDigits(Digits+1);
SetIndexBuffer(0,varMA);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(1, value);
return(0);
}
//-------------------------------------------------------------------------
int deinit()
{
ObjectDelete("AngleA "+Complect);
ObjectDelete("AngleText "+Complect);
Comment("");
return(0);
}
//-------------------------------------------------------------------------
int start()
{
int i, limit, counted_bars=IndicatorCounted();
limit = Bars-counted_bars-1;
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
double MALast, MACurr;
for(i=limit; i >=0; i--)
{
varMA[i] = iMA(NULL,0,ma_per,0,ma_met,PRICE_CLOSE,i);
int nb_0 = GetZZbar(ext);
MALast= varMA[i+nb_0];
MACurr= varMA[i+1];
if (ObjectFind("AngleA "+Complect)<0)
{
ObjectCreate("AngleA "+Complect,OBJ_TRENDBYANGLE,0,Time[i+nb_0],MALast,Time[i+1],MACurr);
ObjectSet("AngleA "+Complect,OBJPROP_STYLE,2);
}
if (ObjectFind("AngleText "+Complect)==-1)
ObjectCreate("AngleText "+Complect,OBJ_TEXT,0,0,0);
ObjectSet("AngleA "+Complect,OBJPROP_TIME2,Time[i+1]);
ObjectSet("AngleA "+Complect,OBJPROP_PRICE2,MACurr);
ObjectSet("AngleA "+Complect,OBJPROP_TIME1,Time[i+nb_0]);
ObjectSet("AngleA "+Complect,OBJPROP_PRICE1,MALast);
ObjectSet("AngleA "+Complect,OBJPROP_COLOR,col_ang);
value[i] = (double)ObjectGet("AngleA "+Complect,OBJPROP_ANGLE);
if(value[i]>90)
value[i] = value[i]-360;
value[i+1] = value[i];
ObjectSetText("AngleText "+Complect,DoubleToStr(value[i],2),8,"Verdana",col_ang);
ObjectMove("AngleText "+Complect,0,Time[0]+8*Period()*60,ObjectGetValueByShift("AngleA "+Complect,i));
}
Comment(value[0]);
return(0);
}
//-------------------------------------------------------------------------
int GetZZbar(int ne=0)
{
double zz;
int j, k=iBars(NULL, 0), ke=0;
for (j =1; j <k; j++)
{
zz=iCustom(NULL, 0, "ZigZag", ExtDepth,5,3, 0,j);
if (zz !=0)
{
ke++;
if (ke > ne)
return(j);
}
}
return(-1);
}
//-------------------------------------------------------------------------
I want to call OnChartEvent() from init() like the code below, so the EA should process first the password and then the rest of the code.
I am just newbie not an expert in coding.
The idea or objective, the password must enter first and correctly, after successful then process the rest of code.
#include <ChartObjects/ChartObjectsTxtControls.mqh>
int init()
{
password_edit.Create(0, "password_edit", 0, 10, 10, 260, 25);
password_edit.BackColor(White);
password_edit.BorderColor(Black);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent( const int id,
const long &lparam,
const double &dparam,
const string &sparam
)
{
//---
if (id == CHARTEVENT_OBJECT_ENDEDIT && sparam == "password_edit" )
{
password_status = -1;
for (int i=0; i<ArraySize(allowed_passwords); i++)
if (password_edit.GetString(OBJPROP_TEXT) == allowed_passwords[i])
{
password_status = i;
break;
}
if (password_status == -1)
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]);
ExpertRemove();
}
else
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[1]);
}
}
}
//+------------------------------------------------------------------+
int OnInit(){
passwordOperation();
return INIT_SUCCEED;
}
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
if (id == CHARTEVENT_OBJECT_ENDEDIT && sparam == "password_edit" )
{
passwordOperation();
}
}
void passwordOperation()
{
password_status = -1;
for (int i=0; i<ArraySize(allowed_passwords); i++)
if (password_edit.GetString(OBJPROP_TEXT) == allowed_passwords[i])
{
password_status = i;
break;
}
if (password_status == -1)
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]);
ExpertRemove();
}
else
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[1]);
}
}