Raphael Tunji
Raphael Tunji

Reputation: 138

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

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);    
   }
}

Upvotes: 0

Views: 1276

Answers (4)

Shubham Wankhede
Shubham Wankhede

Reputation: 1

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

Upvotes: 0

emmanuel
emmanuel

Reputation: 1

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.

Upvotes: 0

Ani
Ani

Reputation: 1

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);
                }
            }
        }
    }
}

Upvotes: 0

Daniel Kniaz
Daniel Kniaz

Reputation: 4691

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.

Upvotes: 0

Related Questions