Beginner In MQL5, Help Needed...

To add comments, please log in or register
Mideel
90
Mideel  

Just starting to code in MQL5, I tried to make very simple martingale EA which will double the lot size if we lose and stop after we win. When I tested this on strategy tester, its always written failed instant buy if I input Buy in the position input variable ( extern variable ), but it works if I choose sell. and its weird if I change the code in line 108, 109..


From..

takeProfit = NormalizeDouble( tick.ask +  _takeProfit * Point() * 10, _Digits ) ;

stopLoss = NormalizeDouble( tick.ask - _stopLoss * Point() * 10, _Digits ) ;


To:

takeProfit = NormalizeDouble( tick.bid+  _takeProfit * Point() * 10, _Digits ) ;

stopLoss = NormalizeDouble( tick.bid- _stopLoss * Point() * 10, _Digits ) ;


It'll work although it seems not connected.


Here's the code:


   enum OrderPosition
   {
      Buy, Sell 
   } ;

   enum OrderStats
   {
      NONE, LOSE, WIN 
   } ;

   input OrderPosition _position = Buy ;  // Position
   input double _lot = 1 ;                // Lot size
   input int _stopLoss = 20 ;             // Stop loss points
   input int _takeProfit = 20 ;           // Take profit point    
   input double _multiply = 2 ;           // Amount of multiply
   input bool _perCandle = true ;         // Minimum interval


   void OnInit()
   {
   
   }

   void OnDeinit(const int reason)
   {   
   
   }

   void OnTick()
   {
      static int level = 1 ;
      static double takeProfit ;
      static double stopLoss ;
      static OrderStats lastOrder = NONE ;
      MqlTradeRequest request ;
      MqlTradeResult result ;
      MqlTick tick ;
      double lastOrderPrice = 0 ;
      double lot ;
      bool test = PositionSelect( _Symbol ) ;         // debug
      int err = GetLastError() ;                      // debug
      
      SymbolInfoTick( Symbol(), tick ) ;
      ZeroMemory( request ) ;
      ZeroMemory( result ) ;
      
      request.action = TRADE_ACTION_DEAL ;
      
      if ( PositionSelect( _Symbol ) == false ) 
      {
          if ( lastOrder == NONE )
          {
                if( _position == Buy )
                {
                  request.action = TRADE_ACTION_DEAL ;
                  request.price = NormalizeDouble( tick.ask, _Digits ) ;
                  request.symbol = _Symbol ;
                  request.volume = _lot ;
                  request.magic = 12345 ;
                  request.type = ORDER_TYPE_BUY ;
                  request.type_filling = ORDER_FILLING_FOK ;
                  request.deviation = 30 ;
                  request.sl = 0.0 ;
                  request.tp = 0.0 ;
                  takeProfit = NormalizeDouble( tick.ask + _takeProfit * Point() * 10, _Digits ) ;
                  stopLoss = NormalizeDouble( tick.ask - _stopLoss * Point() * 10, _Digits ) ;
                  OrderSend( request, result ) ;
                } 
                else
                {
                  request.action = TRADE_ACTION_DEAL ;
                  request.price = NormalizeDouble( tick.bid, _Digits ) ;
                  request.symbol = _Symbol ;
                  request.volume = _lot ;
                  request.magic = 12345 ;
                  request.type = ORDER_TYPE_SELL ;
                  request.type_filling = ORDER_FILLING_FOK ;
                  request.deviation = 30 ;
                  request.sl = 0.0 ;
                  request.tp = 0.0 ;
                  takeProfit = NormalizeDouble( tick.bid - _takeProfit * Point() * 10, _Digits ) ;
                  stopLoss = NormalizeDouble( tick.bid + _stopLoss * Point() * 10, _Digits ) ;
                  OrderSend( request, result ) ;
                }  
          }
          else
          {
               if ( _position == Buy )
               {
                  if ( lastOrder == WIN )
                  {
                     return ;
                  }
                  else
                  {
                     level *= 2 ;
                     request.action = TRADE_ACTION_DEAL ;
                     request.price = NormalizeDouble( tick.ask, _Digits ) ;
                     request.symbol = _Symbol ;
                     request.volume = _lot * level ;
                     request.magic = 12345 ;
                     request.type = ORDER_TYPE_BUY ;
                     request.type_filling = ORDER_FILLING_FOK ;
                     request.deviation = 30 ;
                     request.sl = 0.0 ;
                     request.tp = 0.0 ;
                     takeProfit = NormalizeDouble( tick.ask +  _takeProfit * Point() * 10, _Digits ) ; 
                     stopLoss = NormalizeDouble( tick.ask - _stopLoss * Point() * 10, _Digits ) ;
                     OrderSend( request, result ) ;
                  }
               }
               else
               {
                  if ( lastOrder == WIN )
                  {
                     return ;
                  }
                  else
                  {
                     level *= 2 ;
                     request.action = TRADE_ACTION_DEAL ;
                     request.price = NormalizeDouble( tick.bid, _Digits ) ;
                     request.symbol = _Symbol ;
                     request.volume = _lot * level ;
                     request.magic = 12345 ;
                     request.type = ORDER_TYPE_SELL ;
                     request.type_filling = ORDER_FILLING_FOK ;
                     request.deviation = 30 ;
                     request.sl = 0.0 ;
                     request.tp = 0.0 ;
                     takeProfit = NormalizeDouble( tick.bid - _takeProfit * Point() * 10, _Digits ) ;
                     stopLoss = NormalizeDouble( tick.bid + _stopLoss * Point() * 10, _Digits ) ;
                     OrderSend( request, result ) ;
                  }
               }
          }
      }
      else
      {
             if ( _position == Buy )
             {
               if( tick.bid >= takeProfit )
               {
                  request.action = TRADE_ACTION_DEAL ;
                  request.price = NormalizeDouble( tick.bid, _Digits ) ;
                  request.symbol = _Symbol ;
                  request.volume = _lot * level ;
                  request.magic = 12345 ;
                  request.type = ORDER_TYPE_SELL ;
                  request.type_filling = ORDER_FILLING_FOK ;
                  request.deviation = 30 ;
                  request.sl = 0.0 ;
                  request.tp = 0.0 ;
                  lastOrder = WIN ;
                  OrderSend( request, result ) ;
               }
               else if( tick.bid <= stopLoss )
               {
                  request.action = TRADE_ACTION_DEAL ;
                  request.price = NormalizeDouble( tick.bid, _Digits ) ;
                  request.symbol = _Symbol ;
                  request.volume = _lot * level ;
                  request.magic = 12345 ;
                  request.type = ORDER_TYPE_SELL ;
                  request.type_filling = ORDER_FILLING_FOK ;
                  request.deviation = 30 ;
                  request.sl = 0.0 ;
                  request.tp = 0.0 ;
                  lastOrder = LOSE ;
                  OrderSend( request, result ) ;
               }
            }
            else
            {
               if( tick.ask <= takeProfit)
               {
                  request.action = TRADE_ACTION_DEAL ;
                  request.price = NormalizeDouble( tick.ask, _Digits ) ;
                  request.symbol = _Symbol ;
                  request.volume = _lot * level ;
                  request.magic = 12345 ;
                  request.type = ORDER_TYPE_BUY ;
                  request.type_filling = ORDER_FILLING_FOK ;
                  request.deviation = 30 ;
                  request.sl = 0.0 ;
                  request.tp = 0.0 ;
                  lastOrder = WIN ;
                  OrderSend( request, result ) ;
               }
               else if ( tick.ask >= stopLoss )
               {
                  request.action = TRADE_ACTION_DEAL ;
                  request.price = NormalizeDouble( tick.ask, _Digits ) ;
                  request.symbol = _Symbol ;
                  request.volume = _lot * level ;
                  request.magic = 12345 ;
                  request.type = ORDER_TYPE_BUY ;
                  request.type_filling = ORDER_FILLING_FOK ;
                  request.deviation = 30 ;
                  request.sl = 0.0 ;
                  request.tp = 0.0 ;
                  lastOrder = LOSE ;
                  OrderSend( request, result ) ;
               }
            } 
      }  
   }



To add comments, please log in or register