Learning to code. Need help with this.

 

I Wanted this EA to to buy and close at 20 ma crossings. Only buy when above 50 ma and ignore sells. Similarly only sell trades when below 50 ma and ignore buys.

It was at least opening trades when wanted when i had only 20 ma. so it should the same with new ma. but it is doing so randomly.

Pls help.

//+------------------------------------------------------------------+
//|                                                           _1.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
// Input variables
input double TradeVolume=0.1;
input int StopLoss=200;
input int TakeProfit=1000;
input int MAPeriod1=50;
input int MAPeriod2=20;
// Global variables
bool glBuyPlaced=false, glSellPlaced=false;
// OnTick() event handler
void OnTick()
  {
// Trade structures
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
// Moving average
   double ma1[];
   double ma2[];
   ArraySetAsSeries(ma1,true);
   int maHandle1=iMA(_Symbol,0,MAPeriod1,MODE_SMA,0,PRICE_CLOSE);
   CopyBuffer(maHandle1,0,0,1,ma1);
   ArraySetAsSeries(ma2,true);
   int maHandle2=iMA(_Symbol,0,MAPeriod2,MODE_SMA,0,PRICE_CLOSE);
   CopyBuffer(maHandle2,0,0,1,ma2);
// Close price
   double close[];
   ArraySetAsSeries(close,true);
   CopyClose(_Symbol,0,0,1,close);
// Current position information
   bool openPosition = PositionSelect(_Symbol);
   long positionType = PositionGetInteger(POSITION_TYPE);
    ulong position_ticket=PositionGetTicket(0);  
   if(close[0]>ma1[0])
     {
      // Open buy market order
      if(close[0] < ma2[0])
         m_trade.PositionClose(position_ticket);
      if(close[0] > ma2[0] && glBuyPlaced == false)
        {
        if(positionType==POSITION_TYPE_SELL)
        m_trade.PositionClose(position_ticket);
         ZeroMemory(request);
         request.action = TRADE_ACTION_DEAL;
         request.type = ORDER_TYPE_BUY;
         request.symbol = _Symbol;
         request.volume = TradeVolume;
         request.type_filling = ORDER_FILLING_IOC;
         request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         request.sl = 0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         glBuyPlaced = true;
         glSellPlaced = false;
         request.position = result.order;

        }

      // Modify SL/TP
      if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
        {
         request.action = TRADE_ACTION_SLTP;
         
         double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         if(StopLoss > 0)
            request.sl = positionOpenPrice - (StopLoss * _Point);
         if(TakeProfit > 0)
            request.tp = positionOpenPrice + (TakeProfit * _Point);
         if(request.sl > 0 && request.tp > 0)
            OrderSend(request,result);

        }


     }
   if(close[0] < ma1[0])
     {
      // Open sell market order
      if(close[0] > ma2[0])
         m_trade.PositionClose(position_ticket);
      if(close[0] < ma2[0] && glSellPlaced == false)
        {
         if(positionType==POSITION_TYPE_BUY)
        m_trade.PositionClose(position_ticket);
         ZeroMemory(request);
         request.action = TRADE_ACTION_DEAL;
         request.type = ORDER_TYPE_SELL;
         request.symbol = _Symbol;
         request.volume = TradeVolume;
         request.type_filling = ORDER_FILLING_IOC;
         request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         request.sl = 0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         glBuyPlaced = false;
         glSellPlaced = true;
         request.position = result.order;

         // Modify SL/TP
         if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
           {
            request.action = TRADE_ACTION_SLTP;
            double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            if(StopLoss > 0)
               request.sl = positionOpenPrice - (StopLoss * _Point);
            if(TakeProfit > 0)
               request.tp = positionOpenPrice + (TakeProfit * _Point);
            if(request.sl > 0 && request.tp > 0)
               OrderSend(request,result);

           }
        }
     }
  }
//+------------------------------------------------------------------+

 This MA & Close handle in OnTick fn was fine in my prev prog. so it must be some logical error.

 

also pls let me know how to close my trade if i dont want to include trade class.

I was trying with  request.action=TRADE_ACTION_CLOSE_BY;

            request.type = ORDER_TYPE_CLOSE_BY;

i still dont know how to use em but earlier my trades were opening at right time and didnt close untill endoftest.

but since i started using trade class and  m_trade.PositionClose(position_ticket);    my trades have been closing and opening randomly as dice.


so pls let me know how to use 

request.action=TRADE_ACTION_CLOSE_BY;

            request.type = ORDER_TYPE_CLOSE_BY; to close an open position.

 

