Download MetaTrader 5

Help me with the coding this script

To add comments, please log in or register
Sequisite
27
Sequisite  

Here's what I have come up with so far. I need to get the order which got market executed:

1) Its entry price.

2) Based on entry price, do the 25 points distance. (Entry price of market order which got executed - 25)

3) Execute a SellStop order, with inputs entered (Lot Size, TP, SL)


//+------------------------------------------------------------------+
//|                                                            x.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property show_inputs

int MAGICMA=23423423;

//---- Parametres Externes
extern string BSSS = "Buy, Sell Stop Script!";
extern double BLot = 0.01;
extern int BuySL = 50;
extern int BuyTP = 52;


extern double   SLot = 0.02;
extern int      SellStopSL = 50; 
extern int      SellTP = 54;

extern double  Distance = 25;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   OrderSend(Symbol(),OP_BUY, BLot,Ask, 0,Ask-BuySL*Point, Ask+BuyTP*Point,"BUY",MAGICMA,0,Blue);
  }
//+------------------------------------------------------------------+


I have tried searching everywhere, and even on own, but being too new, I can't crack coding this right, so please help..

Thank you.

whroeder1
17929
whroeder1  
  1. Don't double post!
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. extern int BuySL = 50;
    extern int BuyTP = 52;
       OrderSend(Symbol(),OP_BUY, BLot,Ask, 0,Ask-BuySL*Point, Ask+BuyTP*Point,"BUY",MAGICMA,0,Blue);
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 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.)

  3. You can't move stops (or pending prices) closer to the market than the minimum (MODE_STOPLEVEL * _Point.)
              Requirements and Limitations in Making Trades - Appendixes - MQL4&Tutorial

  4. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  5. Sequisite: I need to get the order which got market executed:
    Follow #3 and then select the order. You can not use any Trade Functions until you select an order.
Sequisite
27
Sequisite  
whroeder1:
  1. Don't double post!
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 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.)

  3. You can't move stops (or pending prices) closer to the market than the minimum (MODE_STOPLEVEL * _Point.)
              Requirements and Limitations in Making Trades - Appendixes - MQL4&Tutorial

  4. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  5. Follow #3 and then select the order. You can not use any Trade Functions until you select an order

I'm sorry. I just thought it would make it faster, but I was wrong, this forum is more active than forexfactory's.


Also, manually it works perfectly, I have had no issues, which is why I finally thought of  making a script for it to ease my work.


Normally I'd first buy at market, or sell. Then use tp/sl modification EA, it modifies my order's tp and sl quickly as soon as it is executed.


Then I place my pending order. If it's buy, I place sellstop 25 points away from the buy order's entry price. (buy order's entry price - 25 = sellstop's entry price). That's it. 


Then for pending order, my EA comes in hand again, it quickly does its work.


But doing all these steps, market runs ahead or behind sometimes, which is why I want to code it in script.


Btw, my broker didn't give me any issues with executing such orders at such a distance, and so on, so it is gonna be perfectly fine, just please help me with the code. I really can't figure to do it own. 


Thanks a lot.

Alain Verleyen
Moderator
36713
Alain Verleyen  

Don't be unpleasant, please.

Konstantin Nikitin
6148
Konstantin Nikitin  
double stopLevel = (int)SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL)+1;
if( OrderSend( _Symbol,
               OP_BUY,
               MathMin( SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX), MathMax( SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN), BLot ) ),
               Ask, 0,
               Ask - MathMax(BuySL, stopLevel) * Point,
               Ask + MathMax(BuyTP, stopLevel) * Point,
               "BUY", MAGICMA, 0, Blue) < 0 )
     Print("OrderSend failed with error #", GetLastError());
Sequisite
27
Sequisite  
Konstantin Nikitin:

Thanks so much for your effort and time.


I didn't quite understand the code. It's just market order for buy..

Konstantin Nikitin
6148
Konstantin Nikitin  
Sequisite:

Thanks so much for your effort and time.

I didn't quite understand the code. It's just market order for buy..

It is advisable to check "STOPSLEVEL". And the correctness of the lot.

Sequisite
27
Sequisite  
Konstantin Nikitin:

It is advisable to check "STOPSLEVEL". And the correctness of the lot.

My buy market order was doing fine, I am too basic to understand your advanced idea, lol..


