Help with EA order placing mechanism, please

To add comments, please log in or register
Bo Jin
233
Bo Jin  

Hello, I am trying to make an EA, the main point is to set a long term sell order and while it is in profit but the trend seems to be reversing, place a buy order to hedge. I would like my EA to only place 1 sell order within a single day while it can place buy orders whenever the conditions are met. I have problem setting this up and would appreciate some help. Below is my code, please let me know how I can fix it. Thanks!


//+------------------------------------------------------------------+
//|                                                           EA.mq4 |
//|                                                                  |
  |
//+------------------------------------------------------------------+

#property link      ""
#property version   "1.00"
#property strict

input bool   OpenBUY=True;
input bool   OpenSELL=True;
input bool   CloseBySignal=True;
input bool   HedgeSell=True;
input bool   HedgeBuy=True;
input double StopLoss=0;

input double TakeProfit=0;

input double TrailingStop=0;
input int    RSIperiod=14;
input double BuyLevel=30;
input double SellLevel=70;
input bool   AutoLot=True;
input double Risk=1;
input double ManualLots=0.1;
input int    MagicNumber=123;
input string Koment="RSIea";
input int    Slippage=10;


//---- buffers


//---
int OrderBuy,OrderSell;
int ticket;
int LotDigits;
int BarsCount = 0;
double Trail,iTrailingStop;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
   OrderBuy=0;
   OrderSell=0;
   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==Koment)
           {
            if(OrderType()==OP_BUY) OrderBuy++;
            if(OrderType()==OP_SELL) OrderSell++;
            if(TrailingStop>0)
              {
               iTrailingStop=TrailingStop;
               if(TrailingStop<stoplevel) iTrailingStop=stoplevel;
               Trail=iTrailingStop*Point;
               double tsbuy=NormalizeDouble(Bid-Trail,Digits);
               double tssell=NormalizeDouble(Ask+Trail,Digits);
               if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>Trail && Bid-OrderStopLoss()>Trail)
                 {
                  ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tsbuy,OrderTakeProfit(),0,Blue);
                 }
               if(OrderType()==OP_SELL && OrderOpenPrice()-Ask>Trail && (OrderStopLoss()-Ask>Trail || OrderStopLoss()==0))
                 {
                  ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tssell,OrderTakeProfit(),0,Blue);
                 }
              }
           }
     }


//--- open position
if(Bars > BarsCount)
    {
   if(OpenBUY && OrderSell>0 && OrderBuy<1 && OrderType()==OP_SELL && OrderOpenPrice()>Ask && Orderopen condition )OPBUY(); 
   if(OpenSELL && OrderSell<1 && Orderopen condition)OPSELL(); 
   BarsCount = Bars;
   }

//--- close position by signal
   if(CloseBySignal)
     {
     if(OrderSell>0  && Orderclose condition ) CloseSell();
     if(OrderBuy>0  && Orderclose condition )  CloseBuy();
     }

//---
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OPBUY()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(StopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;

   ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DodgerBlue);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OPSELL()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(StopLoss>0) StopLossLevel=Ask+StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Bid-TakeProfit*Point; else TakeProfitLevel=0.0;
//---
   ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DeepPink);
  }
//+------------------------------------------------------------------+
                                                               
//+------------------------------------------------------------------+
void CloseSell()
  {
   int  total=OrdersTotal();
   for(int y=OrdersTotal()-1; y>=0; y--)
     {
      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber)
           {
            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
           }
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseBuy()
  {
   int  total=OrdersTotal();
   for(int y=OrdersTotal()-1; y>=0; y--)
     {
      if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber)
           {
            ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
           }
     }
  }
//+------------------------------------------------------------------+
                                                     
//+------------------------------------------------------------------+
double LOT()
  {
   double lotsi;
   double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT);
   double ilot_min =MarketInfo(Symbol(),MODE_MINLOT);
   double tick=MarketInfo(Symbol(),MODE_TICKVALUE);
//---
   double  myAccount=AccountBalance();
//---
   if(ilot_min==0.01) LotDigits=2;
   if(ilot_min==0.1) LotDigits=1;
   if(ilot_min==1) LotDigits=0;
//---
   if(AutoLot)
     {
      lotsi=NormalizeDouble((myAccount*Risk)/10000,LotDigits);
        } else { lotsi=ManualLots;
     }
//---
   if(lotsi>=ilot_max) { lotsi=ilot_max; }
//---
   return(lotsi);
  }
//+------------------------------------------------------------------+
  
