Requests & Ideas (MQL5 only!) - page 7

ITM7
73
ITM7  
Vladimir Karputov:

Crossing of two iMA 1.007


The option of lot selection is included: manual lot or percentage of risk


Thanks, that option is working perfectly fine
ef91
51
ef91  
Vladimir Karputov:

Terms and Conditions:

  1. You describe (you provide) the idea.
  2. I place an open MQL5 code in this branch.
  3. I place this code in a CodeBase.  
  4. I specify the author of the idea and the author of a MQL5 code in a code.
  5. The coding services are provided "as is" and you use them at your own risk.


Hi everyone,

this is a brilliant initiative to share ideas, and I'm sure it'll be very helpful to anyone like me who is trying to put together his/her first EA.


The strategy that I'd like (and I'm trying) to translate into code is very simple:

1. Open a trade on a random position (Long or Short);

2. After 60 seconds check if this trade has reached TP or SL:

      - if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);

      - if it didn't: let it run for 60 more seconds and perform the same check;


And so on. Keep it running like this. Once a trade is open, again point 2. . And again and again.

No index or any chart to take into account. I'd like to try it as they ask me to do it (I'm not asking myself if this is a good strategy, I'm just trying to code it).


Below I'm posting the code that I've been able to write so far.

Thank you to anyone who will take part in this.

E.

//+------------------------------------------------------------------+
//|                                               cheduecoglioni.mq5 |
//|                                                   Gianni Esperti |
//|                                  https://www.gianniesperti.come? |
//+------------------------------------------------------------------+
#property copyright "Gianni Esperti"
#property link      "https://www.gianniesperti.come?"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

#include <Trade\Trade.mqh>
CTrade trade;

uint s;
int r;

int OnInit()
   {
   
      MathSrand( GetTickCount() );
      
   r = MathRand();
   EventSetTimer(60);


   double Ask = NormalizeDouble ( SymbolInfoDouble (_Symbol, SYMBOL_ASK), _Digits );
   
      if (r <= 16383)
            {

               Print(r, " bitches in my Lex");
               trade.Buy (0.01, NULL, Ask, 0, ( Ask + 100 * _Point ), NULL);
               
            }
            else
            {
            
               Print(r, " of my beats in yo stereo");
               trade.Sell (0.01, NULL, Ask, 0, ( Ask - 100 * _Point ), NULL);
               
            }   
    
   return (INIT_SUCCEEDED);
   }
   
void OnTimer()
   {



   }
Fafar.forex
26
Fafar.forex  
Hi Mr. Vladimir,

I have two idea to make an EA on Bollinger bands with specific parameters and limit the open positions at the same time for this EA, 

It only use Bollinger bands, with parameters period 26, deviation 4.5, and shift 0,
I want it to search for the price which touches upper band and where touches lower band. 
Buy for lower band touch and sell for upper band touch. 

But it should work on every tick and max open positions should be 5 , 

Tnx :)
Vladimir Karputov
Moderator
239862
Vladimir Karputov  
ef91:


...

2. After 60 seconds check if this trade has reached TP or SL:

      - if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);

...


I do not quite understand: we do not care what worked - TP or Sl?


Added: if TP or SL works, it means that the position has already closed - that is, the position is no longer there.

Vladimir Karputov
Moderator
239862
Vladimir Karputov  

Capturing trading events is very convenient in function OnTradeTransaction

In OnTradeTransaction, we can determine the deal direction (ENUM_DEAL_ENTRY - we will only be interested in DEAL_ENTRY_OUT) and the deal type (ENUM_DEAL_TYPE - we will only be interested in DEAL_TYPE_BUY and DEAL_TYPE_SELL).

Vladimir Karputov
Moderator
239862
Vladimir Karputov  
ef91:


Hi everyone,

this is a brilliant initiative to share ideas, and I'm sure it'll be very helpful to anyone like me who is trying to put together his/her first EA.


The strategy that I'd like (and I'm trying) to translate into code is very simple:

1. Open a trade on a random position (Long or Short);

2. After 60 seconds check if this trade has reached TP or SL:

      - if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);

      - if it didn't: let it run for 60 more seconds and perform the same check;


And so on. Keep it running like this. Once a trade is open, again point 2. . And again and again.

No index or any chart to take into account. I'd like to try it as they ask me to do it (I'm not asking myself if this is a good strategy, I'm just trying to code it).


Below I'm posting the code that I've been able to write so far.