All I need to know is how to get this order's entry price:


  buy=OrderSend(Symbol(),OP_BUY, BLot,Ask, 0,Ask-BuySL*Point, Ask+BuyTP*Point,"BUY",MAGICMA,0,Blue);


and use it as entry price for my pending sellstop, then based off its entry price, TP and SL..

Lakshan Perera
3303
Lakshan Perera  
Sequisite:

My buy market order was doing fine, I am too basic to understand your advanced idea, lol..


All I need to know is how to get this order's entry price:


  buy=OrderSend(Symbol(),OP_BUY, BLot,Ask, 0,Ask-BuySL*Point, Ask+BuyTP*Point,"BUY",MAGICMA,0,Blue);


and use it as entry price for my pending sellstop, then based off its entry price, TP and SL..

This codes entry price is the Ask price at the time of execution..
I am quit unclear about what you are trying to achieve here, can you please tell me why are you using a script for placing the order, instead of an EA?

Sequisite
27
Sequisite  
Lakshan Perera:

This codes entry price is the Ask price at the time of execution..
I am quit unclear about what you are trying to achieve here, can you please tell me why are you using a script for placing the order, instead of an EA?

Lakshan, thanks for your concerned reply.


Here's a picture of what I'm trying to say:


As you can see, as soon as I market execute an order, whether buy or sell, the pending stop order should be opposite. 

If it's market order buy, then my pending order is sellstop. Same with market order sell.

Manually I first market execute, then place a pending order. But with this script, when I drag onto the chart, it should market execute the order, and then set a pending order 'x' pips away from market order's entry price (entry price, specifically).

Here's my script:


//+------------------------------------------------------------------+
//|                                                            x.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property show_inputs

int MAGICMA=23423423;
int MAGICMA2=23423424;
   
//---- Parametres Externes
extern string BSSS = "Buy, Sell Stop Script!";
extern double BLot = 0.01;
extern int BuySL = 50;
extern int BuyTP = 52;


extern double   SLot = 0.02;
extern int      SellStopSL = 50; 
extern int      SellTP = 54;

extern int  Distance = 25;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   
int buy, sellstop;
   buy=OrderSend(Symbol(),OP_BUY, BLot,Ask, 0,Ask-BuySL*Point, Ask+BuyTP*Point,"BUY",MAGICMA,0,Blue);
                                                
  }
//+------------------------------------------------------------------+
Lakshan Perera
3303
Lakshan Perera  
Sequisite:

Lakshan, thanks for your concerned reply.


Here's a picture of what I'm trying to say:


As you can see, as soon as I market execute an order, whether buy or sell, the pending stop order should be opposite. 

If it's market order buy, then my pending order is sellstop. Same with market order sell.

Manually I first market execute, then place a pending order. But with this script, when I drag onto the chart, it should market execute the order, and then set a pending order 'x' pips away from market order's entry price (entry price, specifically).

Here's my script:


I think this what you wanted..
But remember as said above You need to check stop levels and acceptable lots sizes in your code, but I just wrote the basic code only, you can modify it by looking at the above codes posted by @Konstantin Nikitin

int MAGICMA=23423423;
int MAGICMA2=23423424;
   
//---- Parametres Externs
extern string BSSS = "Buy, Sell Stop Script!";
extern double BLot = 0.01;
extern int BuySL = 50;
extern int BuyTP = 52;

extern double   SLot = 0.02;
extern int      SellStopSL = 50; 
extern int      SellTP = 54;

extern int  Distance = 25;
double pip=Point;

void OnStart()
  {
//---
   if(Digits == 3 || Digits == 5) pip = 10*Point;

   int buy, sellstop;
   buy=OrderSend(Symbol(),OP_BUY, BLot,Ask, 0,NormalizeDouble(Ask-BuySL*pip,Digits), NormalizeDouble(Ask+BuyTP*pip,Digits),"BUY",MAGICMA,0,Blue);
   if(buy > 0)
     {
      double stOpen = Ask-Distance*pip;
      sellstop = OrderSend(Symbol(),OP_SELLSTOP,BLot,NormalizeDouble(stOpen,Digits),0,NormalizeDouble(stOpen+SellStopSL*pip,Digits),NormalizeDouble(stOpen-SellTP*pip,Digits),"SELLSTOP",MAGICMA);
     }
     else
       {
        Print("Fail to open the order ERROR : ",GetLastError());
       }
  }
12
To add comments, please log in or register