MQL5 CTrade Buy() function
MQL5 CTrade Buy() function
Loading saved threads...
user3425561 · External communityPost link
External question — Stack Overflow Stack Exchange
Author: user3425561
Original post: https://stackoverflow.com/questions/47203175
License: CC BY-SA 3.0 — https://creativecommons.org/licenses/by-sa/3.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am new to MQL5. I used to develop a lot in MQL4 and now I am trying to get my code work in MQL5 too. For some reason it seems I can't get very simple things working like opening a Buy position. I am using something like this:
CTrade m_trade
if ( PositionsTotal() == 0 )
m_trade.Buy( 0.1, Symbol(), price, 0, 0 );
All I want is to open a Buy position with a lot size of
0.1
.
In the StrategyTester, it first opens a position with a lot size
0.1
(so far so good), but then after a while the position gets closed (don't know yet why its getting closed) and then a new Buy position gets opened with a lot size as huge as it can possibly be.
What am I doing wrong?
Quote
Report
user3666197 · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: user3666197
Original post: https://stackoverflow.com/a/47208015
License: CC BY-SA 3.0 — https://creativecommons.org/licenses/by-sa/3.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
First:
Before going deeper, let's first also diagnose the situation a step better:
#include<Trade\Trade.mqh>
...
CTrade m_trade;
...
if ( PositionsTotal() == 0 )
{
if ( !trade.Buy( 0.1, _Symbol ) )
{
//--- failure message
Print( "The .Buy() method failed. The return-code = ",
trade.ResultRetcode(),
". Code description: ",
trade.ResultRetcodeDescription()
);
}
else
{
Print( "The .Buy() method executed successfully. The return-code = ",
trade.ResultRetcode(),
" (",
trade.ResultRetcodeDescription(),
")"
);
}
}
Next:
If the
Print()
-ed output above confirms the operation was successful, you may add more details to the
.Buy()
method and re-test:
#include<Trade\Trade.mqh>
...
CTrade m_trade;
...
double aVolume = 0.1;
double aPoint = SymbolInfoDouble( _Symbol, SYMBOL_POINT );
double anAsk = SymbolInfoDouble( _Symbol, SYMBOL_ASK );
double aBid = SymbolInfoDouble( _Symbol, SYMBOL_BID );
int aNumDigits = (int) SymbolInfoInteger( _Symbol, SYMBOL_DIGITS );
double aLongOpen = SymbolInfoDouble( _Symbol, SYMBOL_ASK ),
aLongSL = aBid - 1000 * aPoint,
SL = NormalizeDouble( aLongSL, aNumDigits ),
aLongTP = aBid + 1000 * aPoint,
TP = NormalizeDouble( aLongTP, aNumDigits );
if ( !trade.Buy( aVolume, _Symbol, aLongOpen, SL, TP, "Does this work?" ) )
{ ... }
For going Short,
aShortOpen
,
anAsk
-based {
TP
,
SL
}-prices and symmetric edit changes apply.
Quote
Report
amirfg · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: amirfg
Original post: https://stackoverflow.com/a/69131419
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Just use "CTrade.PositionOpen" instead.
Sample:
Ctrade ctrade;
double dAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
bool bRet = ctrade.PositionOpen(_Symbol, ORDER_TYPE_BUY, 0.1, dAsk, 0, 0, "");
Quote
Report
HackerGprat · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: HackerGprat
Original post: https://stackoverflow.com/a/73996735
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I Simply Use Custom function To Execute Buy And Sell,
buy();
sell();
it will buy / sell 0.01 lot by default, with stop loss 250 & take profit 115 points,
below is the whole custom function
#include<Trade\Trade.mqh>
CTrade trade;
void buy(double lot = 0.01, double take_profits = 115, double stop_loss = 250){
double ask_sell = NormalizeDouble( SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits );
double sl = ( ask_sell - stop_loss* _Point );
double tp = (ask_sell + take_profits* _Point);
trade.Buy( lot, NULL, ask_sell, sl, tp , NULL );
} // buy
void sell(double lot = 0.01, double take_profits = 115, double stop_loss = 250){
double bid_buy = NormalizeDouble( SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits );
double sl = ( bid_buy + stop_loss* _Point );
double tp = ( bid_buy - take_profits* _Point );
trade.Sell( lot, NULL, bid_buy, sl, tp, NULL );
}
How To Use
step 1: Create filename.mqh
step 2: in you next EA or Script , just import it like step 3
step 3: #include "..\foldername\fileName.mqh"
step 4: buy(); -> to buy 0.01 step 5: buy(0.08); -> to buy 0.08 lots.
Same goes for sell(); just 2 line in every ea or Script ,
import mqh file, which containing the code. to buy and sell
just type
buy()
; or
sell();
or with custom lot ,
buy(0.5);
or
sell(0.04);
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: user3666197 Source score (net votes, not local likes): 1 Original post: https://stackoverflow.com/a/47208015 License: CC BY-SA 3.0 — https://creativecommons.org/licenses/by-sa/3.0/ Adaptation: HTML converted to plain text; contact email addresses removed. First: Before going deeper, let's first also diagnose the situation a step better: #include<Trade\Trade.mqh> ... CTrade m_trade; ... if ( PositionsTotal() == 0 ) { if ( !trade.Buy( 0.1, _Symbol ) ) { //--- failure message Print( "The .Buy() method failed. The return-code = ", trade.ResultRetcode(), ". Code description: ", trade.ResultRetcodeDescription() ); } else { Print( "The .Buy() method executed successfully. The return-code = ", trade.ResultRetcode(), " (", trade.ResultRetcodeDescription(), ")" ); } } Next: If the Print() -ed output above confirms the operation was successful, you may add more details to the .Buy() method and re-test: #include<Trade\Trade.mqh> ... CTrade m_trade; ... double aVolume = 0.1; double aPoint = SymbolInfoDouble( _Symbol, SYMBOL_POINT ); double anAsk = SymbolInfoDouble( _Symbol, SYMBOL_ASK ); double aBid = SymbolInfoDouble( _Symbol, SYMBOL_BID ); int aNumDigits = (int) SymbolInfoInteger( _Symbol, SYMBOL_DIGITS ); double aLongOpen = SymbolInfoDouble( _Symbol, SYMBOL_ASK ), aLongSL = aBid - 1000 * aPoint, SL = NormalizeDouble( aLongSL, aNumDigits ), aLongTP = aBid + 1000 * aPoint, TP = NormalizeDouble( aLongTP, aNumDigits ); if ( !trade.Buy( aVolume, _Symbol, aLongOpen, SL, TP, "Does this work?" ) ) { ... } For going Short, aShortOpen , anAsk -based { TP , SL }-prices and symmetric edit changes apply.
Checking account access…