This is my first EA and I'm trying to apply hedging and martingale strategy. Help anyone;)

 
//+------------------------------------------------------------------+
//|                                                           HM.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
   
   double lots[10]        = {0.01,0.03,0.06,0.12,0.24,0.48,0.96,1.92,3.84,7.68};
   
   double tp  = 100*Point;
   double sl  = 200*Point;
   
   double OrderOpenPrice();
   
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
   
   
   for(int n=0 ; n<=9 ; n++)
   {
   
   if(OrdersTotal()==0)
   {
      if(OrderSend(Symbol(),OP_BUY,lots[n],Ask,3,Ask-sl*Point,Ask+tp*Point,NULL,n,0,Green)){}
   }
   
   else if(OrderSelect(n-1,SELECT_BY_POS,MODE_TRADES))
   {
      if(OrderOpenPrice()-Bid>=100*Point && OrderType()==OP_BUY)
      {
         if(OrderSend(Symbol(),OP_SELL,lots[n],Bid,3,Bid+sl*Point,Bid-tp*Point,NULL,n,0,Red)){}
      }
      else if(Ask-OrderOpenPrice()>=100*Point && OrderType()==OP_SELL)
      {
         if(OrderSend(Symbol(),OP_BUY,lots[n],Ask,3,Ask-sl*Point,Ask+tp*Point,NULL,n,0,Green)){}
      }
   }
   
   else 
   return;
   
      
   }
   
   }
//+------------------------------------------------------------------+

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. Need some coding help I have a couple...
 
hfzdn_nzl:
//+------------------------------------------------------------------+
//|                                                           HM.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
   
   double lots[10]        = {0.01,0.03,0.06,0.12,0.24,0.48,0.96,1.92,3.84,7.68};
   
   double tp  = 100*Point;
   double sl  = 200*Point;
   
   double OrderOpenPrice();
   
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
   
   
   for(int n=0 ; n<=9 ; n++)
   {
   
   if(OrdersTotal()==0)
   {
      if(OrderSend(Symbol(),OP_BUY,lots[n],Ask,3,Ask-sl*Point,Ask+tp*Point,NULL,n,0,Green)){}
   }
   
   else if(OrderSelect(n-1,SELECT_BY_POS,MODE_TRADES))
   {
      if(OrderOpenPrice()-Bid>=100*Point && OrderType()==OP_BUY)
      {
         if(OrderSend(Symbol(),OP_SELL,lots[n],Bid,3,Bid+sl*Point,Bid-tp*Point,NULL,n,0,Red)){}
      }
      else if(Ask-OrderOpenPrice()>=100*Point && OrderType()==OP_SELL)
      {
         if(OrderSend(Symbol(),OP_BUY,lots[n],Ask,3,Ask-sl*Point,Ask+tp*Point,NULL,n,0,Green)){}
      }
   }
   
   else 
   return;
   
      
   }
   
   }
//+------------------------------------------------------------------+

 
hfzdn_nzl:
The problem is with the OrderOpenPrice(). I’ve wanted to use the price of the previous open trade and I’ve used that function. Can anyone spot the mistake? It’s just a simple coding though 
 
hfzdn_nzl Can anyone spot the mistake?
  1. When you post code please use the SRC button! Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. if(OrdersTotal()==0) ...
    else if(OrderSelect(n-1,SELECT_BY_POS,MODE_TRADES))
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  3.  for(int n=0 ; n<=9 ; n++){  
       else if(OrderSelect(n-1,SELECT_BY_POS,MODE_TRADES))
    If n is zero then the OrderSelect will always fail.

  4. if(OrderSend(Symbol(),OP_BUY,lots[n],Ask,3,Ask-sl*Point,Ask+tp*Point,NULL,n,0,Green)){}
    
    Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  5.   You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  6. Same as Martingale: Martingale, guaranteed to blow your account eventually.