Download MetaTrader 5

Help !!

To add comments, please log in or register
FxTrading8565
14
FxTrading8565  
Can any one check the EA why it is giving order send error 130 and not placing order when the other is close
extern int start_hour = 2 ;
extern int end_hour = 5 ;
extern int max_trades = 4 ;
extern bool Break_Even = true ;
extern double BE_Pips = 300;   
extern double BE_SL_PIPS = 100 ;  
extern bool   Use_RiskManagement_LotSize=false;
extern double Risk_Percent=2;
extern double take_profit = 400 ; // take profit value for your orders .
extern double stop_loss   = 200 ; // stop loss value for your orders .
extern double lot_size    = 0.1 ; // lot size for your orders .
extern double slippage    = 2   ; // slippage for your orders .
extern bool use_martingale = true ; 
extern double multiply_in_loss = 2 ; 

extern int       BoxLength                        = 8;
extern double    BoxRange                         = 25;
extern string    BoxTimeFrame                     = "0";
extern double    BoxBufferPips                    = 2;
extern int       MinBarsClosedOutsideBO           = 0;
extern int       MaxBreakoutTradesPerBox          = 4;
extern bool      InitialBreakoutDirectionOnly     = false;
extern bool      LongTrades                       = true;
extern bool      ShortTrades                      = true;
extern double    QuickTakeProfit                  = 40;
extern double    StopLossOverride                 = 20;
extern double    EmergencyStopLoss                = 100;
extern bool      BoxMartingale                    = true;
extern bool      AutoReduceBoxSize                = true;
extern int       DaysBack                         = 0;
extern bool      SignalMail                       = false;
extern bool      SignalAlert                      = true;
extern bool      ShowFibs                         = false;
extern string    FibSequence                      = "0.618,1.382,2.618,4.236,5.854,7.472,9.090,11.708,14.326";
extern int       FibsFontSize                     = 7;
extern color     FibsLinesColor                   = DimGray;
extern int       FibsLinesWidth                   = 1;
extern bool      ShowSwings                       = true;
extern int       SwingsFontSize                   = 9;
extern color     SwingsLinesColor                 = Wheat;
extern int       SwingsLinesWidth                 = 1;
extern int       BoxFontSize                      = 9;
extern color     BoxColor                         = 11184640;
extern color     ContBoxColor                     = Violet;
extern bool      ContBoxFullColored               = true;
extern color     BoxBufferLinesColor              = 11184640;
extern int       BoxBufferLinesWidth              = 2;
extern color     ContBoxBufferLinesColor          = Yellow;
extern int       ContBoxBufferLinesWidth          = 2;
extern color     BreakoutPriceColor               = 11184640;
extern int       BreakoutPriceWidth               = 1;
extern color     ContBoxPriceColor                = Blue;
extern int       ChartDisplay                     = 3;
extern int       DisplayPanelFontSize             = 7;
extern color     StatsColor                       = DimGray;
extern color     StatsBGColor                     = Black;
extern color     PipsProfitColor                  = DarkGreen;
extern color     PipsLossColor                    = MediumVioletRed;
extern int       StatsCorner                      = 0;
extern bool      BoxVerticalLineDelimiter         = true;
extern color     BoxVerticalLineColor             = DarkSlateGray;
extern int       SwingLabelsFontSize              = 9;
extern color     SwingLabelsColor                 = Pink;
extern int       BuySignalArrowCode               = 2;
extern color     BuySignalArrowColor              = 11193702;
extern int       BuySignalArrowWidth              = 3;
extern int       SellSignalArrowCode              = 2;
extern color     SellSignalArrowColor             = 5275647;
extern int       SellSignalArrowWidth             = 3;
extern string    Fonts                            = "Calibri";


int magic_b = 25463 ; // magic number for your buy order
int magic_s = 78133 ; // magic number for your sell order
double high_value ;  // high value of indicator 
double low_value ;   // low value of indicator
int ticket_b ; // ticket number for buy order
int ticket_s ; // ticket number for sell order
double lot_risk ; // extern lot size variable
double g            ;
double v            ;
datetime start_box  ;
datetime close_box ;
bool trading ;
double sell_signal ;
double buy_signal ;
double Last_high_value = 0 ;
double Last_low_value = 0 ;
double box_start ;
double last_box = 0 ;
double profit ;
int type ;
double lots ;
double lot_used ;
int magic ;
double count_orders = 0 ;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Alert("The EA Have Been Attach to chart"+Symbol()+"succesfully ");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Alert("The EA have been removed");
   Alert("error number = ",GetLastError());
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
 SSBOD() ;
 Last_Profit() ;
 lot() ;
