Download MetaTrader 5
To add comments, please log in or register
All articles are available from MetaTrader 5. Contribute your own one!
brettosm8
241
brettosm8 2014.06.29 06:29 

Hi there,

I'm trying to test modifications to the martingale system.  So far I've coded enough to play around with some parameters like how much to multiply volume on losing trades.  I tried to run this in Strategy Tester which only shows 2 items in the Results ta (as below) b i.e. orders to open.  Neither of the orders are ever opened and there is no progress after that.


I suspect this is due to my code rather than my Strategy Tester settings..  The code is below.

//-- place levels
extern double boxSize = .0025;
extern double reversalAmount = 1;
long chartID = ChartID();
bool actioned;
double levels[];
string objectNames[];
int arraySize;
int arrayCount;
//-- place orders
bool boxFound;
double boxTop;
double boxBottom;
double stopLossDistance = boxSize * reversalAmount;
double stopLoss;
int ticket;
extern double minVolume = 0.01;
extern double volumeMultiplier = 2;
double volume = minVolume;
int levelCount;
//-- cancelInvalidatedOrders
int totalOrders;
bool orderOpened;
//-- updateStopLoss
double nextBoxTop;
double nextBoxBottom;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      placeLevels();
      placeOrders();
      cancelInvalidatedOrders();
      closeAndEscalateOrders();
      updateStopLoss();
  }
//+------------------------------------------------------------------+
void updateStopLoss()
   {
      // if price reaches next box, update SL.
      if(!OrderSelect(0,SELECT_BY_POS)) Print("Error: Unable to select order! code #", GetLastError());
      
         if(OrderType() == OP_BUY)
            {
               if(Ask >= nextBoxTop)
                  {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(), OrderStopLoss()+boxSize,0,0)) Print("Error: Unable to modify order! code #", GetLastError());
                     nextBoxTop += boxSize;
                  }// if Ask >= boxTop + boxSize
               }// OrderType OP_BUY
      
         if(OrderType() == OP_SELL)
            {
               if(Bid <= nextBoxBottom)
                  {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(), OrderStopLoss()-boxSize,0,0)) Print("Error: Unable to modify order! code #", GetLastError());
                     nextBoxBottom -= boxSize;
                  }// end Bid <= nextBoxBottom
            }// OrderType OP_SELL
   }

void closeAndEscalateOrders()
   {
      // if reahces SL and trade is loss, then escalate trade
      if(!OrderSelect(0, SELECT_BY_POS)) Print("Error: Unable to select order! code #", GetLastError());
      
      if(OrderType() == OP_BUY)
         {
            // if SL reached and trade is loss
            if(Ask <= OrderStopLoss() && OrderProfit() < 0)
               {
                  stopLoss = OrderStopLoss();
                  if(!OrderClose(OrderTicket(),OrderLots(),Ask,3)) Print("Error: Unable to close order! code #", GetLastError());
                  levelCount++;
                  if(!OrderSend(Symbol(),OP_SELL,volume*volumeMultiplier,Bid,3,stopLoss+stopLossDistance,0)) Print("Error: Unable to send order! code #", GetLastError());
               }// end if
               
            // if SL reached and trade is win
            if(Ask <= OrderStopLoss() && OrderProfit() >= 0)
               {
                  if(!OrderClose(OrderTicket(),OrderLots(),Ask,3)) Print("Error: Unable to close order! code #", GetLastError());
               }// end if
         }// end OrderType OP_BUY
      
      if(OrderType() == OP_SELL)
         {
            // if SL reached and trade is loss
            if(Bid >= OrderStopLoss() && OrderProfit() < 0)
               {
                  stopLoss = OrderStopLoss();
                  if(!OrderClose(OrderTicket(),OrderLots(),Bid,3)) Print("Error: Unable to close order! code #", GetLastError());
                  levelCount++;
                  if(!OrderSend(Symbol(),OP_BUY,volume*volumeMultiplier,Ask,3,stopLoss-stopLossDistance,0)) Print("Error: Unable to send order! code #", GetLastError());
               }// end if
               
            // if SL reached and trade is win
            if(Bid >= OrderStopLoss() && OrderProfit() >= 0)
               {
                  if(!OrderClose(OrderTicket(),OrderLots(),Ask,3)) Print("Error: Unable to close order! code #", GetLastError());
               }// end if
         }// end OrderType OP_SELL
   }// end closeAndEscalateOrders

