Need help add some code

 

Hi

I have  free EA  and I want to add Trailing stop,Break even ,And max spread in to it.

Please anyone help me?

#define MAGICMA  20110131
#include <stdlib.mqh>

extern double    Lots = 0.1;
extern int        Distance = 200;
extern int        StopLoss = 500;
extern int        TakeProfit = 500;
extern bool        DEBUG = false;

int order_buy, order_sell;
int delete_unneed_order;            // 1 - if buy triggered, -1 if sell, 0 if pending order (not triggered) was deleted

int start()
{
    int res;

    if (Bars < 100 || IsTradeAllowed() == false)
        return;
       
    /*
    switch (delete_unneed_order)
    {
        case 0:                        // opposite order was already deleted. Do nothing
            break;

        case 1:                        // delete sell order
            if (OrderSelect(order_sell, SELECT_BY_TICKET))
            {
                OrderDelete(order_sell);
                delete_unneed_order = 0;
                if (DEBUG) Print("... sell order deleted");
            } else
                Print("Can\'t delete sell order: can\'t select order!");
            break;

        case -1:                    // delete buy order
            if (OrderSelect(order_buy, SELECT_BY_TICKET))
            {
                OrderDelete(order_buy);
                delete_unneed_order = 0;
                if (DEBUG) Print("... buy order deleted");
            } else
                Print("Can\'t delete buy order: can\'t select order!");
            break;
    }
    */

    // delete opposite order
    if (DEBUG) Print("Trying to delete opposite order if need...");

    if (CalculateOpenedOrders(Symbol()) == 1)    // if BUY opened
    {
        if (OrderSelect(order_sell, SELECT_BY_TICKET))
        {
            OrderDelete(order_sell);
            if (DEBUG) Print("Sell pending order deleted");
        }
    } else if (CalculateOpenedOrders(Symbol()) == -1)    // if SELL opened
    {
        if (OrderSelect(order_buy, SELECT_BY_TICKET))
        {
            OrderDelete(order_buy);
            if (DEBUG) Print("Buy pending order deleted");
        }
    } else
        if (DEBUG) Print("No need to delete anything");

    // trade only on new bar
    if (Volume[0] > 1)
        return;

    // check opened orders exist
    if (CalculateOpenedOrders(Symbol()) != 0)
    {
        if (DEBUG) Print("There are ", CalculateOpenedOrders(Symbol()), " opened orders");
        // they exists, so we do not need to modify pending orders -- write 0's to pending orders counters
        order_buy = 0;
        order_sell = 0;

        return(0);
    }

    if (CalculateOpenedOrders(Symbol()) == 0 && CalculatePlacedOrders(Symbol()) == 0)
    {
        if (DEBUG) Print("There are no opened and no placed orders");
        SetBuySellStops();
    }
   
    if (CalculateOpenedOrders(Symbol()) == 0 && CalculatePlacedOrders(Symbol()) != 0)
    {
        if (DEBUG) Print("There are no opened, but placed orders exists -- need modify them");
        if (order_buy > 0 || order_sell > 0)
        {
            if (DEBUG) Print("Trying to modify placed orders");
            ModifyBuySellStops();
        }
    }

    return(0);
}

int ModifyBuySellStops()
{
    bool res;

    if (OrderSelect(order_buy, SELECT_BY_TICKET))
    {
        double high = NormalizeDouble(iHigh(NULL, 0, 1), Digits);
        res = OrderModify(order_buy, high+Distance*Point, (high+Distance*Point)-StopLoss*Point, (high+Distance*Point)+TakeProfit*Point, 0, Blue);
        if (!res)
        {
            Print("Can\' modify pending order");
        }
    }

    if (OrderSelect(order_sell, SELECT_BY_TICKET))
    {
        double low = NormalizeDouble(iLow(NULL, 0, 1), Digits);
        res = OrderModify(order_sell, low-Distance*Point, (low-Distance*Point)+StopLoss*Point, (low-Distance*Point)-TakeProfit*Point, 0, Red);
        if (!res)
        {
            Print("Can\' modify pending order");
        }
    }

    return(0);
}

