About MQL5, how to close a trade and enter a trade

About MQL5, how to close a trade and enter a trade

Manage alerts

Loading saved threads...

Raphael Tunji · External communityPost link
External question — Stack Overflow Stack Exchange Author: Raphael Tunji Original post: https://stackoverflow.com/questions/55133022 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. In the code below, I want to close all open buy trade before opening a sell in the place where I put plus sign. I want to close all open sell trade before opening a buy in the place where I put asterisk sign. Please, can anyone help me with the code? Thank you in advance for your help #include <Trade\Trade.mqh> CTrade trade; input double MyLotSize = 0.01; void OnTick() { double myMovingAverageArray1[],myMovingAverageArray2[]; int movingAverageDefinition1 = iMA (_Symbol,_Period, 20,0,MODE_EMA,PRICE_CLOSE); int movingAverageDefinition2 = iMA (_Symbol,_Period, 50,0,MODE_EMA,PRICE_CLOSE); ArraySetAsSeries(myMovingAverageArray1,true); ArraySetAsSeries(myMovingAverageArray2,true); CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverageArray1); CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverageArray2); if((myMovingAverageArray1[0]>myMovingAverageArray2[0])&&(myMovingAverageArray1[1]<myMovingAverageArray2[1])) { double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); Comment ("BUY"); *********************************************************** trade.Buy(MyLotSize,NULL,Ask,0,(Ask+60 * _Point),NULL); } if((myMovingAverageArray1[0]<myMovingAverageArray2[0])&&(myMovingAverageArray1[1]>myMovingAverageArray2[1])) { double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); Comment ("SELL"); +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ trade.Sell(MyLotSize,NULL,Bid,0,(Bid-60 * _Point),NULL); } }
Quote
Report
Daniel Kniaz · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Daniel Kniaz Original post: https://stackoverflow.com/a/55138053 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Try the following - void TryToClose(long type){ if(!PositionSelect(_Symbol))return; if(PositionGetInteger(POSITION_TYPE)!=type)return; trade.PositionClose(_Symbol); } add `TryToClose(POSITION_TYPE_BUY)` before opening a sell order.
Quote
Report
Ani · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Ani Original post: https://stackoverflow.com/a/76516626 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. using System; using System.IO; using System.Security.Cryptography; class Program { static void Main() { // Constants for Key and IV byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }; byte[] iv = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; // Path to the PDF file string filePath = "path/to/your/file.pdf"; // Encrypt the PDF EncryptFile(filePath, key, iv); // Decrypt the encrypted PDF string encryptedFilePath = "path/to/your/encrypted/file.pdf"; DecryptFile(encryptedFilePath, key, iv); Console.WriteLine("Encryption and decryption completed successfully!"); } static void EncryptFile(string filePath, byte[] key, byte[] iv) { using (Aes aesAlg = Aes.Create()) { aesAlg.Key = key; aesAlg.IV = iv; // Create a new file to write the encrypted data using (FileStream encryptedFile = File.Create("path/to/your/encrypted/file.pdf")) { // Create an encryptor to perform the stream transformation ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create a CryptoStream to write the encrypted data to the file using (CryptoStream cryptoStream = new CryptoStream(encryptedFile, encryptor, CryptoStreamMode.Write)) { // Read the contents of the PDF file byte[] fileBytes = File.ReadAllBytes(filePath); // Write the encrypted data to the CryptoStream cryptoStream.Write(fileBytes, 0, fileBytes.Length); } } } } static void DecryptFile(string filePath, byte[] key, byte[] iv) { using (Aes aesAlg = Aes.Create()) { aesAlg.Key = key; aesAlg.IV = iv; // Create a new file to write the decrypted data using (FileStream decryptedFile = File.Create("path/to/your/decrypted/file.pdf")) { // Create a decryptor to perform the stream transformation ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create a CryptoStream to read the encrypted data from the file using (CryptoStream cryptoStream = new CryptoStream(decryptedFile, decryptor, CryptoStreamMode.Write)) { // Read the contents of the encrypted file byte[] fileBytes = File.ReadAllBytes(filePath); // Write the decrypted data to the CryptoStream cryptoStream.Write(fileBytes, 0, fileBytes.Length); } } } } }
Quote
Report
emmanuel · External communityPost link
External answer — Stack Overflow Stack Exchange Author: emmanuel Original post: https://stackoverflow.com/a/76854247 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. You can also use the following: if(PositionsTotal() < PositionsTotals) . PositionsTotal() is a built in mql5 function, and PositionsTotals is a variable you can create and change, so it can be 1, etc. By using this logic, you can only have 1 trade open at a time.
Quote
Report
Shubham Wankhede · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Shubham Wankhede Original post: https://stackoverflow.com/a/79414613 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. implicit conversion from 'number' to 'string' XTORE SOFTWAEA STRATEGY.mq5 50 28 implicit conversion from 'number' to 'string' XTORE SOFTWAEA STRATEGY.mq5 82 28 implicit conversion from 'number' to 'string' XTORE SOFTWAEA STRATEGY.mq5 96 28 implicit conversion from 'number' to 'string' XTORE SOFTWAEA STRATEGY.mq5 106 28
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Raphael Tunji Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/55133022 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. In the code below, I want to close all open buy trade before opening a sell in the place where I put plus sign. I want to close all open sell trade before opening a buy in the place where I put asterisk sign. Please, can anyone help me with the code? Thank you in advance for your help #include <Trade\Trade.mqh> CTrade trade; input double MyLotSize = 0.01; void OnTick() { double myMovingAverageArray1[],myMovingAverageArray2[]; int movingAverageDefinition1 = iMA (_Symbol,_Period, 20,0,MODE_EMA,PRICE_CLOSE); int movingAverageDefinition2 = iMA (_Symbol,_Period, 50,0,MODE_EMA,PRICE_CLOSE); ArraySetAsSeries(myMovingAverageArray1,true); ArraySetAsSeries(myMovingAverageArray2,true); CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverageArray1); CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverageArray2); if((myMovingAverageArray1[0]>myMovingAverageArray2[0])&&(myMovingAverageArray1[1]<myMovingAverageArray2[1])) { double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); Comment ("BUY"); *********************************************************** trade.Buy(MyLotSize,NULL,Ask,0,(Ask+60 * _Point),NULL); } if((myMovingAverageArray1[0]<myMovingAverageArray2[0])&&(myMovingAverageArray1[1]>myMovingAverageArray2[1])) { double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); Comment ("SELL"); +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ trade.Sell(MyLotSize,NULL,Bid,0,(Bid-60 * _Point),NULL); } }

Cancel quote

Checking account access…