心平神静 发表于 2021-4-26 22:00:00

给EA加上止盈止损代码

代码如下:

extern int   StopLoss = 200;      //在这里设置止损点数
extern int   TakeProfit = 400;    //在这里设置盈利点数

void OnStart()
{
    bool chenggong = false;
    int i = 0;

    if(TakeProfit!=0)
    {
       for(i=0;i<OrdersTotal();i++)
       {
         if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderTakeProfit()==0)
         {RefreshRates();
                if(OrderType()==OP_BUY||OrderType()==OP_BUYLIMIT||OrderType()==OP_BUYSTOP)
                {
                  chenggong=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+TakeProfit*Point,0);
                }
                if(OrderType()==OP_SELL||OrderType()==OP_SELLLIMIT||OrderType()==OP_SELLSTOP)
                {
                  chenggong= OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-TakeProfit*Point,0);
                }
         }
       }
    }

    if(StopLoss!=0)
    {
       for(i=0;i<OrdersTotal();i++)
       {
         if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol() && OrderStopLoss()==0)
         {RefreshRates();
                if(OrderType()==OP_BUY||OrderType()==OP_BUYLIMIT||OrderType()==OP_BUYSTOP)
                {
                  chenggong=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-StopLoss*Point,OrderTakeProfit(),0);
                }
                if(OrderType()==OP_SELL||OrderType()==OP_SELLLIMIT||OrderType()==OP_SELLSTOP)
                {
                  chenggong= OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+StopLoss*Point,OrderTakeProfit(),0);
                }
         }
       }
    }
}

页: [1]
查看完整版本: 给EA加上止盈止损代码