外汇EA:简单的订单管理(二)

阅读 :
欢迎访问 外 汇 邦 WWW.WaiHuiBang.com

 //+------------------------------------------------------------------+

//| 如果执行达到此点, |
//| 说明没有挂单和开仓。 |
//+------------------------------------------------------------------+
//---- 放置BuyStop 和 SellStop:
double _OpenPriceLevel, _StopLossLevel, _TakeProfitLevel;
_OpenPriceLevel = NormalizeDouble( Ask + Luft*Point, Digits );

if ( StopLoss > 0 )

{ _StopLossLevel = NormalizeDouble( _OpenPriceLevel -
StopLoss*Point, Digits ); }

else
{ _StopLossLevel = 0.0; }

if ( TakeProfit > 0 )

{ _TakeProfitLevel = NormalizeDouble( _OpenPriceLevel + 
TakeProfit*Point, Digits ); }

else
{ _TakeProfitLevel = 0.0; }

if ( OrderSend ( Symbol(), OP_BUYSTOP, Lot, _OpenPriceLevel,
5, _StopLossLevel, _TakeProfitLevel, "",
_MagicNumber ) < 0 )

{
Alert( "OrderSend Error #", GetLastError() );
return(-1);
}

_OpenPriceLevel = NormalizeDouble(Bid - Luft*Point, Digits);

if ( StopLoss > 0 )

{ _StopLossLevel = NormalizeDouble( _OpenPriceLevel +
StopLoss*Point, Digits ); }

else
{ _StopLossLevel = 0.0; }

if ( TakeProfit > 0 )

{ _TakeProfitLevel = NormalizeDouble( _OpenPriceLevel - 
TakeProfit*Point, Digits ); }

else
{ _TakeProfitLevel = 0.0; }

if ( OrderSend ( Symbol(), OP_SELLSTOP, Lot, _OpenPriceLevel,
5, _StopLossLevel,
_TakeProfitLevel, "", _MagicNumber ) < 0 )

{
Alert( "OrderSend Error #", GetLastError() );
return(-1);
}

return(0);
}

现在让我们写出可以简化控制建仓代码的函数,它必须用每个类型的定单进行搜索,然后将这些信息存储在全局变量里,程序如下:

// 在定单特性中的整体变量会被储存:
int _BuyTicket = 0, _SellTicket = 0, _BuyStopTicket = 0;

int _SellStopTicket = 0, _BuyLimitTicket = 0, _SellLimitTicket = 0;

double _BuyLots = 0.0, _SellLots = 0.0, _BuyStopLots = 0.0; 

double _SellStopLots = 0.0, _BuyLimitLots = 0.0, 
_SellLimitLots = 0.0;

double _BuyOpenPrice = 0.0, _SellOpenPrice = 0.0, 
_BuyStopOpenPrice = 0.0;

double _SellStopOpenPrice = 0.0, _BuyLimitOpenPrice = 0.0,
_SellLimitOpenPrice = 0.0;

double _BuyStopLoss = 0.0, _SellStopLoss = 0.0, _BuyStopStopLoss = 0.0;

double _SellStopStopLoss = 0.0, _BuyLimitStopLoss = 0.0, _SellLimitStopLoss = 0.0;

double _BuyTakeProfit = 0.0, _SellTakeProfit = 0.0,
_BuyStopTakeProfit = 0.0;

double _SellStopTakeProfit = 0.0, _BuyLimitTakeProfit = 0.0,
_SellLimitTakeProfit = 0.0;

datetime _BuyOpenTime = -1, _SellOpenTime = -1, 
_BuyStopOpenTime = -1;

datetime _SellStopOpenTime = -1, _BuyLimitOpenTime = -1,
_SellLimitOpenTime = -1;

double _BuyProfit = 0.0, _SellProfit = 0.0, _BuySwap = 0.0, 

_SellSwap = 0.0;
double _BuyCommission = 0.0, _SellCommission = 0.0;

string _BuyComment = "", _SellComment = "", _BuyStopComment = ""; 