//-------------------------------------------------------- 
if( box_start != last_box )
{
 last_box = box_start ;
 count_orders = 0 ;
 if( OrdersTotal() != 0 )
  {
   double h = OrderDelete(ticket_s);
   double a = OrderDelete(ticket_b);
  }
}


if( Hour() >= end_hour && Hour() < start_hour )
 {
  close_all();
 }
//--------------------------------------------------------
if( Hour() >= start_hour && Hour() <= end_hour )
{
   if( OrdersTotal() == 0 )
    {
     if( profit < 0 && use_martingale == true)
     {
      lot_used = lots*multiply_in_loss ;
     }
     else
     {
       lot_used = lot_risk ;
     }
      Alert("order place due to first condition");
       buy_stop()  ; 
       sell_stop() ;
    }
   else 
    {
     
    }  
//--------------------------------------------------------
//--------------------------------------------------------
int type0;
int ticket;
int total = OrdersTotal();
double openPrice, stopPrice,bbprice,bsprice;
if( Break_Even )
{  
 for(int i=total-1;i>=0;i--)
 {
  for(int j=i;j>=0;j--)
  if(OrderSelect(i, SELECT_BY_POS))
   {
    ticket     = OrderTicket();
    type0      = OrderType();
    openPrice  = OrderOpenPrice();
    stopPrice  = OrderStopLoss();
    bbprice    = OrderOpenPrice()+BE_SL_PIPS*Point;
    bsprice    = OrderOpenPrice()-BE_SL_PIPS*Point;
    if(type0 == OP_SELL && stopPrice > openPrice && Ask<= (openPrice-BE_Pips*Point) && OrderSymbol()==Symbol())
    OrderModify(ticket,OrderOpenPrice(),bsprice,OrderTakeProfit(),0,Red);
    else if(type0 == OP_BUY && stopPrice < openPrice && Bid>= (openPrice+BE_Pips*Point)&& OrderSymbol()==Symbol() )
    OrderModify(ticket,OrderOpenPrice(), bbprice,OrderTakeProfit(),0,Blue);
    }
  }
} 
//--------------------------------------------------------  
if( box_start != last_box )     // if there is a new box
 {
  last_box = box_start ;     
  start_box = iTime(NULL,0,0);  // start time of the box 
 }
 
static datetime loct; // last order closed time
for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) 
if (OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  
   &&  OrderType()        <=1
   &&  OrderCloseTime()    >  loct && OrderCloseTime() > start_box
   )
   {
    count_orders ++ ;   
    loct = OrderCloseTime() ;
    break;
   }
//--------------------------------------------------------
if( place_orders()  )
{
if( profit < 0  ) 
 {
  if( type == OP_BUY )
  {
   if( OrdersTotal() == 0 )
   {
   if( use_martingale )
   {
   lot_used = lots * multiply_in_loss ;
   }
   else
   {
   lot_used = lot_risk ;
   } 
   sell_stop();
   buy_stop();
   }
  }
 else if( type == OP_SELL )
  {
   if( OrdersTotal() == 0 )
   {
   if( use_martingale )
   {
   lot_used = lots * multiply_in_loss ;
   }
   else
   {
    lot_used = lot_risk ;
   }
   buy_stop();
   sell_stop();
   }
  } 
 else
  {
   // keep smiling
  } 
 }
}
else
{
//Alert("OrderLimit have been exceedded");
} 
//--------------------------------------------------------
}
//--------------------------------------------------------
}
//--------------------------------------------------------


bool place_orders() 
{    
  if( count_orders >= max_trades ) 
  return(false);
  else if( count_orders <= max_trades )
  return(true);
  else
  return(EMPTY_VALUE);  
} 