Marco vd Heijden
Moderator
12693
Marco vd Heijden  
//+------------------------------------------------------------------+
//|                                                           EA.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+

#property link      ""
#property version   "1.00"
#property strict

input bool   OpenBUY=True;
input bool   OpenSELL=True;
input bool   CloseBySignal=True;
input bool   HedgeSell=True;
input bool   HedgeBuy=True;
input double StopLoss=0;

input double TakeProfit=0;

input double TrailingStop=0;
input int    RSIperiod=14;
input double BuyLevel=30;
input double SellLevel=70;
input bool   AutoLot=True;
input double Risk=1;
input double ManualLots=0.1;
input int    MagicNumber=123;
input string Koment="RSIea";
input int    Slippage=10;

//---- buffers

//---
int OrderBuy,OrderSell;
int ticket;
int LotDigits;
int BarsCount=0;
double Trail,iTrailingStop;
datetime time;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
   OrderBuy=0;
   OrderSell=0;
   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==Koment)
           {
            if(OrderType()==OP_BUY) OrderBuy++;
            if(OrderType()==OP_SELL) OrderSell++;
            if(TrailingStop>0)
              {
               iTrailingStop=TrailingStop;
               if(TrailingStop<stoplevel) iTrailingStop=stoplevel;
               Trail=iTrailingStop*Point;
               double tsbuy=NormalizeDouble(Bid-Trail,Digits);
               double tssell=NormalizeDouble(Ask+Trail,Digits);
               if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>Trail && Bid-OrderStopLoss()>Trail)
                 {
                  ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tsbuy,OrderTakeProfit(),0,Blue);
                 }
               if(OrderType()==OP_SELL && OrderOpenPrice()-Ask>Trail && (OrderStopLoss()-Ask>Trail || OrderStopLoss()==0))
                 {
                  ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tssell,OrderTakeProfit(),0,Blue);
                 }
              }
           }
     }

//--- open position
   if(Bars>BarsCount)
     {
      if(OpenBUY && OrderSell>0 && OrderBuy<1 && OrderType()==OP_SELL && OrderOpenPrice()>Ask && Orderopen condition)OPBUY();
      if(OpenSELL && OrderSell<1 && Orderopen condition)OPSELL();
      BarsCount=Bars;
     }

//--- close position by signal
   if(CloseBySignal)
     {
      if(OrderSell>0 && Orderclose condition) CloseSell();
      if(OrderBuy>0 && Orderclose condition) CloseBuy();
     }

//---
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OPBUY()
  {
   double StopLossLevel;
   double TakeProfitLevel;
   if(StopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;

   ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DodgerBlue);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OPSELL()
  {
   if(time!=iTime(Symbol(),PERIOD_D1,0))
     {
      double StopLossLevel;
      double TakeProfitLevel;
      if(StopLoss>0) StopLossLevel=Ask+StopLoss*Point; else StopLossLevel=0.0;
      if(TakeProfit>0) TakeProfitLevel=Bid-TakeProfit*Point; else TakeProfitLevel=0.0;
      //---
      ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DeepPink);
      if(ticket!=-1)
        {
         time=iTime(Symbol(),PERIOD_D1,0);
        }
     }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
   void CloseSell()
     {
      int  total=OrdersTotal();
      for(int y=OrdersTotal()-1; y>=0; y--)
        {
         if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
            if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber)
              {
               ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
              }
        }
     }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
   void CloseBuy()
     {
      int  total=OrdersTotal();
      for(int y=OrdersTotal()-1; y>=0; y--)
        {
         if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
            if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber)
              {
               ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);
              }
        }
     }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
   double LOT()
     {
      double lotsi;
      double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT);
      double ilot_min =MarketInfo(Symbol(),MODE_MINLOT);
      double tick=MarketInfo(Symbol(),MODE_TICKVALUE);
      //---
      double  myAccount=AccountBalance();
      //---
      if(ilot_min==0.01) LotDigits=2;
      if(ilot_min==0.1) LotDigits=1;
      if(ilot_min==1) LotDigits=0;
      //---
      if(AutoLot)
        {
         lotsi=NormalizeDouble((myAccount*Risk)/10000,LotDigits);
           } else { lotsi=ManualLots;
        }
      //---
      if(lotsi>=ilot_max) { lotsi=ilot_max; }
      //---
      return(lotsi);
     }
//+------------------------------------------------------------------+
Bo Jin
233
Bo Jin  
Marco vd Heijden:

Thanks a lot, Marco! This may be a piece of cake to you, but it really helps me tremendously! :D

To add comments, please log in or register