int SetBuySellStops()
{
    double high = NormalizeDouble(iHigh(NULL, 0, 1), Digits);
    double low = NormalizeDouble(iLow(NULL, 0, 1), Digits);

    order_buy = OrderSend(Symbol(), OP_BUYSTOP, Lots, high+Distance*Point, 3, (high+Distance*Point)-StopLoss*Point, (high+Distance*Point)+TakeProfit*Point, "", MAGICMA, 0, Blue);
    if (DEBUG) Print(">>> Placing buy order: PRICE: ", high+Distance*Point, " SL: ", (high+Distance*Point)-StopLoss*Point, " TP: ", (high+Distance*Point)+TakeProfit*Point);
    if (order_buy == -1)
    {
        Print("Buy error: ", ErrorDescription(GetLastError()));
        return(-1);
    }

    order_sell = OrderSend(Symbol(), OP_SELLSTOP, Lots, low-Distance*Point, 3, (low-Distance*Point)+StopLoss*Point, (low-Distance*Point)-TakeProfit*Point, "", MAGICMA, 0, Red);
    if (DEBUG) Print(">>> Placing sell order: PRICE: ", low-Distance*Point, " SL: ", (low-Distance*Point)+StopLoss*Point, " TP: ", (low-Distance*Point)-TakeProfit*Point);
    if (order_sell == -1)
    {
        Print("Sell error: ", ErrorDescription(GetLastError()));
        return(-1);
    }
   
    return(1);
}

int CalculateOpenedOrders(string symbol)
{
    int buys = 0, sells = 0;

    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
            break;
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICMA)
        {
            if (OrderType() == OP_BUY)
                buys++;
            if (OrderType() == OP_SELL)
                sells++;
        }
    }
   
    if (buys > 0)
        return(buys);
    else
        return(-sells);
}

int CalculatePlacedOrders(string symbol)
{
    int buys = 0, sells = 0;

    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
            break;
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICMA)
        {
            if (OrderType() == OP_BUYSTOP)
                buys++;
            if (OrderType() == OP_SELLSTOP)
                sells++;
        }
    }

    if (buys > 0)
        return(buys);
    else
        return(-sells);

}

Thank you

 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 

angevoyageur: Hello I'm newbie so sorry

Thank you for help.



#define MAGICMA  20110131
#include <stdlib.mqh>

extern double   Lots = 0.1;
extern int              Distance = 200;
extern int              StopLoss = 500;
extern int              TakeProfit = 500;
extern bool             DEBUG = false;

int order_buy, order_sell;
int delete_unneed_order;                        // 1 - if buy triggered, -1 if sell, 0 if pending order (not triggered) was deleted

int start()
{
        int res;

        if (Bars < 100 || IsTradeAllowed() == false)
                return;
                
        /*
        switch (delete_unneed_order)
        {
                case 0:                                         // opposite order was already deleted. Do nothing
                        break;

                case 1:                                         // delete sell order
                        if (OrderSelect(order_sell, SELECT_BY_TICKET))
                        {
                                OrderDelete(order_sell);
                                delete_unneed_order = 0;
                                if (DEBUG) Print("... sell order deleted");
                        } else
                                Print("Can\'t delete sell order: can\'t select order!");
                        break;

                case -1:                                        // delete buy order
                        if (OrderSelect(order_buy, SELECT_BY_TICKET))
                        {
                                OrderDelete(order_buy);
                                delete_unneed_order = 0;
                                if (DEBUG) Print("... buy order deleted");
                        } else
                                Print("Can\'t delete buy order: can\'t select order!");
                        break;
        }
        */

        // delete opposite order
        if (DEBUG) Print("Trying to delete opposite order if need...");

        if (CalculateOpenedOrders(Symbol()) == 1)       // if BUY opened
        {
                if (OrderSelect(order_sell, SELECT_BY_TICKET))
                {
                        OrderDelete(order_sell);
                        if (DEBUG) Print("Sell pending order deleted");
                }
        } else if (CalculateOpenedOrders(Symbol()) == -1)       // if SELL opened
        {
                if (OrderSelect(order_buy, SELECT_BY_TICKET))
                {
                        OrderDelete(order_buy);
                        if (DEBUG) Print("Buy pending order deleted");
                }
        } else
                if (DEBUG) Print("No need to delete anything");

        // trade only on new bar
    if (Volume[0] > 1)
        return;

        // check opened orders exist
        if (CalculateOpenedOrders(Symbol()) != 0)
        {
                if (DEBUG) Print("There are ", CalculateOpenedOrders(Symbol()), " opened orders");
                // they exists, so we do not need to modify pending orders -- write 0's to pending orders counters
                order_buy = 0;
                order_sell = 0;

                return(0);
        }

        if (CalculateOpenedOrders(Symbol()) == 0 && CalculatePlacedOrders(Symbol()) == 0)
        {
                if (DEBUG) Print("There are no opened and no placed orders");
                SetBuySellStops();
        }
        
        if (CalculateOpenedOrders(Symbol()) == 0 && CalculatePlacedOrders(Symbol()) != 0)
        {
                if (DEBUG) Print("There are no opened, but placed orders exists -- need modify them");
                if (order_buy > 0 || order_sell > 0)
                {
                        if (DEBUG) Print("Trying to modify placed orders");
                        ModifyBuySellStops();
                }
        }

        return(0);
}

