Download MetaTrader 5
To add comments, please log in or register
Publish programs in Code Base. Improve your rating!
Donald Gibson
483
Donald Gibson 2015.09.10 23:27 

I have the following function that is supposed to create 5 pending orders.

The distance of the the pending orders are to be 20pips apart.

I can open the inital market order and place the first pending order at the proper distance but the following 4 pending orders are placed at the same price as the first.

How can i place each of the following 4 pending orders 20 pips apart?

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PendingOrders()
  {
//-------------------------------------------------------------------------------------------------
   double PendingDistance=NormalizeDouble(PendingOrderDistance*pips,Digits);
   double PendingPrice=NormalizeDouble(OrderOpenPrice()+PendingDistance,Digits);
//-------------------------------------------------------------------------------------------------
   int i;
   int NewPendingLong;
   int NewPendingShort;

   if(OrdersTotal()<NumberofOrders)
     {
      for(i=0; i<NumberofOrders; i++)
        {
         if(OrderType()==OP_BUY)
           {
            NewPendingLong=OrderSend(Symbol(),OP_BUYSTOP,EA_LotSize,PendingPrice,EA_Slippage,PendingLongSL,PendingLongTP,EA_Comment,EA_MagicNumber,EA_ArrowColor);
            Print("New Pending Long..Error  : "+ErrorDescription(GetLastError()));
           }
         if(OrderType()==OP_SELL)
           {
            NewPendingShort=OrderSend(Symbol(),OP_SELLSTOP,EA_LotSize,PendingPrice,EA_Slippage,PendingLongSL,PendingLongTP,EA_Comment,EA_MagicNumber,EA_ArrowColor);
            Print("New Pending Short..Error  : "+ErrorDescription(GetLastError()));
           }
        }
     }
  }
Keith Watford
Moderator
7904
Keith Watford 2015.09.11 00:24  
void PendingOrders()
  {
//-------------------------------------------------------------------------------------------------
   double PendingDistance=NormalizeDouble(PendingOrderDistance*pips,Digits);
   double PendingPrice=OrderOpenPrice();
//-------------------------------------------------------------------------------------------------
   int i;
   int NewPendingLong;
   int NewPendingShort;

   if(OrdersTotal()<NumberofOrders)
     {
      for(i=0; i<NumberofOrders; i++)
        {
         if(OrderType()==OP_BUY)
           {
            PendingPrice=NormalizeDouble(PendingPrice+PendingDistance,Digits);
            NewPendingLong=OrderSend(Symbol(),OP_BUYSTOP,EA_LotSize,PendingPrice,EA_Slippage,PendingLongSL,PendingLongTP,EA_Comment,EA_MagicNumber,EA_ArrowColor);
            Print("New Pending Long..Error  : "+ErrorDescription(GetLastError()));
           }
         if(OrderType()==OP_SELL)
           {
            PendingPrice=NormalizeDouble(PendingPrice-PendingDistance,Digits);
            NewPendingShort=OrderSend(Symbol(),OP_SELLSTOP,EA_LotSize,PendingPrice,EA_Slippage,PendingLongSL,PendingLongTP,EA_Comment,EA_MagicNumber,EA_ArrowColor);
            Print("New Pending Short..Error  : "+ErrorDescription(GetLastError()));
           }
        }
     }
  }
Something like this?
Donald Gibson
483
Donald Gibson 2015.09.11 07:18  
Nope didn't work.
Keith Watford
Moderator
7904
Keith Watford 2015.09.11 21:35  
4x_Gypsy:
Nope didn't work.

I was puzzled why this didn't work, but must admit that I didn't actually look at the code for your OrderSend(). I was just focused on your code placing the trades the correct distance apart.

I quickly wrote a test EA and found that the reason it doesn't work is that your OrderSend() is wrong. It is missing the Expiration parameter and there is no calculations for the SL and TP.

Adapt this to your needs.

#property strict
//--- input parameters
input int         EA_MagicNumber=999;
input double      EA_LotSize=1;
input int         EA_Slippage=5;
input double      InpPendingLongSL=50;
input double      InpPendingLongTP=50;
input int         InpPendingOrderDistance=20;
input int         NumberofOrders=5;
input string      EA_Comment="Pending";
input color       EA_ArrowColor=clrBlue;

