Please insert the code correctly (use the button ) - the first time I corrected your text and applied the button .
An example of an Expert Advisor (this is an Expert, not a Script !!!). The EA checks the positions for all symbols and for all Magic Numbers.
//+------------------------------------------------------------------+ //| Close Profitable Position.mq5 | //| Copyright © 2020, Vladimir Karputov | //+------------------------------------------------------------------+ #property copyright "Copyright © 2020, Vladimir Karputov" #property version "1.000" /* barabashkakvn Trading engine 3.143 */ #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> //--- CPositionInfo m_position; // object of CPositionInfo class CTrade m_trade; // object of CTrade class //--- input parameters input double InpProfitInMoney = 9; // Profit in money //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties { double profit=m_position.Commission()+m_position.Swap()+m_position.Profit(); if(profit>=InpProfitInMoney) m_trade.PositionClose(m_position.Ticket()); } } //+------------------------------------------------------------------+
Files:
Vladimir Karputov:
Please insert the code correctly (use the button ) - the first time I corrected your text and applied the button .
An example of an Expert Advisor (this is an Expert, not a Script !!!). The EA checks the positions for all symbols and for all Magic Numbers.
i had no idea that button was even there! many thanks for your help, much appreciated!
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi, i am trying to create a script which would close a position when a specified profit is met. so far i have had zero luck at all.
Currently i am using the below code to test out. I can compile it fine with 1 warning, but it doesn't work.... ( i understand that if successful this would close all open positions which is fine for now)
Can anyone please point me in the right direction??
i am very new to this, and am slowly going through an online c++ tutorial to try and learn things, and have been through the help reference but to my clumsy way of thinking it is very obtuse and difficult to use.
the code below is basically cobbled together with parts from ( *** ) tutorials, and what little i have understood from the c++ i have gone through so far.
Many thanks to anyone that can help!