string _SellStopComment = "", _BuyLimitComment = "", 
_SellLimitComment = "";

datetime _BuyStopExpiration = -1, _SellStopExpiration = -1;
datetime _BuyLimitExpiration = -1, _SellLimitExpiration = -1;

void OneTypeOrdersInit( int magic )
{
// 变量归零:
_BuyTicket = 0; _SellTicket = 0; _BuyStopTicket = 0;
_SellStopTicket = 0; _BuyLimitTicket = 0; _SellLimitTicket = 0;

_BuyLots = 0.0; _SellLots = 0.0; _BuyStopLots = 0.0;
_SellStopLots = 0.0; _BuyLimitLots = 0.0; _SellLimitLots = 0.0;

_BuyOpenPrice = 0.0; _SellOpenPrice = 0.0; _BuyStopOpenPrice = 0.0;
_SellStopOpenPrice = 0.0; _BuyLimitOpenPrice = 0.0; 

_SellLimitOpenPrice = 0.0;

_BuyStopLoss = 0.0; _SellStopLoss = 0.0; _BuyStopStopLoss = 0.0;
_SellStopStopLoss = 0.0; _BuyLimitStopLoss = 0.0; 

_SellLimitStopLoss = 0.0;

_BuyTakeProfit = 0.0; _SellTakeProfit = 0.0;
_BuyStopTakeProfit = 0.0;
_SellStopTakeProfit = 0.0; _BuyLimitTakeProfit = 0.0; 

_SellLimitTakeProfit = 0.0;

_BuyOpenTime = -1; _SellOpenTime = -1; _BuyStopOpenTime = -1;
_SellStopOpenTime = -1; _BuyLimitOpenTime = -1; 

_SellLimitOpenTime = -1;

_BuyProfit = 0.0; _SellProfit = 0.0; _BuySwap = 0.0; 

_SellSwap = 0.0;
_BuyCommission = 0.0; _SellCommission = 0.0;

_BuyComment = ""; _SellComment = ""; _BuyStopComment = "";
_SellStopComment = ""; _BuyLimitComment = ""; 

_SellLimitComment = "";

_BuyStopExpiration = -1; _SellStopExpiration = -1;
_BuyLimitExpiration = -1; _SellLimitExpiration = -1;

int _GetLastError = 0, _OrdersTotal = OrdersTotal();
for ( int z = _OrdersTotal - 1; z >= 0; z -- )

{
if ( !OrderSelect( z, SELECT_BY_POS ) )

{
_GetLastError = GetLastError();
Print("OrderSelect(", z, ",SELECT_BY_POS) - Error #", 

_GetLastError );
continue;
}
if ( OrderMagicNumber() == magic && OrderSymbol() == 

Symbol() )
{
switch ( OrderType() )

{
case OP_BUY:
_BuyTicket = OrderTicket();
_BuyLots = NormalizeDouble( OrderLots(), 1 );
_BuyOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_BuyStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_BuyTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_BuyOpenTime = OrderOpenTime();
_BuyProfit = NormalizeDouble( OrderProfit(), 2 );
_BuySwap = NormalizeDouble( OrderSwap(), 2 );
_BuyCommission = NormalizeDouble( OrderCommission(),
2 );
_BuyComment = OrderComment();
break;
case OP_SELL:
_SellTicket = OrderTicket();
_SellLots = NormalizeDouble( OrderLots(), 1 );
_SellOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_SellStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_SellTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_SellOpenTime = OrderOpenTime();
_SellProfit = NormalizeDouble( OrderProfit(), 2 );
_SellSwap = NormalizeDouble( OrderSwap(), 2 );
_SellCommission = NormalizeDouble( OrderCommission(),
2 );
_SellComment = OrderComment();
break;
case OP_BUYSTOP:
_BuyStopTicket = OrderTicket();
_BuyStopLots = NormalizeDouble( OrderLots(), 1 );
_BuyStopOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_BuyStopStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_BuyStopTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_BuyStopOpenTime = OrderOpenTime();
_BuyStopComment = OrderComment();
_BuyStopExpiration = OrderExpiration();
break;
case OP_SELLSTOP:
_SellStopTicket = OrderTicket();
_SellStopLots = NormalizeDouble( OrderLots(), 1 );
_SellStopOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_SellStopStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_SellStopTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_SellStopOpenTime = OrderOpenTime();
_SellStopComment = OrderComment();
_SellStopExpiration = OrderExpiration();
break;
case OP_BUYLIMIT:
_BuyLimitTicket = OrderTicket();
_BuyLimitLots = NormalizeDouble( OrderLots(), 1 );
_BuyLimitOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_BuyLimitStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_BuyLimitTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_BuyLimitOpenTime = OrderOpenTime();
_BuyLimitComment = OrderComment();
_BuyLimitExpiration = OrderExpiration();
break;
case OP_SELLLIMIT:
_SellLimitTicket = OrderTicket();
_SellLimitLots = NormalizeDouble( OrderLots(), 1 );
_SellLimitOpenPrice = NormalizeDouble( OrderOpenPrice(),
Digits );
_SellLimitStopLoss = NormalizeDouble( OrderStopLoss(),
Digits );
_SellLimitTakeProfit = NormalizeDouble( OrderTakeProfit(),
Digits );
_SellLimitOpenTime = OrderOpenTime();
_SellLimitComment = OrderComment();
_SellLimitExpiration = OrderExpiration();
break;
}

}
}
}