int ModifyBuySellStops()
{
        bool res;

        if (OrderSelect(order_buy, SELECT_BY_TICKET))
        {
                double high = NormalizeDouble(iHigh(NULL, 0, 1), Digits);
                res = OrderModify(order_buy, high+Distance*Point, (high+Distance*Point)-StopLoss*Point, (high+Distance*Point)+TakeProfit*Point, 0, Blue);
                if (!res)
                {
                        Print("Can\' modify pending order");
                }
        }

        if (OrderSelect(order_sell, SELECT_BY_TICKET))
        {
                double low = NormalizeDouble(iLow(NULL, 0, 1), Digits);
                res = OrderModify(order_sell, low-Distance*Point, (low-Distance*Point)+StopLoss*Point, (low-Distance*Point)-TakeProfit*Point, 0, Red);
                if (!res)
                {
                        Print("Can\' modify pending order");
                }
        }

        return(0);
}

int SetBuySellStops()
{
        double high = NormalizeDouble(iHigh(NULL, 0, 1), Digits);
        double low = NormalizeDouble(iLow(NULL, 0, 1), Digits);

        order_buy = OrderSend(Symbol(), OP_BUYSTOP, Lots, high+Distance*Point, 3, (high+Distance*Point)-StopLoss*Point, (high+Distance*Point)+TakeProfit*Point, "", MAGICMA, 0, Blue);
        if (DEBUG) Print(">>> Placing buy order: PRICE: ", high+Distance*Point, " SL: ", (high+Distance*Point)-StopLoss*Point, " TP: ", (high+Distance*Point)+TakeProfit*Point);
        if (order_buy == -1)
        {
                Print("Buy error: ", ErrorDescription(GetLastError()));
                return(-1);
        }

        order_sell = OrderSend(Symbol(), OP_SELLSTOP, Lots, low-Distance*Point, 3, (low-Distance*Point)+StopLoss*Point, (low-Distance*Point)-TakeProfit*Point, "", MAGICMA, 0, Red);
        if (DEBUG) Print(">>> Placing sell order: PRICE: ", low-Distance*Point, " SL: ", (low-Distance*Point)+StopLoss*Point, " TP: ", (low-Distance*Point)-TakeProfit*Point);
        if (order_sell == -1)
        {
                Print("Sell error: ", ErrorDescription(GetLastError()));
                return(-1);
        }
        
        return(1);
}

int CalculateOpenedOrders(string symbol)
{
        int buys = 0, sells = 0;

        for (int i = 0; i < OrdersTotal(); i++)
        {
                if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
                        break;
                if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICMA)
                {
                        if (OrderType() == OP_BUY)
                                buys++;
                        if (OrderType() == OP_SELL)
                                sells++;
                }
        }
        
        if (buys > 0)
                return(buys);
        else
                return(-sells);
}

int CalculatePlacedOrders(string symbol)
{
        int buys = 0, sells = 0;

        for (int i = 0; i < OrdersTotal(); i++)
        {
                if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
                        break;
                if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGICMA)
                {
                        if (OrderType() == OP_BUYSTOP)
                                buys++;
                        if (OrderType() == OP_SELLSTOP)
                                sells++;
                }
        }

        if (buys > 0)
                return(buys);
        else
                return(-sells);
}
 
suttisak:
What's your question ?
 
angevoyageur:
What's your question ?

Hi,

How I add Trailing stop,Break even ,And max spread in to it.


Thank you.

 
suttisak:

Hi,

How I add Trailing stop,Break even ,And max spread in to it.


Thank you.

You need a coder. See Freelance section.
Reason: