I need help with a code - page 2

 
deVries:


To me it is still looking you know nothing about coding

and you do nothing to get know something about coding

what attempt have you done making this ??

If you want help show your attempt making it

There is still nothing to help you with ...

Yes. It is totally new to me. Is there a book you can refer me to?
 
diamstar:
Yes. It is totally new to me. Is there a book you can refer me to?

Book in One File
Download it - 2 Mb
 
deVries:

Book in One File
Download it - 2 Mb
Thank you. I will write up something after finishing the book, no matter how bad.
 
diamstar:
30mins


Here is something that may help to get you started.

And about that ancient Chinese thing, unless you already speak Chinese stick with mql. It's easier, you can get more help with it and if you ever have a bright idea, mql is going to help you make a ton of money. It is also fun.

So here we go :

//+------------------------------------------------------------------+
//|                                               News_Trader_v1.mq4 |
//|                                            Copyright © 2013 _3DE |
//|                                        https://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013 _3DE"
#property link      "https://www.metaquotes.net"
extern string  Note1       ="Set parameters in Pips not points !";
extern string  Note2       ="Set day of the month for the news !";
extern string  Note3       ="Set to zero to trade every day    !";
extern string  Note4       ="Leave SetDistance to zero if trading news !";
extern int     TakeProfit  =25;// Take profit pips
extern int     StopLoss    =20;// Stop loss pips (manual trading)
extern int     SetDistance = 0;// Sistance for BuyStop and SellStop from price at news time
extern int     DayOfNews   = 0;// Day of the month of news
extern int     NewsHour    = 0;// Hour of news
extern int     NewsMin     = 0;// Minute of news
extern int     Expiration  = 5;// Expiration of pending orderes
extern int     BEPips      =11;// Move to break even after BEPips
extern int     TrailingStop= 0;// What distance to keep trailing
extern int     Slip        = 5;// Slippage
extern int     MagicNumber=2210;// Must be unique for every chart
extern double  Lots        =0.1;
extern bool    WriteLog=false;// Write a log file 
extern string  TradeLog    ="MI_Log";
input  string  EaComment   ="NewsTrader_EA";

double h,l,ho,lo,hso,lso,htp,ltp,sp,price;