int close_all() // user defne function for deleting pending order and closing market order.
{
 int total=OrdersTotal();                  // declaring total to Orders Total function.
 for(int i=total-1;i>=0;i--)
  {
   int y=OrderSelect(i,SELECT_BY_POS);           // selecting the order.
   int typec=OrderType();                // declaring typr to OrdersTYpe.
   bool result=false;                    // declaring result to false.
   switch(typec) // checking orders type.
   {
    //Close opened long positions.
    case OP_BUY       : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red); // if it is a buy order than close the order with the given parameters
    break;            // breaking the loop
    //Close opened short positions.
    case OP_SELL      : result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Red); // if it is a sell order than use the followinf parametrs for closing the trade
    break;            // breaking the loop
    //Close pending orders.
    case OP_BUYLIMIT  : result=OrderDelete(OrderTicket());                 // if it is buy limit order.
    case OP_BUYSTOP   : result=OrderDelete(OrderTicket());                  // if it is buy styop order.
    case OP_SELLLIMIT : result=OrderDelete(OrderTicket());                  // if it is sell limit order.
    case OP_SELLSTOP  : result=OrderDelete(OrderTicket());   //  if it is sell stop order or any one of order from the above cases use the given parameters for its deletion 
   }
  if(result==false) // declaring result to false
   {
   // Alert("Order ",OrderTicket()," failed to close. Error:",GetLastError());     // if order was not selected than give us its ticket number and tell us its error number
    Sleep(3000);                          // stoping ther functions for 3000 seconds
   }
  }
  return(0);                                // return the value zero
}
//+------------------------------------------------------------------+

void Last_Profit()
{
static datetime lastOrderCloseTime;
    for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) 
    if (
        OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  
    &&  OrderType()        <=1
    &&  OrderCloseTime()    >  lastOrderCloseTime
       )
     {
      profit = OrderProfit() ;
      type = OrderType() ;
      lots = OrderLots() ;
      magic = OrderMagicNumber();
      lastOrderCloseTime = OrderCloseTime();
     }
    
}

void sell_stop()
{
 Alert("value of lot = ",lot_used);
 Alert("value of low line = ",low_value);
 Alert("value of stoploss = ",low_value+(stop_loss*Point));
 Alert("value of takeprofit = ",low_value-(take_profit*Point) );
 ticket_s = OrderSend(Symbol(),OP_SELLSTOP,lot_used,low_value,slippage,0,low_value-(take_profit*Point),"sell stop order",magic_s,0,Red);
 Alert("Sell Stop Order Error = ",GetLastError());
}

void buy_stop()
{
 Alert("value of lot = ",lot_used);
 Alert("value of high line = ",high_value);
 Alert("value of stoploss = ",high_value-(stop_loss*Point));
 Alert("value of takeprofit = ",high_value+(take_profit*Point));
 ticket_b = OrderSend(Symbol(),OP_BUYSTOP,lot_used,high_value,slippage,0,high_value+(take_profit*Point),"buy stop order",magic_b,0,Blue);
 Alert("Buy stop order Error = ",GetLastError());
}

