Close order automatically after a given period and/or SL - page 2

 
don't worry u get used to that
 
nirvanamac:


Yes of course I understand...I don't want to have slaves for my EA's....the only thing I need to know is how to understand in which way a program works...

If you have a simple EA like SMA Cross...this is not a great problem...but the more you want to achieve the more harder it gets...(at least for me)


//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
  for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {
          if(OrderMagicNumber()==BuyMagicNumber) {int halt1=1;}
          if(OrderMagicNumber()==SellMagicNumber) {int halt2=1;}
        }
     }
}

It looks like the most of the code you have used is from this https://www.mql5.com/en/code

What are the progam lines above you have used doing according to you ??

 
nirvanamac:
I think you mean the hole code or?
I repeat
you just have to code it. Where is your OrderClose() and your test for time?
nirvanamac:
My problem is to fill the code into my present one...:
No Slaves here, learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
 

Hi together, I think I found the right way, but it is still wrong. Acually the code looks like this:

//+------------------------------------------------------------------+
//|                                            Der Stundentrader.mq4 |
//|                                                         Der Marc |
//|                                  Es gibt gar keine Internetseite |
//+------------------------------------------------------------------+
#property copyright "Der Marc"
#property link      "Es gibt gar keine Internetseite"

//Wichtige Variablen
extern double    Lots=0.01;
extern int       Slippage=5;
extern int MagicNumber =1;
extern int  TradeHour3=3;
extern int  TradeHour4=4;
extern int  TradeHour7=7;
extern int  TradeHour10=10;
extern int  TradeHour17=17;
extern int  TradeHour18=18;
extern int  TradeHour20=20;
extern int  TradeHour12=12;
extern int  TradeHour23=23;

//Globale Variablen
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   UsePoint = PipPoint(Symbol());
   UseSlippage = GetSlippage(Symbol(), Slippage);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
// Buy criteria
OrderSelect(SellTicket || BuyTicket, SELECT_BY_TICKET);
       //Close Order
       if(OrderCloseTime() == 0 && SellTicket > 0 || BuyTicket > 0)
         {
          double CloseLots = OrderLots();
          double ClosePrice = Ask;
          bool Closed = OrderClose(SellTicket ,CloseLots, ClosePrice, UseSlippage, Red);
         }
         double OpenPrice = Ask;
//BuyOrder         
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20==Hour())) //Signal Buy
 {
   int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"time trader buy order ",MagicNumber,0,Blue);
    }
SellTicket = 0; 
BuyTicket = 0;
 
// Sell criteria
OrderSelect(BuyTicket || SellTicket, SELECT_BY_TICKET);
       //Close Order
      if(OrderCloseTime() == 0 && BuyTicket > 0 || BuyTicket > 0)
      {
       CloseLots = OrderLots();
       ClosePrice = Bid;
       Closed = OrderClose(BuyTicket, CloseLots, ClosePrice, UseSlippage, Red);
      }
               OpenPrice = Bid;
 //SellOrder
 if ((TradeHour12==Hour())||(TradeHour23==Hour())) //Signal Sell
 {
   int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"time trader sell order ",MagicNumber,0,Green);
    }  
SellTicket = 0;
BuyTicket = 0; 
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//Pip Point Function
  double PipPoint (string Currency)
  {
   int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
   if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
   else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
   return (CalcPoint);
  }

