Questions from a "dummy" - page 186

 
Yedelkin:
Not quite. For for statement, you need to specify the type of variable i. To select a position - first use PositionGetSymbol(i), then - look through the properties of the selected position.

Thank you. Right?

int TotalBullPositions()
{
  int Counter=0;
  for(int i = 0; i < PositionsTotal(); i++)
  {
    if(PositionSelect(Symbol()))
    {
      if(PositionGetInteger(POSITION_MAGIC)==Magic && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) 
      Counter++;}}
  return(Counter);
}
 
G001:

Thank you. Right?

Yeah, that's pretty much the pattern. This line right here.

PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY

the compiler will most likely generate a warning. It can be fixed by explicitly casting the PositionGetInteger(POSITION_TYPE) to the required enum type.

This line

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

some programmers prefer to run in descending order instead of ascending one.

Addendum. Why did you change the correct line to if(PositionSelect(Symbol()))?

 
Yedelkin:

Yes, that's roughly the pattern. This line right here.

the compiler will most likely generate a warning message. It can be treated by explicitly casting the PositionGetInteger(POSITION_TYPE) to the required enum type.

This line

some people prefer to run in descending rather than ascending order.

Addendum. Why did you change the correct line to if(PositionSelect(Symbol()))?

Thank you very much.

if(PositionSelect(Symbol()))

Changed to:

PositionGetSymbol(i)==Symbol()

Or I'll change it to this one:

PositionSelect(PositionGetSymbol(i))

I'll check which option works.

Thank you.

 
G001: Swapped to:

Or I'll switch to this one:

I'll see which one works.

Just read the description of the PositionGetSymbol() function . :)
 
Thank you very much. Everything is already working.

Now I have to take the signals from the indicator:

//+------------------------------------------------------------------+ 
//|                                                      MACDATR.mq5 | 
//|                                      Copyright © 2011, Svinozavr | 
//+------------------------------------------------------------------+ 
//---- Indicator settings
#property indicator_separate_window 
#property indicator_buffers 4 
#property indicator_plots   4
#property indicator_level1 +0.0005
#property indicator_level2 -0.0005
#property indicator_levelcolor DimGray
#define RESET 0
//-----
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 Gray
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_label1 "MACD"
//-----
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_color2 Green
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
#property indicator_label2 "Bull"
//-----
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_color3 Red
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
#property indicator_label3 "Bear"
//-----
#property indicator_type4 DRAW_LINE
#property indicator_color4 Olive
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
#property indicator_label4 "ATR"
//-----
//----- Indicator parameters
//+------------------------------------------------------------------+
input uint FastEMA      = 12;
input uint SlowEMA      = 26;
input uint SignalEMA = 9;
input int  ATRG         = 0;
input ENUM_APPLIED_PRICE AppliedPrice=PRICE_CLOSE;
//+------------------------------------------------------------------+
//-----
double ATRmin=0;
double kATR=1;
int min_rates_total;
int ATRHandle,MACDHandle;
double MACDBuffer[],ATRBuffer[],Bull[],Bear[];
//+------------------------------------------------------------------+    
//| MACD indicator initialization function                           | 
//+------------------------------------------------------------------+  
void OnInit()
{
//-----
  if(ATRG) min_rates_total=int(MathMax(FastEMA,SlowEMA)+ATRG);
  else min_rates_total=2*int(MathMax(FastEMA,SlowEMA));
//-----
  int ATR;
  if(!ATRG) ATR=int(SlowEMA); 
  else ATR=ATRG;
  ATRmin*=_Point;
//-----
  ATRHandle=iATR(NULL,0,ATR);
  if(ATRHandle==INVALID_HANDLE)Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà ATR");
//-----
  MACDHandle=iMACD(NULL,0,FastEMA,SlowEMA,SignalEMA,AppliedPrice);
  if(MACDHandle==INVALID_HANDLE)Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà MACD");
//-----
  SetIndexBuffer(0,MACDBuffer,INDICATOR_DATA);
  PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
  ArraySetAsSeries(MACDBuffer,true);
//-----
  SetIndexBuffer(1,Bull,INDICATOR_DATA);
  PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
  ArraySetAsSeries(Bull,true);
//-----
  SetIndexBuffer(2,Bear,INDICATOR_DATA);
  PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,min_rates_total);
  ArraySetAsSeries(Bear,true);
//-----
  SetIndexBuffer(3,ATRBuffer,INDICATOR_DATA);
  PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,min_rates_total);
  ArraySetAsSeries(ATRBuffer,true);
//-----
  string shortname;
  StringConcatenate(shortname,"MACDATR (",FastEMA,", ",SlowEMA,", ",SignalEMA,", ",EnumToString(AppliedPrice),")");
//-----
  IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//-----
  IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//-----
}
//+------------------------------------------------------------------+  
//| MACD 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[]
                )
  {
//----- Check for data
  if(rates_total<min_rates_total) return(0);
//-----
  int to_copy,limit,i;
  double atr,Atr[];
  datetime Time[1];
//-----
  if(prev_calculated>rates_total || prev_calculated<=0)
  {
    limit=rates_total-min_rates_total;
  }
  else limit=rates_total-prev_calculated;
//----- 
  ArraySetAsSeries(Atr,true);
//-----
  to_copy=limit+1;
//-----
  if(CopyBuffer(ATRHandle,0,0,to_copy,Atr)<=0) return(RESET);
  if(CopyBuffer(MACDHandle,MAIN_LINE,0,to_copy,MACDBuffer)<=0) return(RESET);
//-----
  for(i=limit; i>=0 && !IsStopped(); i--)
  {
    atr=kATR*Atr[i]; // ATR
    atr=MathMax(atr,ATRmin);
//-----
    if(MACDBuffer[i]>0) {ATRBuffer[i]=MACDBuffer[i]-atr;}
    if(MACDBuffer[i]<0) {ATRBuffer[i]=MACDBuffer[i]+atr;}
  }
//-----
  for(i=limit; i>=0 && !IsStopped(); i--)
  {
//-----
    Bear[i]=0;
    Bull[i]=0;
//-----
    if(MACDBuffer[i]>0 && MACDBuffer[i+1]<MACDBuffer[i] && ATRBuffer[i]>=0) {Bull[i]=MACDBuffer[i];}
    if(MACDBuffer[i]<0 && MACDBuffer[i+1]>MACDBuffer[i] && ATRBuffer[i]<=0) {Bear[i]=MACDBuffer[i];}
  }
//+------------------------------------------------------------------+
//----- Done
  return(rates_total);
}
//+------------------------------------------------------------------+

Doing this and it's not working.

double Bull[3];
double Bear[3];

Indicator=iCustom(NULL,IndiTF,"MACDATR",FastEMA,SlowEMA,SignalEMA,ATRG,AppliedPrice);
return(0);}
..................
  CopyBuffer(Indicator,1,0,3,Bull);
  ArraySetAsSeries(Bull,true);
  CopyBuffer(Indicator,2,0,3,Bear);
  ArraySetAsSeries(Bear,true);
.....................
if(Bull[1] > 0.0 && Bull[2] <= 0.0)
........................
if(Baer[1] < 0.0 && Bear[2] >= 0.0)
........................
 
G001: Now you have to take the signals from the indicator: I do this and it doesn't work.

Are you checking for invalidity of the handwheel?

Indicator=iCustom(NULL,IndiTF,"MACDATR",FastEMA,SlowEMA,SignalEMA,ATRG,AppliedPrice);
 
Yedelkin:

Do you check for invalidity of the handl?

No. Please show me what it is and how to do it.
Thank you for your time.
 
G001:
No. Please show me what it is and how to do it.
Thank you for your time.

Yes, you already have a similar check, elsewhere:

MACDHandle=iMACD(NULL,0,FastEMA,SlowEMA,SignalEMA,AppliedPrice);
  if(MACDHandle==INVALID_HANDLE)Print(" Íå óäàëîñü ïîëó÷èòü õåíäë èíäèêàòîðà MACD");

