Need help with two (2) questions with close price . - page 2

 

Code is easier to read if indented and formated. To post code use the SRC button in the header of where you enter text alongside other editing goodies like Bold and Underline

//+------------------------------------------------------------------+
//|Trailing Stop |
//|Setup 1H or daily charts |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Global Variables / Includes |
//+------------------------------------------------------------------+
extern double StopLoss = 250; // StopLoss
extern double TrailingStop = 250; //Trailing Stop
extern double BreakPoiont = 100; // Breakout points
extern double TakeProfit = 350; //Take Profit
extern double TI = 60; //Short term Trading Indicator time frame
extern int MagicNumber = 602231220;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{//0
int cnt, total, tradecnt, ticket, err;

tradecnt=0;
total=OrdersTotal();

//=========================== Set Trading Criterias ==============================
double SL = StopLoss*Point; // StopLoss
double TP = TakeProfit*Point; // TakeProfit
double P = BreakPoiont*Point; // Breakout points
double TS = TrailingStop*Point; //Trailing Stop

double H=iHigh(NULL,TI,0);
double L=iLow(NULL,TI,0);
double C=iClose(NULL,TI,0);
double O=iOpen(NULL,TI,0);

datetime Bar1Time=Time[1];
double CloseOfBar0_ASAP;

//================= Close or Apply Trailing Stop Lost ===================== 
if (total> 0)
   {//1
    for(cnt=0;cnt<total;cnt++)
      {//2
       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
       if (OrderType()<=OP_SELL && // check for opened position 
           OrderSymbol()==Symbol() && // check for symbol
           OrderMagicNumber() == MagicNumber ) //check for EA Assing Number
         {//3
          // should it be closed?
          if (OrderType()==OP_BUY) // long position is opened
            {//4
             // check for long position (BUY) possibility
             // check for ZERO StopLoss 
             if (OrderStopLoss()==0)
               {//5
                OrderModify(OrderTicket(),OrderOpenPrice(),C-TS,OrderTakeProfit(),0,Yellow);
                return(0); // exit 
               }//5
             else
               {//5
                if (Bar1Time != Time[1]) // 1st tick of new bar
                  {//6
                   OrderClose(OrderTicket(),OrderLots(),Ask,3,Gold); // close position
                   CloseOfBar0_ASAP = Close[1];
                   Bar1Time = Time[1];
                   return(0); // exit 
                  }//6
                else
                  {//6
                   if (OrderStopLoss()==(C-TS))
                   return(0); // exit
                     {//7
                      if (C-OrderStopLoss()>TS)
                        {//8
                         if (OrderStopLoss()<(C-TS))
                           {//9
                            OrderModify(OrderTicket(),OrderOpenPrice(),C-TS,OrderTakeProfit(),0,Yellow);
                            return(0);
                           }//9
                        }//8
                     }//7
                  }//6
               }//5
            }//4
          else // go to short position
            {//4
             // check for short position (SELL) possibility
             // check for ZERO StopLoss 
             if (OrderStopLoss()==0)
               {//5
                OrderModify(OrderTicket(),OrderOpenPrice(),C+TS,OrderTakeProfit(),0,Yellow);
                return(0); // exit 
               }//5 
             else
               {//5
                if (Bar1Time != Time[1]) // 1st tick of new bar
                  {//6
                   OrderClose(OrderTicket(),OrderLots(),Bid,3,Gold); // close position
                   CloseOfBar0_ASAP = Close[1];
                   Bar1Time = Time[1];
                   return(0); // exit 
                  }//6
                else
                 {//6
                  if(OrderStopLoss() == (C+TS))
                  return(0); // exit
                 {//7
                  // check for Long Trailing Stop
                  if((OrderStopLoss()-C)>(TS))
                 {//8
                  if (OrderStopLoss() > (C+TS))
                    {//9
                     OrderModify(OrderTicket(),OrderOpenPrice(),C+TS,OrderTakeProfit(),0,Yellow);
                     return(0);
                    }//9
                 }//8
                 }//7
                 }//6
               }//5
            }//4
         }//3 
      }//2
   }//1
return(0);
}//0
// the end. 

 
Ickyrus:

Code is easier to read if indented and formated. To post code use the SRC button in the header of where you enter text alongside other editing goodies like Bold and Underline


Good to know, thank you
 

  1. When closing/deleting orders in the presence of multiple orders (multiple charts/multiple EA) you MUST COUNT DOWN
 
RaptorUK:
To use functions like OrderTicket() and OrderLots() you first have to select the order using OrderSelect() . . did you do that ? also, if you use OrderClosePrice() (instead of Bid) it will work for an OP_BUY or an OP_SELL


Actually, belive this will work

                if (TimeHour(OrderOpenTime()) != TimeHour(TimeCurrent()))  // 1st tick of new bar
                  {
                  OrderClose(OrderTicket(),OrderLots(),Bid,3,Gold); // close position
                  return(0); // exit   
Reason: