![自制通道交易法.mq4 自制通道交易法.mq4](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
关于止损点的问题,不建议超出几个固定点就止损。
价格到达上下轨位,下单的止损为前一高点,高点与轨道线价格差建议不超20点,要以盈亏比设定是否合算。如果超过前期高点,考虑反向开单,开仓时点为突破止损线后回采。
//============原则
高胜算:
盈亏的概率(次数亏少盈多)
盈亏的空间(金额点数上亏少盈多)
盈亏的频率(时间)(避免频繁交易,适度)
//===========实施方案
消息:基本面
无重大消息,按势走;
重大消息,按行情EA做;
势:200日、100日,50日MA,通道线、Chart以4小时及日线为准。1小时Chart进场准备。
态:实际上在势的关键位置,要考虑Bar的形态组合,如孕线,外包线等。指标的优劣,sdr,macd,rsi,kd
位:盈亏比,止损与盈利空间是否合适。
//自制通道线买入卖出法.mq4
//+-----------半自动,边测试完善---------------------+
//| 自制通道线买入卖出法.mq4 |
//| Copyright 2016, ong. |
//| https://www.ong.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, ong."
#property link "https://www.ong.com"
#property version "1.00"
#property strict
//----draw chanel and paste it's name--------
//extern string ChanelnameA="ch_EURUSD";
string ChanelnameA=NULL;
extern string currency="通道线作用货币名";
extern string Strade="紧急交易关闭";
extern double 保平触发值=100;
extern double 利润防守比例=0.7;
extern double Lot=0.03;//下单量
//---S1--if rsi_sell_1<45,can't sell
extern double rsi_sell_1=50;
extern double rsi_sell_2=70;
extern double sto_sell_1=45;
//---S2--if rsi_sell
extern double rsi_sell_3=30;
extern double sto_sell_2=40;
//------------------------------------
//---B1--if rsi_sell_1<45,can't buy
extern double rsi_buy_1=30;
extern double rsi_buy_2=50;
extern double sto_buy_1=75;
//---B2--if rsi_sellsto_sell_2,cann't buy;
extern double rsi_buy_3=75;
extern double sto_buy_2=90;
//----------
double rsi0=0;
double sto0=0;
//----------------------------------------------------------------------------------
double arraya[4];
int MagicBuyLimit=1101;
int MagicSellLimit=1102;
int glbOrderType=NULL;
int glbOrderTicket=NULL;
string glbOrderSymbol=NULL;
double glbOrderOpenPrice=NULL;
double glbOrderStopLoss=NULL;
double glbOrderLots=NULL;
double glbOrderTp=NULL;
double glbOrderP=NULL;
double glbOrderPoint=NULL;
double p_sl=0;
double glbOrderDigits=NULL;
double glbMinlot=NULL;
string PH="ph_"+StringSubstr(ChanelnameA,3,0);
//每一次编写,并不刷新MT窗口中"已经运行的"EA参数设定值。只有在MT中退出EA,再运行"编写"EA,上部的外部参数才生效。
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//----only effect to the order that it's magicnumber is null(无幻号订单,手动单的自动处理)--开始
if (OrderFind(NULL))
{
//---if the value of "sl","tp" isn't set,then set that firstly!
if (glbOrderType==0||glbOrderType==2||glbOrderType==4)
{
if (glbOrderType==0)
{
double yldsb=(Bid-glbOrderOpenPrice);//盈利点数
//----
if (yldsb>=80*glbOrderPoint&&glbOrderStopLoss<(glbOrderOpenPrice+yldsb*glbOrderPoint*0.5))
{
double slb=NULL;
if (yldsb==80*glbOrderPoint) slb=glbOrderOpenPrice;
if (yldsb>150*glbOrderPoint && yldsb<400*glbOrderPoint ) slb=glbOrderOpenPrice+yldsb*0.5;
if (yldsb>400*glbOrderPoint) slb=glbOrderOpenPrice+yldsb*0.7;
if (OrderModify(glbOrderTicket,glbOrderOpenPrice,slb,glbOrderTp,0,clrRosyBrown)==FALSE)
Print("自选单错误号:",GetLastError());
}
}
if (glbOrderStopLoss==NULL )
{
if (OrderModify(glbOrderTicket,glbOrderOpenPrice,glbOrderOpenPrice-150*glbOrderPoint,glbOrderOpenPrice+MathAbs(arraya[2]),0,clrRosyBrown)==FALSE)
Print("自选单错误号:",GetLastError());
}
}
if (glbOrderType==1||glbOrderType==3||glbOrderType==5)
{
if (glbOrderType==1)
{
double yldss=(glbOrderOpenPrice-Ask);//盈利点数
//----
if (yldss>=80*glbOrderPoint&&glbOrderStopLoss>(glbOrderOpenPrice-yldss*0.5))
{
double sls=NULL;
if (yldss==80*glbOrderPoint) sls=glbOrderOpenPrice;
if (yldss>150*glbOrderPoint && yldss<400*glbOrderPoint ) sls=glbOrderOpenPrice+yldss*0.5;
if (yldss>400*glbOrderPoint) sls=glbOrderOpenPrice+yldss*0.7;
if (OrderModify(glbOrderTicket,glbOrderOpenPrice,sls,glbOrderTp,0,clrRosyBrown)==FALSE)
Print("自选单错误号:",GetLastError());
}
}
if (glbOrderStopLoss==NULL)//未设止损的,设定止损
{
if (OrderModify(glbOrderTicket,glbOrderOpenPrice,glbOrderOpenPrice+150*Point,glbOrderOpenPrice-MathAbs(arraya[2])*0.8,0,clrRosyBrown)==FALSE)
Print("自选单错误号:",GetLastError());
}
}
}
//-----------------手动单处理结束——————————————————————————————————————
//---
//-在当前"图表Chart"中找物件,找到返回窗口(window)号,主窗为0,其它1,2....否则负数--
// if find the chanel line in the original "currncy" chart of draw the line .
//"if" operator only effect to the next line, not all yet! if you want effcet to muti line,use the {} include it!
if (Strade!="紧急交易关闭")
{
;
PlaySound("alert.wav");
}
if (Lot>0.1)
{
;
PlaySound("alert.wav");
}
ChanelnameA="ch_"+Symbol();
// Print("趋势线名称",ChanelnameA);
if (ObjectFind(0,ChanelnameA)==0 && Symbol()==StringSubstr(ChanelnameA,3,0)&& Lot<=0.1)
{
//-----------------------------------------------------------
//-----------------------------------------------------
string name=ChanelnameA;
//调动自定义通道函数
chanel(name);
double sl =arraya[0]+ 150*Point;//selllimit's sl
double tp =arraya[1]+ 150*Point;//selllimit's tp
Print("卖单时的sl::",sl);
double slb =arraya[1]- 150*Point;//buylimit's sl
double tpb =arraya[0]- 150*Point;//buylimit's tp
rsi0=iRSI(NULL,0,14,PRICE_CLOSE,0);
sto0=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,0);
//Print("数组高值symbol----",Symbol(),":::",arraya[0]);
//Print("fffffffffffffff",glbOrderSymbol);
// Print("数组低值",arraya[1]);
// Print("数组差值",arraya[2]);
//%%%%-------order not exist------------------------------------------------------------
//%%%%-------order not exist------------------------------------------------------------
// if order's symbol = the chart's symbol and magicnumber not exist
//---selllimit no exist------------
if (OrderFind( MagicSellLimit)==false &&arraya[3]<1)
{
//-----sell紧急模式,such as "sell_EURUSD"
//Print("操作方向",StringSubstr(Strade,0,4),"商品名",StringSubstr(Strade,5,0));
if (StringSubstr(Strade,0,4)=="sell" && StringSubstr(Strade,5,0)==Symbol())
{
if (OrderSend(Symbol(),OP_SELL,Lot,Ask,3,sl, tp,"",MagicSellLimit,0,Red)==false)
Print("err(",GetLastError(),")");
Print("操作方向",StringSubstr(Strade,0,4),"商品名",StringSubstr(Strade,5,0));
}
//-----sell正常模式-----
if (((rsi0>rsi_sell_1&&rsi0
{
Print("可执行卖","sto0的值",sto0);
if ((arraya[0]-Ask)Ask && Strade=="紧急交易关闭" )
{
if (OrderSend(Symbol(),OP_SELLLIMIT,Lot,arraya[0]-Point*50,3,sl, tp,"",MagicSellLimit,0,Red)==false)
Print("err(",GetLastError(),")");
}
}
}
// ----buylimit no exist ----
if (OrderFind( MagicBuyLimit)==false && arraya[3]<1)
{
//------buy(bull)-紧急模式---such as bull_EURUSD----
if (StringSubstr(Strade,0,4)=="bull" && StringSubstr(Strade,5,0)==Symbol())
{
if (OrderSend(Symbol(),OP_BUY,Lot,Bid,3,slb, tpb,"",MagicBuyLimit,0,Red)==false)
Print("err(",GetLastError(),")");
Print("操作方向",StringSubstr(Strade,0,4),"商品名",StringSubstr(Strade,5,0));
}
//------buy(bull) 正常模式-------
if (((rsi0>rsi_buy_1&&rsi0sto_buy_1)||rsi0>rsi_buy_3 || sto0>sto_buy_2)==false)
{
Print("可执行买","sto0的值",sto0);
if ((Ask-arraya[1])arraya[1]&& Strade=="紧急交易关闭" )
{
if (OrderSend(Symbol(),OP_BUYLIMIT,Lot,arraya[1]+Point*50,3,slb, tpb,"",MagicBuyLimit,0,Red)==false)
Print("err(",GetLastError(),")");
}
}
}
//%%%%-------order not exist------over---------------------------------------------
//@@@@@@-----selllimit--order exist------------------------------------------------------------
if (OrderFind( MagicSellLimit)==true && glbOrderSymbol==Symbol())
{
//-1-modify the trading order---when profit is higher 200 points than openprice,设定盈利25点保平
if (glbOrderType==1)
{
Print("dddddddddddddddddddd",glbOrderStopLoss);
double sell_ask=glbOrderOpenPrice-Ask;//即时sell单盈利点数
double sell_slj=glbOrderOpenPrice-sell_ask*利润防守比例;//sell单阶段性步步盈利,追踪止损值
if (sell_ask>保平触发值*Point && sell_ask<200*Point)
{
//tp upto 150 point,保平
double s=glbOrderOpenPrice-50*Point;
Print("ssssssssss",s);
if(OrderModify(glbOrderTicket, glbOrderOpenPrice,(glbOrderOpenPrice-50*Point), tp, 0, Green) == false)
Print("Err (", GetLastError(), ")");
}
if ( sell_ask>200*Point&& glbOrderStopLoss> sell_slj )
{
Print("jjjjjjjjjjjj",sell_slj);
if(OrderModify(glbOrderTicket,glbOrderOpenPrice,sell_slj, tp, 0, Green) == false)
Print("Err (", GetLastError(), ")");
}
//Print("有魔号的订单号",glbOrderTicket);
// Print("订单币种",OrderSymbol());
}
//-2-modify the pending sell order---
//-when the price of ask or bid is bigger or sammer then openprice(200 point)
if (glbOrderType==3)
{
if (arraya[0]-Ask>200*Point || Ask-arraya[0]>0)
{
if (OrderDelete(glbOrderTicket)==false)
Print("ERR(",GetLastError(),")");
}
if ( OrderModify(glbOrderTicket, arraya[0]-50*Point, sl, tp, 0, Green) == false)
Print("Err (", GetLastError(), ")");
// Print("OPEN_TYPE:",glbOrderType);
}
}
//@@@@@@@@@-------selllimit----order exist----over-----
//######-----buylimit--order exist------------------
if (OrderFind( MagicBuyLimit)==true && glbOrderSymbol==Symbol())
{
//-1-modify the trading order---when profit is higher 200 points than openprice,设定盈利25点保平
if (glbOrderType==0)
{
double buy_bid=Bid-glbOrderOpenPrice;//即时buy单盈利点数
Print("即时buy单Bid数",Bid,"即时buy单开仓价",glbOrderOpenPrice);
Print("即时buy单盈利点数",buy_bid);
double buy_slj=glbOrderOpenPrice+buy_bid*利润防守比例;//buy单阶段性步步为赢,追踪止损值
if (buy_bid>保平触发值*Point && buy_bid<200*Point)
{
if(OrderModify(glbOrderTicket, glbOrderOpenPrice,(glbOrderOpenPrice+50*Point), tpb, 0, Green) == false)
Print("Err (", GetLastError(), ")kkkkkkkkk");
}
if (buy_bid>200*Point&& buy_slj>glbOrderStopLoss)
{
if(OrderModify(glbOrderTicket, glbOrderOpenPrice, buy_slj, tpb, 0, Green) == false)
Print("Err (aaa", GetLastError(), ")");
}
}
//-2-modify the pending buy order---
//-when the price of ask or bid is bigger or sammer then openprice(200 point)
if (glbOrderType==2&& glbOrderSymbol==Symbol())
{
Print("挂买单时BID值",Bid);
if (Bid-arraya[1]>200*Point||Bid
{
if (OrderDelete(glbOrderTicket)==false)
Print("ERRbbb(",GetLastError(),")");
}
if ( OrderModify(glbOrderTicket, arraya[1]+50*Point, slb, tpb, 0, Green) == false)
Print("Errccc (", GetLastError(),")","买单时的止损", slb,"买单时的止盈",tpb);
// Print("OPEN_TYPE:",glbOrderType);
}
}
//#######--------buylimit---order exist----over---------------
}
//---
}
//+----------------------------------------------------------------------+
//+------------自定义函数chanel()-----start---------------------+
//|求取通道高低值
//|create a funtion the chanel angle is up or down
//|when the price of first point is bigger than second one,the chanel line is up!
//|暂定两个点的为主线,对线为次线。MT4定义主线上左边点为点1,右边点为点2,对应的次线上的点为点3.
//|取得点3时间对应的主线价格,可知两线差。
//|直接取得主线即时价格,可知次线即时价。
void chanel( string name)
{
ObjectFind(0,name);
double pa=ObjectGet(name,OBJPROP_PRICE3);
//如果不加(datetime),就会出现possible loss of data due to type conversion提示,说见博客
double pb=ObjectGetValueByTime(0,name, (datetime)ObjectGet(name,OBJPROP_TIME3),0);
double pd=NormalizeDouble(pa-pb,Digits);
//pm is the now price of main line,ps is the now price of secondary line
double pm=ObjectGetValueByShift(name,0);
double ps=pm+pd;
//if pd>0 ,次线价高
double phcn=0;
double plcn=0;
if (pd>0)
{
phcn=NormalizeDouble(ps,Digits);
plcn=NormalizeDouble(pm,Digits);
}
else
{
phcn=NormalizeDouble(pm,Digits);
plcn=NormalizeDouble(ps,Digits);
}
ArrayInitialize(arraya,NULL);
arraya[0]=phcn;
arraya[1]=plcn;
arraya[2]=pd;
//-------------price out chanel----------
//datetime some_time=D'2016.05.13 09:00';
datetime some_time=(datetime)ObjectGet(name,OBJPROP_TIME1);
int bars_ct1=iBarShift(Symbol(),PERIOD_CURRENT,some_time);//chanel's TIME point shift number
int i=NULL;
for ( i=0;i
{
Comment("bar's number ",bars_ct1,Symbol()," 未突破! ","突破的位置:---");
double chi=ObjectGetValueByShift(name,i);
double cli=chi+pd;
arraya[3]=0;
if (pd>0)
{
cli=ObjectGetValueByShift(name,i);
chi=cli+pd;
}
if (Close[i]>chi||Close[i]
{
arraya[3]=1;//有突破通道现象
Comment("bar's number ",bars_ct1,Symbol()," 突破!!!! ","突破的位置:::",i);
Print(Symbol(),"有突破通道现象",i);
break;
//comment\print:comment区别
// comment保持最后的状态不变。如上面最后i值为21,如果后面没有条件触发,comment会在主图一直显示21.所以在上面加了一行comment
//而print,如果没有条件触发,则不再输出任何内容。
}
}
//Comment("ww11ww",High[29],High[2]);
return;
}
//+------------------chanel()---over---------------------------------+
//+------------------自定义orderfind()---start-----------------------+
//|find the Order of EA auto place exist or no?
//|
//| |
//+------------------------------------------------------------------+
bool OrderFind(int Magic)
{
glbOrderType = -1;
glbOrderTicket = -1;
int total = OrdersTotal();
bool res = false;
for(int cnt = 0 ; cnt < total ; cnt++)
{
if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==false)
Print("ERR(",GetLastError(),")");
if(OrderMagicNumber() == Magic && OrderSymbol() == Symbol()&& Symbol()==StringSubstr(ChanelnameA,3,0))
{
glbOrderType = OrderType();
glbOrderSymbol=OrderSymbol();
glbOrderTicket = OrderTicket();
glbOrderOpenPrice=OrderOpenPrice();
glbOrderStopLoss=OrderStopLoss();
glbOrderLots=OrderLots();
glbOrderTp=OrderTakeProfit();
glbOrderP=OrderProfit();
glbOrderPoint=Point;
glbOrderDigits=Digits;
glbMinlot = MarketInfo(OrderSymbol(),MODE_MINLOT);
Print(Symbol(),"ggggggggggggggggg",glbMinlot,"点值",glbOrderPoint);
res = true;
}
}
return(res);
}
//------------------orderfind()----over-------------------------------+
//+------------自定义sl_line()-----start-------------这部分没写完现在无效--------+
//|sl is stopline,find object of "sl_bl","sl_sl"
//|line value as the value of sl
bool sl_line( string linename)
{
bool res=false;
if (ObjectFind(0,linename)==0 && Symbol()==StringSubstr(linename,5,0))
{
p_sl=NormalizeDouble(ObjectGetDouble(0,linename,OBJPROP_PRICE,0),Digits);
res=true;
Print("找到了!值:",p_sl);
}
else Print("ERR(",GetLastError(),")没有损线");
return(res);
}
//+----------------sl_line()---over---------------------------------+