Setting SL and TP everytime position opens

 
#include<Trade\Trade.mqh> CTrade trade;
int handlee;

double Openingsale=0;double SLsale=0; double TPsale=0;bool sale=false;

void OnInit()
   { 
    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 && sale==false)
     {
         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(1);
   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]);
 }          

         
     
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
Files:
Capture.PNG  58 kb
 
watson mulenga:
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
What's sale variable used for?

Delete "sale==false" inside your if, or set it to false in the begin of loop.



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
  • www.mql5.com
CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
_MAHA_ #:
What's sale variable used for?

Delete "sale==false" inside your if, or set it to false in the begin of loop.



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
 
watson mulenga #:
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
        }
     }
 
R4tna C #:

Based on your code sample, it could be something like this

This should be on Init()?
R4tna C #:

Based on your code sample, it could be something like this

#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?

 
watson mulenga #: .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
  1. Can't be done. The moment you open a trade, you are in a loss because of the spread. Control your risk.
  2. 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.

    1. 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.

    2. 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.)

    3. 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)

    4. You must normalize lots properly and check against min and max.

    5. You must also check Free Margin to avoid stop out

    6. 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.

 
watson mulenga #:
This should be on Init()?

Like this Sir?

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

 
R4tna C #:

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

Okay thanks 
Reason: