(转)
[转] MT4自定义函数库
//获取最近持仓单
|
//订单查询函数
/* 功能:
根据MAGIC编号和订单类型,查找相应订单,并返回订单编号(ticket)
如果没有找到返回0; */
int SearchOrder (int MAGIC,int ORDERTYPE)
{
int i=0;
for ( int z = OrdersTotal() - 1; z >= 0; z -- )
{
if ( !OrderSelect( z, SELECT_BY_POS ) )
{
Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );
continue;
}
if (OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber() != MAGIC ) continue;
if ( OrderType()==ORDERTYPE ) i=OrderTicket();
}
return(i);
}
//移动止损函数
/*功能: 检查以MAGIC标记的当前图表的以成交订单。当盈利点数大于一定值(MOVE)的时候,移动止损价格。每次移动STEP点数。 三个参数均为整数。不论成功与否均返回0;*/ |
int MoveStop ( int MAGIC, int MOVE, int STEP) // MoveStop(MAGIC, MOVE);
{
if (MOVE<=0) return(0);
double MoveStopPrice;
for ( int z = OrdersTotal() - 1; z >= 0; z -- )
{
if ( !OrderSelect( z, SELECT_BY_POS ) )
{
Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );
continue;
}
if (OrderSymbol()!=Symbol())continue;
if (OrderMagicNumber() != MAGIC )continue;
switch (OrderType())
{
case OP_BUY :
{
MoveStopPrice=NormalizeDouble(Bid-MOVE*Point,Digits);
if (MoveStopPrice>OrderOpenPrice() && OrderStopLoss()<MoveStopPrice)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),MoveStopPrice+STEP*Point,OrderTakeProfit(),OrderExpiration()))
{
Alert("MoveStop_OrderModify Error #",GetLastError());
return(-1);
}
}
break;
}
case OP_SELL:
{
MoveStopPrice=NormalizeDouble(Ask+MOVE*Point,Digits);
if (MoveStopPrice<OrderOpenPrice() && OrderStopLoss()>MoveStopPrice)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),MoveStopPrice-STEP*Point,OrderTakeProfit(),OrderExpiration()))
{
Alert("MoveStop_OrderModify Error #",GetLastError());
return(-1);
}
}
break;
}
default: break;
}
}
return(0);
}
//判断均线交叉函数
{
return(i); |
//移动止损
void tr(int magic, int triling_i)
{
for ( int k=0; k< OrdersTotal(); k++)
{
OrderSelect( k, SELECT_BY_POS);
if ( OrderMagicNumber() ==magic )
{
if (OrderType() ==OP_BUY & & NormalizeDouble(Ask-OrderOpenPrice(), Digits) > =triling_i*Point & & OrderStopLoss()< NormalizeDouble(Ask-triling_i*Point,Digits))
{
if (!OrderModify(OrderTicket(), OrderOpenPrice(),NormalizeDouble(Ask-triling_i*Point,Digits),OrderTakeProfit(),0,Blue)){
Print("Trailing stop error Buy :"+error(GetLastError()));
}
}
if (OrderType()==OP_SELL && NormalizeDouble(Bid+triling_i*Point,Digits)<=OrderOpenPrice() && (OrderStopLoss()>NormalizeDouble(Bid+triling_i*Point,Digits) || OrderStopLoss()==0)) {
if (!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid+triling_i*Point,Digits),OrderTakeProfit(),0,Red)){
Print("Trailing stop error Sell :"+error(GetLastError()));
}
}
}
}
}
//在图表上输出多行的Comment(注释)
extern int MaxCommentsToShow=25;
//An array to hold all of the comments
string allcomments[];
//+------------------------------------------------------------------+
//| Manage comments - Written by DMS |
//+------------------------------------------------------------------+
void managecomments(string addcomment)
{
string tempcomments[];
int commentscroll;
string output;
int CommentCount = ArrayRange(allcomments, 0);
if(CommentCount<MaxCommentsToShow)
{
ArrayResize(tempcomments,CommentCount+1);
ArrayCopy(tempcomments,allcomments,1,0,WHOLE_ARRAY);
}
else
{
ArrayResize(tempcomments,MaxCommentsToShow);
ArrayCopy(tempcomments,allcomments,1,0,MaxCommentsToShow-1);
}
tempcomments[0]=addcomment;
CommentCount = ArrayRange(tempcomments, 0);
ArrayResize(allcomments,CommentCount);
ArrayCopy(allcomments,tempcomments,0,0,CommentCount);
for(commentscroll=0;commentscroll<CommentCount;commentscroll++)
{
output = output + allcomments[commentscroll] +"\n";
}
Comment(output);
}
通过Object在屏幕上写字
void writetext(string Labelname,string da
// 通过Object写文字(文件名, 内容, X坐标, Y坐标, 字色, 文字大小 )
{
ObjectDelete(Labelname);
ObjectCreate(Labelname, OBJ_LABEL, 0, 0, 0);
ObjectSetText(Labelname, da
ObjectSet(Labelname, OBJPROP_CORNER, 0);
ObjectSet(Labelname, OBJPROP_XDISTANCE, x);
ObjectSet(Labelname, OBJPROP_YDISTANCE, y);
}
例:int start() { writetext("例1", "文字屏幕显示", 100, 400, Red, 25) }
//获得开仓价
//在start函数里的使用的句子:
double LastBuyOpenPrice;
LastBuyOpenPrice=GetBuyOrderOpenPrice();
--------------//自定义函数----------------------------------------
double GetBuyOrderOpenPrice()
{
for ( int i = OrdersTotal()-1; i>=0; i-- ) //从单手数减减循环查找
{
OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); // 选择交易中的订单类型
if(OrderSymbol() == Symbol() && OrderType() == OP_BUY ) // 单符号为指定符号 且 单类型为 买单
{
return( OrderOpenPrice() ); //返回单开仓价
}
}
return( -1 ); //如果没有指定的持仓单,则返回(-1)
}
如果真
double iif(bool a, double b, double c) {if(a) return(b);return(c);}
判断开仓时间间隔是否达到
bool checkM()
{
int total =OrdersTotal();
bool Hcounter;
datetime opentm = OrderOpenTime();
if (total>=1)
OrderSelect(total-1,SELECT_BY_POS,MODE_TRADES);
if (TimeCurrent()-opentm >Period()*60) Hcounter =true;
else Hcounter =false;
return (Hcounter);
}
//=======================================================================
int Crossed( double 均线1, double 均线2)
{
static int 最后方向 =0;
static int 当前方向 =0;
static bool 第一时间 = true; // 在首个装载时不工作, 等待第一个穿越
if(第一时间==true)
{第一时间 =false;
return (0);
}
if(均线1 > 均线2)
当前方向 = 2; // 多头市场
if(均线1 < 均线2)
当前方向 = 1; // 空头市场
if(当前方向 != 最后方向) // 多空已改变
{最后方向 = 当前方向;
return (最后方向);
else return (0); // 没改变
}
return ();
//SAR跟踪止损
void TR(int 魔术号){
for (int k=0;k<OrdersTotal();k++){
double SAR=iSAR(Symbol(), 0, 0.02, 0.2, 1);
OrderSelect(k,SELECT_BY_POS);
if (OrderMagicNumber()==魔术号){
if (OrderType()==OP_BUY && OrderStopLoss()<SAR && Ask>SAR) {
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SAR,0,0,Blue)){
Print("Trailing stop error Buy :"+GetLastError());
{
{
if (OrderType()==OP_SELL && OrderStopLoss()>SAR && Bid<SAR) {
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SAR,0,0,Red)){
Print("Trailing stop error Sell :"+GetLastError());
{
{
{
}
}
//获得最新订单的开仓价
start函数里的使用:
==================================================
double LastBuyOpenPrice;
LastBuyOpenPrice=GetBuyOrderOpenPrice();
==================================================
//函数定义
double GetBuyOrderOpenPrice()//如果没有指定的持仓单,则返回-1
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY )
{
return(OrderOpenPrice());
}
}
return(-1);
}
//获得最近出场单收益(可能有错)
int GetLastClosePositionProfit()
{
return(iBarShift(NULL,0,GetLastHistOrderCloseTime(),false)); //根据平掉单的时间,来取得这个单平掉时的K线序号
}
//-----+
datetime GetLastHistOrderCloseTime() //返回的是最近历史平掉单的时间
{
for(int i=OrdersHistoryTotal()-1; i>=0; i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
return(OrderProfit()); //但是这里却返回的是最近历史平掉单的获利值
}
}
}
联系客服