string filename;
int pointMultiply=10;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   if(Digits==3 || Digits==5)
     {
      pointMultiply  *=10;
      TakeProfit     *=10;
      StopLoss       *=10;
      BEPips         *=10;
      TrailingStop   *=10;
      SetDistance    *=10;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   int OrdersCondition,minofday,minofnews;

   filename=Symbol()+TradeLog+"-"+Month()+"-"+Day()+".txt";

   if(BEPips>0) DoBE(BEPips);

   if(TrailingStop>0) DoTrail();

   OrdersCondition=CheckOrdersCondition();
   if(Day()==DayOfNews || DayOfNews==0)
     {
      minofday=Hour()*60+Minute();
      minofnews=NewsHour*60+NewsMin;

      if((minofday==minofnews-2) || (minofday==minofnews-1))
        {
         h=iHigh(NULL,PERIOD_M1,0);
         l=iLow(NULL,PERIOD_M1,0);
         for(i=1;i<=3;i++) if(iHigh(NULL,PERIOD_M1,i)>h) h=iHigh(NULL,PERIOD_M1,i);
         for(i=1;i<=3;i++) if(iLow(NULL,PERIOD_M1,i)<l) l=iLow(NULL,PERIOD_M1,i);
         sp=Ask-Bid;
         ho=h+sp+(pointMultiply)*Point;
         lo=l-(pointMultiply)*Point;
         hso=h+sp;
         lso=l;
         htp=ho+TakeProfit*Point-sp;
         ltp=lo-TakeProfit*Point+sp;
         //---
         ho=NormalizeDouble(ho,Digits);
         lo=NormalizeDouble(lo,Digits);
         hso=NormalizeDouble(hso,Digits);
         lso=NormalizeDouble(lso,Digits);
         htp=NormalizeDouble(htp,Digits);
         ltp=NormalizeDouble(ltp,Digits);
         if(SetDistance!=0)
           {
            price=(Ask+Bid)/2;
            h=price+SetDistance*Point;
            l=price-SetDistance*Point;
            ho=NormalizeDouble(h+sp,Digits);
            lo=NormalizeDouble(l-sp,Digits);
            hso=NormalizeDouble(ho-StopLoss*Point,Digits);
            htp=NormalizeDouble(ho+TakeProfit*Point,Digits);
            lso=NormalizeDouble(lo+StopLoss*Point,Digits);
            ltp=NormalizeDouble(lo-TakeProfit*Point,Digits);
           }
         if(OrdersCondition==0)
           {
            if(WriteLog)Write("Opening BuyStop & SellStop, OrdersCondition="+OrdersCondition+" MinOfDay="+minofday);
            OpenBuyStop();
            OpenSellStop();
           }

         if(OrdersCondition==10)
           {
            if(WriteLog)Write("Opening SellStop, OrdersCondition="+OrdersCondition+" MinOfDay="+minofday);
            OpenSellStop();
           }

         if(OrdersCondition==1)
           {
            if(WriteLog)Write("Opening BuyStop , OrdersCondition="+OrdersCondition+" MinOfDay="+minofday);
            OpenBuyStop();
           }
        }
     }
   if((minofday>=minofnews) && (minofday<=minofnews+Expiration-1))
     {

      if(OrdersCondition==1001)
        {
         if(WriteLog)Write("Deleting SellStop Because of BuyStop Hit, OrdersCondition="+OrdersCondition+" MinOfDay="+minofday);
         DeleteSellStop();
        }

      if(OrdersCondition==110)
        {
         if(WriteLog)Write("Deleting BuyStop Because of SellStop Hit, OrdersCondition="+OrdersCondition+" MinOfDay="+minofday);
         DeleteBuyStop();
        }
     }

   if(minofday>=minofnews+Expiration)
     {
      if(OrdersCondition==11)
        {
         if(WriteLog)Write("Deleting BuyStop and SellStop Because 5 min expired, OrdersCondition="+OrdersCondition+" MinOfDay="+minofday);
         DeleteBuyStop();
         DeleteSellStop();
        }

      if((OrdersCondition==10) || (OrdersCondition==110))
        {
         if(WriteLog)Write("Deleting BuyStop Because 5 min expired, OrdersCondition="+OrdersCondition+" MinOfDay="+minofday);
         DeleteBuyStop();
        }

      if((OrdersCondition==1) || (OrdersCondition==1001))
        {
         if(WriteLog)Write("Deleting SellStop Because 5 min expired, OrdersCondition="+OrdersCondition+" MinOfDay="+minofday);
         DeleteSellStop();
        }
     }

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| int CheckOrdersCondition()                                       |
//+------------------------------------------------------------------+

int CheckOrdersCondition()
  {
   int result=0;
   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if((OrderType()==OP_BUY) && (OrderSymbol()==Symbol()) && (OrderMagicNumber()==MagicNumber))
        {
         result+=1000;
        }
      if((OrderType()==OP_SELL) && (OrderSymbol()==Symbol()) && (OrderMagicNumber()==MagicNumber))
        {
         result+=100;
        }
      if((OrderType()==OP_BUYSTOP) && (OrderSymbol()==Symbol()) && (OrderMagicNumber()==MagicNumber))
        {
         result+=10;
        }
      if((OrderType()==OP_SELLSTOP) && (OrderSymbol()==Symbol()) && (OrderMagicNumber()==MagicNumber))
        {
         result+=1;
        }

     }
   return(result); // 0 means we have no trades
  }
// OrdersCondition Result Pattern
//    1    1    1    1
//    b    s    bs   ss
//  
//+------------------------------------------------------------------+
//|void OpenBuyStop()                                                |
//+------------------------------------------------------------------+

