100 Nord券
以前能用的,现在新版本编译错误,请高手修改,附件有一个指标源码,一个是EA源码。根据指标高于80卖,低于80买,并设有止损止盈。
发表不了附件,全部源码贴上。
画线指标正常,源码如下:(文件名为 zdncg ,EA运行时直接调用),下面源码是画线指标和EA两个,有分界线,别复制错了
//+------------------------------------------------------------------+
//| zdncg .mq4 |
//| kingf_c@126.com |
//| www.ss-log.net |
//+------------------------------------------------------------------+
#property copyright "sun5kong@163.com"
#property link ".net"
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 Yellow
#property indicator_color2 Gray
#property indicator_color3 Gray
#property indicator_color4 Gray
#property indicator_color5 Gray
#property indicator_color6 Gray
#property indicator_color7 Blue
#property indicator_color8 Red
//--- input parameters
extern int NPeriod=8;
//--- buffers
double ZDNCG[];
double ZDNCG1[];
double ZDNCG2[];
double Line80[];
double Line_80[];
double Line60[];
double Line_60[];
double ExtMapBuffer[];
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
SetIndexDrawBegin(1,NPeriod);
SetIndexBuffer(0,ZDNCG);
SetIndexLabel(0,"YDNCG");
SetIndexStyle(1,DRAW_NONE);
SetIndexBuffer(1,ExtMapBuffer);
SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1);
SetIndexBuffer(2,Line80);
SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,1);
SetIndexBuffer(3,Line_80);
SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,1);
SetIndexBuffer(4,Line60);
SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,1);
SetIndexBuffer(5,Line_60);
SetIndexStyle(6,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexDrawBegin(6,NPeriod);
SetIndexBuffer(6,ZDNCG2);
SetIndexStyle(7,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexBuffer(7,ZDNCG1);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int limit,i;
if(Bars<NPeriod){ return(0);}
if(counted_bars>0) counted_bars--;
ExtCountedBars = counted_bars;
limit=Bars-counted_bars;
ema();
for(i=0;i<limit;i++){
double dval =(ExtMapBuffer-50)*2;
if(dval<0)
{
ZDNCG2 = dval;
// ZDNCG = 0;
}else
{
ZDNCG1 = dval;
// ZDNCG2 = 0;
}
ZDNCG = dval;
Line80 = 80;
Line_80 = -80;
Line60 = 60;
Line_60 = -60;
}
//----
//----
return(0);
}
//+------------------------------------------------------------------+
void ema(int MA_Period = 3)
{
double pr=2.0/(MA_Period+1);
int pos=Bars-2;
if(ExtCountedBars>2) pos=Bars-ExtCountedBars-1;
//---- main calculation loop
while(pos>=0)
{
if(pos==Bars-2){
ExtMapBuffer[pos+1]=getVal(pos+1);
}
ExtMapBuffer[pos]=getVal(pos)*pr+ExtMapBuffer[pos+1]*(1-pr);
pos--;
}
}
double getVal(int i)
{
double LowN = Low[iLowest(NULL,0,MODE_LOW,NPeriod,i)];
double HighN = High[iHighest(NULL,0,MODE_HIGH,NPeriod,i)];
return ((Close-LowN)*100/(HighN-LowN));
}
分界线--------分界线-------------分界线-----------------分界线-----------------分界线--------------分界线----------------------------
EA源码,以前能用,现在不能用需要高手修改
//+------------------------------------------------------------------+
//| EA起点.mq4 |
//| kingf_c@126.com |
//| www.ss-log.net |
//+------------------------------------------------------------------+
//20110913改进版
#property copyright "kingf_c@126.com"
//扩展参数
extern int LotsType=1;//资金管理模式1:固定。2:风险系数管理。3:资金比例
extern double LOTS=0.1;
extern double MaximumRisk=0.02;//风险系数
extern double LotR=0.01;//资金比例//这种方式
extern double Slip_Page=3;//滑点
extern int TP=55,SL=50;//大于平台最小停损限制
extern int TS=10; //不同平台对最低停损间距不同,未必能改单止损。
//指标参数
extern int NPeriod=8;//YJD指标参数
//基本函数用
string EA名称="EA";
int objtick=0; //暂时忽略这个
int Magic=304;//魔法数
//==============系统参数管理
double Point_;//更方便
//函数:历史成交
int His.b,His.s;
double His.LastBuyTime,His.LastSellTime;
//函数:检查持仓
double b=0,s=0,mode=0,ss,bb,com;//买、卖、持仓模式,暂时没看到com的赋值作用
double LastOrderType,LastPricesell,LastPricebuy,LastPrice,OpenSells,OpenSellStops,OpenSellLimits,OpenBuys,OpenBuyStops,OpenBuyLimits;
double OrderProfits,BuysProfits,SellsProfits;//分开买和卖的获利情况2011.9.18
//函数:资金管理
double LotsMin,LotsMax,LOT;
//函数:买、卖
double ASK,BID,TPP,SLL,LOTMM,TICKET;//买卖辅助函数里面
int Stop_Level;
int Print_Order=0;//下单的时候,打出Order的信息。本EA用自定义的
int 判断=0;//暂时没看到判断的作用
//指标参数
double YJD.0,YJD.1;
//策略参数
datetime EA启动时间;
datetime 买.下单时,卖.下单时;
//+------------------------------------------------------------------+
//| initialization function |
//+------------------------------------------------------------------+
int init()
{
//----通用参数和赋值管理
//对Point_管理
if(MarketInfo(Symbol(), MODE_DIGITS)==4 || MarketInfo(Symbol(), MODE_DIGITS)==2){Point_=Point;}
if(MarketInfo(Symbol(), MODE_DIGITS)==5 || MarketInfo(Symbol(), MODE_DIGITS)==3){Point_=Point*10;}
if(StringFind(Symbol(),"GOLD",0)!=-1 || StringFind(Symbol(),"XAU",0)!=-1){Point_=Point_*10;}//对黄金放大10倍,对不同的平台要注意货币对名称不同
//对挂单的限制
Stop_Level= MarketInfo(Symbol(),MODE_STOPLEVEL);
EA启动时间=TimeCurrent();
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
历史成交(EA启动时间);
检查持仓();
资金管理();
指标管理();
策略管理();
注释();
//止损追踪管理();
//----
return(0);
}
void 注释(){
string sComment = "";
string sp = "----------------------------------------\n";
string NL = "\n";
sComment=sComment+"EA运行始于:"+TimeToStr(EA启动时间,TIME_SECONDS)+NL+sp;
sComment=sComment+"开仓止盈:"+TP+"点 | 止损:"+SL+"点 | 移动追踪:"+TS+"点"+NL;
Comment(sComment);
}
//+------------------------------------------------------------------+
void 指标管理(){
YJD.0=iCustom(NULL,0,"zdncg",NPeriod,0,0);
YJD.1=iCustom(NULL,0,"zdncg",NPeriod,0,1);
}
void 策略管理(){
if(His.LastBuyTime==0){
//EA运行期间无成交记录,执行买策略
买策略();
}
if(His.LastSellTime==0){
卖策略();
}
}
void 买策略(){
if(b==0 //只可以有一单
&&YJD.0<-80
//&&YJD.1>=-80//
&&买.下单时!=Time[0]
){
买(0,LOTS);
买.下单时=Time[0];
}
}
void 卖策略(){
if(s==0
&&YJD.0>80
//&&YJD.1<=80//
&&卖.下单时!=Time[0]
){
卖(0,LOTS);
卖.下单时=Time[0];
}
}
//从指定的开始时间,计算历史成交
void 历史成交(datetime start_time){
if(start_time<=0){return(0);}
His.b=0;His.s=0;//这两个未必用的上
His.LastBuyTime=0;
His.LastSellTime=0;
for(int cnt=0;cnt<=OrdersHistoryTotal();cnt++){
OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if(OrderOpenTime()>start_time){
if(OrderType()==OP_BUY){
His.LastBuyTime=OrderOpenTime();
}
if(OrderType()==OP_SELL){
His.LastSellTime=OrderOpenTime();
}
}
}
}
//有效赋值,迟早都要加入全货币的?
void 检查持仓(){
b=0;s=0;mode=0;//注意这个地方必须重新赋值
//最后的买卖价,对策略有帮助.或者加入最后的买卖时间控制,效果可能更好。
LastOrderType=0;LastPricesell=0;LastPricebuy=0;LastPrice=0;
OpenSells=0;OpenSellStops=0;OpenSellLimits=0;OpenBuys=0;OpenBuyStops=0;OpenBuyLimits=0;
OrderProfits=0;BuysProfits=0;SellsProfits=0;
for (int cnt=0; cnt<=OrdersTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol()&&OrderMagicNumber()==Magic)
{
mode=OrderType();LastOrderType=OrderType();
LastPrice=OrderOpenPrice();
OrderProfits+=OrderProfit();
if(OrderType()==OP_SELL) {OpenSells++;s++; LastPricesell=OrderOpenPrice();}//赋值有点冲突吧
if(OrderType()==OP_SELLSTOP) {OpenSellStops++;}
if(OrderType()==OP_SELLLIMIT){OpenSellLimits++;}
if(OrderType()==OP_BUY) {OpenBuys++;b++; LastPricebuy=OrderOpenPrice();}
if(OrderType()==OP_BUYSTOP) {OpenBuyStops++;}
if(OrderType()==OP_BUYLIMIT){OpenBuyLimits++;}
}
}
}
void 资金管理(){
LotsMin = MarketInfo(Symbol(), MODE_MINLOT);
LotsMax = MarketInfo(Symbol(), MODE_MAXLOT);
if(LotsType==1){
LOT=LOTS;
}
if(LotsType==2){
LOT=NormalizeDouble(MaximumRisk * AccountBalance()/AccountLeverage(),2);
}
if(LotsType==3){
LOT=NormalizeDouble(AccountBalance()*LotR,2);
}
//修正lot
if(LOT<LotsMin){LOT=LotsMin;}
if(LOT>LotsMax){LOT=LotsMax;}
}
//通用函数有点麻烦。引用外部参数,需要注意objtick++;
void 买(string Sy,double Sy_Lot){
if (Sy=="" || Sy=="0") Sy=Symbol();
LOTMM=NormalizeDouble(Sy_Lot, 2);
if(Sy_Lot>MarketInfo(Sy, MODE_MINLOT)){LOTMM=NormalizeDouble(Sy_Lot, 2);}
if(Sy_Lot<MarketInfo(Sy, MODE_MINLOT)){LOTMM=MarketInfo(Sy, MODE_MINLOT);}
if(Sy_Lot>MarketInfo(Sy, MODE_MAXLOT)){LOTMM=MarketInfo(Sy, MODE_MAXLOT);}
if(Symbol()=="XAGUSDFXF"){LOTMM = NormalizeDouble(LOTMM, 1);}
ASK = NormalizeDouble(MarketInfo(Sy, MODE_ASK), MarketInfo(Sy, MODE_DIGITS));
if(AccountFreeMarginCheck(Sy,OP_BUY,LOTMM)<=0 || GetLastError()==134) {return;}
if(AccountFreeMarginCheck(Sy,OP_SELL,LOTMM)<=0 || GetLastError()==134) {return;}
if (TP==0) { TPP=0; } if(TP>0) { TPP=ASK+TP*MarketInfo(Sy, MODE_POINT); }
if (SL==0) { SLL=0; } if(SL>0) { SLL=ASK-SL*MarketInfo(Sy, MODE_POINT); }
TICKET=OrderSend(Sy,OP_BUY,LOTMM,ASK,Slip_Page,SLL,TPP,"M"+Period()+""+EA名称+"",Magic,0,Violet);
if(TICKET<0){
Print("OrderSend失败#",GetLastError());
}else{
if(Print_Order==1){
Text("Ticket"+TICKET,"B"+DoubleToStr(Sy_Lot,2),Time[0],ASK-(25*MarketInfo(Sy, MODE_POINT)),10,White);
}
}
}//end 买上
//------------------------------------------------------------------------------------
void 卖(string Sy,double Sy_Lot){
if (Sy=="" || Sy=="0"){ Sy=Symbol();}
LOTMM=NormalizeDouble(Sy_Lot, 2);
if(Sy_Lot>MarketInfo(Sy, MODE_MINLOT)){LOTMM=NormalizeDouble(Sy_Lot, 2);}
if(Sy_Lot<MarketInfo(Sy, MODE_MINLOT)){LOTMM=MarketInfo(Sy, MODE_MINLOT);}
if(Sy_Lot>MarketInfo(Sy, MODE_MAXLOT)){LOTMM=MarketInfo(Sy, MODE_MAXLOT);}
if(Symbol()=="XAGUSDFXF"){LOTMM = NormalizeDouble(LOTMM, 1);}
if(AccountFreeMarginCheck(Sy,OP_BUY,LOTMM)<=0 || GetLastError()==134) {return;}
if(AccountFreeMarginCheck(Sy,OP_SELL,LOTMM)<=0 || GetLastError()==134){ return;}
BID = NormalizeDouble(MarketInfo(Sy, MODE_BID), MarketInfo(Sy, MODE_DIGITS));
if (TP==0) { TPP=0; } if(TP>0) { TPP=BID-(TP*MarketInfo(Sy, MODE_POINT)); }
if (SL==0) { SLL=0; } if(SL>0) { SLL=BID+(SL*MarketInfo(Sy, MODE_POINT)); }
TICKET=OrderSend(Sy,OP_SELL,LOTMM,BID,Slip_Page,SLL,TPP,"M"+Period()+""+EA名称+"",Magic,0,GreenYellow);
if(TICKET<0){
Print("OrderSend失败#",GetLastError());
}else{
if(Print_Order==1){
Text("Ticket"+TICKET,"S"+DoubleToStr(Sy_Lot,2),Time[0],BID+(25*MarketInfo(Sy, MODE_POINT)),10,White);
}
}
//ObjectCreate("dd2myxE"+DoubleToStr(objtick,0), OBJ_TEXT, 0, Time[0], High[0]+(25*MarketInfo(Sy, MODE_POINT)));
//ObjectSetText("dd2myxE"+DoubleToStr(objtick,0),"S",22,"Arial",White); //by 7x51
}//end 卖下
//主动性挂单离场,不能挂单的时候,虚拟离场。或者需要全局变量Exit_Price
void 挂单止损(int Ticket,double Exit_Price,int ST_Point){
if(Exit_Price<=0 || ST_Point==0){ return(0);}//无效参数离开
OrderSelect(Ticket,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic){
if(OrderType()==OP_BUY){
if(Exit_Price>0){
if(MathAbs(Exit_Price-Bid)>Point_*Stop_Level){//可以的话就直接修改止损
OrderModify(OrderTicket(),OrderOpenPrice(),Exit_Price,OrderTakeProfit(),0,Blue);
}else{
if(Bid>=Exit_Price){
OrderClose(OrderTicket(),OrderLots(),Bid,Slip_Page,Red);
}
}
}//end if Exit_Price>0
//可正负,相对现价?相对开盘价,当然相对现价好啦。可以变相移动止损。
if(MathAbs(ST_Point)>Stop_Level){
OrderModify(OrderTicket(),OrderOpenPrice(),Bid+ST_Point*Point_,OrderTakeProfit(),0,Blue);
}else{
挂单止损(OrderTicket(),Bid+ST_Point*Point_,0);
//能否循环调用呢?这个时候的价格是什么情况呢?或者用全局变量吧。未测试
}
//end ST_Point
}//end OP_BUY
if(OrderType()==OP_SELL){
if(Exit_Price>0){
if(MathAbs(Ask-Exit_Price)>Point_*Stop_Level){
OrderModify(OrderTicket(),OrderOpenPrice(),Exit_Price,OrderTakeProfit(),0,Blue);
}else{
if(Ask<=Exit_Price){
OrderClose(OrderTicket(),OrderLots(),Ask,Slip_Page,Red);
}
}
}//end Exit_Price
if(MathAbs(ST_Point)>Stop_Level){
OrderModify(OrderTicket(),OrderOpenPrice(),Ask-ST_Point*Point_,OrderTakeProfit(),0,Blue);
}else{
挂单止损(OrderTicket(),Ask-ST_Point*Point_,0);
}//end ST_Point
}//end OP_SELL
}
}//未测试
void 挂单止盈(int Ticket,double Exit_Price,int ST_Point){
if(Exit_Price<=0 || ST_Point==0){ return(0);}//无效参数离开
OrderSelect(Ticket,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic){
}
}
void 止损追踪管理()
{
int cnt;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()&& OrderMagicNumber()==Magic ) //&& OrderMagicNumber()==MAGICMA)
{
if (OrderType()==OP_SELL)
{
if(TS>0)
{
if((OrderOpenPrice()-MarketInfo(Symbol(), MODE_ASK))>(MarketInfo(Symbol(), MODE_POINT)*TS))
{
if((OrderStopLoss()>(MarketInfo(Symbol(), MODE_ASK)+MarketInfo(Symbol(), MODE_POINT)*TS)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),MarketInfo(Symbol(), MODE_ASK)+MarketInfo(Symbol(), MODE_POINT)*TS,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
if (OrderType()==OP_BUY)
{
if(TS>0)
{
if(MarketInfo(Symbol(), MODE_BID)-OrderOpenPrice()>MarketInfo(Symbol(), MODE_POINT)*TS)
{
if(OrderStopLoss()<MarketInfo(Symbol(), MODE_BID)-MarketInfo(Symbol(), MODE_POINT)*TS)
{
OrderModify(OrderTicket(),OrderOpenPrice(),MarketInfo(Symbol(), MODE_BID)-MarketInfo(Symbol(), MODE_POINT)*TS,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
}
}
}//end 止损追踪
//对比修正版 by kingf_c@126.com
void CloseAllOrder()//平掉所有仓位包括挂单
{ //由牛版主提供
int tick[200];
int PingFlag=0,PingStop=0;
int j=0;
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{ j=j+1;
tick[j]=OrderTicket();
Print("全部平仓! :",tick[j]);
}
}
if (j!=0)
{
for(i=1;i<=j;i++)
{
OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUY)
{
if(OrderClose(OrderTicket(),OrderLots(),Bid,Slip_Page)==false)
{
PingFlag=1;
Print("多头平仓失败"+GetLastError());
}
}
//OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUYSTOP)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("多头挂单撤销失败"+GetLastError());
}
}
//OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUYLIMIT)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("多头挂单撤销失败"+GetLastError());
}
}
if(OrderType()==OP_SELL)
{
if(OrderClose(OrderTicket(),OrderLots(),Ask,Slip_Page)==false)
{
PingFlag=1;
Print("空头平仓失败"+GetLastError());
}
}
if(OrderType()==OP_SELLSTOP)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("空头挂单撤销失败"+GetLastError());
}
}
if(OrderType()==OP_SELLLIMIT)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("空头挂单撤销失败"+GetLastError());
}
}
}
}
if (PingFlag==0) PingStop=1;
}//end closeall
//平掉所有仓位包括挂单,关闭指定魔法号的单,好笨,其实不需要另外建立一个的
void CloseMagicOrder(int magic)
{ //由牛版主提供
int tick[200];
int PingFlag=0,PingStop=0;
int j=0;
for(int i=0;i<OrdersTotal();i++)//重新改正回来,终于能正常平仓了
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
{ j=j+1;
tick[j]=OrderTicket();
Print("全部平仓! :",tick[j]);
}
}
if (j!=0)
{
for(i=1;i<=j;i++)
{
OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUY)
{
if(OrderClose(OrderTicket(),OrderLots(),Bid,Slip_Page)==false)
{
PingFlag=1;
Print("多头平仓失败"+GetLastError());
}
}
//OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUYSTOP)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("多头挂单撤销失败"+GetLastError());
}
}
//OrderSelect(tick, SELECT_BY_TICKET);
if(OrderType()==OP_BUYLIMIT)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("多头挂单撤销失败"+GetLastError());
}
}
if(OrderType()==OP_SELL)
{
if(OrderClose(OrderTicket(),OrderLots(),Ask,Slip_Page)==false)
{
PingFlag=1;
Print("空头平仓失败"+GetLastError());
}
}
if(OrderType()==OP_SELLSTOP)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("空头挂单撤销失败"+GetLastError());
}
}
if(OrderType()==OP_SELLLIMIT)
{
if(OrderDelete(OrderTicket(),CLR_NONE)==false)
{
PingFlag=1;
Print("空头挂单撤销失败"+GetLastError());
}
}
}
}
if (PingFlag==0) PingStop=1;
}
//关闭单张
int 关闭单(int Ticket){
int PingFlag=0;
if(Ticket<1){ return(0);}
if(OrderSelect(Ticket,SELECT_BY_TICKET)){
if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol()){
if(OrderType()==OP_BUY){//关闭买
if(OrderClose(OrderTicket(),OrderLots(),Bid,Slip_Page)==false)
{
PingFlag=1;
Print("多头平仓失败"+GetLastError());
}
}
if(OrderType()==OP_SELL){
if(OrderClose(OrderTicket(),OrderLots(),Ask,Slip_Page)==false)
{
PingFlag=1;
Print("空头平仓失败"+GetLastError());
}
}
}
}else{
Alert("Ticket is error!");
}
return(PingFlag);
}
//返回参数,判断关闭是否成功
int 关闭卖()
{判断=0;//这个位置为什么要赋值呢?
double myAsk;
double myBid;
int myTkt;
double myLot;
int myTyp;
int i,error_num=0;//返回error_num判断未平仓的
bool result = false;
for(i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
if( s>0 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) //OrderSymbol()==Symbol() )//&& OrderMagicNumber()==Magic)
{
myAsk=MarketInfo(OrderSymbol(),MODE_ASK);
myBid=MarketInfo(OrderSymbol(),MODE_BID);
myTkt=OrderTicket();
myLot=OrderLots();
myTyp=OrderType();
if(myTyp==OP_SELL){
result =OrderClose(myTkt, myLot, myAsk, Slip_Page, Red);
if(result==false){
error_num++;//有无意义呢?
Alert("Close ",Symbol(),"还有",OrdersTotal(),"账单未结 现在盈亏::",AccountProfit(),"");//即使未完全关闭都不是失败..
}
}
/* switch( myTyp )
{
case OP_SELL :result = OrderClose(myTkt, myLot, myAsk, Slip_Page, Red);
com=2;//com的做用是什么呢?
Alert("Close ",Symbol()," 关闭失败 还有",OrdersTotal(),"账单未结 现在盈亏::",AccountProfit(),"");
}
end switch*/
} //if
} //for
return(error_num);
} // closeeverything
//------------------------------------------------------------------------------------
int 关闭买()
{判断=0;
double myAsk;
double myBid;
int myTkt;
double myLot;
int myTyp;
int i,error_num=0;
bool result = false;
for(i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
if( b>0 && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic) //OrderSymbol()==Symbol() )//&& OrderMagicNumber()==Magic)
{
myAsk=MarketInfo(OrderSymbol(),MODE_ASK);
myBid=MarketInfo(OrderSymbol(),MODE_BID);
myTkt=OrderTicket();
myLot=OrderLots();
myTyp=OrderType();
if(myTyp==OP_BUY){
result = OrderClose(myTkt, myLot, myBid, Slip_Page, Yellow);
if(result==false){
error_num++;
Alert("Close ",Symbol(),"还有",OrdersTotal(),"账单未结 现在盈亏::",AccountProfit(),"");//同上,未完全关闭也不是失败
}
}
/* switch( myTyp )
{
//Close opened long positions
case OP_BUY :result = OrderClose(myTkt, myLot, myBid, Slip_Page, Yellow);
com=2;
Alert("Close ",Symbol()," 关闭失败 还有",OrdersTotal(),"账单未结 现在盈亏::",AccountProfit(),"");
}
*/
} //if
} //for
return(error_num);
} // closeeverything
//必须要控制周期内的开仓数。
/*
void 一天的单(){
int daystart=StrToTime(TimeToStr(TimeCurrent(),TIME_DATE));//时间转换
int cnt=0;
//现在挂单
for (cnt=0; cnt<=OrdersTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol()) //未加入魔法数,测试中
{
if(OrderOpenTime()>daystart){
一天的单数++;
}
}
}
//历史单
for (cnt=0; cnt<=OrdersHistoryTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);
if (OrderSymbol()==Symbol()) //未加入魔法数,测试中
{
if(OrderOpenTime()>daystart){
一天的单数++;
}
}
}
}
*/
//工具函数
//在屏幕上显示标签,统一标签名称。免得搞乱了。
//LabelName:标签名称;LabelDoc:文本内容;LabelX:标签X位置;LabelY:标签Y位置;
//DocSize:文本字号;DocStyle:文本字体;DocColor:文本颜色,Conner:角落,默认1即右上角
void Label(string LabelName,string LabelDoc,int LabelX,int LabelY,int DocSize=12,string DocStyle="arial",color DocColor=Red,int Conner=1)
{
ObjectDelete(LabelName);
ObjectCreate(LabelName, OBJ_LABEL, 0, 0, 0);
ObjectSetText(LabelName,LabelDoc,DocSize,DocStyle,DocColor);
ObjectSet(LabelName,OBJPROP_CORNER,Conner);
ObjectSet(LabelName, OBJPROP_XDISTANCE, LabelX);
ObjectSet(LabelName, OBJPROP_YDISTANCE, LabelY);
}
void Text(string TextName,string TextDoc,datetime Time1,double Price1,int Size=12,color Color=White){
ObjectDelete(TextName);
ObjectCreate(TextName,OBJ_TEXT,0,Time1,Price1);
ObjectSetText(TextName,TextDoc,Size,"arial",Color);
}
//辅助横线, 增加颜色控制,文字标记
void HLine(string HLineName,double Price1,color Color=Red)
{
ObjectDelete(HLineName);
ObjectCreate(HLineName,OBJ_HLINE,0,0,0);
ObjectSet(HLineName,OBJPROP_PRICE1,Price1);
ObjectSet(HLineName,OBJPROP_COLOR,Color);
}
//写记录文件
void LogWrite (string filename, string mydata)
{
int myhandle;
string gregorian=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS);
Print(" "+Symbol()+" "+mydata+" "+gregorian);
// don't log anything if testing or if user doesn't want it
if(IsTesting()) return(0);
// if(KillLogging) return(0);
myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";");
if(myhandle>0)
{
FileSeek(myhandle,0,SEEK_END);
FileWrite(myhandle, " "+Symbol()+" "+mydata+" "+gregorian);
FileClose(myhandle);
}
}
我来回答