It sets SL and TP on the first order the second it doesnt. refer to the picture first trade it sets sl 2nd one it doesnt
Btw, Read the Documentation. You can set TP\SL when placeing an Order.
![Documentation on MQL5: Standard Library / Trade Classes / CTrade Documentation on MQL5: Standard Library / Trade Classes / CTrade](https://c.mql5.com/36/57/documentation-on-mql5-standard.png)
- www.mql5.com
What's sale variable used for?
Btw, Read the Documentation. You can set TP\SL when placeing an Order.
Thanks it worked..Sir how can I write the code to close a trade when in a loss of -1usd? I have been trying but Im a beginner
Based on your code sample, it could be something like this
double profit; ulong Ticket = 0; for(int a = 0; a < PositionsTotal() ; a++) { Ticket = PositionGetTicket(a); profit = PositionGetDouble(POSITION_PROFIT); if(NormalizeDouble(profit,2) == NormalizeDouble(-1.00,2)) { //Close the trade } }
#include<Trade\Trade.mqh> CTrade trade; int handlee; double Openingsale=0;double SLsale=0; double TPsale=0;bool sale=false; void OnInit() {double profit; ulong Ticket = 0; for(int a = 0; a < PositionsTotal() ; a++) { Ticket = PositionGetTicket(a); profit = PositionGetDouble(POSITION_PROFIT); if(NormalizeDouble(profit,2) == NormalizeDouble(-1.00,2)) { //Close the trade } } { string name = "Market\\Dark Point MT5.ex5" ; handlee = iCustom(_Symbol,PERIOD_CURRENT,name); } } void OnTick(){ for(int a=0; a<PositionsTotal() ; a++) { ulong Ticket=PositionGetTicket(a); if(_Symbol == PositionGetString(POSITION_SYMBOL)) { if(PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL) { Openingsale=PositionGetDouble(POSITION_PRICE_OPEN); SLsale= Openingsale +500*_Point; TPsale= Openingsale -500*_Point; trade.PositionModify(Ticket,SLsale,TPsale); sale=true; } } } double indiSell1[]; CopyBuffer(handlee,1,1,1,indiSell1); if(indiSell1[0]>0 && PositionsTotal() ==0) trade.Sell(0.3); double indiSellB1[]; CopyBuffer(handlee,1,1,1,indiSellB1); double indiSell2[]; CopyBuffer(handlee,1,1,1,indiSell2); double indiSell2B[]; CopyBuffer(handlee,1,1,1,indiSell2B); double indiSell3[]; CopyBuffer(handlee,1,1,1,indiSell3); double indiSell3B[]; CopyBuffer(handlee,1,1,1,indiSell3B); Comment("\nIndi indiSell1: ",indiSell1[0], "\nIndi indiSell1: ",indiSellB1[0], "\nIndi indiSell1: ",indiSell2[0], "\nIndi indiSell1: ",indiSell2B[0], "\nIndi indiSell1: ",indiSell3[0], "\nIndi indiSell1: ",indiSell3[0]); }
Like this Sir?
- Can't be done. The moment you open a trade, you are in a loss because of the spread. Control your risk.
-
Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.
-
You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.
-
AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)
-
Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
Lot value calculation off by a factor of 100 - MQL5 programming forum (2019) -
You must normalize lots properly and check against min and max.
-
You must also check Free Margin to avoid stop out
-
For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
-
No - in OnInit() it will only get executed once and never again. You would put it in OnTick()
However this was just to give you an idea how to code - there are many more considerations as pointed out by @whroeder1
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use