void OpenBuyStop()
  {
   int ticket,tries;
   tries=0;
   if(!GlobalVariableCheck("InTrade"))
     {
      while(tries<3)
        {
         GlobalVariableSet("InTrade",TimeCurrent());  // set lock indicator
         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,ho,Slip,hso,htp,EaComment,MagicNumber,0,Red);
         if(WriteLog)Write("in function OpenBuyStop OrderSend Executed , ticket ="+ticket);
         GlobalVariableDel("InTrade");   // clear lock indicator
         if(ticket<=0)
           {
            tries++;
           }
         else tries=3;
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OpenSellStop()
  {
   int ticket,tries;
   tries=0;
   if(!GlobalVariableCheck("InTrade"))
     {
      while(tries<3)
        {
         GlobalVariableSet("InTrade",CurTime());  // set lock indicator
         ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,lo,Slip,lso,ltp,EaComment,MagicNumber,0,Red);
         if(WriteLog)Write("in function OpenSellStop OrderSend Executed , ticket ="+ticket);
         GlobalVariableDel("InTrade");   // clear lock indicator
         if(ticket<=0)
           {
            tries++;
           }
         else tries=3;
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DoBE(int byPips)
  {
   for(int i=0; i<OrdersTotal(); i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber)) // only look if mygrid and symbol...
        {
         if(OrderType()==OP_BUY) if(Bid-OrderOpenPrice()>byPips*Point) if(OrderStopLoss()<OrderOpenPrice())
           {
            if(WriteLog)Write("Movine StopLoss of Buy Order to BE+1");
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Point,OrderTakeProfit(),Red);
           }
         if(OrderType()==OP_SELL) if(OrderOpenPrice()-Ask>byPips*Point) if(OrderStopLoss()>OrderOpenPrice())
           {
            if(WriteLog)Write("Movine StopLoss of Buy Order to BE+1");
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Point,OrderTakeProfit(),Red);
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DoTrail()
  {
   for(int i=0; i<OrdersTotal(); i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber)) // only look if mygrid and symbol...
        {

         if(OrderType()==OP_BUY)
           {
            if(Bid-OrderOpenPrice()>Point*TrailingStop)
              {
               if(OrderStopLoss()<Bid-Point*TrailingStop)
                 {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                  return;
                 }
              }
           }

         if(OrderType()==OP_SELL)
           {
            if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
              {
               if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                 {
                  OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                  return;
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteBuyStop()
  {
   for(int i=0; i<OrdersTotal(); i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber) && (OrderType()==OP_BUYSTOP))
        {
         OrderDelete(OrderTicket());
         if(WriteLog)Write("in function DeleteBuyStopOrderDelete Executed");
        }

     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteSellStop()
  {
   for(int i=0; i<OrdersTotal(); i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber) && (OrderType()==OP_SELLSTOP))
        {
         OrderDelete(OrderTicket());
         if(WriteLog)Write("in function DeleteSellStopOrderDelete Executed");
        }

     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Write(string str)
  {
   int handle;

   handle=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV,"/t");
   FileSeek(handle,0,SEEK_END);
   FileWrite(handle,str+" Time "+TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS));
   FileClose(handle);
  }
//+------------------------------------------------------------------+


I have added a few bits and pieces of code to make it suitable for news trading and manual entry . It will probably suit your purpose .

This EA was originally intended to trade the news so you input the news day of the month, hour and minute, take profit in pips, Expiration in minute - how many minutes after the news to close the pending orders if the move didn't happen, BEPips - if one order gets triggered and wast to move to break even, after how many pips in profit. If If trailing stop = 0 there will be no trailing. If you set the trailing, set the number of pips to keep away from the price if in profit.

For manual trading, you can leave the DayOfNews to zero so EA will trade every day at the specified hour and minute or you can manually set the day every day when you trade.

Also you need to set StopLoss to the amount of pips (not points) you desire.

Set the SteDistance to whatever distance from the price at the specified time you want the pending orders (30 pips I believe you said).

Set Expiration in minutes to how many minutes you want the orders to be active before EA will delete them if not filled.

Anyway, have a look trough the code and if you have any questions, post them here or pm me.

 
diamstar:
Thank you. I will write up something after finishing the book, no matter how bad.


That's the spirit :)

If you try, you will find people that will help you

 
diamstar:
Thank you. I will write up something after finishing the book, no matter how bad.


diamstar, learning is fun if you get the right help . Don't fall for it and believe that if it looks like ancient Chinese at first it's not worth it.

Also, don't be discouraged by those who don't really want to help. There's plenty help available, you only need to find it.

Her's a piece of advice for you if you want to learn :

1.Keep in touch with people who are willing to share (knowledge, resources, code, etc). You'll need them

2. Read the documentation and ask questions so you have a good understanding of what to read. You don't need to memorize everything, just to know what's available and where to find it.

3.Don't over kill yourself trying to write every bit of code yourself. It's like reinventing the wheel.

4.Get your hands on any free EA's you can download and go trough the code. Understand how it's structured, understand what each function does and then study how it does it.

5. USE code and functions that work and you like in your code, you can copy code in any text editor or as scripts in MetaEditor and save it under a meaningful name for future or just make notes of what you like in what EA so you can find it when you need it.

Hope it helps.

Good luck

 
thrdel:


Here is something that may help to get you started.

And about that ancient Chinese thing, unless you already speak Chinese stick with mql. It's easier, you can get more help with it and if you ever have a bright idea, mql is going to help you make a ton of money. It is also fun.

So here we go :


I have added a few bits and pieces of code to make it suitable for news trading and manual entry . It will probably suit your purpose .

This EA was originally intended to trade the news so you input the news day of the month, hour and minute, take profit in pips, Expiration in minute - how many minutes after the news to close the pending orders if the move didn't happen, BEPips - if one order gets triggered and wast to move to break even, after how many pips in profit. If If trailing stop = 0 there will be no trailing. If you set the trailing, set the number of pips to keep away from the price if in profit.

For manual trading, you can leave the DayOfNews to zero so EA will trade every day at the specified hour and minute or you can manually set the day every day when you trade.

Also you need to set StopLoss to the amount of pips (not points) you desire.

Set the SteDistance to whatever distance from the price at the specified time you want the pending orders (30 pips I believe you said).

Set Expiration in minutes to how many minutes you want the orders to be active before EA will delete them if not filled.

Anyway, have a look trough the code and if you have any questions, post them here or pm me.


I think that the OP is a newbie programmer. I doubt that he/she will understand your code.

Would you have been able to understand your code when you were first starting to learn?

 
GumRai:


I think that the OP is a newbie programmer. I doubt that he/she will understand your code.

Would you have been able to understand your code when you were first starting to learn?

Am really grateful. I will try out the code and still read the book so I can understand every bit of it. I will update you on any issue I come up with. Thanks
 
GumRai:


I think that the OP is a newbie programmer. I doubt that he/she will understand your code.

Would you have been able to understand your code when you were first starting to learn?


Well GumRay, the code is there, documentation for mql is available, the code has some explanations and we are here to help. As I said, " you have any questions, post them here or pm me".

That doesn't mean I have all the answers, just that I'm willing to help if diamstar is willing to learn.

" I doubt that he/she will understand your code." for now or not entirely . Also the code may not be 100% what he/she is looking for (I did try my best given the info available) but it is a good start.

You know how much easier it is to learn with practical examples, right?

I'm aware that there's so much info in that EA for a newbie and that there may be many questions asked about it but that's why we're here, aren't we?

Sure I wasn't able to understand all of that when I was learning (I'm still learning) at least not all of it the first time but diamstar managed to find the right forum to ask for help and seems determined to learn .

Let's wish him/her all the best and help out if we can.

 
thrdel:


Well GumRay, the code is there, documentation for mql is available, the code has some explanations and we are here to help. As I said, " you have any questions, post them here or pm me".

That doesn't mean I have all the answers, just that I'm willing to help if diamstar is willing to learn.

" I doubt that he/she will understand your code." for now or not entirely . Also the code may not be 100% what he/she is looking for (I did try my best given the info available) but it is a good start.

You know how much easier it is to learn with practical examples, right?

I'm aware that there's so much info in that EA for a newbie and that there may be many questions asked about it but that's why we're here, aren't we?

Sure I wasn't able to understand all of that when I was learning (I'm still learning) at least not all of it the first time but diamstar managed to find the right forum to ask for help and seems determined to learn .

Let's wish him/her all the best and help out if we can.

I truly appreciate all assistance rendered. No matter how complex d code is, It is great help which I might not have come up with in months and the books am reading makes d understanding relatively easy. U learn to read n understand b4 writing something meaningful.
Reason: