MT4批量开启和删除订单的脚本程序
两个脚本,第一个BatchOpen.mq4用来在一定区间内自动按照一定间隔挂单,
第二个BatchClose.mq4用来自动删除符合要求或者所有的单子.
安装方法同其他标准脚本,以脚本方式安装即可.
两个脚本运行前都可以设置相应参数, BatchOpen的参数如下:
ActionBuy -> true或者false, 表示挂单为买入或者卖出
RangeStart -> 挂单范围的起点
RangeEnd -> 挂单范围的终点
TotalLots -> 挂单的总手数
OrderCount -> 挂单分割成的总数量, 必须大于等于2
Slippage -> 允许的滑点数,可以为0
StopLoss -> 所挂的单的止损位, 可以为0表示无止损
TakeProfit -> 所挂的单的获利位, 可以为0表示无获利
Comments -> 注释,默认为"BatchOpen Auto Generated", 可以用后面的BatchClose来通过
特定的注释匹配删除特定的单.
Expiration -> 挂单过期时间,未测试,默认不过期
Delay -> 每两个单之间的等待时间,默认不等待
比如以如下的参数设置
ActionBuy = true
RangeStart = 1.1850
RangeEnd = 1.1870
TotalLots = 1.0
OrderCount = 5
Slippage = 0
StopLoss = 1.1830
TakeProfit = 1.1920
为例, 运行后将自动挂上如下5单:
1.1850, 0.2 lots, S/L 1.1830, T/P 1.1920
1.1855, 0.2 lots, S/L 1.1830, T/P 1.1920
1.1860, 0.2 lots, S/L 1.1830, T/P 1.1920
1.1865, 0.2 lots, S/L 1.1830, T/P 1.1920
1.1870, 0.2 lots, S/L 1.1830, T/P 1.1920
如果当手数之类的不能整除的话多余的会放到最后一单,同时
每单大小最小必须为0.1 Lots
另外注意挂单时候的价位以及止损, 获利价位都必须和
当前价位之类的有最小7点的差别, 否则该单会失败.
BatchClose对应为批处理关闭符合要求的单子, 设置参数为:
Comments -> 用来设置匹配的单子注释, 注释不匹配的不予删除, 不设置则表示匹配任意注释
CommentsMatchStart -> 用来设置注释匹配时候是全匹配还是只匹配开头部分即可,默认为只匹配开头
PendingOnly -> 表示是否只匹配挂单,默认为否,匹配所有单据
Slippage -> 允许的滑点数,可以为0
Magic -> 表示用来匹配的单据MagicNumber, 默认0表示匹配所有单据.
默认参数运行表示删除所有的单子(包括挂单和已成交单),一般如果想只删除BatchOpen自动生成的单子
可以把Comments项改成"BatchOpen"即可(前提是 BatchOpen运行的时候注释为默认的), 如果只想删除挂单
则修改PendingOnly 为true.
不过这两个脚本实际意义不大,MT4上应该没有几个人有真实帐户的. 有兴趣的自己试试吧.
//+------------------------------------------------------------------+
//| BatchOpen.mq4 |
//| [email protected] |
//| www.talkforex.com |
//+------------------------------------------------------------------+
#property copyright "[email protected]"
#property link "www.talkforex.com"
#property show_inputs
#include
extern bool ActionBuy = true;
extern double RangeStart = 1.1850;
extern double RangeEnd = 1.1870;
extern double TotalLots = 1.0;
extern int OrderCount = 5;
extern int Slippage = 0;
extern double StopLoss = 0.0;
extern double TakeProfit = 0.0;
extern string Comments = "BatchOpen Auto Generated";
extern datetime Expiration = 0;
extern int Delay = 0;
#define BATCH_OPEN_MAGIC 06021610
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int ActionType = 0;
int start()
{
//----
//inputs checking
if (OrderCount < 2) {
MessageBox("Not enough OrderCount, must be greater than 1 at least");
return (-1);
}
double LotsPerOrder, LotsLeft;
int i, n;
n = TotalLots * 10/OrderCount;
LotsPerOrder = n;
LotsPerOrder /= 10;
LotsLeft = TotalLots - LotsPerOrder * OrderCount;
if (LotsPerOrder < 0.1) {
MessageBox("Not enough TotalLots or too many OrderCount, each order must be 0.1 lots at least");
return (-1);
}
if (LotsPerOrder + LotsLeft >= 10000) {
MessageBox("Too many TotalLots, each order must be less than 10000 lots");
return (-1);
}
n = NormalizeDouble((MathAbs(RangeStart - RangeEnd) * MathPow(10, Digits)), 0)/(OrderCount-1);
if (n < 1) {
MessageBox("Too many OrderCount, each order must have 1 pips diff at least");
}
double volume, price;
string symbol;
int cmd, magic, ticket, cnt, failed;
symbol = Symbol();
magic = BATCH_OPEN_MAGIC;
volume = LotsPerOrder;
cnt = 0;
failed = 0;
for (i = 0; i < OrderCount; i++) {
price = RangeStart + i * n * Point;
if (i == OrderCount-1) {
volume += LotsLeft;
//price = RangeEnd;
}
RefreshRates();
if (ActionBuy) {
if (price < Ask) {
cmd = OP_BUYLIMIT;
} else {
cmd = OP_BUYSTOP;
}
} else {
if (price < Bid) {
cmd = OP_SELLSTOP;
} else {
cmd = OP_SELLLIMIT;
}
}
ticket = OrderSend(symbol, cmd, volume, price, Slippage, StopLoss,
TakeProfit, Comments, magic, Expiration, CLR_NONE);
if (ticket < 0) {
Print("Failed to place order at price " + price + ", Error: " + ErrorDescription(GetLastError()));
failed++;
} else {
cnt++;
}
Sleep(Delay);
}
//----
Print(cnt + " orders placed, " + OrderCount + " requested, " + failed + " failed");
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| BatchClose.mq4 |
//| [email protected] |
//| www.talkforex.com |
//+------------------------------------------------------------------+
#property copyright "[email protected]"
#property link "www.talkforex.com"
#property show_inputs
#include
extern string Comments = "";
extern bool CommentsMatchStart = true;
extern bool PendingOnly = false;
extern int Slippage = 0;
extern int Magic = 0;
bool OrderDeleteOrClose()
{
int cmd;
double price;
int ticket;
ticket = OrderTicket();
cmd=OrderType();
if(cmd == OP_BUY || cmd == OP_SELL) {
if (cmd == OP_BUY) {
price = Bid;
} else {
price = Ask;
}
if (!OrderClose(ticket, OrderLots(), price, Slippage, CLR_NONE)) {
Print("Failed to close order " +
ticket + " Error: " + ErrorDescription(GetLastError())
);
return (false);
} else {
return (true);
}
} else {
//pending orders
if (!OrderDelete(ticket)) {
Print("Failed to delete order " +
ticket + " Error: " + ErrorDescription(GetLastError())
);
return (false);
} else {
return (true);
}
}
}
int start()
{
bool result;
int cmd,total,ticket, cnt, failed;
string c;
//----
total=OrdersTotal();
//----
cnt = 0;
failed = 0;
for(int i=total-1; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
if (PendingOnly) {
cmd=OrderType();
if(cmd == OP_BUY || cmd == OP_SELL) {
continue;
}
}
if (Magic != 0) {
if (OrderMagicNumber() != Magic) {
continue;
}
}
if (StringGetChar(Comments, 0) != 0) {
if (CommentsMatchStart) {
c = StringSubstr(OrderComment(), 0, StringLen(Comments));
} else {
c = OrderComment();
}
if (Comments != c) {
continue;
}
}
OrderPrint();
if (!OrderDeleteOrClose()) {
failed++;
} else {
cnt++;
}
}
else {
Print("Failed to select order " + i + " Error: " +
ErrorDescription(GetLastError())
);
failed++;
}
}
Print(cnt + " orders deleted from " + total + " total orders, " + failed + " failed");
//----
return(0);
}
//+------------------------------------------------------------------+
3楼
原帖由 老正 于 2006-2-17 00:13 发表 呵呵 老大 也许可以用做鱼网交易的实验
韬客社区www.talkfx.co
发表于:2006-02-17 05:16只看该作者
4楼
楼主强!隔三差五的总能给大家一些好东西。
不过俺是菜鸟,楼主能再给说说 MT4 的脚本该怎么安装使用就好了。
发表于:2006-10-31 14:56只看该作者
5楼
这是去年我短消息请wfy05帮忙给写的脚本,今天因需要再查询又来看了一遍,顺便顶一下。
韬客社区www.talkfx.co
发表于:2009-03-12 13:43只看该作者
6楼
脚本怎么样运行呢
韬客社区www.talkfx.co
发表于:2010-12-28 13:54只看该作者
7楼
多谢楼主:034: :034:
韬客社区www.talkfx.co
发表于:2012-10-22 07:01只看该作者
8楼
好脚本,一键N单
韬客社区www.talkfx.co
发表于:2012-11-19 06:45只看该作者
9楼
批量开启和删除订单的脚本程序
韬客社区www.talkfx.co