web statistics


Bagian 2 - Contoh EA

Discussion in 'Tutorial MQL4' started by white_tiger, Oct 19, 2008.

  1. mediator.investor

    mediator.investor
    Expand Collapse
    New Member

    Joined:
    Jul 1, 2012
    Messages:
    18
    Likes Received:
    0
    kenapa EA yang coba saya buat ngga mau action sesuai logic yang saya buat

    saya sudah coba membuat robot trading untuk hangseng,
    dengan logic :

    a. Entry BUY :
    pada saat pembukaan candle ke 2 volume candle naik 20 point Dan candle pertama harus dlm kondisi naik.

    b. Entry SELL :
    pada saat pembukaan candle ke 2 volume candle turun 20 point Dan candle pertama harus dlm kondisi turun.

    saya pakai TakingProfit = 200 point
    StopLoss = 50 Point
    dan TrailingStop = 60 Point


    tetapi kenapa EA ini entry posisi di candle pertama bukan candle ke 2, Pak?
    dan trailing stopnya ngga berfungsi dengan sempurna,
    dalam artian ngga mau langsung pindah ke harga BEP.

    mohon pencerahannya....!!!!
     
  2. tabman

    tabman
    Expand Collapse
    New Member

    Joined:
    May 24, 2012
    Messages:
    2
    Likes Received:
    0
    Minta tulung, cara magic number gmn ya?
    Misale, mo cek order ataupun mo close order berdasrakan magic
    di contoh ea tsb, masih diberikan ordersend dnegan menambahkan magic number.
    misalnya kita mo close, tp kan harus milih magic number tertentu
    jangan sampe, yg nomor magic lain, ter close
    lha, itu gmn ya?
    Trims
     
  3. bian

    bian
    Expand Collapse
    New Member

    Joined:
    Feb 22, 2012
    Messages:
    12
    Likes Received:
    0
    Salam Masta...minta tulung. mengapa Ea ini tidak mau op di real acc...

    extern double Lots = 1.0;
    extern int Slippage = 3,
    magicNumber = 429191,
    stopLoss = 30,
    takeProfit = 30,
    trailingStop = 15,
    pipsLimit=15,
    pipsStop=20;
    extern bool ITradedOnThisBar;

    extern int startTrade = 0,
    endTrade = 23;

    extern double point,
    step=0.03,
    maks=0.2;

    extern int periode1=2,
    periode2=4,
    ratio = 1;

    extern int scaleX=20,
    scaleY=20,
    offsetX=35,
    offsetY=20,
    fontSize=20,
    corner=1,
    symbolCodeBuy=217,
    symbolCodeSell=218,
    symbolCodeNoSignal=73;

    extern color signalBuyColor=YellowGreen,
    signalSellColor=Tomato,
    noSignalColor=DarkGray,
    textColor=Gold;

    int period[] = {1,5,15,30,60,240,1440,10080,43200};
    string periodString[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
    string signalNameString[]={"MA","KLR","SAR"};
    string cpRight = "copyright (c)2013, Bhakti Budiman Novanda S.T., S.Kom.";
    //+------------------------------------------------------------------+
    //| expert initialization function |
    //+------------------------------------------------------------------+
    int init()
    {
    //----
    //sebenernya gak pake jga gak apa sih. LOL
    if(Digits==3 || Digits==5){
    point=Point*ratio; //Perbesar nilai
    }


    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert deinitialization function |
    //+------------------------------------------------------------------+
    int deinit()
    {
    //----

    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert start function |
    //+------------------------------------------------------------------+
    int start()
    {
    //----


    double customIndi, customIndi2, sarIndi, ma1now, ma1before, ma2now, ma2before, priceCandle;
    bool cust_Cond1, cust_Cond2, cust2_Cond1, cust2_Cond2, ma_Cond1, ma_Cond2, sar_Cond1, sar_Cond2, buy_Cond, sell_Cond;

    //cek jadwal trading.
    if((Hour()>=startTrade) && (Hour()<=endTrade) && (DayOfWeek()==6 || DayOfWeek()==0)){
    bool tradeTime = true;
    ObjectSetText("hariLabel", "EA: I am Working...",8,"Tahoma", textColor);
    }else{
    tradeTime = false;
    ObjectSetText("hariLabel", "EA: Market Closed!, Time to take a Rest..",8,"Tahoma", textColor);
    }

    //Inisialisasi indikator yang dipakai
    priceCandle = iClose(NULL,0,0);

    //indikator super trend
    customIndi = iCustom(NULL,0,"Kolier_SuperTrend_Indi",0,0);

    //indikator Laguerre
    //customIndi2 = iCustom(NULL,0,"Laguerre-ACS1",0,0);

    //indikator Parabolic SAR
    sarIndi = iSAR(NULL,0,step,maks,0);

    //Moving Average period 2/4
    ma1now = iMA(NULL,0,periode1,0,MODE_SMA,PRICE_CLOSE,0);
    ma2now = iMA(NULL,0,periode2,0,MODE_SMA,PRICE_CLOSE,0);

    ma1before = iMA(NULL,0,periode1,0,MODE_SMA,PRICE_CLOSE,1);
    ma2before = iMA(NULL,0,periode2,0,MODE_SMA,PRICE_CLOSE,1);


    //menentukan kondisi kriteria buy/sell yang akan digunakan
    //Kolier super trend kriteria
    cust_Cond1 = customIndi < priceCandle; //buy
    cust_Cond2 = customIndi > priceCandle; //sell

    //Laguerre kriteria
    //cust2_Cond1 = customIndi2 > 0.45; //buy
    //cust2_Cond2 = customIndi2 < 0.45; //sell

    //Moving average kriteria
    ma_Cond1 = ((ma2now < ma1now) && (ma2before > ma1before)) || ((ma2now < ma1now) && (ma2before < ma1before)); //buy
    ma_Cond2 = ((ma2now > ma1now) && (ma2before < ma1before)) || ((ma2now > ma1now)&&(ma2before > ma1before)); //sell


    //SAR Kriteria
    sar_Cond1 = sarIndi < priceCandle; //buy
    sar_Cond2 = sarIndi > priceCandle; //sell

    //Kondisi Buy/Sell berdasarkan 3 indikator
    //Jika ingin menggunakan indikator Kolier saja, tinggal di hapus bagian : && (ma_Cond1 == true) && (sar_Cond1 == true)
    //Begitu juga kondisi sell nya.
    buy_Cond = (cust_Cond1 == true);
    sell_Cond = (cust_Cond2 == true);

    //check order lalu pasang Buy/Sell jika salah satu kondisi terpenuhi
    //Saya lagi coba" coding pake buyLimit, kalo gak mau pake pending order tinggal UNCOMMENT buy() dan sell(),
    //kemudian jangan lupa COMMENT buyLimit() dan sellLimit() nya.
    if(OrdersTotal()<1){
    if(buy_Cond == true)
    {
    buy();
    //sellStop();
    //sellLimit();
    //buyLimit();
    //buyStop();
    }else if(sell_Cond == true)
    {
    sell();
    //sellStop();
    //sellLimit();
    //buyLimit();
    //buyStop();
    }else{
    //do nothing laah

    }
    }

    //seperti biasa loop order yang ada untuk pasang TS atau Closing order
    for(int cnt=0; cnt<OrdersTotal();cnt++){
    OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES);
    if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()){
    if(OrderType()==OP_BUY){
    if(sell_Cond==true){
    close_Buy();
    return(0);
    }else{
    //close_Buy();
    return(0);
    }
    /*
    if(trailingStop>0){
    if(Bid-OrderOpenPrice()>Point*trailingStop){
    if(OrderStopLoss()<Bid-Point*trailingStop){
    modTrailBuy();
    return(0);
    }
    }
    }
    */
    }
    else{
    if(buy_Cond==true){
    close_Sell();
    return(0);
    }else{
    //close_Sell();
    return(0);
    }
    /*
    if(trailingStop>0){
    if((OrderOpenPrice()-Ask)>(Point-trailingStop)){
    if((OrderStopLoss()>(Ask+Point*trailingStop)) || (OrderStopLoss()==0)){
    modTrailSell();
    return(0);
    }
    }
    }
    */
    }
    }
    }





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

    /*
    MY FUNCTIONS
    */

    //SL BUy Bid-stopLoss*point
    //SL Sell Ask+stopLoss*point
    //TP BUY Ask+takeProfit*Point
    //TP Sell Bid-takeProfit*Point
    //Imidiately Order
    void buy()
    {
    OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Order Buy", magicNumber, 0, Purple);
    }
    void sell()
    {
    OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Order Sell", magicNumber, 0, Red);
    }
    //---> End of Imidiately Order

    //Ini Perangkap Running Pricenya.
    //Pending Order Method
    void buyLimit()
    {
    OrderSend(Symbol(), OP_BUYLIMIT,Lots,Ask - (Point*pipsLimit),Slippage,0,Ask+takeProfit*Point,"Order Buy Limit", magicNumber,0,Green);
    }

    void sellLimit()
    {
    OrderSend(Symbol(), OP_SELLLIMIT,Lots,Bid+(Point*pipsLimit),Slippage,0,Bid-takeProfit*Point,"Order Sell Limit", magicNumber, 0, Blue);
    }

    void buyStop()
    {
    OrderSend(Symbol(), OP_BUYSTOP,Lots, Ask + (Point*pipsStop), Slippage,0,Ask+takeProfit*Point,"Order Buy Stop", magicNumber,0,Green);
    }

    void sellStop()
    {
    OrderSend(Symbol(), OP_SELLSTOP,Lots, Bid - (Point*pipsStop), Slippage,0,Bid-takeProfit*Point,"Order Sell Stop", magicNumber, 0, Blue);
    }
    //--> End of Pending Order Method

    void close_Buy()
    {
    OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);
    }
    void close_Sell()
    {
    OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet);
    }
    void modTrailSell()
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+point*trailingStop,OrderTakeProfit(),0,Red);
    }
    void modTrailBuy()
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-point*trailingStop,OrderTakeProfit(),0,Blue);
    }
     
  4. bian

    bian
    Expand Collapse
    New Member

    Joined:
    Feb 22, 2012
    Messages:
    12
    Likes Received:
    0
    Minta tolong bro

    Salam Masta...minta tulung. mengapa Ea ini tidak mau op di real acc...

    extern double Lots = 1.0;
    extern int Slippage = 3,
    magicNumber = 429191,
    stopLoss = 30,
    takeProfit = 30,
    trailingStop = 15,
    pipsLimit=15,
    pipsStop=20;
    extern bool ITradedOnThisBar;

    extern int startTrade = 0,
    endTrade = 23;

    extern double point,
    step=0.03,
    maks=0.2;

    extern int periode1=2,
    periode2=4,
    ratio = 1;

    extern int scaleX=20,
    scaleY=20,
    offsetX=35,
    offsetY=20,
    fontSize=20,
    corner=1,
    symbolCodeBuy=217,
    symbolCodeSell=218,
    symbolCodeNoSignal=73;

    extern color signalBuyColor=YellowGreen,
    signalSellColor=Tomato,
    noSignalColor=DarkGray,
    textColor=Gold;

    int period[] = {1,5,15,30,60,240,1440,10080,43200};
    string periodString[] = {"M1", "M5", "M15", "M30", "H1", "H4", "D1", "W1", "MN"};
    string signalNameString[]={"MA","KLR","SAR"};
    string cpRight = "copyright (c)2013, Bhakti Budiman Novanda S.T., S.Kom.";
    //+------------------------------------------------------------------+
    //| expert initialization function |
    //+------------------------------------------------------------------+
    int init()
    {
    //----
    //sebenernya gak pake jga gak apa sih. LOL
    if(Digits==3 || Digits==5){
    point=Point*ratio; //Perbesar nilai
    }


    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert deinitialization function |
    //+------------------------------------------------------------------+
    int deinit()
    {
    //----

    //----
    return(0);
    }
    //+------------------------------------------------------------------+
    //| expert start function |
    //+------------------------------------------------------------------+
    int start()
    {
    //----


    double customIndi, customIndi2, sarIndi, ma1now, ma1before, ma2now, ma2before, priceCandle;
    bool cust_Cond1, cust_Cond2, cust2_Cond1, cust2_Cond2, ma_Cond1, ma_Cond2, sar_Cond1, sar_Cond2, buy_Cond, sell_Cond;

    //cek jadwal trading.
    if((Hour()>=startTrade) && (Hour()<=endTrade) && (DayOfWeek()==6 || DayOfWeek()==0)){
    bool tradeTime = true;
    ObjectSetText("hariLabel", "EA: I am Working...",8,"Tahoma", textColor);
    }else{
    tradeTime = false;
    ObjectSetText("hariLabel", "EA: Market Closed!, Time to take a Rest..",8,"Tahoma", textColor);
    }

    //Inisialisasi indikator yang dipakai
    priceCandle = iClose(NULL,0,0);

    //indikator super trend
    customIndi = iCustom(NULL,0,"Kolier_SuperTrend_Indi",0,0);

    //indikator Laguerre
    //customIndi2 = iCustom(NULL,0,"Laguerre-ACS1",0,0);

    //indikator Parabolic SAR
    sarIndi = iSAR(NULL,0,step,maks,0);

    //Moving Average period 2/4
    ma1now = iMA(NULL,0,periode1,0,MODE_SMA,PRICE_CLOSE,0);
    ma2now = iMA(NULL,0,periode2,0,MODE_SMA,PRICE_CLOSE,0);

    ma1before = iMA(NULL,0,periode1,0,MODE_SMA,PRICE_CLOSE,1);
    ma2before = iMA(NULL,0,periode2,0,MODE_SMA,PRICE_CLOSE,1);


    //menentukan kondisi kriteria buy/sell yang akan digunakan
    //Kolier super trend kriteria
    cust_Cond1 = customIndi < priceCandle; //buy
    cust_Cond2 = customIndi > priceCandle; //sell

    //Laguerre kriteria
    //cust2_Cond1 = customIndi2 > 0.45; //buy
    //cust2_Cond2 = customIndi2 < 0.45; //sell

    //Moving average kriteria
    ma_Cond1 = ((ma2now < ma1now) && (ma2before > ma1before)) || ((ma2now < ma1now) && (ma2before < ma1before)); //buy
    ma_Cond2 = ((ma2now > ma1now) && (ma2before < ma1before)) || ((ma2now > ma1now)&&(ma2before > ma1before)); //sell


    //SAR Kriteria
    sar_Cond1 = sarIndi < priceCandle; //buy
    sar_Cond2 = sarIndi > priceCandle; //sell

    //Kondisi Buy/Sell berdasarkan 3 indikator
    //Jika ingin menggunakan indikator Kolier saja, tinggal di hapus bagian : && (ma_Cond1 == true) && (sar_Cond1 == true)
    //Begitu juga kondisi sell nya.
    buy_Cond = (cust_Cond1 == true);
    sell_Cond = (cust_Cond2 == true);

    //check order lalu pasang Buy/Sell jika salah satu kondisi terpenuhi
    //Saya lagi coba" coding pake buyLimit, kalo gak mau pake pending order tinggal UNCOMMENT buy() dan sell(),
    //kemudian jangan lupa COMMENT buyLimit() dan sellLimit() nya.
    if(OrdersTotal()<1){
    if(buy_Cond == true)
    {
    buy();
    //sellStop();
    //sellLimit();
    //buyLimit();
    //buyStop();
    }else if(sell_Cond == true)
    {
    sell();
    //sellStop();
    //sellLimit();
    //buyLimit();
    //buyStop();
    }else{
    //do nothing laah

    }
    }

    //seperti biasa loop order yang ada untuk pasang TS atau Closing order
    for(int cnt=0; cnt<OrdersTotal();cnt++){
    OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES);
    if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()){
    if(OrderType()==OP_BUY){
    if(sell_Cond==true){
    close_Buy();
    return(0);
    }else{
    //close_Buy();
    return(0);
    }
    /*
    if(trailingStop>0){
    if(Bid-OrderOpenPrice()>Point*trailingStop){
    if(OrderStopLoss()<Bid-Point*trailingStop){
    modTrailBuy();
    return(0);
    }
    }
    }
    */
    }
    else{
    if(buy_Cond==true){
    close_Sell();
    return(0);
    }else{
    //close_Sell();
    return(0);
    }
    /*
    if(trailingStop>0){
    if((OrderOpenPrice()-Ask)>(Point-trailingStop)){
    if((OrderStopLoss()>(Ask+Point*trailingStop)) || (OrderStopLoss()==0)){
    modTrailSell();
    return(0);
    }
    }
    }
    */
    }
    }
    }





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

    /*
    MY FUNCTIONS
    */

    //SL BUy Bid-stopLoss*point
    //SL Sell Ask+stopLoss*point
    //TP BUY Ask+takeProfit*Point
    //TP Sell Bid-takeProfit*Point
    //Imidiately Order
    void buy()
    {
    OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Order Buy", magicNumber, 0, Purple);
    }
    void sell()
    {
    OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Order Sell", magicNumber, 0, Red);
    }
    //---> End of Imidiately Order

    //Ini Perangkap Running Pricenya.
    //Pending Order Method
    void buyLimit()
    {
    OrderSend(Symbol(), OP_BUYLIMIT,Lots,Ask - (Point*pipsLimit),Slippage,0,Ask+takeProfit*Point,"Order Buy Limit", magicNumber,0,Green);
    }

    void sellLimit()
    {
    OrderSend(Symbol(), OP_SELLLIMIT,Lots,Bid+(Point*pipsLimit),Slippage,0,Bid-takeProfit*Point,"Order Sell Limit", magicNumber, 0, Blue);
    }

    void buyStop()
    {
    OrderSend(Symbol(), OP_BUYSTOP,Lots, Ask + (Point*pipsStop), Slippage,0,Ask+takeProfit*Point,"Order Buy Stop", magicNumber,0,Green);
    }

    void sellStop()
    {
    OrderSend(Symbol(), OP_SELLSTOP,Lots, Bid - (Point*pipsStop), Slippage,0,Bid-takeProfit*Point,"Order Sell Stop", magicNumber, 0, Blue);
    }
    //--> End of Pending Order Method

    void close_Buy()
    {
    OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);
    }
    void close_Sell()
    {
    OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet);
    }
    void modTrailSell()
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+point*trailingStop,OrderTakeProfit(),0,Red);
    }
    void modTrailBuy()
    {
    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-point*trailingStop,OrderTakeProfit(),0,Blue);
    }
     
  5. bian

    bian
    Expand Collapse
    New Member

    Joined:
    Feb 22, 2012
    Messages:
    12
    Likes Received:
    0
    Salam bro.. bagaimana menulis code ea jika saya mahu Op Buy MA4 +MA8 bersilang di atas MA50. Manakala Op sell MA4+ MA8 di bawah MA50..kalau boleh ditambah trailing....terima kasih
     

Share This Page