EA to place Fixed SL and TP when trading Buy/Sell by market, on three charts

 

Hello, good afternoon, I hope everybody is fine

I show you the following code that I have been doing at my own pace, because i am new to this and i am learning.

Its objective is that: When I make a buy/sell by market, Stop Loss and Take Profit are already place by default

With the idea that it can save me the time of placing Stop Loss and Take Profit manually or by dragging/trailing  on the charts

This is because the three assets that I trade are very volatile (US30, NAS100 and SPX500) in the time frame of M1, and those few seconds where the SL and TP is set, make the difference. The convenience of automatically placing SL and TP when executing the order by market, would be incredible. For this reason I need your valuable experience to help me get this up and running.

Graphically what you want is attached and the conditions are as follows:

1. When Buying/Selling by market manually US30, automatically place -12,5 pips of SL and +37,5 pips of TP (Regarding the price where y Bought/Sold)

2. When Buying/Selling by market manually NAS100, automatically place -8 pips of SL and +24 pips of TP (Regarding the price where y Bought/Sold)

3. When Buying/Selling by market manually SPX500, automatically place -1,5 pips of SL and +4,5 pips of TP (Regarding the price where y Bought/Sold)

Thanks for your time!!

#include <Trade/Trade.mqh>
CTrade trade;

/*To open operations*/
ulong trade_ticket = 0;
bool time_passed = true;

//    0.5% per operation -> Lots = Risk/(SL+Spread), 
//    Example: 0.5% of 10000$ -> 10000$*0.005 = 100$
//    (10000*0.005/12,5+1,5) Lots for US30
//    US30     12,5 pips of SL + 1,5 pips of spread 
//    NAS100   8 pips of SL + 1 pip of spread      
//    SPX500   1,5 pips of SL + 0,5 pip of spread

double get_lotage()                       
{
 double balance = AccountInfoDouble(ACCOUNT_BALANCE);     // Call the balance to calculate Lots (Position)
   return NormalizeDouble(balance,1);                     // Balance with 1 decimal
 double VolumeUS30 = balance*0.005/14;                    // Volume of US30 (Position)
   return NormalizeDouble(VolumeUS30, 1);
 double VolumeNAS100 = balance*0.005/9;                   // Volume of NAS100 (Position)
   return NormalizeDouble(VolumeNAS100, 1);
 double VolumeSPX500 = balance*0.005/2;                   // Volume of SPX500(Position)
   return NormalizeDouble(VolumeSPX500, 1);
}
   
void OnStart(void)
{
   double MyPoint = Point();
   if(Digits() == 3 || Digits() == 5)
      MyPoint = Point() * 10;
   double SL = 0;
   double TP = 0;
   double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
   double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);


//BUY-SELL PROCESS OF US30//                       // Lots = Risk/(SL+Spread)
if(Symbol() == "US30")                             // Risk = 0,5% of actual balance
  {                                                // US30: 12,5 pips of SL + 1,5 pips of spread 
   double LotesUS30 = VolumeUS30;
   SL = 12.5;
   TP = 37.5;
   trade.Buy(LotesUS30, Symbol(), 0, Ask - SL * MyPoint, Ask + TP * MyPoint, NULL);       // Buy US30
   trade.Sell(LotesUS30, Symbol(), 0, Bid + SL * MyPoint, Bid - TP * MyPoint, NULL);      // Sell US30
  }
   
//BUY-SELL PROCESS OF NAS100//                     // Lots = Risk/(SL+Spread)
if(Symbol() == "NAS100")                           // Risk = 0,5% of actual balance
  {                                                // NAS100: 8 pips of SL + 1 pip of spread                             
   double LotesNAS100 = VolumeNAS100;
   SL = 8;
   TP = 24;
   trade.Buy(LotesNAS100, Symbol(), 0, Ask - SL * MyPoint, Ask + TP * MyPoint, NULL);       // Buy NAS100
   trade.Sell(LotesNAS100, Symbol(), 0, Bid + SL * MyPoint, Bid - TP * MyPoint, NULL);      // Sell NAS100
  }
  
//BUY-SELL PROCESS OF SPX500//                     // Lots = Risk/(SL+Spread)
if(Symbol() == "SPX500")                           // Risk = 0,5% of actual balance
  {                                                // SPX500: 1,5 pips of SL + 0,5 pip of spread                             
   double LotesSPX500 = VolumeSPX500;
   SL = 1.5;
   TP = 4.5;
   trade.Buy(LotesSPX500, Symbol(), 0, Ask - SL * MyPoint, Ask + TP * MyPoint, NULL);       // Buy SPX500
   trade.Sell(LotesSPX500, Symbol(), 0, Bid + SL * MyPoint, Bid - TP * MyPoint, NULL);      // Sell SPX500
  }
}
Files:
 
   trade.Buy(LotesUS30, Symbol(), 0, Ask - SL * MyPoint, Ask + TP * MyPoint, NULL);       // Buy US30

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

Reason: