Why is this not producing any results on strategy tester?

Back to topics list To post a new topic, please log in or register
avatar
18
BartWinter 2015.03.14 18:52
 

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.
     }
   }
 }
avatar
18
BartWinter 2015.03.14 19:00 #
 

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.
     }
   }
 } 
}

 

avatar
Moderator
3419
GumRai 2015.03.14 21:21 #
 
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 

avatar
18
BartWinter 2015.03.15 11:46 #
 
Thanks for the help. How would you suggest is an efficient way to calculate a stop loss?
avatar
13006
WHRoeder 2015.03.15 14:03 #
 
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

avatar
18
BartWinter 2015.03.16 21:01 #
 
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?
avatar
13006
WHRoeder 2015.03.17 13:57 #
 
Back to topics list  

To add comments, please log in or register