Results 1 to 2 of 2
 0 Attachment(s)    

Thread: How to hedge all position market and bending order ,and send SL TP after halph second

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    4

    Default How to hedge all position market and bending order ,and send SL TP after halph second

    #include <stdlib.mqh>
    #include <stderror.mqh>

    //---- input parameters
    extern double Lots=0.01;
    extern int LotDigits=2;
    extern double StopDistance ;
    extern double TakeProfit =; //0 Means auto calculate profit from StopLoss
    extern double StopLoss=;
    //extern bool FirstOrderIsLong = true;
    bool FirstOrderIsLong = true;
    extern int Period_MA = 20;
    extern int MagicNumber=999;
    extern bool UseHourTrade = false;
    extern int StartHourTrade = 6; // start trading on this hour
    extern int EndHourTrade = 18; // end trading on this hour
    extern bool FiveDigitBroker = true;
    extern int TimeFrame = 0;
    extern double InitialRisk=4;

    //extern int StartHour=0;
    //extern int EndHour=24;

    int Slippage = 3;

    double OpenBuy=0;
    double OpenSell=0;
    double OpenBuyLimit=0;
    double OpenSellLimit=0;
    double OpenBuyStop=0;
    double OpenSellStop=0;
    double EntryPrice=0;

    double LastBuyStopPrice=0;
    double LastSellStopPrice=0;
    double BuyStopPrice=0;
    double SellStopPrice=0;
    //double aLots[]={1,3,6,12,24,48,96,192,384,768};
    //double aLots[]=(0.1, 0.3, 0.9, 2.7, 8.1, 24.3, 72.9, 218.7, 656.1, 1968.3);
    double MaxLots=0;

    double MA; // MA value on 0 bar
    bool Fact_Up = true; // Fact of report that price..
    bool Fact_Dn = true; //..is above or below MA

    //Trade Signal
    #define _NONE 0
    #define _BUY 1
    #define _SELL 2
    #define _CLOSEBUY 4
    #define _CLOSESELL 8
    #define _OPENBUYSTOP 16
    #define _OPENSELLSTOP 32
    #define _DELETEBUYSTOP 64
    #define _DELETESELLSTOP 128

    int cur_signal=_NONE;

    static double point;
    //+------------------------------------------------------------------+
    //| expert initialization function |
    //+------------------------------------------------------------------+
    int init()
    {
    //---- TODO: add your code here
    point = Point;
    if (FiveDigitBroker)
    point = Point * 10;
    CheckOpenPositions();
    return(0);
    }

    //+------------------------------------------------------------------+
    //| expert deinitialization function |
    //+------------------------------------------------------------------+
    int deinit()
    {
    //---- TODO: add your code here
    return(0);
    }

    //+------------------------------------------------------------------+
    //| expert start function |
    //+------------------------------------------------------------------+
    int start()
    {
    double llots;
    double lSL;

    if (TakeProfit==0) TakeProfit=StopLoss/2;

    if (StopDistance!=TakeProfit)
    if (StopDistance!=(TakeProfit/2))
    return (-1);
    if (TimeFrame!=0)
    if ((TimeFrame!=PERIOD_M1) && (TimeFrame!=PERIOD_M5) && (TimeFrame!=PERIOD_M15) && (TimeFrame!=PERIOD_M30) && (TimeFrame!=PERIOD_H1) && (TimeFrame!=PERIOD_H4) && (TimeFrame!=PERIOD_D1) && (TimeFrame!=PERIOD_W1) && (TimeFrame!=PERIOD_MN1))
    return (-1);

    if(MaxLots==0) llots=LotSize(); else
    if(MaxLots==LotSize()) llots=3*LotSize(); else
    llots=MaxLots*2;

    lSL=StopLoss;

    CheckSignal();


    if(cur_signal & _BUY !=0)
    OpenOrder(OP_BUY, llots, Ask, Slippage, lSL, TakeProfit, WindowExpertName(), MagicNumber, 0, Blue);

    if(cur_signal & _SELL !=0)
    OpenOrder(OP_SELL, llots, Bid, Slippage, lSL, TakeProfit, WindowExpertName(), MagicNumber, 0, Red);

    if(cur_signal & _CLOSEBUY !=0)
    {
    CloseLongs(MagicNumber);
    }

    if(cur_signal & _CLOSESELL !=0)
    {
    CloseShorts(MagicNumber);
    }

    if(cur_signal & _DELETEBUYSTOP !=0)
    DeleteOpenOrder(OP_BUYSTOP);

    if(cur_signal & _DELETESELLSTOP !=0)
    DeleteOpenOrder(OP_SELLSTOP);

    if(cur_signal & _OPENBUYSTOP !=0)
    {
    DeleteOpenOrder(OP_BUYSTOP);
    if(FirstOrderIsLong) BuyStopPrice=EntryPrice; else BuyStopPrice=EntryPrice+StopDistance*point;
    Print ("First Order Is Long = " + FirstOrderIsLong);
    OpenOrder(OP_BUYSTOP, llots, BuyStopPrice, Slippage, lSL, TakeProfit, WindowExpertName(), MagicNumber, 0, Blue);
    }

    if(cur_signal & _OPENSELLSTOP !=0)
    {
    DeleteOpenOrder(OP_SELLSTOP);
    Print ("Entry Price = " + NormalizeDouble(EntryPrice,Digits));
    if(FirstOrderIsLong) SellStopPrice=EntryPrice-StopDistance*point; else SellStopPrice=EntryPrice;
    Print ("First Order Is Long = " + FirstOrderIsLong);
    OpenOrder(OP_SELLSTOP, llots, SellStopPrice, Slippage, lSL, TakeProfit, WindowExpertName(), MagicNumber, 0, Red);
    }

    CheckOpenPositions();
    HandleOpenPositions();
    return(0);
    }
    //+------------------------------------------------------------------+


    void CheckSignal()
    {
    cur_signal=_NONE;

    if(OpenBuy==0 && OpenSell==0)
    {
    if(OpenBuyStop>0) cur_signal |= _DELETEBUYSTOP;
    if(OpenSellStop>0) cur_signal |= _DELETESELLSTOP;

    if(cur_signal==_NONE)
    {
    // to see if it's possible to made a trade
    if(BlockTradingFilter1())
    return(0);

    MA=iMA(NULL,TimeFrame,Period_MA,0,MODE_SMA,PRICE_C LOSE,0);

    if (Bid > MA && Fact_Up == true) // Checking if price above
    {
    Fact_Dn = true; // Report about price above MA
    Fact_Up = false; // Don't report about price below MA
    //Alert("Price is above MA(",Period_MA,")."); // Alert
    FirstOrderIsLong = true;
    }
    //--------------------------------------------------------------------
    if (Bid < MA && Fact_Dn == true) // Checking if price below
    {
    Fact_Up = true; // Report about price below MA
    Fact_Dn = false; // Don't report about price above MA
    //Alert("Price is below MA(",Period_MA,").");// Alert
    FirstOrderIsLong = false;
    }


    if(FirstOrderIsLong) cur_signal |= _BUY; else cur_signal |= _SELL;
    EntryPrice=0;
    }
    }

    if(OpenBuy!=0 || OpenSell!=0)
    {
    if(OpenBuy>OpenSell && OpenSellStop==0)
    {
    cur_signal |= _OPENSELLSTOP;
    }
    if(OpenBuy<OpenSell && OpenBuyStop==0)
    {
    cur_signal |= _OPENBUYSTOP;
    }
    }
    }

    //+------------------------------------------------------------------+
    //| FILTER BLOCK MODULES
    //+------------------------------------------------------------------+
    bool BlockTradingFilter1()
    { //Original code Contributed by Wackena
    bool BlockTrade=false; //trade by default
    if (UseHourTrade)
    {
    if( !(Hour() >= StartHourTrade && Hour() <= EndHourTrade && Minute()<= 3) )
    {
    // Comment("Non-Trading Hours!");
    BlockTrade=true;
    }
    }
    return (BlockTrade);
    }
    Last edited by aaabid2002; 12-28-2009 at 00:57.

  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    4

    Default

    hi funyo.is it possible to hedg all position market and bending order.and make sl tp late by second to trade with ECN broker.
    thank you for all your help .

Similar Threads

  1. Hedge order
    By claypot in forum MQL programming
    Replies: 12
    Last Post: 09-29-2014, 06:16
  2. Order Send Erorrs
    By Doodman in forum MQL programming
    Replies: 10
    Last Post: 03-26-2012, 12:01
  3. Hedge order (EURUSD/USDCHF)
    By getting in forum Ideas for expert advisors
    Replies: 1
    Last Post: 06-21-2010, 16:18
  4. Replies: 4
    Last Post: 12-27-2009, 17:10
  5. Add Close previous order when open new position
    By darkkiller in forum MQL programming
    Replies: 1
    Last Post: 03-21-2009, 05:55

Tags for this Thread

add, broker, ecn, expert, hedge, hours, how to, magic, report, sells, slippage, sma, trading

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •