设为首页 收藏本站 切换语言 切换语言

02-基本框架ATR设置止盈止损

| 发表于 昨天 14:14 | 显示全部楼层 |复制链接
//+------------------------------------------------------------------+
//|                                                        01-马丁.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//显示在主窗口上
#property indicator_chart_window
//绘制缓冲数组的数量2
#property indicator_buffers 2
//绘制的属性为2
#property indicator_plots   2
//缓冲数组的意思:存储某个绘制属性的具体指标的值,而且每根k线都有指标值
//--- plot 线1
#property indicator_label1  "线1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot 线2
#property indicator_label2  "线2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrMediumSpringGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- indicator buffers
//动态数组:元素的个数不确定
//存储的时每根k线对应的指标值
double         线1Buffer[];
double         线2Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

//指标开发的事件处理程序
int OnInit()
  {
//--- indicator buffers mapping
//给缓冲数组规定元素的个数,然后让缓冲数组的元素个数随着k线的数量的增加而动态变化
   SetIndexBuffer(0,线1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,线2Buffer,INDICATOR_DATA);

//---
   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[])
  {
   /*
   //---ATR画止损止盈。1.5倍
    for(int i=0;i<rates_total;i++)
      {
       线1Buffer[i]=close[i]+1.5*iATR(Symbol(),PERIOD_CURRENT,21,i);
       线2Buffer[i]=close[i]-1.5*iATR(Symbol(),PERIOD_CURRENT,21,i);
      }


   */
   int jishu=0;
   if(prev_calculated==0)
     {
      jishu=rates_total;
      if(prev_calculated>0)
        {
         jishu=rates_total-prev_calculated+1;
        }
     }
   for(int i=0; i<jishu; i++)
     {
      线1Buffer[i]=close[i]+1.2*iATR(Symbol(),0,21,i);
      线2Buffer[i]=close[i]+1.2*iATR(Symbol(),0,21,i);
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


最近访问 头像模式
举报

评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册

 简体中文国旗 简体中文
 繁體中文国旗 繁體中文
 English国旗 English(英语)
 日本語国旗 日本語(日语)
 Deutsch国旗 Deutsch(德语)
 Русский язык国旗 Русский язык(俄语)
 بالعربية国旗 بالعربية(阿拉伯语)
 Türkçe国旗 Türkçe(土耳其语)
 Português国旗 Português(葡萄牙语)
 ภาษาไทย国旗 ภาษาไทย(泰国语)
 한어国旗 한어(朝鲜语/韩语)
 Français国旗 Français(法语)
翻译