void cancelInvalidatedOrders()
   {
      totalOrders = OrdersTotal();
      
      // Determine if an order has been opened.
      while (!orderOpened)
         {
         for (int i = 0; i < totalOrders;i++)
            {
               if(!OrderSelect(i, SELECT_BY_POS)) Print("Error: Unable to select order! code #", GetLastError());             
               if (OrderType() == OP_BUY || OrderType() == OP_SELL) orderOpened = true;
            }// end for
         }// end while
      
      if (orderOpened)
         {
            for (int i = 0; i < totalOrders; i++)
               {
                  if(!OrderSelect(i, SELECT_BY_POS)) Print("Error: Unable to select order! code #", GetLastError());
                  if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP)
                     {
                        if(!OrderDelete(OrderTicket())) Print("Error: Unable to delete order! code #", GetLastError());
                     }// end if
               }// end for
         }// end if
   }// end cancelInvalidatedOrders

void placeOrders()
   {
       // locate box price is trading within
      int i = 0;
      while (!boxFound)
         {
            if(Ask <= levels[i])
               {
                  boxTop = StrToDouble(DoubleToStr(levels[i],4));
                  boxBottom = StrToDouble(DoubleToStr(levels[i-1],4));
                  nextBoxTop = boxTop + boxSize;
                  nextBoxBottom = boxBottom - boxSize;
                  boxFound = true;
               }// end if
            i++;
         }// end while
      
      // place orders at box's top and bottom
      levelCount++;
      if(!OrderSend(Symbol(), OP_BUYSTOP, volume, boxTop, 3, boxTop - stopLossDistance, 0, "", levelCount))
         {
            Print("OrderSend failed with error #",GetLastError());
         }// end if
      if(!OrderSend(Symbol(), OP_SELLSTOP, volume, boxBottom, 3, boxBottom + stopLossDistance, 0, "", levelCount))
         {
            Print("OrderSend failed with error #",GetLastError());
         }// end if
   }// end placeOrders
   
void placeLevels()
   {
      while (!actioned)
      {
         ObjectsDeleteAll(chartID,0);
         
         // calculate the number of levels
         for(double i=0;i<=2.0010;i+= NormalizeDouble(boxSize,4))
         {
            arraySize++;
         }// end for
         Print("calculated the number of levels");
         
         ArrayResize(levels,arraySize);
         ArrayResize(objectNames,arraySize);
         
         // populate arrays for the levels and their object names
         for(double level=0;level<=2.0010;level+= NormalizeDouble(boxSize,4))
         {
            levels[arrayCount] = level;
            objectNames[arrayCount] = "HL at: " + DoubleToStr(level);
            arrayCount++;
         }// end for
         Print("populated arrays for the levels and their object names");
         
         // create the HLine objects
         for(int i = 0; i < arraySize; i++)
         {
            if(!ObjectCreate(chartID,objectNames[i],OBJ_HLINE,0,0,levels[i])){
               Print("Error: can't create HLINE! code #",GetLastError());
            }// end if
            
            ObjectSet(objectNames[i],OBJPROP_COLOR,clrRed);
         }// end for
         
         actioned = true;
      }//end while
   }//end placeLevels()


Assistance appreciated.

Thanks,

Brad

whroeder1
13600
whroeder1 2014.06.29 13:11  
brad: Assistance appreciated.
  1. if(!OrderSelect(0,SELECT_BY_POS)) Print("Error: Unable to select order! code #", GetLastError());
    
    This makes the EA incompatible with every other EA including itself (on other charts) and manual trading. Use a OrderSelect loop and magic number. Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  2. if(OrderType() == OP_BUY)
       {
          // if SL reached and trade is loss
          if(Ask <= OrderStopLoss() && OrderProfit() < 0)
    
    You buy at the Ask and sell at the Bid. So for a buy the SL is at the bid. If the Ask is below the SL, the Bid is definitely below, and the order has already been closed by the broker. This code will never execute.
  3. boxTop = StrToDouble(DoubleToStr(levels[i],4));
    boxBottom = StrToDouble(DoubleToStr(levels[i-1],4));
    
    Why are you converting a price to a string and back to a price? boxTop = levels[i];
  4. You don't even need that array
    boxBottom = boxSize * int(Bid / boxSize);
    boxTop    = boxBottom + boxSize;
    
  5. if (orderOpened)
       {
          for (int i = 0; i < totalOrders; i++)
    
    In the presence of multiple order you MUST count down. Get in the habit. Loops and Closing or Deleting Orders - MQL4 forum
  6.          actioned = true;
    When do you ever reset that?
  7. Neither of the orders are ever opened and there is no progress after that. I suspect this is due to my code...
    How can it be due to your code. The orders are pending on market price. Your code it irrelevant. You didn't run the tester far enough for the market to trigger your order.
brettosm8
241
brettosm8 2014.06.30 13:47  
Quite a bit of work for me to do there.  Thanks very much WHRoeder.
/
To add comments, please log in or register