Results 1 to 2 of 2
 2 Attachment(s)    

Thread: Need help to verify what wrong with my code

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2

    Default Need help to verify what wrong with my code

    i have add hidden tp and sl code given by funyoo. compile with no error.
    but when i try to run this ea tp and sl still available in broker section.

    here is code:



    extern double TakeProfit = 10;
    extern double StopLoss = 10;
    extern double TrailingStop = 40;
    extern double Lots = 0.1;
    extern int Magic= 16384;
    extern double SAR_Step = 0.02;
    extern double SAR_Maximum = 0.2;
    extern double Shift=0.0;
    extern bool HideSL=false;//|---------------------hide stop loss
    extern bool HideTP=false;//|---------------------hide take profit


    //+------------------------------------------------------------------+
    //| |
    //+------------------------------------------------------------------+
    int start()
    {

    double LastBuyOpenPrice=0;
    double LastSellOpenPrice=0;
    int BuyOpenPosition=0;
    int SellOpenPosition=0;
    int TotalOpenPosition=0;
    int cnt=0;
    int total, ticket;

    for(cnt=0;cnt<OrdersTotal();cnt++)
    {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==Ma gic&&OrderCloseTime()==0)
    {
    TotalOpenPosition++;
    if(OrderType()==OP_BUY)
    {
    BuyOpenPosition++;
    LastBuyOpenPrice=OrderOpenPrice();
    }
    if(OrderType()==OP_SELL)
    {
    SellOpenPosition++;
    LastSellOpenPrice=OrderOpenPrice();
    }
    }
    }

    if((HideSL&&StopLoss>0&&Bid<(LastBuyOpenPrice-StopLoss*Point))||(HideTP&&TakeProfit>0&&Bid>(Last BuyOpenPrice+TakeProfit*Point)))
    {
    CloseBuyOrders(Magic);
    }
    if((HideSL&&StopLoss>0&&Ask>(LastSellOpenPrice+Sto pLoss*Point))||(HideTP&&TakeProfit>0&&Ask<(LastSel lOpenPrice-TakeProfit*Point)))
    {
    CloseSellOrders(Magic);
    } // end

    // int cnt, ticket, total;
    // initial data checks
    // it is important to make sure that the expert works with a normal
    // chart and the user did not make any mistakes setting external
    // variables (Lots, StopLoss, TakeProfit,
    // TrailingStop) in our case, we check TakeProfit
    // on a chart of less than 100 bars
    if(Bars<100)
    {
    Print("bars less than 100");
    return(0);
    }
    if(TakeProfit<10)
    {
    Print("TakeProfit less than 10");
    return(0); // check TakeProfit
    }
    // to simplify the coding and speed up access
    // data are put into internal variables
    double SARCurrent=iSAR(NULL,0,SAR_Step,SAR_Maximum,Shift) ;
    double SARPrevious=iSAR(NULL,0,SAR_Step,SAR_Maximum,Shift +1);

    total=OrdersTotal();
    if(total<1)
    {
    // no opened orders identified
    if(AccountFreeMargin()<(1000*Lots))
    {
    Print("We have no money. Free Margin = ", AccountFreeMargin());
    return(0);
    }
    // check for long position (BUY) possibility
    if(SARCurrent<Bid && SARPrevious>Bid)
    {
    ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,1,(StopL oss),Ask+(TakeProfit)*Point,"Buy",Magic,0,Green);
    if(ticket>0)
    {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());
    }
    else Print("Error opening BUY order : ",GetLastError());
    Sleep(6000);
    RefreshRates();
    return(0);
    }
    // check for short position (SELL) possibility
    if(SARCurrent>Ask && SARPrevious<Ask)
    {
    ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,1,(Stop Loss),Bid-(TakeProfit)*Point,"Sell",Magic,0,Red);
    if(ticket>0)
    {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());
    }
    else Print("Error opening SELL order : ",GetLastError());
    Sleep(6000);
    RefreshRates();
    return(0);
    }
    return(0);
    }
    // it is important to enter the market correctly,
    // but it is more important to exit it correctly...
    for(cnt=0;cnt<total;cnt++)
    {
    OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
    if(OrderType()<=OP_SELL && // check for opened position
    OrderSymbol()==Symbol()) // check for symbol
    {
    if(OrderType()==OP_BUY) // long position is opened
    {
    // should it be closed?
    if(SARCurrent>Ask && SARPrevious<Ask)
    {
    OrderClose(OrderTicket(),OrderLots(),Bid,1,Violet) ; // close position
    return(0); // exit
    }
    // check for trailing stop
    if(TrailingStop>0)
    {
    if(Bid-OrderOpenPrice()>Point*TrailingStop)
    {
    if(OrderStopLoss()<Bid-Point*TrailingStop)
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
    return(0);
    }
    }
    }
    }
    else // go to short position
    {
    // should it be closed?
    if(SARCurrent<Bid && SARPrevious>Bid)
    {
    OrderClose(OrderTicket(),OrderLots(),Ask,1,Violet) ; // close position
    return(0); // exit
    }
    // check for trailing stop
    if(TrailingStop>0)
    {
    if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
    {
    if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Poi nt*TrailingStop,OrderTakeProfit(),0,Red);
    return(0);
    }
    }
    }
    }
    }
    }
    return(0);
    }


    //|---------close buy orders

    int CloseBuyOrders(int Magic)
    {
    int result,total=OrdersTotal();

    for (int cnt=total-1;cnt>=0;cnt--)
    {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbo l())
    {
    if(OrderType()==OP_BUY)
    {
    OrderClose(OrderTicket(),OrderLots(),Bid,3);
    switch(OrderType())
    {
    case OP_BUYLIMIT:
    case OP_BUYSTOP:
    result=OrderDelete(OrderTicket());
    }
    }
    }
    }
    return(0);
    }

    //|---------close sell orders

    int CloseSellOrders(int Magic)
    {
    int result,total=OrdersTotal();

    for(int cnt=total-1;cnt>=0;cnt--)
    {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber()==Magic&&OrderSymbol()==Symbo l())
    {
    if(OrderType()==OP_SELL)
    {
    OrderClose(OrderTicket(),OrderLots(),Ask,3);
    switch(OrderType())
    {
    case OP_SELLLIMIT:
    case OP_SELLSTOP:
    result=OrderDelete(OrderTicket());
    }
    }
    }
    }
    return(0);
    }
    // the end.

  2. #2

    Default

    Try this...
    Good Luck Bill
    Attached Files Attached Files
    Last edited by BillR; 02-24-2010 at 01:39.

Similar Threads

  1. Blessings 3 (sorry wrong forum it's a free one)
    By randy1953 in forum Expert advisors live statements
    Replies: 6
    Last Post: 05-24-2014, 16:18
  2. Need Code for S/L and some more
    By Monkesy in forum Ideas for expert advisors
    Replies: 0
    Last Post: 04-05-2011, 11:30
  3. Please help me to code this EA
    By sshirbhate in forum MQL programming
    Replies: 2
    Last Post: 03-21-2011, 13:03
  4. Help with code
    By fxwealth in forum MQL programming
    Replies: 1
    Last Post: 03-25-2010, 18:10
  5. Time filter logic is wrong
    By kyojee in forum MQL programming
    Replies: 10
    Last Post: 12-22-2009, 04:47

Tags for this Thread

100, add, broker, coding, expert, funyoo, magic, money, sells, stoploss, trailingstop

Posting Permissions

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