Preguntas de un "tonto" - página 186

[Eliminado]  
Yedelkin:
No del todo. Para la sentencia for, hay que especificar el tipo de variable i. Para seleccionar una posición - primero use PositionGetSymbol(i), luego - busque en las propiedades de la posición seleccionada.

Gracias.¿Verdad?

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:

Gracias.¿Verdad?

Sí, ese es más o menos el patrón. Esta línea de aquí.

PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY

el compilador probablemente generará una advertencia. Se puede arreglar haciendo un casting explícito de PositionGetInteger(POSITION_TYPE) al tipo enum requerido.

Esta línea

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

algunos programadores prefieren ejecutar en orden descendente en lugar de ascendente.

Adenda. ¿Por qué ha cambiado la línea correcta a if(PositionSelect(Symbol()))?

[Eliminado]  
Yedelkin:

Sí, ese es más o menos el patrón. Esta línea de aquí.

el compilador probablemente generará un mensaje de advertencia. Puede tratarse mediante el casting explícito de PositionGetInteger(POSITION_TYPE) al tipo enum requerido.

Esta línea

algunas personas prefieren correr en orden descendente en lugar de ascendente.

Adenda. ¿Por qué ha cambiado la línea correcta a if(PositionSelect(Symbol()))?

Muchas gracias.

if(PositionSelect(Symbol()))

Cambiado a:

PositionGetSymbol(i)==Symbol()

O lo cambiaré por éste:

PositionSelect(PositionGetSymbol(i))

Comprobaré qué opción funciona.

Gracias.

 
G001: Cambiado a:

O me cambiaré a este:

Veré cuál funciona.

Basta con leer la descripción de la función PositionGetSymbol(). :)
[Eliminado]  
Muchas gracias. Todo está ya funcionando.

Ahora tengo que tomar las señales del indicador:

//+------------------------------------------------------------------+ 
//|                                                      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);
}
//+------------------------------------------------------------------+

Haciendo esto y no está funcionando.

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: Ahora tienes que tomar las señales del indicador: hago esto y no funciona.

¿Está comprobando la invalidez del volante?

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

¿Comprueban la invalidez del handl?

No. Por favor, muéstrame qué es y cómo se hace.
Gracias por su tiempo.
 
G001:
No. Por favor, muéstrame qué es y cómo se hace.
Gracias por su tiempo.

Sí, ya tiene un control similar, en otro lugar:

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

Si INVALID_HANDLE, imprime un error

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

Sí, ya tiene un control similar, en otro lugar:

Si hay un INVALID_HANDLE, imprime un error

Muchas gracias. Ahora lo he entendido, no he escrito el indicador, no sabía que se debía hacer en un EA también.
Gracias una vez más.
[Eliminado]  

Estoy completamente agotado. No se abre correctamente.

No lee correctamente las señales de los indicadores.

Por favor, ayuda. ¿Dónde está el 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);
      }                             
    }
  }
}
//+------------------------------------------------------------------+