现在我们将函数用到智能交易程序中:

extern int _MagicNumber = 1123;
extern double Lot = 0.1;

extern int StopLoss = 60; 
// 止损点的间距(0 - d无)
extern int TakeProfit = 100; 

// 赢利点的间距 (0 - 无)
extern int TrailingStop = 50; 
//追踪止损点 (0 - 无)

extern int Luft = 20; 

// 挂单交易放置水平的间距

#include <OneTypeOrdersControl.mq4>

int start()
{
int _GetLastError = 0;

//---- 记住开仓的参量(如果可用)

OneTypeOrdersInit( _MagicNumber );

//---- 如果我们两个都是挂单交易,退出
//---- 等待他们开启
if ( _BuyStopTicket > 0 && _SellStopTicket > 0 ) return(0);

//---- 如果 BUY 仓位开仓

if ( _BuyTicket > 0 )
{
//---- 如果SellStop 还没有删除,删除它:

if ( _SellStopTicket > 0 )
{
if ( !OrderDelete( _SellStopTicket ) )

{
Alert( "OrderDelete 错误#", GetLastError() );
return(-1);
}

}
//---- 检测止损被移动:
//---- 如果追踪止损不是很小,
if ( TrailingStop > MarketInfo( Symbol(), 

MODE_STOPLEVEL ) )
{
//---- 如果赢利仓位超过追踪止损点,
if ( NormalizeDouble( Bid - _BuyOpenPrice, Digits ) >
NormalizeDouble( TrailingStop*Point, Digits ) )

{
//---- 如果新止损水平超过当前仓位
//---- (或者当前仓位没有止损),
if(NormalizeDouble( Bid - TrailingStop*Point, 

Digits ) > _BuyStopLoss
|| _BuyStopLoss <= 0.0 )

{
//---- 修改定单
if ( !OrderModify( _BuyTicket, _BuyOpenPrice,
NormalizeDouble( Bid - TrailingStop*Point,
Digits ),
_BuyTakeProfit, 0 ) )

{
Alert( "OrderModify 错误#", 
GetLastError() );
return(-1);
}

}
}
}
//---- 如果没有开仓仓位,退出,无事可做
return(0);
}

//---- 这个单元格与BUY仓位的单元格相似

//---- 这就是我们不能做标注的原因...
if ( _SellTicket > 0 )

{
if ( _BuyStopTicket > 0 )
{

if ( !OrderDelete( _BuyStopTicket ) )
{

Alert( "OrderDelete错误 #", GetLastError() );
return(-1);
}

}
if(TrailingStop > MarketInfo( Symbol(), MODE_STOPLEVEL))

{
if(NormalizeDouble( _SellOpenPrice - Ask, Digits ) >
NormalizeDouble( TrailingStop*Point, Digits ) )

{
if(NormalizeDouble( Ask + TrailingStop*Point, 

Digits ) < _SellStopLoss
|| _SellStopLoss <= 0.0 )

{
if(!OrderModify( _SellTicket, _SellOpenPrice,
NormalizeDouble( Ask + TrailingStop*Point, 

Digits ),
_SellTakeProfit, 0 ) )
{
Alert( "OrderModify Error #", 

GetLastError() );
return(-1);
}
}
}

}
return(0);
}

5. 总结

最后,我想来比较一下使用函数与否在搜索定单时的速度。为此,我们用相同的版本来连续测试 “Every tick” 模式10次(用_MagicNumber最优化),用 MetaTrader 软件来计算时间 — 时间时自己计算的。

结果如下:

智能交易

10 测试的时间 (mm:ss)

CrossMACD_beta
(不包含函数)

07:42

CrossMACD

11:37

DoublePending_beta
(不包含函数)

08:18

DoublePending

09:42

正如你的表格里看到的,使用函数的智能交易程序稍稍慢了一点,这作为使源代码简单易懂的代价,应该还算合理。
无论如何,每个人都有是否使用函数的自由。

 

欢迎访问 外 汇 邦 WWW.WaiHuiBang.com
本文标题:外汇EA:简单的订单管理(二) - MT4平台MQL4编程学习
本文地址:http://www.ea666.cn/fxschool/autotrading/mql4/40633.html

相关文章

  • 外汇交易基础知识ABC和外汇交易程序MQL4

    金融市场的运作首先是交易业务。正如我们儿时熟知的,按照自己的直觉进行买卖。但是外汇交易却有些不同。本文将解析外汇交易的一些想法和外汇交易程序MQL 4。...

    MQL4编程学习
  • 错误146 ("交易作业忙") 和如何处理

    来自智能交易和脚本的交易,只能够提供一个并在交易作业中开启 (来自智能交易和脚本的自动交易作业)。这就是为什么如果智能交易业务作业忙,其他的智能交易或脚本在此刻不能调用函数生成错误146 (ERR_TRADE_CONTEXT_BUSY)。 换句话说,只有一个智能交易(脚本)可以准时交易。所以其他智能交易尝试开始交易将被错误 146停止。文章将会找到问题的解决方案。 ...

    MQL4编程学习
  • 外汇EA编程教程——操作符与表达式

      简介: 计算操作符有 +,-,*,/ 。%(求余),bool类型运算符(&&代表“与”,||代表“或”)。赋值运算符,=,+=,*=. 1 表达式,例如: a++; b = 10; x = (y * z) / (w + 2) + 127; 表达式必须以;号结尾。 2 算术运算,例如:...

    MQL4编程学习
  • 最详尽的Windows版外汇VPS挂EA使用指南
    最详尽的Windows版外汇VPS挂EA使用指南

    VPS 使用指南这些指示中可查看以下各项: 连接至远程桌面 安装“专家顾问” 修改 VPS 密码 维护账户安全连接至远程桌面要连接至 VPS 主机,您需要进行以下步骤: “开始”-“所有程序”-“附件”-“远程桌面连接”。...

    MQL4编程学习
  • 程序化交易人物志之盈透创始人Thomas Peterffy
    程序化交易人物志之盈透创始人Thomas Peterffy

    在用计算机和高速网络的今天,自动化交易已逐渐取代了传统的交易模式。然而,技术改变并不是一蹴而就的,技术的进步是一个循序渐进的过程。在这个过程中,又有哪些人参与其中,改变了交易,改变了市场,也改变了他们自己的命运呢? ...

    MQL4编程学习
你可能感兴趣