Why is this not producing any results on strategy tester?

 

Hello,

This is the code which strategy tester won't print any results from. I don't know why. Any help would be very greatly appreciated.

//--- input parameters
extern double   Lots=20;
extern int      StopLoss=20;

int MAX_ORDERS = 5;

int MagicNumber = 99;
int ticket;

double Ma1,Ma2;


void start()
{

//The below section is with regards to opening a buy or sell trade.

if(OrdersTotal()<MAX_ORDERS) //Defines the initial parameter which sets the total number of orders.
 {
  Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
  Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
  
  if (Ma1<Ma2) //The begining of a buy signal.
   {
    ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,StopLoss,0,"test_4 placed this trade", MagicNumber,0,Green); //Defines the placement of an order.
    if (ticket>0) //This is to check if an order has been successfully placed.
     {
      Print("Order Placed #", ticket);
     }
    else
     {
      Print("Order Placed Failed , error #", GetLastError());
     }
   }
   
   if (Ma1>Ma2) //The begining of a sell signal.
   {
    ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,StopLoss,0,"test_4 placed this trade", MagicNumber,0,Blue); //Defines the placement of an order.
    if (ticket>0) //This is to check if an order has been successfully placed.
     {
      Print("Order Placed #", ticket);
     }
    else
     {
      Print("Order Placed Failed , error #", GetLastError());
     }
   }
 }

//The section below is with regards to closing live orders.

MAX_ORDERS = OrdersTotal();

for(ticket = MAX_ORDERS - 1; ticket >= 0; ticket --) //The decrement allows the program to count through all the trades in the loop.
 {
  if(OrderSelect(ticket,SELECT_BY_POS,MODE_TRADES))//If the OrderSelect fails advance the loop to the next 'ticket'
  
  if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == OP_BUY) //This part aims to close a buy order.
   {
    Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    
   
    if(Ma1>Ma2)
     {
      if(!OrderClose(ticket,Lots,Bid,3,Red)) //Trying to close the order.
       Print("OrderClose failed, order number:",ticket,"Error:",GetLastError()); //If OrderClose failed.
     }
    }
   
  if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == OP_SELL) //This part aims to close a sell order.
   {
    Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    
    if(Ma1<Ma2)
     {
      if(!OrderClose(ticket,Lots,Ask,3,Red)) //Trying to close the order.
       Print("OrderClose failed, order number:",ticket,"Error:",GetLastError()); //If OrderClose failed.
     }
   }
 }
 

I edited it and it still doesn't produce results...

//--- input parameters
extern double   Lots=0.20;
extern int      StopLoss=20;

int MAX_ORDERS = 5;

int MagicNumber = 99;
int ticket;

double Ma1,Ma2;


void start()
{

//The below section is with regards to opening a buy or sell trade.

if(OrdersTotal()<MAX_ORDERS) //Defines the initial parameter which sets the total number of orders.
 {
  Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
  Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
  
  if (Ma1<Ma2) //The begining of a buy signal.
   {
    ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,StopLoss,0,"test_4 placed this trade", MagicNumber,0,Green); //Defines the placement of an order.
    if (ticket>0) //This is to check if an order has been successfully placed.
     {
      Print("Order Placed #", ticket);
     }
    else
     {
      Print("Order Placed Failed , error #", GetLastError());
     }
   }
   
   if (Ma1>Ma2) //The begining of a sell signal.
   {
    ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,StopLoss,0,"test_4 placed this trade", MagicNumber,0,Blue); //Defines the placement of an order.
    if (ticket>0) //This is to check if an order has been successfully placed.
     {
      Print("Order Placed #", ticket);
     }
    else
     {
      Print("Order Placed Failed , error #", GetLastError());
     }
   }
 }

//The section below is with regards to closing live orders.

MAX_ORDERS = OrdersTotal();

for(ticket = MAX_ORDERS - 1; ticket >= 0; ticket --) //The decrement allows the program to count through all the trades in the loop.
 {
  if(!OrderSelect(ticket,SELECT_BY_POS,MODE_TRADES)) continue;//If the OrderSelect fails advance the loop to the next 'ticket'
  
  if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == OP_BUY) //This part aims to close a buy order.
   {
    Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    
   
    if(Ma1>Ma2)
     {
      if(!OrderClose(ticket,Lots,Bid,3,Red)) //Trying to close the order.
       Print("OrderClose failed, order number:",ticket,"Error:",GetLastError()); //If OrderClose failed.
     }
    }
   
  if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == OP_SELL) //This part aims to close a sell order.
   {
    Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
    
    if(Ma1<Ma2)
     {
      if(!OrderClose(ticket,Lots,Ask,3,Red)) //Trying to close the order.
       Print("OrderClose failed, order number:",ticket,"Error:",GetLastError()); //If OrderClose failed.
     }
   }
 } 
}
 
void start()
  {

//The below section is with regards to opening a buy or sell trade.

   if(OrdersTotal()<MAX_ORDERS) //Defines the initial parameter which sets the total number of orders.
     {
      Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
      Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.

      if(Ma1<Ma2) //The begining of a buy signal.
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"test_4 placed this trade",MagicNumber,0,Green); //Defines the placement of an order.
         if(ticket>0) //This is to check if an order has been successfully placed.
           {
            Print("Order Placed #",ticket);
           }
         else
           {
            Print("Order Placed Failed , error #",GetLastError());
           }
        }

      if(Ma1>Ma2) //The begining of a sell signal.
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,StopLoss,0,"test_4 placed this trade",MagicNumber,0,Blue); //Defines the placement of an order.
         if(ticket>0) //This is to check if an order has been successfully placed.
           {
            Print("Order Placed #",ticket);
           }
         else
           {
            Print("Order Placed Failed , error #",GetLastError());
           }
        }
     }

//The section below is with regards to closing live orders.

   //MAX_ORDERS=OrdersTotal();

   for(ticket=OrdersTotal()-1; ticket>=0; ticket --) //The decrement allows the program to count through all the trades in the loop.
     {
      if(!OrderSelect(ticket,SELECT_BY_POS,MODE_TRADES)) continue;//If the OrderSelect fails advance the loop to the next 'ticket'

      if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==OP_BUY) //This part aims to close a buy order.
        {
         Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
         Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.


         if(Ma1>Ma2)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Red)) //Trying to close the order.
               Print("OrderClose failed, order number:",ticket,"Error:",GetLastError()); //If OrderClose failed.
           }
        }

      if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol() && OrderType()==OP_SELL) //This part aims to close a sell order.
        {
         Ma1=iMA(Symbol(),PERIOD_M1,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.
         Ma2=iMA(Symbol(),PERIOD_M1,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defines the moving average.

         if(Ma1<Ma2)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Red)) //Trying to close the order.
               Print("OrderClose failed, order number:",ticket,"Error:",GetLastError()); //If OrderClose failed.
           }
        }
     }
  }

You need to calculate the stoploss value. trying to open a buy order with a stoploss at 20.0000 will usually fail. It will work for sells but be pretty useless

Do not change the value of MAX_ORDERS 

In the closing code, ticket is an index, not a ticket number 

 
Thanks for the help. How would you suggest is an efficient way to calculate a stop loss?
 
BartWinter: Thanks for the help. How would you suggest is an efficient way to calculate a stop loss?
  1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  3. Do NOT use TickValue by itself - DeltaPerlot
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out
 
Thanks a lot for this. This help is really great thank you. However, upon testing the code it only seems to place buy orders and not sell orders. With the corrections made is there any reason why that is?