这个没用的。
// 手数
extern double Lots = 0.01; // 手数
extern double TakeProfit = 100.0; // 止盈点数
extern int MaxLevels = 5; // 最大逆加层数
extern double HedgeOffset = 50.0; // 首尾单止盈平衡点数
extern int Slippage = 3; // 滑点
extern int magicNumber = 123456; // 魔术数字
// 全局变量
bool hasPosition = false; // 是否有持仓
int buyLevels = 0; // 多单逆加层数
int sellLevels = 0; // 空单逆加层数
double buyPrice = 0.0; // 多单开仓价
double sellPrice = 0.0; // 空单开仓价
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// 初始化逻辑
// 如果有持仓,将 hasPosition 设置为 true
if(OrdersTotal() > 0)
{
hasPosition = true;
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert advisor start function |
//+------------------------------------------------------------------+
int start()
{
// 开仓逻辑
if(!hasPosition)
{
// 以现价双向建仓
if(OrderSend(Symbol(), OP_SELL, Lots, Ask, Slippage, 0, 0, "Oscillation EA", magicNumber) &&
OrderSend(Symbol(), OP_BUY, Lots, Bid, Slippage, 0, 0, "Oscillation EA", magicNumber))
{
Print("订单已发送");
hasPosition = true;
// 设置多单和空单的开仓价
buyPrice = Bid;
sellPrice = Ask;
}
else
{
Print("无法发送订单错误:" + GetLastError());
}
}
// 检查止盈条件
if(hasPosition)
{
CheckTakeProfit();
}
return(0);
}
//+------------------------------------------------------------------+
//| Check take profit condition |
//+------------------------------------------------------------------+
void CheckTakeProfit()
{
// 多单止盈
if(OrdersTotal() > 0 && OrderType() == OP_BUY && (Bid - buyPrice) >= TakeProfit * Point)
{
CloseAllBuyOrders();
}
// 空单止盈
if(OrdersTotal() > 0 && OrderType() == OP_SELL && (sellPrice - Ask) >= TakeProfit * Point)
{
CloseAllSellOrders();
}
}
//+------------------------------------------------------------------+
//| Close all buy orders |
//+------------------------------------------------------------------+
void CloseAllBuyOrders()
{
int totalOrders = OrdersTotal();
for(int i = totalOrders - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType() == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrNONE);
buyLevels = 0;
sellLevels = 0;
hasPosition = false;
break;
}
}
}
}
//+------------------------------------------------------------------+
//| Close all sell orders |
//+------------------------------------------------------------------+
void CloseAllSellOrders()
{
int totalOrders = OrdersTotal();
for(int i = totalOrders - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clrNONE);
buyLevels = 0;
sellLevels = 0;
hasPosition = false;
break;
}
}
}
}
//+------------------------------------------------------------------+
//| Expert advisor deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// 在EA停止运行时平仓所有持仓
CloseAllBuyOrders();
CloseAllSellOrders();
}
//+------------------------------------------------------------------+
|