//Get Slippage Function
int GetSlippage(string Currency, int SlippagePips)
 {
  int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
  if(CalcDigits == 2 || CalcDigits == 3) double CalcSlippage = SlippagePips;
  else if(CalcDigits == 4 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
  return (CalcSlippage);
  }

I have the problem that my EA closes all Orders after opened. As you can see I filled in the OrderClosetime for all Buy & SellTickets. I can not find the function to tell the EA that it must be closed after 1hour...

 
nirvanamac:


Yes of course I understand...I don't want to have slaves for my EA's....the only thing I need to know is how to understand in which way a program works...

If you have a simple EA like SMA Cross...this is not a great problem...but the more you want to achieve the more harder it gets...(at least for me)

My current EA is quite simple . . . I have over 2000 line of code in it . . . most of that is nothing to do with the method but is housekeeping and standard functions that need to be in any EA I will ever write . . . like calculating position sizes, placing orders, counting orders, checking orders close when they hit TP or SL, error reporting, etc, etc . . . I spent months writing, testing, modifying, testing . . . my code.
 
RaptorUK:
My current EA is quite simple . . . I have over 2000 line of code in it . . . most of that is nothing to do with the method but is housekeeping and standard functions that need to be in any EA I will ever write . . . like calculating position sizes, placing orders, counting orders, checking orders close when they hit TP or SL, error reporting, etc, etc . . . I spent months writing, testing, modifying, testing . . . my code.


Did you upload it here?

 
nirvanamac:


Did you upload it here?

No, why would I do that ?
 
My code (without the trading logic) is over 1500 lines of code and I did upload it.
 

Hi together,

after a few days spending time with programming and looking through other EA's here you can you the current result:

//+------------------------------------------------------------------+
//|                                            Der Stundentrader.mq4 |
//|                                                         Der Marc |
//|                                  Es gibt gar keine Internetseite |
//+------------------------------------------------------------------+
#property copyright "Der Marc"
#property link      "Es gibt gar keine Internetseite"

//Wichtige Variablen
extern double    Minlot=0.01;
extern int       Digits2Round=2;
extern int       PercentOfFreeDepo=1;
extern int       Slippage=5;
extern int       MagicNumber =1;
extern int       TradeHour3=3;
extern int       TradeHour4=4;
extern int       TradeHour7=7;
extern int       TradeHour10=10;
extern int       TradeHour17=17;
extern int       TradeHour18=18;
extern int       TradeHour20=20;
extern int       TradeHour12=12;
extern int       TradeHour23=23;
extern int       StopLoss=40;

//Globale Variablen
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   UsePoint = PipPoint(Symbol());
   UseSlippage = GetSlippage(Symbol(), Slippage);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   double FreeDepo=NormalizeDouble(AccountBalance()-AccountMargin(),Digits2Round);
   double Risk=NormalizeDouble((FreeDepo*PercentOfFreeDepo/100),Digits2Round);
   double Lot=NormalizeDouble(Risk/(StopLoss/0.0001)*0.1,Digits2Round);
//===================== Lets determine lot size and risk ===================================
if ( Lot<Minlot )
 {
   Lot=Minlot;
 }
Comment( "\n","Acceptable risk is ",PercentOfFreeDepo, "% = ",Risk," of the free money ",FreeDepo," in lots = ",Lot);
// Buy criteria
OrderSelect(SellTicket || BuyTicket, SELECT_BY_TICKET);
       //Close Order
       if((OrderOpenTime()+3600) > TimeCurrent() && SellTicket > 0 || BuyTicket > 0)
         {
          double CloseLots = OrderLots();
          double ClosePrice = Ask;
          bool Closed = OrderClose(SellTicket ,CloseLots, ClosePrice, UseSlippage, Red);
         }
         double OpenPrice = Ask;
//BuyOrder         
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20==Hour())) //Signal Buy
 {
   int openbuy=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,StopLoss,0,"time trader buy order ",MagicNumber,0,Blue);
    }
SellTicket = 0; 
BuyTicket = 0;
 // Sell criteria
OrderSelect(BuyTicket || SellTicket, SELECT_BY_TICKET);
       //Close Order
      if((OrderOpenTime()+3600) > TimeCurrent() && SellTicket > 0 || BuyTicket > 0)
      {
       CloseLots = OrderLots();
       ClosePrice = Bid;
       Closed = OrderClose(BuyTicket, CloseLots, ClosePrice, UseSlippage, Red);
      }
               OpenPrice = Bid;
 //SellOrder
 if ((TradeHour12==Hour())||(TradeHour23==Hour())) //Signal Sell
 {
   int opensell=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,StopLoss,0,"time trader sell order ",MagicNumber,0,Green);
    }  
SellTicket = 0;
BuyTicket = 0; 
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//Pip Point Function
  double PipPoint (string Currency)
  {
   int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
   if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
   else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
   return (CalcPoint);
  }

//Get Slippage Function
int GetSlippage(string Currency, int SlippagePips)
 {
  int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
  if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
  else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
  return (CalcSlippage);
  }

Now I have the problem, that during there are more orders opened than 1. I also get the error message "130"...