Only open one trade per currency pair.

 

I created a function that will count the number of positions for any symbol you give it. 

int PositionCounter(string SymName)
{
 int PositionCount = 0;
 
 for(int i = PositionsTotal()-1;i>=0;i--)
   {
     string CurrencyPair = PositionGetSymbol(i);
     
    if(SymName == CurrencyPair)
    {
      PositionCount = PositionCount + 1;
    }
     
     
   }
    return PositionCount;
  }

My only problem is when I attempt to call this function and store the integer value it returns to a variable and then use this varible to check if multiple positions are open for this pair it doesnt work. Would appreciate any insight:

void OnTick()
{

 double Lots = 0.01; 

 PC1 = PositionCounter("AUDCAD"); 

 double Ask1  = NormalizeDouble(SymbolInfoDouble("AUDCAD",SYMBOL_ASK),2); //Grab Ask price from chart
 double Bid1  = NormalizeDouble(SymbolInfoDouble("AUDCAD",SYMBOL_BID),2); // Grab Bid price
 string ask1  = DoubleToString(Ask1,2); //convert the double to string
 string bid1  = DoubleToString(Bid1,2); //convert the double to string

 int FileHandle           = FileOpen("results.txt",FILE_READ|FILE_SHARE_READ|FILE_TXT|FILE_ANSI,"|",CP_ACP); //open file

 string TextString        = FileReadString(FileHandle); //read file string

 int StringSearchBUY      = StringFind(TextString,"BUY"); // search the string from file for the string word "BUY"

 int StringSearchSELL     = StringFind(TextString,"SELL"); // search the string from file for the string word "SELL"

 int SSBP1   = StringFind(TextString,bid1); // search the string from file for the bid
 int SSAP1   = StringFind(TextString,ask1); // search the string from file for the ask

 FileClose(FileHandle); //close the file

if(StringSearchBUY >= 0 && (SSBP1 >=0 || SSAP1 >=0) && PC1 == 0) //if the buy string is found and the ask/bid price is found in file string then open buy position if no positions are open
{
  trade.Buy(NormalizeDouble(Lots,2),"AUDCAD",Ask1,NULL,NULL);
}   
if(StringSearchSELL >= 0 && (SSBP1 >=0 || SSAP1 >=0) && PC1 == 0) //if the sell string is found and the ask/bid price is found in file string open sell position if no positions are open
{
   trade.Sell(NormalizeDouble(Lots,2),"AUDCAD",Bid1,NULL,NULL);
}

}


int PositionCounter(string SymName)
{
 int PositionCount = 0;
 
 for(int i = PositionsTotal()-1;i>=0;i--)
   {
     string CurrencyPair = PositionGetSymbol(i);
     
    if(SymName == CurrencyPair)
    {
      PositionCount = PositionCount + 1;
    }
     
     
   }
    return PositionCount;
  }

There are no problems with opening and reading the file but I keep running into the problem of opening multiple positions when the conditions are met.

 
You have no control over the result of a trade request. You have no control over whether a position was opened or not. Therefore, you will often open several positions in a row.
 
Vladimir Karputov:
You have no control over the result of a trade request. You have no control over whether a position was opened or not. Therefore, you will often open several positions in a row.
The "&&PC1 == 0" condition isn't enough? because as soon as the position is open PC1 will no longer equal 0
 
Marquese Singfield :
The "&&PC1 == 0" condition isn't enough? because as soon as the position is open PC1 will no longer equal 0

This is not enough. As I say, you do not control the execution of the position at all.

You have sent a trade order - until the trade order is executed, you can enter Ontick several times and issue several more orders. You need to work according to the algorithm:

  • issue a trade order
  • stop issuing trade orders until you receive confirmation in OnTradeTransaction
 
Vladimir Karputov:

This is not enough. As I say, you do not control the execution of the position at all.

You have sent a trade order - until the trade order is executed, you can enter Ontick several times and issue several more orders. You need to work according to the algorithm:

  • issue a trade order
  • stop issuing trade orders until you receive confirmation in OnTradeTransaction
Thank you!

I never considered this type of handling! As I am not very familiar with OnTradeTransaction.

Could you please tell me How would I check for confirmation?
 
  1. Marquese Singfield: As I am not very familiar with OnTradeTransaction. Could you please tell me How would I check for confirmation?

    Perhaps you should read the manual. MQL5 ReferenceMQL5 programsProgram RunningOnTrade
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  2. Marquese Singfield: I never considered this type of handling! 

    You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

 
William Roeder:
  1. Perhaps you should read the manual. MQL5 ReferenceMQL5 programsProgram RunningOnTrade
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  2. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

