Download MetaTrader 5

Static stop loss as soon as order comes in profit.

To add comments, please log in or register
Deposit to your account via Visa QIWI Wallet
Vivek Vyas
217
Vivek Vyas 2014.07.08 13:33 
//+------------------------------------------------------------------+
//|                                        Modify SL_Profit Lock.mq4 |
//|                                        MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern double Amount10 = 14;
extern bool AllOrderTypes10 = true;
extern int BuyStoploss5 = 5;
extern int BuyTakeprofit5 = 0;
extern double Amount2 = 14;
extern int SellStoploss1 = 5;
extern int SellTakeprofit1 = 0;
// This allows the ea to manage all existing trades
extern string  OverRide1="ManageAllTrades will override all others";
extern bool    ManageAllTrades=true;
// User can choose a variety of trade managment triggers.
// These are for use on a chart that controls the currency of that chart
extern string  ManagementStyle="You can select more than one option";
extern bool    ManageByMagicNumber=true;
extern int     MagicNumber=274693;
extern bool    ManageByTickeNumber=true;
extern int     TicketNumber;

// local variables
double PipValue=1;    // this variable is here to support 5-digit brokers
bool Terminated = false;
string LF = "\n";  // use this in custom or utility blocks where you need line feeds
int NDigits = 4;   // used mostly for NormalizeDouble in Flex type blocks
int ObjCount = 0;  // count of all objects created on the chart, allows creation of objects with unique names
int current = 0;



int init()
{
    NDigits = Digits;
    
    if (false) ObjectsDeleteAll();      // clear the chart
    
    
    Comment("");    // clear the chart
}

// Expert start
int start()
{
    if (Bars < 10)
    {
        Comment("Not enough bars");
        return (0);
    }
    if (Terminated == true)
    {
        Comment("EA Terminated.");
        return (0);
    }
    
    OnEveryTick3();
    
}

void OnEveryTick3()
{
    if (true == false && false) PipValue = 10;
    if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;
    
    CheckOpenOrders10();
    CheckOpenOrders2();
    
}

void CheckOpenOrders10()
{
    double profit = 0;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (AllOrderTypes10 || (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 0))
            {
                profit += OrderProfit();
            }
        }
        else
        {
            Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
        }
    }
    
    if (profit > Amount10)
    {
        BuyOrderModify5();
        
    }
}

void BuyOrderModify5()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 0)
        {
            double price = Ask;
            if (true == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price - BuyStoploss5*PipValue*Point, price + BuyTakeprofit5*PipValue*Point, 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}

void CheckOpenOrders2()
{
    double profit = 0;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (true || (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 0))
            {
                profit += OrderProfit();
            }
        }
        else
        {
            Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
        }
    }
    
    if (profit > Amount2)
    {
        SellOrderModify1();
        
    }
}

