MT4变色均线
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Blue
#property indicator_color2 Crimson
#property indicator_color3 DeepPink
#property indicator_color4 ForestGreen
#property indicator_color5 Gold
#property indicator_color6 Lime
#property indicator_color7 Magenta
extern int HMAPeriod=15;
extern int HMAType=3;
extern int SMAPeriod=34;
extern int SMAType=1;
extern int LMAPeriod=180;
extern int LMAType=1;
double Uptrend[];
double Dntrend[];
double ExtMapBuffer[];
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
int init()
{
IndicatorBuffers(9);
SetIndexBuffer(0, Uptrend);
SetIndexBuffer(1, Dntrend);
SetIndexBuffer(2, ExtMapBuffer);
ArraySetAsSeries(ExtMapBuffer, true);
SetIndexBuffer(4,ExtMapBuffer1);
SetIndexBuffer(3,ExtMapBuffer2);
SetIndexBuffer(6,ExtMapBuffer3);
SetIndexBuffer(5,ExtMapBuffer4);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,3);
SetIndexStyle(2,DRAW_LINE,STYLE_DOT,0);
SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,3);
SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,3);
SetIndexStyle(6,DRAW_LINE,STYLE_SOLID,3);
SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,3);
return(0);
}
double SMA(int x, int p)
{
return(iMA(NULL, 0, p, 0, HMAType, PRICE_WEIGHTED, x));
}
int start()
{
int limit;
int counted_bars = IndicatorCounted();
if (counted_bars<0) return(-1);
if (counted_bars>0) counted_bars--;limit = Bars - counted_bars;
int x = 0;
int p = MathSqrt(HMAPeriod);
int e = Bars - counted_bars + HMAPeriod + 1;
double vect[], trend[];
if(e > Bars) e = Bars;
ArrayResize(vect, e);
ArraySetAsSeries(vect, true);
ArrayResize(trend, e);
ArraySetAsSeries(trend, true);
for(x = 0; x < e; x++)
{
vect[x] = 2*SMA(x, HMAPeriod/2) - SMA(x, HMAPeriod);
}
for(x = 0; x < e-HMAPeriod; x++)
ExtMapBuffer[x] = iMAOnArray(vect, 0, p, 0, HMAType, x);
for(x = e-HMAPeriod; x >= 0; x--)
{
trend[x] = trend[x+1];
if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;
if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;
if (trend[x]>0)
{ Uptrend[x] = ExtMapBuffer[x];
if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];
Dntrend[x] = EMPTY_VALUE;
}
else
if (trend[x]<0)
{
Dntrend[x] = ExtMapBuffer[x];
if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];
Uptrend[x] = EMPTY_VALUE;
}
}
double MA_Cur0, MA_Prev0, MA_Cur1, MA_Prev1;
for(int i=limit; i>=0; i--)
{
MA_Cur0 = iMA(NULL,0,SMAPeriod,0,SMAType,PRICE_WEIGHTED,i);
MA_Prev0 = iMA(NULL,0,SMAPeriod,0,SMAType,PRICE_WEIGHTED,i+1);
ExtMapBuffer2[i] = MA_Cur0;
ExtMapBuffer1[i] = MA_Cur0;
if (MA_Prev0 > MA_Cur0)
{
ExtMapBuffer2[i] = EMPTY_VALUE;
}
else if (MA_Prev0 < MA_Cur0)
{
ExtMapBuffer1[i] = EMPTY_VALUE;
}
else
{
ExtMapBuffer1[i]=EMPTY_VALUE;
ExtMapBuffer2[i]=EMPTY_VALUE;
}
MA_Cur1 = iMA(NULL,0,LMAPeriod,0,LMAType,PRICE_WEIGHTED,i);
MA_Prev1 = iMA(NULL,0,LMAPeriod,0,LMAType,PRICE_WEIGHTED,i+1);
ExtMapBuffer4[i] = MA_Cur1;
ExtMapBuffer3[i] = MA_Cur1;
if (MA_Prev1 > MA_Cur1)
{
ExtMapBuffer4[i] = EMPTY_VALUE;
}
else if (MA_Prev1 < MA_Cur1)
{
ExtMapBuffer3[i] = EMPTY_VALUE;
}
else
{
ExtMapBuffer4[i]=EMPTY_VALUE;
ExtMapBuffer3[i]=EMPTY_VALUE;
}
}
int m,s;
m=Time[0]+Period()*60-CurTime();
s=m%60;
m=(m-m%60)/60;
ObjectDelete("time");
if(ObjectFind("time") != 0)
{
ObjectCreate("time", OBJ_TEXT, 0, Time[0], Close[0]+5*Point);
ObjectSetText("time", " <"+m+":"+s+" $"+
(DoubleToStr(Close[0],Digits)), 13, "Verdana", Navy);
}
else
{
ObjectMove("time", 0, Time[0], Close[0]+5*Point);
}
return(0);
}
5M剥头皮 系统 源码!
#include <stdlib.mqh>
extern double Lots = 1.0;
extern double TakeProfit = 10;
extern double Stoploss = 17;
extern double TrailingStop = 9;
extern double Slippage = 2;
extern double risk = 5.0;
extern double Pips = 16;
extern double Perc = 5;
double Points;
//int color arrow_color=CLR_NONE;
int init ()
{
Points = MarketInfo (Symbol(), MODE_POINT);
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double Levv=0;
int cnt=0, total;
if(Bars<10)
{
Print("bars less than 100");
return(0);
}
if(OrdersTotal()<1)
{
if(AccountFreeMargin()<(1*Lots))
{
Print("BrokeAsAJoke");
return(0);
}
Levv= (MathCeil(AccountEquity() * risk / 10000)/10);
// (BUY)
if (Close[1]>Close[2])
{
OrderSend(Symbol(),OP_BUY,Levv,Bid,Slippage,Bid-Stoploss*Points,Ask+TakeProfit*Points,0,0,Red);
return(0);
}
// (SELL)
if (Close[1]<Close[2])
{
OrderSend(Symbol(),OP_SELL,Levv,Ask,Slippage,Ask+Stoploss*Points,Bid-TakeProfit*Points,0,0,Red);
return(0);
}
}
total=OrdersTotal();
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
//if((OrderOpenTime() - (CurTime() >= 300))|| (AccountProfit() >2))
if((CurTime() - (OrderOpenTime() >= 300)) || (AccountProfit() >2))
{
OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet);
return(0);
}
}
}
}
total=OrdersTotal();
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_BUY && OrderSymbol()==Symbol())
{
if(OrderType()==OP_SELL)
{
//if ((OrderOpenTime() - (CurTime() >= 300))|| (AccountProfit() >2))//1 Day//
if((CurTime() - (OrderOpenTime() >= 300)) || (AccountProfit() >2))
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet);
return(0);
}
}
}
}
}
自己做的EA
//+------------------------------------------------------------------+
//| MACD Sample.mq4 |
//| Copyright ?2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
extern double T2 = 50;
extern double L = 0.1;
extern double T3 = 30;
extern double MA1=3;
extern double MA2=2;
extern double MA3=26;
int start()
{
double M1, M2, S;
double S1, M3, M4;
int c, t1, to;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(T2<10)
{
Print("TakeProfit less than 10");
return(0);
}
M1=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
M2=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
S=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
S1=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
M3=iMA(NULL,0,MA3,0,MODE_EMA,PRICE_CLOSE,0);
M4=iMA(NULL,0,MA3,0,MODE_EMA,PRICE_CLOSE,1);
to=OrdersTotal();
if(to<1)
{
if(AccountFreeMargin()<(1000*L))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if(M1<0 && M1>S && M2<S1 &&
MathAbs(M1)>(MA1*Point) && M3>M4)
{
t1=OrderSend(Symbol(),OP_BUY,L,Ask,3,0,Ask+T2*Point,"macd sample",16384,0,Green);
if(t1>0)
{
if(OrderSelect(t1,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(M1>0 && M1<S && M2>S1 &&
M1>(MA1*Point) && M3<M4)
{
t1=OrderSend(Symbol(),OP_SELL,L,Bid,3,0,Bid-T2*Point,"macd sample",16384,0,Red);
if(t1>0)
{
if(OrderSelect(t1,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
for(c=0;c<to;c++)
{
OrderSelect(c, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(M1>0 && M1<S && M2>S1 &&
M1>(MA2*Point))
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
if(T3>0)
{
if(Bid-OrderOpenPrice()>Point*T3)
{
if(OrderStopLoss()<Bid-Point*T3)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*T3,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else
{
if(M1<0 && M1>S &&
M2<S1 && MathAbs(M1)>(MA2*Point))
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
if(T3>0)
{
if((OrderOpenPrice()-Ask)>(Point*T3))
{
if((OrderStopLoss()>(Ask+Point*T3)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*T3,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
均线交叉箭头
//+------------------------------------------------------------------+
//| Copyright ?2005, maningok@163.com
//| http://man2078.home4u.china.com/|
//+------------------------------------------------------------------+
#property copyright "maningok@163.com"
#property link "http://man2078.home4u.china.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 2//////////909add
#property indicator_color2 Red
#property indicator_width2 2//////////909add
//---- buffers
double buy[],sell[];
extern int fast=5;
extern int slow=13;
double LastAlertTime=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,233);
SetIndexBuffer(0,buy);
SetIndexEmptyValue(0,0.0);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,234);
SetIndexBuffer(1,sell);
SetIndexEmptyValue(1,0.0);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int start()
{
//----
for (int i=Bars-1;i>=0;i--)
{
if
(
(
iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i)<iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i)
&& iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i+1)>iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i+1)
)
)
{
sell[i]=High[i]+2*Point;
if (i==1 && LastAlertTime!=Time[0])
{
Alert("Negative Cross");
LastAlertTime=Time[0];
}
}
if
(
(
iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i)>iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i)
&& iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i+1)<iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i+1)
)
)
{
buy[i]=Low[i]-2*Point;
if (i==1 && LastAlertTime!=Time[0])
{
Alert("Positive Cross");
LastAlertTime=Time[0];
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
MT4画箭头的均线
//+------------------------------------------------------------------+
//| Copyright ?2005, maningok@163.com
//| http://man2078.home4u.china.com/|
//+------------------------------------------------------------------+
#property copyright "maningok@163.com"
#property link "http://man2078.home4u.china.com"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 2//////////909add
#property indicator_color2 Red
#property indicator_width2 2//////////909add
//---- buffers
double buy[],sell[];
extern int fast=5;
extern int slow=13;
double LastAlertTime=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,233);
SetIndexBuffer(0,buy);
SetIndexEmptyValue(0,0.0);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,234);
SetIndexBuffer(1,sell);
SetIndexEmptyValue(1,0.0);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int start()
{
//----
for (int i=Bars-1;i>=0;i--)
{
if
(
(
iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i)<iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i)
&& iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i+1)>iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i+1)
)
)
{
sell[i]=High[i]+2*Point;
if (i==1 && LastAlertTime!=Time[0])
{
Alert("Negative Cross");
LastAlertTime=Time[0];
}
}
if
(
(
iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i)>iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i)
&& iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i+1)<iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i+1)
)
)
{
buy[i]=Low[i]-2*Point;
if (i==1 && LastAlertTime!=Time[0])
{
Alert("Positive Cross");
LastAlertTime=Time[0];
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
EA
//+------------------------------------------------------------------+
//| Ma-EA.mq4 |
//| 462152079 |
//| wenbb_4@126.com |
//+------------------------------------------------------------------+
#define MAGICMA 11223344
extern double Lots = 0.2;
extern double MaximumRisk = 0.02;
extern string readme1 = "均线周期设置";
extern double maperiod1 = 9;
extern double maperiod2 = 60;
extern string readme2 = "均线种类";
extern int mamode = 1;
extern string readme3 = "价格种类";
extern int price = 0;
double ma;
double ma1;
double ma2;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0; int hh1=Hour();
int hh2=Hour();
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- return lot size
if(lot<Lots) lot=Lots;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
int res;
//---- get Moving Average
double D=iStochastic(NULL,0,14,3,5,MODE_SMA,0,MODE_SIGNAL,0); //当周期的D值
double D1=iStochastic(NULL,0,14,3,5,MODE_SMA,0,MODE_SIGNAL,1); //当周期的D值
//---- sell conditions
if(D<=50 && D1>50 && TimeHour(TimeCurrent())>1 && TimeHour(TimeCurrent())<14 )
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+Point*30,0,"",MAGICMA,0,Red);
return;
}
//---- buy conditions
if(D>=50 && D<50 && TimeHour(TimeCurrent())>1 && TimeHour(TimeCurrent())<14 )
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-Point*30,0,"",MAGICMA,0,Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
//---- get Moving Average
double D=iStochastic(NULL,0,14,3,5,MODE_SMA,0,MODE_SIGNAL,0); //当周期的D值
double D1=iStochastic(NULL,0,14,3,5,MODE_SMA,0,MODE_SIGNAL,1); //当周期的D值
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if(D<=30)
{OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+Point*30,0,"",MAGICMA,0,Red);
}
if(Bid-OrderOpenPrice()>(Point*13) && Bid-OrderOpenPrice()<(Point*15))//最新买价减去止损价大于20点
{OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*8,OrderTakeProfit(),0,Red);//追踪止损在最新买价的下30点
return(0);
}
if(Bid-OrderStopLoss()>(Point*31))//最新买价减去止损价大于20点
{OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*30,OrderTakeProfit(),0,Red);//追踪止损在最新买价的下30点
return(0);
}
return;
}
if(OrderType()==OP_SELL)
{if(D>=70)
{OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-Point*30,0,"",MAGICMA,0,Blue);
}
if(OrderOpenPrice()-Ask>(Point*13) && OrderOpenPrice()-Ask<(Point*15))//止损减去最新卖价或者止损等于0
{OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*8,OrderTakeProfit(),0,Blue);//追踪止损在最新卖价的上30点
return(0);
}
if(OrderStopLoss()-Ask>(Point*31) || (OrderStopLoss()==0))//止损减去最新卖价或者止损等于0
{OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*30,OrderTakeProfit(),0,Blue);//追踪止损在最新卖价的上30点
return(0);
}
return;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
}
//+------------------------------------------------------------------+
会变色的均线
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Green
extern int 变色均线=18;
double duo[];
double kong[];
int init()
{
SetIndexBuffer(0,duo);
SetIndexBuffer(1,kong);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexDrawBegin(0,变色均线);
SetIndexDrawBegin(1,变色均线);
IndicatorDigits(Digits);
return(0);
}
int start()
{
double temp0,temp1;
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int i=limit; i>=0; i--)
{
duo[i]=EMPTY_VALUE;
kong[i]=EMPTY_VALUE;
temp0=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i);
temp1=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i+1);
if(iClose(NULL,0,i)>=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i))
{duo[i]=temp0; duo[i+1]=temp1;}
else {kong[i]=temp0; kong[i+1]=temp1;}
}
return(0);
}
-----------------------------------------------------------------------------------------------------------
当然,上面这个是以价格在均线上下方为条件的,我们也可以以MACD、KDJ、RSI等指标作为均线变色的条件。我们还可以更进一步,把双色线改为三色线等等
===================================================
语句简要解释如下:
===================================================
#property indicator_chart_window
指标放在主图
#property indicator_buffers 2
设置指标线数组为2个
#property indicator_color1 Red
#property indicator_color2 Green
设置第一条指标线颜色值为Red,第二条颜色值为Green
extern int 变色均线=18;
设立一个自定义变量,允许外部值修改,整数形,变量名为"变色均线",默认值18
double duo[];
设立一个自定义数组,双精度型,名称为duo
该数组在后面用于存储红线数据
double kong[];
设立一个自定义数组,双精度型,名称为kong
该数组在后面用于存储绿线数据
int init()
设立初始化函数init。init为系统规定函数名,函数内容自定义。该函数在指标被加载时运行一次
{
SetIndexBuffer(0,duo);
SetIndexBuffer(1,kong);
设置第一、二条指标线的数组为duo和kong
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
设置第一、二条指标线的样式,线型为连续曲线
SetIndexDrawBegin(0,变色均线);
SetIndexDrawBegin(1,变色均线);
设置第一、二条指标线的最左边的起始划线位置
IndicatorDigits(Digits);
设置指标精确到的小数位数
Digits是当前汇率小数位,日系Digits=2,其他币对Digits=4
return(0);
init函数结束,返回零值
}
int start()
设立触发函数start。start为系统规定函数名,函数内容自定义。当数据变动时,start函数被触发
{
double temp0,temp1;
设立双精度型自定义变量temp0、temp1
int limit;
设立自定义变量limit,整数型
int counted_bars=IndicatorCounted();
设立整数型自定义变量counted_bars,并将IndicatorCounted()的值赋给counted_bars
IndicatorCounted()为缓存数,即已经计算过值的烛柱数
if(counted_bars<0) return(-1);
如果counted_bars值小于零,start函数结束
if(counted_bars>0) counted_bars--;
如果counted_bars值大于零,则counted_bars值减掉1。这是为了配合下一句,以避免limit相差1而出错
limit=Bars-counted_bars;
给limit赋值
Bars为图表中的烛柱数
counted_bars为缓存数,即已经运算过的烛柱数
这样limit的值就是未经运算的烛柱数
这样做的目的是避免重复运算,优化程序
for(int i=limit; i>=0; i--)
循环语句,括号中有三个语句:
第一句int i=limit; 表示循环从i=limit开始
第二句i>=0; 这是循环的条件,如果条件满足则执行大括号中的循环体,如果条件不满足,则中止循环,跳到大括号下面的语句执行
第三句i--,这是循环步调控制语句,每循环一次后执行一次此语句。
i--相当于i=i-1,即i值在原有数值上减少1
{
duo[i]=EMPTY_VALUE;
kong[i]=EMPTY_VALUE;
给数组duo和kong在i位置上赋空值
EMPTY_VALUE:空值
temp0=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i);
temp1=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i+1);
把均线在i和i+1位置上均线值,分别赋给temp0和temp1
这是为了使后面的语句看起来简洁
if(iClose(NULL,0,i)>=iMA(NULL,0,变色均线,0,MODE_SMA,PRICE_CLOSE,i))
判断条件语句:如果价格高于均线
{duo[i]=temp0; duo[i+1]=temp1;}
判断执行语句:给数组元素duo[i]、duo[i+1]分别赋值
else {kong[i]=temp0; kong[i+1]=temp1;}
如果判断条件不成立,即价格低于均线:则给数组元素kong[i]、kong[i+1]分别赋值
}
return(0);
start函数结束,返回零值
}
评论