Einbinden eines eigenen Indikators

 
Hallo,
Ich habe einen Indikator selbst programmiert. Dieser funktioniert auch. 
Jetz ist meine Frage, wie ich diesen in meinen EA einbauen kann. Ich habe keine Ahnung und diese internet tutorials helfen mir auch nicht weiter.
Danke im voraus.
 
Das machst du mit iCustom. Tipp es ein und drück F1
 
Otto Pauser:
Das machst du mit iCustom. Tipp es ein und drück F1

Danke für die schnelle Antwort!

Jedoch funktioniert es bei mir noch nicht. Wenn ich den Indikator einzeln Teste geht er, aber wenn ich ihn mit iCustom() in den EA einbinde wird er nicht angezeigt.

Hier mein code:

Indikator

//+------------------------------------------------------------------+
//|                                                          LRL.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                                 https://mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://mql5.com"
#property version   "1.00"
#property description "Linear regression MA (LSMA)"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot LRL
#property indicator_label1  "LSMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrCrimson
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input uint                 InpPeriod         =  10;            // Period
input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_CLOSE;   // Applied price
//--- indicator buffers
double         BufferLRL[];
double         BufferMA[];
//--- global variables
int            period;
int            handle_ma;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   period=int(InpPeriod<1 ? 1 : InpPeriod);
   SetIndexBuffer(0,BufferLRL,INDICATOR_DATA);
   SetIndexBuffer(1,BufferMA,INDICATOR_CALCULATIONS);
   IndicatorSetString(INDICATOR_SHORTNAME,"LSMA("+(string)period+")");
   IndicatorSetInteger(INDICATOR_DIGITS,Digits());
   ArraySetAsSeries(BufferLRL,true);
   ArraySetAsSeries(BufferMA,true);
   ResetLastError();
   handle_ma=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpAppliedPrice);
   if(handle_ma==INVALID_HANDLE)
     {
      Print("The iMA(1) object was not created: Error ",GetLastError());
      return INIT_FAILED;
     }
//---
   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[])
{
   if(rates_total<period) return 0;

   int limit=rates_total-prev_calculated;
   if(limit>1)
     {
      limit=rates_total-period-1;
      ArrayInitialize(BufferLRL,EMPTY_VALUE);
      ArrayInitialize(BufferMA,EMPTY_VALUE);
     }

   int copied=0,count=(limit==0 ? 1 : rates_total);
   copied=CopyBuffer(handle_ma,0,0,count,BufferMA);
   if(copied!=count) return 0;

   for(int i=limit; i>=0 && !IsStopped(); i--)
     {
      double x=0,y=0,xy=0,x2=0;
      for(int j=0; j<period; j++)
        {
         y+=BufferMA[i+j];
         xy+=BufferMA[i+j]*j;
         x+=j;
         x2+=j*j;
        }
      double temp=period*x2-x*x;
      double m=(period*xy-x*y)/(temp!=0 ? temp : DBL_MIN);
      double yint=(y+m*x)/period;
      BufferLRL[i]=yint-m*period;
     }


   return(rates_total);
  }

EA

#include <Trade\Trade.mqh>
CTrade trade;

ENUM_POSITION_TYPE      type;
//+------------------------------------------------------------------+
//|                                                      RSI_vga.mq5 |
//|                                                                  |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "M,K"
#property link      "https://www.mql5.com"
#property version   "1.00"
//---- plot TSI
//--- input parameters
input double StopLossValue=20;
input double SaveStopValue=10;
//const input int PositionQuantity=1;
const input double Lots=0.1;
   input double SSTOCHoch=80;
   input double SSTOCTief=20;
   input double Zähler=16;
   input ENUM_APPLIED_PRICE RSIPrice=PRICE_CLOSE;
int handle; 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
   handle=iCustom(NULL,0,"LRL",10,PRICE_CLOSE); 
   return (0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
      double Equity=AccountInfoDouble(ACCOUNT_EQUITY);
      PROCESS(Ask,Bid,Balance);
      CheckTrailingStop(Ask,Bid);
  }
void Trade(string Type,double Ask,double Bid, double Balance){
   if(Type=="sell"){
      if(!PositionSelect(_Symbol)){
         ApplyAllerts("sell",NULL);
         trade.Sell(Lots,NULL,Bid,(Bid+StopLossValue),NULL,NULL);
      }
   } else if(Type=="buy"){
      if(!PositionSelect(_Symbol)){
         ApplyAllerts("buy",NULL);
         trade.Buy(Lots,NULL,Ask,(Ask-StopLossValue),NULL,NULL);
      }
   }
}

void CheckTrailingStop(double Ask,double Bid){
   double SlBuy=NormalizeDouble(Ask-SaveStopValue,_Digits);
   double SlSell=NormalizeDouble(Bid+SaveStopValue,_Digits);
   
   for(int i=PositionsTotal()-1;i>=0; i--){
      string symbol=PositionGetSymbol(i);
      if(_Symbol==symbol){
         ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
         double CurrentStopLoss = PositionGetDouble(POSITION_SL);
         double PosPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         
         type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
         
         //ApplyAllerts("positionModify",EnumToString(type));
         if(type==POSITION_TYPE_BUY){
            if(Ask>PosPrice&&(CurrentStopLoss<SlBuy)){
               if(PosPrice<Ask-SaveStopValue){
                  trade.PositionModify(PositionTicket,(Ask-SaveStopValue),NULL);

               }
            }
         }
         if(type==POSITION_TYPE_SELL){
            if(Bid<PosPrice&&(CurrentStopLoss>SlSell)){
               if(PosPrice>Bid+SaveStopValue){
                 trade.PositionModify(PositionTicket,(Bid+SaveStopValue),NULL);
               }
            }
         }
      }
   }
}

void ApplyAllerts(string value,string parameters){
   if(value=="buy"){
      Alert("Position erstellt: Kauf");
   } else if(value=="sell"){
      Alert("Position erstellt: Verkauf");
   } else if(value=="stopModify"){
      if(parameters==EnumToString(POSITION_TYPE_BUY)){
         Alert("Position Modifiziert: KaufPosition");
      } else if(parameters==EnumToString(POSITION_TYPE_SELL)){
         Alert("Position Modifiziert: VerkaufsPosition");
      }
   }
}

 

Kompliment, wirklich schöner Programmierstil!

Den Custom-Indikator siehst du auch nicht im Tester sondern nur Standard-Indikatoren wie zB den iMA.

Du ladest deinen Indikator in den Chart und speicherst das als Template Tester.tpl, und schon siehst du den Indi im Tester.

Die Parameter sollten natürlich übereinstimmen.

Zur OnInit() noch ein kleiner Hinweis:

Beim Wechsel des Timeframes im Chart, werden Indikatoren und Experten neu initialisiert. Dadurch ändert sich der Timeframe des EAs. Das ist wahrscheinlich unerwünscht.

int OnInit(){
   handle=iCustom(NULL,0,"LRL",10,PRICE_CLOSE); 
   return (0);
}

// würde ich ändern in

int OnInit(){
   handle=iCustom(_Symbol,inp_Timeframe,"LRL",10,PRICE_CLOSE); 
   return (0);
}

Teste es mit diesem mini EA.

int OnInit()
{
   PlaySound("ok");
   Print(EnumToString(_Period));
   return(INIT_SUCCEEDED);
}

void OnTick()
{
}

Und die OnDeinit() kannst du getrost weglassen.

 
Otto Pauser:

Kompliment, wirklich schöner Programmierstil!

Den Custom-Indikator siehst du auch nicht im Tester sondern nur Standard-Indikatoren wie zB den iMA.

Du ladest deinen Indikator in den Chart und speicherst das als Template Tester.tpl, und schon siehst du den Indi im Tester.

Die Parameter sollten natürlich übereinstimmen.

Zur OnInit() noch ein kleiner Hinweis:

Beim Wechsel des Timeframes im Chart, werden Indikatoren und Experten neu initialisiert. Dadurch ändert sich der Timeframe des EAs. Das ist wahrscheinlich unerwünscht.

Teste es mit diesem mini EA.

Und die OnDeinit() kannst du getrost weglassen.

Vielen Dank abermals!

Jetzt funktioniert alles.

Ohne dich währe ich verloren!

 
Marius May:

Vielen Dank abermals!

Jetzt funktioniert alles.

Ohne dich währe ich verloren!

wie unterscheidt sich dieser indikator von bestehenden Linear regression indikatoren?

Grund der Beschwerde: