Hello, I need help

 

Hello, I need help in this code below, I want this expert advisor to sell if the last trade was of buy and buy if the last trade was of sale.

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

void OnTick()
  {
      double ask, bid, last;
      
      ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      last = SymbolInfoDouble(_Symbol, SYMBOL_LAST);
      
    
      if(PositionsTotal()==0)
         {
            Comment("Compra");
            trade.Buy(1, _Symbol, ask, ask-1, ask+1, "");
         }
      else if(PositionsTotal()==0)
         {
            Comment("Venda");
            trade.Sell(1, _Symbol, bid, bid+1, bid-1, ""); 
         }
   
  }
 

What is (in your understanding) a "transaction"? It:

  1. deal ENTER THE MARKET
  2. deal EXIT THE MARKET
?
 
Vladimir Karputov:

What is (in your understanding) a "transaction"? It:

  1. deal ENTER THE MARKET
  2. deal EXIT THE MARKET
?
yes.
 
cmagule :
yes.

What "yes"? Give an exact answer - there are TWO options in my question.

 
You are referring to a "trade", which is not exact enough for your question.

A buy trade goes as follows:
Place order, get filled with one or more buy deals.
This results in a position.

This position is closed by sell deals, eventually until it is liquidated.

Same goes for a short sell trade. Only vice versa.

So what are you referring to?

The position or the deals?


 
Dominik Egert:
You are referring to a "trade", which is not exact enough for your question.

A buy trade goes as follows:
Place order, get filled with one or more buy deals.
This results in a position.

This position is closed by sell deals, eventually until it is liquidated.

Same goes for a short sell trade. Only vice versa.

So what are you referring to?

The position or the deals?


i am referring a position.
 
cmagule:
i am referring a position.

Example:

ATTENTION: Stop Loss and Take Profit prices are NOT NORMALIZED!!!

//+------------------------------------------------------------------+
//|                                    Reverse the last position.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
//---
ENUM_POSITION_TYPE   m_last_positions  = WRONG_VALUE; // last position type
//--- input parameters
input int      Input1=9;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- forced initialization of variables
   m_last_positions  = WRONG_VALUE; // last position type
//--- initialize the generator of random numbers
   MathSrand(GetTickCount());
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlTick tick;
   if(!SymbolInfoTick(Symbol(),tick))
      return;
//--- check if a position is present and display the time of its changing
   if(!PositionSelect(Symbol()))
     {
      if(m_last_positions==WRONG_VALUE)
        {
         int math_rand=MathRand();
         if(math_rand<32767/2)
           {
            Comment("Start BUY ...");
            m_trade.Buy(1,Symbol(),tick.ask,tick.ask-1.0,tick.ask+1.0, "");
           }
         else
           {
            Comment("Start SELL ...");
            m_trade.Sell(1,Symbol(),tick.bid,tick.bid+1.0,tick.bid-1.0, "");
           }
        }
      else
        {
         if(m_last_positions==POSITION_TYPE_BUY)
           {
           
           }
         else
           {
           
           }
        }
     }
   else
     {
      //--- receive position type
      m_last_positions=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
     }
  }
//+------------------------------------------------------------------+
 
ladimir Karputov:




thank you sir, is exactly what i was looking for.

 

Good afternoon, what happened to my account <...> with an initial investment of <...>?

Because it is indicated as "NOT VALID" until 1.6.2021 it was visible!

You can answer me on my email <...> as not knowing English I can use an online translator from Google.

I am worried and do not understand what happened to the sums paid.

I look forward to your verification and a reply email.

I speak only Italian....

Thank you. <...>


 
Get in touch with your broker.

Community cannot help you with your issue.