My apologies for the laziness, thank you for your time.
 
Vladimir Karputov:

This is not enough. As I say, you do not control the execution of the position at all.

You have sent a trade order - until the trade order is executed, you can enter Ontick several times and issue several more orders. You need to work according to the algorithm:

  • issue a trade order
  • stop issuing trade orders until you receive confirmation in OnTradeTransaction

I realized recently that this actually doesn't relate to the goal I am trying to achieve. "stop issuing trade order until....." The goal is that there will be no more trade orders after the first. the issue is not that I want to wait for confirmation of execution of the order. I do not want to place more than one order......period...confirmation or not I only want to send one request and never another for the same pair. Even though I am entering OnTick during the order being sent, I should not place another request ever again after the first is sent. And unfortunately after hours of reviewing how OnTradeTransaction is working it is only analyzing  the process of trade transactions. I can not use this to stop transactions from taking place all together. Unless I am missing something?

Can I somehow implement a waiting period between the first request till confirmation and then call my positionCounter function after this?

 
Marquese Singfield :

I realized recently that this actually doesn't relate to the goal I am trying to achieve. "stop issuing trade order until ....." The goal is that there will be no more trade orders after the first.  the issue is not that I want to wait for confirmation of execution of the order. I do not want to place more than one order......period...confirmation or not I only want to send one request and never another for the same pair. Even though I am entering OnTick during the order being sent, I should not place another request ever again after the first is sent. And unfortunately after hours of reviewing how OnTradeTransaction is working it is only analyzing  the process of trade transactions. I can not use this to stop transactions from taking place all together. Unless I am missing something?

Can I somehow implement a waiting period between the first request till confirmation and then call my positionCounter function after this?

Example of a blank: 

We catch the transaction

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

Example of a blank: 

We catch the transaction

Thank you for your help. Actually checking if OrdersTotal() == 0; seemed to do the trick.
 
Vladimir Karputov:

This is not enough. As I say, you do not control the execution of the position at all.

You have sent a trade order - until the trade order is executed, you can enter Ontick several times and issue several more orders. You need to work according to the algorithm:

  • issue a trade order
  • stop issuing trade orders until you receive confirmation in OnTradeTransaction
I am currently confused by this statement after running into the issue again. I do not understand what is meant by we "enter the OnTick several time and issue several more orders". to illustrate  my confusion I hard coded the variables to change after the first order send. "PC1" is now a global variable .
int PC1 = 0;

void OnTick()
{

 double Lots = 0.01; 

  double Ask1  = NormalizeDouble(SymbolInfoDouble("AUDCAD",SYMBOL_ASK),2); //Grab Ask price from chart
 double Bid1  = NormalizeDouble(SymbolInfoDouble("AUDCAD",SYMBOL_BID),2); // Grab Bid price
 string ask1  = DoubleToString(Ask1,2); //convert the double to string
 string bid1  = DoubleToString(Bid1,2); //convert the double to string

 int FileHandle           = FileOpen("results.txt",FILE_READ|FILE_SHARE_READ|FILE_TXT|FILE_ANSI,"|",CP_ACP); //open file

 string TextString        = FileReadString(FileHandle); //read file string

 int StringSearchBUY      = StringFind(TextString,"BUY"); // search the string from file for the string word "BUY"

 int StringSearchSELL     = StringFind(TextString,"SELL"); // search the string from file for the string word "SELL"

 int SSBP1   = StringFind(TextString,bid1); // search the string from file for the bid
 int SSAP1   = StringFind(TextString,ask1); // search the string from file for the ask

 FileClose(FileHandle); //close the file

if(StringSearchBUY >= 0 && (SSBP1 >=0 || SSAP1 >=0) && PC1 == 0) //if the buy string is found and the ask/bid price is found in file string then open buy position if no positions are open
{
  trade.Buy(NormalizeDouble(Lots,2),"AUDCAD",Ask1,NULL,NULL);
  PC1 = 1;
}   
if(StringSearchSELL >= 0 && (SSBP1 >=0 || SSAP1 >=0) && PC1 == 0) //if the sell string is found and the ask/bid price is found in file string open sell position if no positions are open
{
   trade.Sell(NormalizeDouble(Lots,2),"AUDCAD",Bid1,NULL,NULL);
   PC1 = 1;
}

}



But still multiple positions are being opened when the conditions are met. This makes no sense to me if the global variable is set to 1 automatically after the first pass in OnTick() [execution from top to bottom of code in OnTick()]. Any thoughts @William Roeder?