Thank you to anyone who will take part in this.

E.


Here is the preliminary code:

//+------------------------------------------------------------------+
//|                                               cheduecoglioni.mq5 |
//|                                                   Gianni Esperti |
//|                                  https://www.gianniesperti.come? |
//+------------------------------------------------------------------+
#property copyright "Gianni Esperti"
#property link      "https://www.gianniesperti.come?"
#property version   "1.000"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
//---
ulong                m_magic=15489;                // magic number
ENUM_POSITION_TYPE   m_close_pos_type=POSITION_TYPE_BUY;
datetime             m_time_last_trade=0;
bool                 m_is_trade=true;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   m_trade.SetExpertMagicNumber(m_magic);
   m_close_pos_type=POSITION_TYPE_BUY;
   m_time_last_trade=0;
   m_is_trade=true;
//---
   return (INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(TimeCurrent()-m_time_last_trade>60 && m_is_trade)
     {
      MqlTick last_tick;
      if(!SymbolInfoTick(Symbol(),last_tick))
         return;
      if(last_tick.ask==0.0 || last_tick.bid==0.0)
         return;
      Print(__FUNCTION__", TimeCurrent()-m_time_last_trade=",(long)(TimeCurrent()-m_time_last_trade));

      if(m_close_pos_type==POSITION_TYPE_SELL)
        {
         Print("Open Buy");
         m_trade.Buy(0.01,NULL,last_tick.ask,
                     last_tick.bid-100*Point(),
                     last_tick.bid+100*Point());
        }
      else if(m_close_pos_type==POSITION_TYPE_BUY)
        {
         Print("Open Sell");
         m_trade.Sell(0.01,NULL,last_tick.bid,
                      last_tick.ask+100*Point(),
                      last_tick.ask-100*Point());
        }

     }
  }
//+------------------------------------------------------------------+
//| 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_entry        =0;
      long     deal_type         =0;
      string   deal_symbol       ="";
      long     deal_magic        =0;
      long     deal_time         =0;
      if(HistoryDealSelect(trans.deal))
        {
         deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_type=HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_time=HistoryDealGetInteger(trans.deal,DEAL_TIME);
        }
      else
         return;
      if(deal_symbol==Symbol() && deal_magic==m_magic)
        {
         if(deal_entry==DEAL_ENTRY_OUT)
           {
            if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
              {
               m_close_pos_type=(deal_type==DEAL_TYPE_BUY)?POSITION_TYPE_SELL:POSITION_TYPE_BUY;
               m_time_last_trade=(datetime)deal_time;
               m_is_trade=true;
               Print(__FUNCTION__", DEAL_ENTRY_OUT, m_time_last_trade=",TimeToString(m_time_last_trade));
              }
           }
         else if(deal_entry==DEAL_ENTRY_IN)
           {
            m_time_last_trade=(datetime)deal_time/*TimeCurrent()*/;
            m_is_trade=false;
           }
        }
     }
  }
//+------------------------------------------------------------------+


Files:
ef91
51
ef91  
Vladimir Karputov:


I do not quite understand: we do not care what worked - TP or Sl?


Added: if TP or SL works, it means that the position has already closed - that is, the position is no longer there.


Exactly, either way (TP or SL) just close it and open a new one.

But if you tell me that once I set TP or SL, the buy function automatically closes it, it should open straight away the second trade: same value opposite direction.


Does it make sense? Sorry for not knowing these basic rules, but I'm new to all of this.


Thank you for helping out

Vladimir Karputov
Moderator
239862
Vladimir Karputov  
ef91:


Exactly, either way (TP or SL) just close it and open a new one.

But if you tell me that once I set TP or SL, the buy function automatically closes it, it should open straight away the second trade: same value opposite direction.


Does it make sense? Sorry for not knowing these basic rules, but I'm new to all of this.


Thank you for helping out


Then I remove the time interval check (60 seconds)?
ef91
51
ef91  
Vladimir Karputov:

Then I remove the time interval check (60 seconds)?


Actually yes. Cause at the 60 sec mark (if it didn't reach tp or sl) it'd keep it alive anyway.


The first version of the code includes the 60 sec check, right?

Vladimir Karputov
Moderator
239862
Vladimir Karputov  
ef91:


Actually yes. Cause at the 60 sec mark (if it didn't reach tp or sl) it'd keep it alive anyway.


The first version of the code includes the 60 sec check, right?


Yes, in post  code includes the 60 sec check.