Learning to code. Need help with this. - page 2

 
William Roeder:

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

It is english.

Ty means thank you

em'on means them on

ur means your.


It is day2day abbreviations.


On the way to moderator..????? keep going.

 
Saurabh Suman :

It is english.

Ty means thank you

em'on means them on

ur means your.


It is day2day abbreviations.


On the way to moderator..????? keep going.

You are at an international forum. Don't use dubious slang. Personally, I find it difficult to understand what you are writing.

 
Can you explain me why this simple program is showing a lot of noise signals..???
//+------------------------------------------------------------------+
//|                                                           _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"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
// Input variables
input double TradeVolume=0.1;
input int StopLoss=100;
input int TakeProfit=1000;
input int MAPeriod=10;
// Global variables
bool glBuyPlaced, glSellPlaced;
double ma[];
int maHandle;
MqlTradeRequest brequest;
MqlTradeRequest srequest;
// OnTick() event handler
void OnInit()
  {
   maHandle=iMA(_Symbol,0,MAPeriod,MODE_SMA,0,PRICE_TYPICAL);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   MqlTradeRequest request;
   MqlTradeResult result;
   MqlRates rates[];
   ZeroMemory(request);
// Moving average
   ArraySetAsSeries(ma,true);
   CopyBuffer(maHandle,0,0,1,ma);
// Close price
   ArraySetAsSeries(rates,true);
   CopyRates(_Symbol,PERIOD_M15,0,1,rates);
// Current position information
   bool openPosition = PositionSelect(_Symbol);
   long positionType = PositionGetInteger(POSITION_TYPE);
// Open buy market order
   if(rates[0].close > ma[0])
     {
      m_trade.PositionClose(srequest.position);
      if(PositionSelectByTicket(brequest.position)==false)
        {
         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);
         brequest.position=result.order;
         // Modify SL/TP
         if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
           {
            request.action = TRADE_ACTION_SLTP;
            request.position = result.order;
            do
               Sleep(100);
            while(PositionSelect(_Symbol) == false);
            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);
            glBuyPlaced = true;
            glSellPlaced = false;
           }
        }
     }
// Open sell market order
   if(rates[0].close < ma[0])
     {
      m_trade.PositionClose(brequest.position);
      if(PositionSelectByTicket(srequest.position)==false)
        {
         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_BID);
         request.sl = 0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         srequest.position=result.order;
         // Modify SL/TP
         if((result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
            && (StopLoss > 0 || TakeProfit > 0))
           {
            request.action = TRADE_ACTION_SLTP;
            request.position = result.order;
            do
               Sleep(100);
            while(PositionSelect(_Symbol) == false);
            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);
            glBuyPlaced = false;
            glSellPlaced = true;
           }
        }
     }
  }
have a look at these screenshots. Even if i am working with close prices,  these trades close before close of the bar. that too multiple times. On 1st june it has opened aroud 100 trades before 3:30. Whereas the close price has crossed the MA lesser than 10 times. So this thing is confusing me.Pls help.
Files:
1.png  56 kb
2.png  80 kb
3.PNG  138 kb
 

saurabhsuman003: It is english. It is day2day abbreviations.

On the way to moderator..????? keep going.

  1. Not it is not. Write English.
  2. I have warned you and linked to why, Keith (a moderator) has warned you. Keep going and enjoy your ban.
 
William Roeder:
  1. Not it is not. Write English.
  2. I have warned you and linked to why, Keith (a moderator) has warned you. Keep going and enjoy your ban.

Chill man.

i was kidding. No grudge on this side.

saurabhsuman003:
Can you explain me why this simple program is showing a lot of noise signals..??? have a look at these screenshots. Even if i am working with close prices,  these trades close before close of the bar. that too multiple times. On 1st june it has opened aroud 100 trades before 3:30. Whereas the close price has crossed the MA lesser than 10 times. So this thing is confusing me.Pls help.
Can you help me with this...???
 

One deal per bar

//+------------------------------------------------------------------+
//|                                             One deal per bar.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input ulong    InpDeviation         = 10;          // Deviation, in points (1.00045-1.00055=10 points)
input bool     InpPrintLog          = false;       // Print log
input ulong    InpMagic             = 300;         // Magic number
//---
datetime       m_last_deal_time;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
   RefreshRates();
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(InpDeviation);
//---
   iTime(m_symbol.Name(),Period(),0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   datetime time=iTime(m_symbol.Name(),Period(),0);
   if(time!=m_last_deal_time)
      m_trade.Buy(m_symbol.LotsMin(),m_symbol.Name());
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;
      if(deal_symbol==m_symbol.Name() && deal_magic==InpMagic)
         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
            if(deal_entry==DEAL_ENTRY_IN)
               m_last_deal_time=iTime(m_symbol.Name(),Period(),0);
     }
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
      return(false);
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+
Files:
 
saurabhsuman003:

Chill man.

i was kidding. No grudge on this side.

Can you help me with this...???

Since you're learning, variables that are prefixed with "m_" have a very special meaning. It's a naming convention to indicate that a variable is defined as a class member. You should only use it for class members, and if you don't know what that is yet then that's okay but just know that it is one of the worst mistakes you could possibly make when naming global variables. The reasoning is as soon as you introduce classes into your code you're going to accidentally shadow the (wrongly named) global variables, and trust me that will be a very bad day for you if real money is on the line. Instead use the conventional "g_" prefix when naming your global variables. "g_name" for globals; "m_name" for class members... get it? :) 

 
nicholish en:

Since you're learning, variables that are prefixed with "m_" have a very special meaning. It's a naming convention to indicate that a variable is defined as a class member. You should only use it for class members, and if you don't know what that is yet then that's okay but just know that it is one of the worst mistakes you could possibly make when naming global variables. The reasoning is as soon as you introduce classes into your code you're going to accidentally shadow the (wrongly named) global variables, and trust me that will be a very bad day for you if real money is on the line. Instead use the conventional "g_" prefix when naming your global variables. "g_name" for globals; "m_name" for class members... get it? :) 

Vladimir Karputov:

One deal per bar

Thanks man.

I need to learn a lot more.

Now i see my silly mistakes.

I'll give a break to running after codes and return to studying basics.   Thanks a lot.