bool              FirstTick=true;
double            SLDecimal;
double            TPDecimal;
double            PendingDistance;

#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   if(Digits==3 || Digits==5)
     {
      SLDecimal=InpPendingLongSL*Point*10;
      TPDecimal=InpPendingLongTP*Point*10;
      PendingDistance=InpPendingOrderDistance*Point*10;
     }
   else
     {
      SLDecimal=InpPendingLongSL*Point;
      TPDecimal=InpPendingLongTP*Point;
      PendingDistance=InpPendingOrderDistance*Point;
     }

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(FirstTick)
     {
      double PendingLongSL=Ask-SLDecimal;
      double PendingLongTP=Ask+SLDecimal;
      int ticket=OrderSend(Symbol(),OP_BUY,EA_LotSize,Ask,EA_Slippage,PendingLongSL,PendingLongTP,EA_Comment,
                           EA_MagicNumber,EA_ArrowColor);
      if(ticket>0)
        {
         PendingOrders(ticket);
         FirstTick=false;
        }
     }
//---

  }
//+------------------------------------------------------------------+
void PendingOrders(int ticket)
  {
   if(OrderSelect(ticket,SELECT_BY_TICKET))
     {
      double PendingPrice=OrderOpenPrice();
      int i;
      int NewPendingLong;
      int NewPendingShort;

      if(OrdersTotal()<NumberofOrders)
        {
         for(i=0; i<NumberofOrders; i++)
           {
            if(OrderType()==OP_BUY)
              {
               PendingPrice=NormalizeDouble(PendingPrice+PendingDistance,Digits);
               double PendingLongSL=PendingPrice-SLDecimal;
               double PendingLongTP=PendingPrice+SLDecimal;
               Print("Entry=",DoubleToStr(PendingPrice,Digits));
               Print("SL=",DoubleToStr(PendingLongSL,Digits));
               NewPendingLong=OrderSend(Symbol(),OP_BUYSTOP,EA_LotSize,PendingPrice,EA_Slippage,PendingLongSL,
                                        PendingLongTP,EA_Comment,EA_MagicNumber,0,EA_ArrowColor);
               Print("New Pending Long..Error  : "+ErrorDescription(GetLastError()));
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+

 Only done for the buy orders

Donald Gibson
483
Donald Gibson 2015.09.11 22:41  
GumRai:

I was puzzled why this didn't work, but must admit that I didn't actually look at the code for your OrderSend(). I was just focused on your code placing the trades the correct distance apart.

I quickly wrote a test EA and found that the reason it doesn't work is that your OrderSend() is wrong. It is missing the Expiration parameter and there is no calculations for the SL and TP.

Adapt this to your needs.

 Only done for the buy orders

Thank You very much. I will work on it.
Keith Watford
Moderator
7904
Keith Watford 2015.09.11 23:10  
4x_Gypsy:
Thank You very much. I will work on it.
I put the order on First tick so that I could check it in the strategy tester and it worked properly.
Donald Gibson
483
Donald Gibson 2015.09.12 00:31  
GumRai:
I put the order on First tick so that I could check it in the strategy tester and it worked properly.

Well it works good on this end too. I will test it live next week to be sure.

Thank You, Thank You, Thank You and my girlfriend thanks you too (because now i will shutup about it, lol).

Donald Gibson
483
Donald Gibson 2015.09.14 07:40  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void PlacePendingOrders(int OpenTicket)
  {
//places pending orders at a pre-determined distance from the market order and each other  
/*
//-------------------------------------------------------------------------------------------------
//Global Order SetUp
//-------------------------------------------------------------------------------------------------
extern string GlobalEAInputs="Inputs for all Trades";//Alert
extern double EA_LotSize=0.01;//LotSize
extern int    EA_Slippage=0;//Slippage
extern string EA_Comment="Place Comment Here";//Comment
extern int    EA_MagicNumber=0001;//Magic Number
extern color  EA_ArrowColor=clrNONE;//Arrow Color
extern string EA_break="";//=======================================
//-------------------------------------------------------------------------------------------------
//Static Stops for StopLoss and TakeProfit
//-------------------------------------------------------------------------------------------------
extern string staticstop="Use Static Stops for SL and TP";
extern int    Static_StopLoss=50;//StopLoss
extern int    Static_TakeProfit=100;//TakeProfit
extern string StaticStopsbreak="";//=======================================
//-------------------------------------------------------------------------------------------------
//Pending Orders
extern string Pending_Orders="Pending Orders";
extern int    PendingOrderDistance=5;//Pending Order Distance
extern int    NumberofOrders=5;//Number of Orders
//-------------------------------------------------------------------------------------------------
  */
//-------------------------------------------------------------------------------------------------
   double PendingDistance=NormalizeDouble(PendingOrderDistance*pips,Digits);
   double PendingPrice=OrderOpenPrice();
//-------------------------------------------------------------------------------------------------
   int NewPendingOrder=0;
//-------------------------------------------------------------------------------------------------
   if(OrderSelect(OpenTicket,SELECT_BY_TICKET))
      if(OrdersTotal()<NumberofOrders)
         for(i=0; i<NumberofOrders; i++)
           {
            if(OrderType()==OP_BUY)
              {
               PendingPrice=NormalizeDouble(PendingPrice+PendingDistance,Digits);
               double PendingLongSL=NormalizeDouble(PendingPrice-(Static_StopLoss*pips),Digits);
               double PendingLongTP=NormalizeDouble(PendingPrice+(Static_TakeProfit*pips),Digits);
               NewPendingOrder=OrderSend(Symbol(),OP_BUYSTOP,EA_LotSize,PendingPrice,EA_Slippage,
                                         PendingLongSL,PendingLongTP,EA_Comment,EA_MagicNumber,0,EA_ArrowColor);
               Print("New Pending Long..Error  : "+ErrorDescription(GetLastError()));
              }
            if(OrderType()==OP_SELL)
              {
               PendingPrice=NormalizeDouble(PendingPrice-PendingDistance,Digits);
               double PendingShortSL=NormalizeDouble(PendingPrice+(Static_StopLoss*pips),Digits);
               double PendingShortTP=NormalizeDouble(PendingPrice-(Static_TakeProfit*pips),Digits);
               NewPendingOrder=OrderSend(Symbol(),OP_SELLSTOP,EA_LotSize,PendingPrice,EA_Slippage,
                                         PendingShortSL,PendingShortTP,EA_Comment,EA_MagicNumber,0,EA_ArrowColor);
               Print("New Pending Short..Error  : "+ErrorDescription(GetLastError()));
              }
           }
//-------------------------------------------------------------------------------------------------
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeletePendingOrders()
  {
//only deletes pending orders if the conditions produce a close signal  
//-------------------------------------------------------------------------------------------------
   int DeleteOrders=0;
//-------------------------------------------------------------------------------------------------
   for(i=OrdersTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)
           {
            DeleteOrders=OrderDelete(OrderTicket());
            Print("Delete Pending..Error  : "+ErrorDescription(GetLastError()));
           }
//------------------------------------------------------------------------------------------------- 
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteAllOrders()
  {
//only deletes pending orders if any of the market orders close at the stoploss
//-------------------------------------------------------------------------------------------------
   int CloseTicket=0;
   int DeleteOrders=0;
//-------------------------------------------------------------------------------------------------
   if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
     {
      if(OrderStopLoss()==OrderClosePrice())
        {
         for(i=OrdersTotal()-1;i>=0;i--)
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
              {
               DeleteOrders=OrderDelete(OrderTicket());
               Print("Delete Pending..Error  : "+ErrorDescription(GetLastError()));
               CloseTicket=OrderClose(OrderTicket(),OrderLots(),Bid,EA_Slippage,EA_ArrowColor);
               Print("Close Long Order..Error  : "+ErrorDescription(GetLastError()));
               CloseTicket=OrderClose(OrderTicket(),OrderLots(),Ask,EA_Slippage,EA_ArrowColor);
               Print("Close Short Order..Error  : "+ErrorDescription(GetLastError()));
              }
        }
     }
  }

GumRai

Works perfectly. Thank You again.

I added 2 functions to delete the pending orders when the market order(s) close. I am now working on a way to combine them into 1 function.

Happy Trading to you/

/
To add comments, please log in or register