You are making a gross mistake: in MQL5, the work with receiving indicator data is divided into two steps. Step 1: create the HANDL of the indicator (THIS IS DONE ONLY ONCE - In OnInit !!!). Step 2: get the data (as a rule, this is done in OnTick ().


And by the way, the CTrade trading class is the easiest way. Nothing is easier! 

Correct the errors and I'll show you the lightweight code.
 
Vladimir Karputov:

You are making a gross mistake: in MQL5, the work with receiving indicator data is divided into two steps. Step 1: create the HANDL of the indicator (THIS IS DONE ONLY ONCE - In OnInit !!!). Step 2: get the data (as a rule, this is done in OnTick ().


And by the way, the CTrade trading class is the easiest way. Nothing is easier! 

Correct the errors and I'll show you the lightweight code.

Is this what you meant..???

//+------------------------------------------------------------------+
//|                                                           _1.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
// Input variables
input double TradeVolume=0.1;
input int StopLoss=200;
input int TakeProfit=1000;
input int MAPeriod1=50;
input int MAPeriod2=20;
   double ma1[];
   double ma2[];
   double close[];
   int OnInit()
  {

// Global variables

// Moving average

   
   ArraySetAsSeries(ma1,true);
   int maHandle1=iMA(_Symbol,0,MAPeriod1,MODE_SMA,0,PRICE_CLOSE);
    CopyBuffer(maHandle1,0,0,1,ma1);
   ArraySetAsSeries(ma2,true);
   int maHandle2=iMA(_Symbol,0,MAPeriod2,MODE_SMA,0,PRICE_CLOSE);
   CopyBuffer(maHandle2,0,0,1,ma2);
   // Close price;
   ArraySetAsSeries(close,true);
   CopyClose(_Symbol,0,0,1,close);
   return(INIT_SUCCEEDED);
  }
// OnTick() event handler
void OnTick()
  {
   bool glBuyPlaced=false, glSellPlaced=false;
// Trade structures
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
// Current position information
   bool openPosition = PositionSelect(_Symbol);
   long positionType = PositionGetInteger(POSITION_TYPE);
   ulong position_ticket=PositionGetTicket(0);
   if(close[0]>ma1[0])
     {
      // Open buy market order
      if(close[0] < ma2[0])
         m_trade.PositionClose(position_ticket);
      if(close[0] > ma2[0] && glBuyPlaced == false)
        {
         if(positionType==POSITION_TYPE_SELL)
            m_trade.PositionClose(position_ticket);
         ZeroMemory(request);
         request.action = TRADE_ACTION_DEAL;
         request.type = ORDER_TYPE_BUY;
         request.symbol = _Symbol;
         request.volume = TradeVolume;
         request.type_filling = ORDER_FILLING_IOC;
         request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         request.sl = 0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         glBuyPlaced = true;
         glSellPlaced = false;
         request.position = result.order;

        }

      // Modify SL/TP
      if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
        {
         request.action = TRADE_ACTION_SLTP;

         double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         if(StopLoss > 0)
            request.sl = positionOpenPrice - (StopLoss * _Point);
         if(TakeProfit > 0)
            request.tp = positionOpenPrice + (TakeProfit * _Point);
         if(request.sl > 0 && request.tp > 0)
            OrderSend(request,result);

        }


     }
   if(close[0] < ma1[0])
     {
      // Open sell market order
      if(close[0] > ma2[0])
         m_trade.PositionClose(position_ticket);
      if(close[0] < ma2[0] && glSellPlaced == false)
        {
         if(positionType==POSITION_TYPE_BUY)
            m_trade.PositionClose(position_ticket);
         ZeroMemory(request);
         request.action = TRADE_ACTION_DEAL;
         request.type = ORDER_TYPE_SELL;
         request.symbol = _Symbol;
         request.volume = TradeVolume;
         request.type_filling = ORDER_FILLING_IOC;
         request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         request.sl = 0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         glBuyPlaced = false;
         glSellPlaced = true;
         request.position = result.order;

         // Modify SL/TP
         if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
           {
            request.action = TRADE_ACTION_SLTP;
            double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            if(StopLoss > 0)
               request.sl = positionOpenPrice - (StopLoss * _Point);
            if(TakeProfit > 0)
               request.tp = positionOpenPrice + (TakeProfit * _Point);
            if(request.sl > 0 && request.tp > 0)
               OrderSend(request,result);

           }
        }
     }
  }
//+------------------------------------------------------------------+
 
Saurabh Suman: Is this what you meant..???
   int maHandle1=iMA(_Symbol,0,MAPeriod1,MODE_SMA,0,PRICE_CLOSE);
    CopyBuffer(maHandle1,0,0,1,ma1);

No. What part of "get the data in OnTick" was unclear?

 
Saurabh Suman:

Is this what you meant..???

Step 1: create the HANDL of the indicator (THIS IS DONE ONLY ONCE - In OnInit !!!).
 
Like this..???
Vladimir Karputov:
Step 1: create the HANDL of the indicator (THIS IS DONE ONLY ONCE - In OnInit !!!).
// Input variables
input double TradeVolume=0.1;
input int StopLoss=200;
input int TakeProfit=1000;
input int MAPeriod1=50;
input int MAPeriod2=20;

int OnInit()
  {
 
   int maHandle1=iMA(_Symbol,0,MAPeriod1,MODE_SMA,0,PRICE_CLOSE);

   int maHandle2=iMA(_Symbol,0,MAPeriod2,MODE_SMA,0,PRICE_CLOSE);
 
   return(INIT_SUCCEEDED);
  }
// OnTick() event handler
void OnTick()
{
double ma1[];
 ArraySetAsSeries(ma1,true);
double ma2[];
ArraySetAsSeries(ma2,true);
double close[];
ArraySetAsSeries(close,true);
   CopyBuffer(maHandle1,0,0,1,ma1);
   CopyBuffer(maHandle2,0,0,1,ma2);
  CopyClose(_Symbol,0,0,1,close);
   bool glBuyPlaced=false, glSellPlaced=false;
// Trade structures
   MqlTradeRequest request;
   MqlTradeResult result;
 
Saurabh Suman :
Like this..???

Better now.

But even better is needed: the resulting handle needs to be checked. See example: Simple EA Three iMA, read help: iMA .

How to start with MQL5
How to start with MQL5
  • 2020.08.20
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 

Ty man...

I have logical errors perhaps. I'll be back if i cant solve em'on my own.

And ur article is nc.

 
Saurabh Suman:

Ty man...

I have logical errors perhaps. I'll be back if i cant solve em'on my own.

And ur article is nc.

Please use English in the forum.

 

Please post only in English on this forum.
          Please don't write ur - it's "you are" or "your" - MQL4 programming forum 2014.03.04

Reason: