有懂的不,看看这个问题在哪里,
if(OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"",MAGICMA,0,Blue)<0)
if(OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"",MAGICMA,0,clrRed)<0)
注意lots变量大小写,这就两判断下单是否成功的条件句{:1_183:} 大小写都试了,还是不能用 顶下 if(OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"",MAGICMA,0,Blue)==false) {:1_179:} triple3 发表于 2021-12-31 16:44
if(OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"",MAGICMA,0,Blue)==false)
还是用不了呀
triple3 发表于 2021-12-31 16:44
if(OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"",MAGICMA,0,Blue)==false)
//+------------------------------------------------------------------+
//| 01.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#define MAGICMA20211220
//--- input parameters
input double Input1=0.01;//交易手数
input int shortMaPeriod=12; //短期均线
input int LongMaPeriod=48;//长期均线
input ENUM_MA_METHOD method = MODE_SMA;//均线方式
//+------------------------------------------------------------------+
//主函数入口:OnTick 函数每一个报价执行一次 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
//---检查是否有单子
if(IsOrderExist()==false)
CheckForOpen();//没有单子就检查是否能开单
else
CheckForClose();//有单子就检查是否应该平仓
}
//+------------------------------------------------------------------+
//|检查是否有单子,true=有单子,false=没单子
//+------------------------------------------------------------------+
bool IsOrderExist()
{
for(int i=0;i<OrdersTotal();i++)//扫描所有单子
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;//挨个选中
if(OrderSymbol()!=Symbol())continue;//货币名称不符,跳过
if(OrderMagicNumber()!=MAGICMA)continue;//魔术号不符,跳过
return(true);//找到单子了,返回true
}
return(false);//没有找到单子,返回false
}
//+------------------------------------------------------------------+
//|判断开仓条件
//+------------------------------------------------------------------+
void CheckForOpen()
{
if(Volume>1)
return;//每次K线收线才开单
double maShort=iMA(NULL,0,shortMaPeriod,0,method,PRICE_CLOSE,0);//短期移动平均线1
double maLong=iMA(NULL,0,LongMaPeriod,0,method,PRICE_CLOSE,0);//长期移动平均线2
//做多条件:价格在两条均线的上方,并且价格向上穿越短期均线
if(Open<maShort && Close>maShort && Close>maLong)
{
if(OrderSend(Symbol(),OP_BUY,LOTS,Ask,3,0,0,"",MAGICMA,0,clrBlue)<0)
Print("开仓失败:",GetLastError());
}
//做空条件:价格在两条均线的下方,并且价格向下穿越短期均线
if(Open>maShort && Close<maShort && Close<maLong)
{
if(OrderSend(Symbol(),OP_SELL,LOTS,Bid,3,0,0,"",MAGICMA,0,clrRed)<0)
Print("开仓失败:",GetLastError());
}
}
//+------------------------------------------------------------------+
//|判断平仓条件
//+------------------------------------------------------------------+
void CheckForClose()
{
if(Volume>1)
return;//每次K线收线才平仓
double maShort=iMA(NULL,0,shortMaPeriod,0,method,PRICE_CLOSE,0);//短期移动平均线
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue;
if(OrderSymbol()!=Symbol())continue;//货币名称不符,跳过
if(OrderMagicNumber()!=MAGICMA)continue;//魔术号不符,跳过
if(OrderType()==OP_BUY)//多单
{
if(Open>shortMaPeriod && Close<shortMaPeriod)//价格向下穿越短期均线,平多单
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))//平仓
Print("平仓失败:",GetLastError());
}
}
if(OrderType()==OP_SELL)//空单
{
if(Open<shortMaPeriod && Close>MaPeriod)//价格向上穿越短期均线,平空单
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))//平仓
Print("平仓失败:",GetLastError());
}
}
}
}
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| 01.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#define MAGICMA20211220
//--- input parameters
input double Input1=0.01;//交易手数
input int shortMaPeriod=12; //短期均线
input int LongMaPeriod=48;//长期均线
input ENUM_MA_METHOD method = MODE_SMA;//均线方式
//+------------------------------------------------------------------+
//主函数入口:OnTick 函数每一个报价执行一次 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
//---检查是否有单子
if(IsOrderExist()==false)
CheckForOpen();//没有单子就检查是否能开单
else
CheckForClose();//有单子就检查是否应该平仓
}
//+------------------------------------------------------------------+
//|检查是否有单子,true=有单子,false=没单子
//+------------------------------------------------------------------+
bool IsOrderExist()
{
for(int i=0;i<OrdersTotal();i++)//扫描所有单子
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;//挨个选中
if(OrderSymbol()!=Symbol())continue;//货币名称不符,跳过
if(OrderMagicNumber()!=MAGICMA)continue;//魔术号不符,跳过
return(true);//找到单子了,返回true
}
return(false);//没有找到单子,返回false
}
//+------------------------------------------------------------------+
//|判断开仓条件
//+------------------------------------------------------------------+
void CheckForOpen()
{
if(Volume>1)
return;//每次K线收线才开单
double maShort=iMA(NULL,0,shortMaPeriod,0,method,PRICE_CLOSE,0);//短期移动平均线1
double maLong=iMA(NULL,0,LongMaPeriod,0,method,PRICE_CLOSE,0);//长期移动平均线2
//做多条件:价格在两条均线的上方,并且价格向上穿越短期均线
if(Open<maShort && Close>maShort && Close>maLong)
{
if(OrderSend(Symbol(),OP_BUY,Input1,Ask,3,0,0,"",MAGICMA,0,clrBlue)<0)
Print("开仓失败:",GetLastError());
}
//做空条件:价格在两条均线的下方,并且价格向下穿越短期均线
if(Open>maShort && Close<maShort && Close<maLong)
{
if(OrderSend(Symbol(),OP_SELL,Input1,Bid,3,0,0,"",MAGICMA,0,clrRed)<0)
Print("开仓失败:",GetLastError());
}
}
//+------------------------------------------------------------------+
//|判断平仓条件
//+------------------------------------------------------------------+
void CheckForClose()
{
if(Volume>1)
return;//每次K线收线才平仓
double maShort=iMA(NULL,0,shortMaPeriod,0,method,PRICE_CLOSE,0);//短期移动平均线
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue;
if(OrderSymbol()!=Symbol())continue;//货币名称不符,跳过
if(OrderMagicNumber()!=MAGICMA)continue;//魔术号不符,跳过
if(OrderType()==OP_BUY)//多单
{
if(Open>shortMaPeriod && Close<shortMaPeriod)//价格向下穿越短期均线,平多单
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))//平仓
Print("平仓失败:",GetLastError());
}
}
if(OrderType()==OP_SELL)//空单
{
if(Open<shortMaPeriod && Close>shortMaPeriod)//价格向上穿越短期均线,平空单
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))//平仓
Print("平仓失败:",GetLastError());
}
}
}
}
//+------------------------------------------------------------------+ triple3 发表于 2022-1-5 19:30
//+------------------------------------------------------------------+
//| ...
现在不平仓怎么解决,能加微跟你学学吗