If INVALID_HANDLE, print an error

ResetLastError();
  Indicator=iCustom(....);
  if(Indicator==INVALID_HANDLE) Print("_LastError=",_LastError); 
 
Yedelkin:

Yes you already have a similar check, elsewhere:

If there is an INVALID_HANDLE, print an error

Thank you very much. I have understood now, I have not written the indicator, I didn't know that it should be done in an EA as well.
Thank you once again.
 

I'm completely exhausted. Doesn't open properly.

Doesn't read the indicator signals correctly.

Please help. Where is the error?

//+------------------------------------------------------------------+
//|                                                       Expert.mq5 |
//+------------------------------------------------------------------+
// Input Parameters
//+------------------------------------------------------------------+
input int    TakeProfit     = 550;
input int    StopLoss       = 550;
input int    OrderDrive     = 20;
input double  LotSize       = 0.01;
//+------------------------------------------------------------------+
input ENUM_TIMEFRAMES IndiTF=PERIOD_CURRENT;
input int    FastEMA        = 12;
input int    SlowEMA        = 26;
input int    SignalEMA      = 9;
input int    ATRG           = 0;
input ENUM_APPLIED_PRICE AppliedPrice=PRICE_CLOSE;
//+------------------------------------------------------------------+
MqlTradeRequest request;
MqlTradeResult result;
MqlTradeCheckResult check;
int Indicator;
double Bull[];
double Bear[];
double Ask,Bid;
int i,pos,Spread;
ulong StopLevel;
//+------------------------------------------------------------------+
int OnInit()
{
  ResetLastError();
  Indicator=iCustom(Symbol(),IndiTF,"MACDATR",FastEMA,SlowEMA,SignalEMA,ATRG,AppliedPrice);
  if(Indicator==INVALID_HANDLE) Print("HandleError = ",_LastError); 
  return(0);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason){}
//+------------------------------------------------------------------+
void OnTick()
{
//-----
  CopyBuffer(Indicator,1,0,3,Bull);
  ArraySetAsSeries(Bull,true);
//-----
  CopyBuffer(Indicator,2,0,3,Bear);
  ArraySetAsSeries(Bear,true);
//-----
  Ask = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),_Digits);
  Bid = NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),_Digits);
  Spread=int(SymbolInfoInteger(Symbol(),SYMBOL_SPREAD));
  StopLevel=SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
//+------------------------------------------------------------------+
  if(OrdersTotal() < 1 && PositionsTotal() < 1)
  {
//----- Open BUY_STOP
    if(Bear[1] >= 0.0 && Bear[2] < 0.0)
    {
      request.action = TRADE_ACTION_PENDING;
      request.symbol = _Symbol;
      request.volume = LotSize;
      request.price=NormalizeDouble(Ask+StopLevel*_Point,_Digits);
      request.sl = NormalizeDouble(request.price - StopLoss*_Point,_Digits);
      request.tp = NormalizeDouble(request.price + TakeProfit*_Point,_Digits);
      request.type=ORDER_TYPE_BUY_STOP;
      request.type_filling=ORDER_FILLING_FOK;
      if(OrderCheck(request,check))
      {
        OrderSend(request,result);
      }
    }
//----- Open SELL_STOP
    if(Bull[1] <= 0.0 && Bull[2] > 0.0)
    {
      request.action = TRADE_ACTION_PENDING;
      request.symbol = _Symbol;
      request.volume = LotSize;
      request.price=NormalizeDouble(Bid-StopLevel*_Point,_Digits);
      request.sl = NormalizeDouble(request.price + StopLoss*_Point,_Digits);
      request.tp = NormalizeDouble(request.price - TakeProfit*_Point,_Digits);
      request.type=ORDER_TYPE_SELL_STOP;
      request.type_filling=ORDER_FILLING_FOK;
      if(OrderCheck(request,check))
      {
        OrderSend(request,result);
      }                             
    }
  }
}
//+------------------------------------------------------------------+