void SellOrderModify1()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 0)
        {
            double price = Bid;
            if (true == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price + SellStoploss1*PipValue*Point, price - SellTakeprofit1*PipValue*Point, 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}



int deinit()
{
    if (false) ObjectsDeleteAll();
    
    
}

Please guide me to something which can auto adjust my stoploss to the allowable level say some pips as soon as it is in profit and lock it with this stoploss changing the previous stop loss.

I call it static stop loss which will move only once to a minimum stoploss as soon as order comes in profit.Its different from trailing stop.I want to lock my profit at the minimum allowable level and do not want to trail it along just stay there.

And How can it be applied on one chart and it would work for every open order or every open chart.

Example :- ATC multiporpose trade manager  

qjol
3243
qjol 2014.07.08 14:02  

in short ?! OrderModify

in more detailed way ?! Freelance !

https://www.mql5.com/en/forum/152192


whroeder1
14504
whroeder1 2014.07.08 14:04  
expertarts: Please guide me to something which can...
You have only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
Vivek Vyas
217
Vivek Vyas 2014.07.09 18:45  
WHRoeder:
expertarts: Please guide me to something which can...
You have only three choices: Search for it, learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.


Here is the attempt to do so.Modify all order stop loss from current ask/bid to 5 pips below when order comes in 14 pips profit.

Now I am not sure will it work on every open order.I tried it on backtest with simulator but no message in journel tab when the profit of 14 pips is hit.

Now its my attempt to achieve so.It might be a small project for someone but that is it how i know above all.If I got help it will move if not  then what can be done.

CHEERS!!! 

Vivek Vyas
217
Vivek Vyas 2014.07.09 18:47  
qjol:

in short ?! OrderModify

in more detailed way ?! Freelance !

https://www.mql5.com/en/forum/152192



if u keen to help with the code its attached now.give me a solution out of it according to the requirement.

CHEERS!!! 

whroeder1
14504
whroeder1 2014.07.09 19:54  
expertarts: Here is the attempt to do so.
expertarts: help with the code its attached
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

    Nothing attached
  2. No problem stated.
qjol
3243
qjol 2014.07.09 20:39  

the problem is: it's not "your" attempted code

it's coming from an EA builder, so please...

Vivek Vyas
217
Vivek Vyas 2014.07.10 11:40  
//+------------------------------------------------------------------+
//|                                        Modify SL_Profit Lock.mq4 |
//|                                        MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern double Amount10 = 14;
extern bool AllOrderTypes10 = true;
extern int BuyStoploss5 = 5;
extern int BuyTakeprofit5 = 0;
extern double Amount2 = 14;
extern int SellStoploss1 = 5;
extern int SellTakeprofit1 = 0;
// This allows the ea to manage all existing trades
extern string  OverRide1="ManageAllTrades will override all others";
extern bool    ManageAllTrades=true;
// User can choose a variety of trade managment triggers.
// These are for use on a chart that controls the currency of that chart
extern string  ManagementStyle="You can select more than one option";
extern bool    ManageByMagicNumber=true;
extern int     MagicNumber=274693;
extern bool    ManageByTickeNumber=true;
extern int     TicketNumber;

// local variables
double PipValue=1;    // this variable is here to support 5-digit brokers
bool Terminated = false;
string LF = "\n";  // use this in custom or utility blocks where you need line feeds
int NDigits = 4;   // used mostly for NormalizeDouble in Flex type blocks
int ObjCount = 0;  // count of all objects created on the chart, allows creation of objects with unique names
int current = 0;



int init()
{
    NDigits = Digits;
    
    if (false) ObjectsDeleteAll();      // clear the chart
    
    
    Comment("");    // clear the chart
}

// Expert start
int start()
{
    if (Bars < 10)
    {
        Comment("Not enough bars");
        return (0);
    }
    if (Terminated == true)
    {
        Comment("EA Terminated.");
        return (0);
    }
    
    OnEveryTick3();
    
}

void OnEveryTick3()
{
    if (true == false && false) PipValue = 10;
    if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;
    
    CheckOpenOrders10();
    CheckOpenOrders2();
    
}

void CheckOpenOrders10()
{
    double profit = 0;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (AllOrderTypes10 || (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 0))
            {
                profit += OrderProfit();
            }
        }
        else
        {
            Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
        }
    }
    
    if (profit > Amount10)
    {
        BuyOrderModify5();
        
    }
}

void BuyOrderModify5()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 0)
        {
            double price = Ask;
            if (true == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price - BuyStoploss5*PipValue*Point, price + BuyTakeprofit5*PipValue*Point, 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}

void CheckOpenOrders2()
{
    double profit = 0;
    for (int i=OrdersTotal()-1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (true || (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 0))
            {
                profit += OrderProfit();
            }
        }
        else
        {
            Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
        }
    }
    
    if (profit > Amount2)
    {
        SellOrderModify1();
        
    }
}

void SellOrderModify1()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 0)
        {
            double price = Bid;
            if (true == false)
            {
                price = OrderOpenPrice();
            }
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), price + SellStoploss1*PipValue*Point, price - SellTakeprofit1*PipValue*Point, 0, White);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
    }
    
}



int deinit()
{
    if (false) ObjectsDeleteAll();
    
    
}
Vivek Vyas
217
Vivek Vyas 2014.07.10 11:46  

Find attached code.

There's no problem compiling the code.the problem is its been not executed and it doesn't work or modify order as soon as the order's is in profit.I backtested it with simulation on stratergy tester but no results.I want it to execute when order in in profit of 14 pips place a stop loss below/above as per market order in accordance with current Ask/Bid Price.

Thanks for looking into the topic.

What might be the solution???

Vivek Vyas
217
Vivek Vyas 2014.07.10 11:48  
qjol:

the problem is: it's not "your" attempted code

it's coming from an EA builder, so please...

Yes,you are right!!!!! I save my time building code via EA builder so its like that.!!

qjol
3243
qjol 2014.07.10 15:09  
expertarts:

Yes,you are right!!!!! I save my time building code via EA builder so its like that.!!

 


ho, now i get it

and ...you expect us to spend "your" time to find out what's the EA builder's mistake instead of you ?? !!

12
To add comments, please log in or register