void SSBOD()
 { 
  high_value = iCustom(NULL,0,"AuthorizedSSBODynamicBreakoutBoxFinalEdition_v2.2nnew_box_alertx",BoxLength,BoxRange,BoxTimeFrame,BoxBufferPips,MinBarsClosedOutsideBO,MaxBreakoutTradesPerBox,InitialBreakoutDirectionOnly,LongTrades,ShortTrades,QuickTakeProfit,StopLossOverride,EmergencyStopLoss,BoxMartingale,AutoReduceBoxSize,DaysBack,SignalMail,SignalAlert,ShowFibs,FibSequence,FibsFontSize,FibsLinesColor,FibsLinesWidth,ShowSwings,SwingsFontSize,SwingsLinesColor,SwingsLinesWidth,BoxFontSize,BoxColor,ContBoxColor,ContBoxFullColored,BoxBufferLinesColor,BoxBufferLinesWidth,ContBoxBufferLinesColor,ContBoxBufferLinesWidth,BreakoutPriceColor,BreakoutPriceWidth,ContBoxPriceColor,ChartDisplay,DisplayPanelFontSize,StatsColor,StatsBGColor,PipsProfitColor,PipsLossColor,StatsColor,BoxVerticalLineDelimiter,BoxVerticalLineColor,SwingLabelsFontSize,SwingLabelsColor,BuySignalArrowCode,BuySignalArrowColor,BuySignalArrowWidth,SellSignalArrowCode,SellSignalArrowColor,SellSignalArrowWidth,Fonts,2,0);
  low_value  = iCustom(NULL,0,"AuthorizedSSBODynamicBreakoutBoxFinalEdition_v2.2nnew_box_alertx",BoxLength,BoxRange,BoxTimeFrame,BoxBufferPips,MinBarsClosedOutsideBO,MaxBreakoutTradesPerBox,InitialBreakoutDirectionOnly,LongTrades,ShortTrades,QuickTakeProfit,StopLossOverride,EmergencyStopLoss,BoxMartingale,AutoReduceBoxSize,DaysBack,SignalMail,SignalAlert,ShowFibs,FibSequence,FibsFontSize,FibsLinesColor,FibsLinesWidth,ShowSwings,SwingsFontSize,SwingsLinesColor,SwingsLinesWidth,BoxFontSize,BoxColor,ContBoxColor,ContBoxFullColored,BoxBufferLinesColor,BoxBufferLinesWidth,ContBoxBufferLinesColor,ContBoxBufferLinesWidth,BreakoutPriceColor,BreakoutPriceWidth,ContBoxPriceColor,ChartDisplay,DisplayPanelFontSize,StatsColor,StatsBGColor,PipsProfitColor,PipsLossColor,StatsColor,BoxVerticalLineDelimiter,BoxVerticalLineColor,SwingLabelsFontSize,SwingLabelsColor,BuySignalArrowCode,BuySignalArrowColor,BuySignalArrowWidth,SellSignalArrowCode,SellSignalArrowColor,SellSignalArrowWidth,Fonts,3,0);
  box_start = iCustom(NULL,0,"AuthorizedSSBODynamicBreakoutBoxFinalEdition_v2.2nnew_box_alertx",BoxLength,BoxRange,BoxTimeFrame,BoxBufferPips,MinBarsClosedOutsideBO,MaxBreakoutTradesPerBox,InitialBreakoutDirectionOnly,LongTrades,ShortTrades,QuickTakeProfit,StopLossOverride,EmergencyStopLoss,BoxMartingale,AutoReduceBoxSize,DaysBack,SignalMail,SignalAlert,ShowFibs,FibSequence,FibsFontSize,FibsLinesColor,FibsLinesWidth,ShowSwings,SwingsFontSize,SwingsLinesColor,SwingsLinesWidth,BoxFontSize,BoxColor,ContBoxColor,ContBoxFullColored,BoxBufferLinesColor,BoxBufferLinesWidth,ContBoxBufferLinesColor,ContBoxBufferLinesWidth,BreakoutPriceColor,BreakoutPriceWidth,ContBoxPriceColor,ChartDisplay,DisplayPanelFontSize,StatsColor,StatsBGColor,PipsProfitColor,PipsLossColor,StatsColor,BoxVerticalLineDelimiter,BoxVerticalLineColor,SwingLabelsFontSize,SwingLabelsColor,BuySignalArrowCode,BuySignalArrowColor,BuySignalArrowWidth,SellSignalArrowCode,SellSignalArrowColor,SellSignalArrowWidth,Fonts,4,0);
  
 }


double RMLotSize()
  {
   double acctbal=0.00;
   if(AccountBalance()>0){acctbal=AccountBalance();}
//-------------------------------------------------------------------------------------------------
//risk percentage
   double riskpercentage=0.00;
   if(Risk_Percent>0){riskpercentage=Risk_Percent/100;}
//-------------------------------------------------------------------------------------------------
//amount of capitol to risk based on percentage
   double riskcapitol=0.00;
   if(acctbal>0){riskcapitol=acctbal*riskpercentage;}
//-------------------------------------------------------------------------------------------------
//lotsize based on risk capitol
   double lotsize=0.00;
   if(riskcapitol>0){lotsize=riskcapitol*0.01;}
//-------------------------------------------------------------------------------------------------
//if lotsize is more than is permitted change the lotsize to the maximum  permitted
   if(lotsize>MarketInfo(Symbol(),MODE_MAXLOT)) {lotsize=MarketInfo(Symbol(),MODE_MAXLOT);}
//-------------------------------------------------------------------------------------------------
//if lotsize is less than is permitted change the lotsize to the minimum  permitted  
   if(lotsize<MarketInfo(Symbol(),MODE_MINLOT)) {lotsize=MarketInfo(Symbol(),MODE_MINLOT);}
//-------------------------------------------------------------------------------------------------
   return(lotsize);
  }
//+------------------------------------------------------------------+
void lot()
{
if(Use_RiskManagement_LotSize)
{
 lot_risk = RMLotSize() ;
}
else if( !Use_RiskManagement_LotSize )
 {
  lot_risk = lot_size ;
 }
else
 {
  //Alert("Error using Risk ManageMent = ",GetLastError());
 } 
}
To add comments, please log in or register