adventures of a newbie - page 4

 

Hello legends of mql. I cooked up the pseudo code for you during my lunch hour at work. Here it is. let me know what's missing and i'l add it on.

N&P Pseudo Code
With this strategy we want to have 1 code, be able to attach it to any chart, and it would run on 5 currencies, entering both short and long positions on each one if the rules meet.
---EURUSD---
If ema7>ema14>sma50 and PriceNow is < TopFilterLevel (eg:1.3080 for eurusd. This I will adjust on a daily basis) then:
Buy EURUSD Lots(lots to be external variable, eg 0.01). Else: (ie. conditions not met) don't buy.

If ema7<ema14<sma50 and PriceNow is > BottomFilterLevel (eg: 1.1508 for eurusd) then:
Short EURUSD Lots (agian, external variable). Else (ie. conditions not met don't short).

If BUY position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD.
If SHORT position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD.
If 20 pips are not reached (either buy or sell) then keep being in the market until the position is closed

manually. (ideall we put this into OrderSend function to keep code shorter).

----GBPUSD---
exactly the same code as above.

----USDJPY---
SAME AS ABOVE

---USDCHF---
SAME AS ABOVE

---AUDUSD---
SAME AS ABOVE

**Code specifics
both buy and short trades can be executed simultaneously for any given currency if the criteria stated above meet
***no stoploss is needed for this code

**no money management code for this one

So how does this look?

 
niko:

Hello legends of mql. I cooked up the pseudo code for you during my lunch hour at work. Here it is. let me know what's missing and i'l add it on.

N&P Pseudo Code
With this strategy we want to have 1 code, be able to attach it to any chart, and it would run on 5 currencies, entering both short and long positions on each one if the rules meet.
---EURUSD---
If ema7>ema14>sma50 and PriceNow is < TopFilterLevel (eg:1.3080 for eurusd. This I will adjust on a daily basis) then:
Buy EURUSD Lots(lots to be external variable, eg 0.01). Else: (ie. conditions not met) don't buy.

If ema7<ema14<sma50 and PriceNow is > BottomFilterLevel (eg: 1.1508 for eurusd) then:
Short EURUSD Lots (agian, external variable). Else (ie. conditions not met don't short).

If BUY position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD.
If SHORT position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD.
If 20 pips are not reached (either buy or sell) then keep being in the market until the position is closed

manually. (ideall we put this into OrderSend function to keep code shorter).

----GBPUSD---
exactly the same code as above.

----USDJPY---
SAME AS ABOVE

---USDCHF---
SAME AS ABOVE

---AUDUSD---
SAME AS ABOVE

**Code specifics
both buy and short trades can be executed simultaneously for any given currency if the criteria stated above meet
***no stoploss is needed for this code

**no money management code for this one

So how does this look?

Hey guys, the basic tutorial on coding really helped to get my head around this (although the detailed coding part is lacking). To show you I have been busy, I attached the code below. The idea i think is there but it still returns lots of errors. You will see everything is under one big bracket in Start() the reason I did that is because mql kept coming back with error that variable can't be declared globally, and when i made it all in 1 bracket it seemed to have stopped that error (although lots of other errors persisted). I used notepad++ to check the brackets align. So did am I at least going the right way with this?

//+------------------------------------------------------------------+
//|                                     N&P 1DailyUpTrendExec.mq4 |
//| Copyright Nick Lou & Pete Arh 2009                               |
//|                                     20090523                     |
//|                                                                  |
//+------------------------------------------------------------------+

extern double    Lots=0.01;
extern double    TakeProfit=20;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+


   //-------------------Declaring All Variables and Conditions


//--------------------declaration end


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


   // Order counting code
   // Return:  0 = no orders
   //          >0 = # long orders
   //          <0 = # short orders
      int CalcOpenOrders()
         {
         int long=0,short=0;
      for(int i=0;i<OrdersTotal();i++)   
         //i set to 0 to stop bugs from
         //crawling in, i<Orderstotal means if i is behind the number of orders
         //in market then start counting orders and make i that number
         {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
         //this function selects an order for further processing
         if(OrderType()==OP_BUY)long++;
         if(OrderType()==OP_SELL)short++;
         }
      if(long>0)return(long);
      if(short>0)return(-short);
      }

//------------ (fingers crossed this is right). I still don't get it why we need to count orders.


//--------------DECLARATION OF VARIABLES-------------------------
double ema1,ema2,ema3,closeup, e1over2, e2over3, e1under2, e2under3,
eurusdbuyok, eurusdshortok;

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start ()
   {
   ema1= iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0);
   ema2= iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
   ema3= iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
   e1under2=ema1<ema2;
   e2under3=ema2<ema3;
   e1over2=ema1>ema2;
   e2over3=ema2>ema3;

   if(Bars<75)
      {
      Print("Bars less than 100");
      return(0);
      }


   //------------------EURUSD Block-------------------------
   //check order type, if it doesn't equal to buy already then buy


   //here i am setting a condition which will be
   //checked before a trade is opened, it shoudl state 'It's okay to enter
   //into long trade so long as there is either 1 order in place already or
   //none)
  
   // Call function. the returnvalue (output) of the function is stored into ReturnVal
   int ReturnVal = CalcOpenOrders();
  
   // Check output of function
   if (ReturnVal >0)  // >0 = long orders. See CalcOpenOrders() 
      eurusdbuyok=1;
      else
      {
      eurusdbuyok=0;
      return(0);
      }

   if (ReturnVal<0)   // <0 = short orders. See CalcOpenOrders()
      eurusdshortok=1;
      else
      {
      eurusdshortok=0;
      return(0);
      }


   //--------------condition ends

   if(eurusdshortok==1)
      {
      static int ticket;
      // deleted if(OrdersTotal()==0)
      if(e1under2 && e2under3)     // short function
         {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"ShortOrder ",0,0,Red);
         if(ticket>0)
            {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("SHORT order opened : ",OrderOpenPrice());
            }
         }
      return(0);
      }



   //  -------------------------------------------------------------------------------------------
   if (eurusdbuyok==1)
      {
      static int ticket1;
      // deleted if(OrdersTotal()==0)
      if(e1over2 && e2over3 && eurusdbuyok==1) //buy function
         {
         ticket1=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green);
         //What's 12345 for? I ADDED ASk-30*Point for stop loss
         if(ticket1>0)
            {
            if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES))
            Print("BUY order opened : ",OrderOpenPrice());
            }
         }
      return(0);
      }

   return(0);
   } 
this is the latest version of the code. syntatically everthing seems to be ok, but when i run it through strategy tester it returns no trades. something is wrong with logic. need help. I'm becoming quite familiar with code now, well the structure of program side of it.
 
niko wrote >>

Hello legends of mql. I cooked up the pseudo code for you during my lunch hour at work. Here it is. let me know what's missing and i'l add it on.

N&P Pseudo Code
With this strategy we want to have 1 code, be able to attach it to any chart, and it would run on 5 currencies, entering both short and long positions on each one if the rules meet.
---EURUSD---
If ema7>ema14>sma50 and PriceNow is < TopFilterLevel (eg:1.3080 for eurusd. This I will adjust on a daily basis) then:
Buy EURUSD Lots(lots to be external variable, eg 0.01). Else: (ie. conditions not met) don't buy.

If ema7<ema14<sma50 and PriceNow is > BottomFilterLevel (eg: 1.1508 for eurusd) then:
Short EURUSD Lots (agian, external variable). Else (ie. conditions not met don't short).

If BUY position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD.
If SHORT position reaches 20 pips from point of entry then: TAKEPROFIT on EURUSD.
If 20 pips are not reached (either buy or sell) then keep being in the market until the position is closed

manually. (ideall we put this into OrderSend function to keep code shorter).

----GBPUSD---
exactly the same code as above.

----USDJPY---
SAME AS ABOVE

---USDCHF---
SAME AS ABOVE

---AUDUSD---
SAME AS ABOVE

**Code specifics
both buy and short trades can be executed simultaneously for any given currency if the criteria stated above meet
***no stoploss is needed for this code

**no money management code for this one

So how does this look?

Hi Niko

Your pseudo code looks pretty good for a first attempt.

I would however like to see it a bit more structured and there are a couple of questions that it raises for me.

I was able to easily and quickly put it into the sort of format that I would typically use myself. It is attached here as a text file for you to have a look at. You will need to save this file and open it in notepad or a similar editor to see the formatting.

Note that at this stage we are not writing in any specific computer language . We are just trying to specify clearly and unambiguously what we are trying to do.

You may notice that the pseudo code is quite specific and "legalistic". This is how we have to talk to computers. We need to spell out very clearly what it is we want them to do. Otherwise they have a tendency to generate garbage.

You will also notice the use of "Blocks" to arrange things into logical groups. These may be useful later on to help us structure the code properly. As someone pointed out earlier in this discussion, we dont do this just for fun. We do it to make the code more readible, more understandable, more maintainable and ultimately more reliable and bug free. The later is extremely important in software that is going to be used for live trading inless of course you have buckets of money that you are willing to throw away :) . Seriously, trading is difficult enough without also having to deal with buggy software.

Please have a look at what I have sent you and feel free to make any changes that you think are necessary. There are a couple of important points missing from my pseudo code. Can you spot them?

You may also like to deal with the two questions that I have raised in the pseudo code. A number of other questions come to mind but these can be better left until the actual coding phase.

If you have not already done so you might want to try out the the Notepad++ editor that FXtrader2008 mentioned earlier in this discussion. Use any tools that you can find that make life a bit easier. Please post your revised pseudo code back to me as a text file attachment. I find trying to write structured code, pseudo or otherwise, in this HTML editor a bit tedious and messy.

Regards

Tim

Files:
 
TSWilson:

Hi Niko

Your pseudo code looks pretty good for a first attempt.

I would however like to see it a bit more structured and there are a couple of questions that it raises for me.

I was able to easily and quickly put it into the sort of format that I would typically use myself. It is attached here as a text file for you to have a look at. You will need to save this file and open it in notepad or a similar editor to see the formatting.

Note that at this stage we are not writing in any specific computer language . We are just trying to specify clearly and unambiguously what we are trying to do.

You may notice that the pseudo code is quite specific and "legalistic". This is how we have to talk to computers. We need to spell out very clearly what it is we want them to do. Otherwise they have a tendency to generate garbage.

You will also notice the use of "Blocks" to arrange things into logical groups. These may be useful later on to help us structure the code properly. As someone pointed out earlier in this discussion, we dont do this just for fun. We do it to make the code more readible, more understandable, more maintainable and ultimately more reliable and bug free. The later is extremely important in software that is going to be used for live trading inless of course you have buckets of money that you are willing to throw away :) . Seriously, trading is difficult enough without also having to deal with buggy software.

Please have a look at what I have sent you and feel free to make any changes that you think are necessary. There are a couple of important points missing from my pseudo code. Can you spot them?

You may also like to deal with the two questions that I have raised in the pseudo code. A number of other questions come to mind but these can be better left until the actual coding phase.

If you have not already done so you might want to try out the the Notepad++ editor that FXtrader2008 mentioned earlier in this discussion. Use any tools that you can find that make life a bit easier. Please post your revised pseudo code back to me as a text file attachment. I find trying to write structured code, pseudo or otherwise, in this HTML editor a bit tedious and messy.

Regards

Tim

Hey TSW. Thank you for this. I can see you put quite a bit of time into this so I really appreciate it. I attached the updated pseudo code, but in case it didn't attach, here it is below. I tried to see what you missed out, but couldn't quite figure it out, a few things I added but they were just clarifications of things.


Where do we go from here? Whilst we are creating this 'proper' code, I am still trying to solve the puzzle of the code above. It doesn't follow our pseudo code as it is an old patched up code, but I am still quite puzzled with it as it should work, at least for 1 currency. Key thing is building it in this unorganised haphazard way is teaching me the actual coding elements (like how brackets affect the script, where to declare variables, etc). Any ideas on why that code doesn't work as it is now?

Program Title  - N&P Pseudo Code

START BLOCK - List of Currency Pairs

     EURUSD
     GBPUSD
     USDJPY
     USDCHF
     AUDUSD

END BLOCK - List Of Currency Pairs



START BLOCK - Configuration Parameters

      Set the value of TopFilterLevel manually on a daily basis for EURUSD
      Set the value of BottomFilterLevel manually on a daily basis EURUSD
      Set the value of TopFilterLevel manually on a daily basis for GBPUSD
      Set the value of BottomFilterLevel manually on a daily basis GBPUSD

      Set the value of TopFilterLevel manually on a daily basis for USDJPY
      Set the value of BottomFilterLevel manually on a daily basis USDJPY
      Set the value of TopFilterLevel manually on a daily basis for USDCHF
      Set the value of BottomFilterLevel manually on a daily basis USDCHF

      Set the value of TopFilterLevel manually on a daily basis for AUDUSD
      Set the value of BottomFilterLevel manually on a daily basis AUDUSD

      Set Lot size manually for All positions at once (same lot size)

END BLOCK - Configuration Parameters



START BLOCK  - Entry Rules

     If   the 7 Period Exponential Moving Average is greater than the 14 Period Exponential Moving Average  AND
          the 14 Period Exponential Moving Average is greater than the  50 period Simple Moving Average       AND
          Current Price   is less than TopFilterLevel  THEN
                       Signal a  BUY (Long) entry condition

     If   the  7 Period Exponential Moving Average is less than 14 Period Exponential Moving Average  AND
          the 14 Period Exponential Moving Average is less than the 50 period Simple Moving Average       AND
          Current Price   is less than  BottomFilterLevel  THEN
                       Signal a  SELL (Short) entry condition

     ***  Question ***
      What time periods do you want to use for your moving averages? Are the periods  Minutes, Hours, Days, Weeks, Months or  do they need to be variable?
***Answer *** Excellent question. It will be 5 minute periods for all moving averages, they do not need to be variable.

END BLOCK - Entry Rules
      


START BLOCK - Manual Close

      **** Question ***
      You say 
                  "Keep being in the market until the position is closed manually.
                   (ideall we put this into OrderSend function to keep code shorter)."
       
       What exactly do you want the program to do in this block?

***I was unclear on this before, my apologies. There should be no block as this in the code. I will exit positions manually when needed through the trade/terminal window (I assume that’s possible and won’t mess up the EA execution, right). So actually I don’t know if we need to code this flexibility or not? What do you think

END BLOCK - Manual Close  



START BLOCK  - Exit Rules

     If  any Open Position is greater than or equal to 20 pips from the entry level THEN
        Close the Position

END BLOCK  - Exit Rules



START BLOCK - Main  - This block controls the whole flow of the program
         With each Currency Pair in the List of Currency Pairs block do all of the following

                  Process the Exit Rules Block - 
                                    (Note that in practice this would normally just be the automatic execution of a Take Profit  Stop
                                    but we should mention it here in the pseudo code for completness. Of course you may have something else in mind
                                    such as using the program to close an open position—nope not for this code.)
                  Check the Entry Rules block  to see if a BUY or SELL  condition exists for this Currency Pair 

                  Check to see if  there is already  an open  BUY  position for this Currency Pair
                  Check to see if  there is already  an open SELL  position for this Currency Pair

                  If there is no open BUY position  for this currency pair   AND
                      a Buy condition exixts for this currency pair THEN
                              Open a BUY Position  for this currency pair
				Size: Pre-determined Lots

ELSE		If there is already 1 open BUY position for this currency pair		DO NOT OPEN BUY Position for this currency pair.

                   If there is no open SELL position  for this currency pair   AND
                      a SELL condition exixts for this currency pair THEN
                              Open a SELL Position  for this currency pair 
				Size predetermined lots.
  
ELSE		If there is already 1 sell position open for this currency pair		Do not open SELL Positions for this currency pair     
              
END BLOCK - Main 




PS: I will often use just 1 direction on a currency pair (eg: just short, for eurusd for 2 days for instance). How could we build this into the code. My assumption is I could just put ‘//’ infront of the parts of the code I don’t want to use (Eg: infront of buy or sell orders within each currency pair) and then remove them when I need to use that part of code next time. Will this work with a code structured in this way? 

**I can’t really think of anything you left out on purpose, to be honest. Unless it’s to check if there are sufficient funds for the trade, but that’s not necessary yet. Maybe in future versions of the strategy.
 
niko wrote >>

Hey guys, the basic tutorial on coding really helped to get my head around this (although the detailed coding part is lacking). To show you I have been busy, I attached the code below. The idea i think is there but it still returns lots of errors. You will see everything is under one big bracket in Start() the reason I did that is because mql kept coming back with error that variable can't be declared globally, and when i made it all in 1 bracket it seemed to have stopped that error (although lots of other errors persisted). I used notepad++ to check the brackets align. So did am I at least going the right way with this?

this is the latest version of the code. syntatically everthing seems to be ok, but when i run it through strategy tester it returns no trades. something is wrong with logic. need help. I'm becoming quite familiar with code now, well the structure of program side of it.

Hey guys/gals, any help with the above bit of code (not the pseudo code but the code code) while i'm learning how to do it the proper way.

I had a brainstorm today, I think maybe the error is there is one function counting the orders, and instead maybe I need 2 functions separately, one counts the buys and another sells. I programmed the code below (changed a few things to account for this), you can see below. But still it was all messed up as I am still a complete beginner. Any ideas on this one?

//+------------------------------------------------------------------+
//|                                     N&P 1DailyUpTrendExec.mq4 |
//| Copyright Nick Lou & Pete Arh 2009                               |
//|                                     20090523                     |
//|                                                                  |
//+------------------------------------------------------------------+

extern double    Lots=0.01;
extern double    TakeProfit=20;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+


  //-------------------Declaring All Variables and Conditions


//--------------------declaration end


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


  // Order counting code
  // Return:  0 = no orders
  //          >0 = # long orders
  //          <0 = # short orders
     int CalcOpenOrders()
        {
        int long=0,short=0;
     for(int i=0;i<OrdersTotal();i++)
        //i set to 0 to stop bugs from
        //crawling in, i<Orderstotal means if i is behind the number of orders
        //in market then start counting orders and make i that number
        {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
        //this function selects an order for further processing
        if(OrderType()==OP_BUY)long++;
        if(OrderType()==OP_SELL)short++;
        }
     if(long>0)return(long);
     if(short>0)return(-short);
     }

//------------ (fingers crossed this is right). I still don't get it
why we need to count orders.


//--------------DECLARATION OF VARIABLES-------------------------
double ema1,ema2,ema3,closeup, e1over2, e2over3, e1under2, e2under3,
eurusdbuyok, eurusdshortok;

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start ()
  {
  ema1= iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0);
  ema2= iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
  ema3= iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
  e1under2=ema1<ema2;
  e2under3=ema2<ema3;
  e1over2=ema1>ema2;
  e2over3=ema2>ema3;

  if(Bars<75)
     {
     Print("Bars less than 100");
     return(0);
     }


  //------------------EURUSD Block-------------------------
  //check order type, if it doesn't equal to buy already then buy


  //here i am setting a condition which will be
  //checked before a trade is opened, it shoudl state 'It's okay to enter
  //into long trade so long as there is either 1 order in place already or
  //none)

  // Call function. the returnvalue (output) of the function is
stored into ReturnVal
  int ReturnVal = CalcOpenOrders();

  // Check output of function
  if (ReturnVal >0)  // >0 = long orders. See CalcOpenOrders()
     eurusdbuyok=1;
     else
     {
     eurusdbuyok=0;
     return(0);
     }

  if (ReturnVal<0)   // <0 = short orders. See CalcOpenOrders()
     eurusdshortok=1;
     else
     {
     eurusdshortok=0;
     return(0);
     }


  //--------------condition ends

  if(eurusdshortok==1)
     {
     static int ticket;
     // deleted if(OrdersTotal()==0)
     if(e1under2 && e2under3)     // short function
        {
        ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"ShortOrder
",0,0,Red);
        if(ticket>0)
           {
           if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
           Print("SHORT order opened : ",OrderOpenPrice());
           }
        }
     return(0);
     }



  //  -------------------------------------------------------------------------------------------
  if (eurusdbuyok==1)
     {
     static int ticket1;
     // deleted if(OrdersTotal()==0)
     if(e1over2 && e2over3 && eurusdbuyok==1) //buy function
        {
        ticket1=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,Ask+TakeProfit*Point,"",0,0,Green);
        //What's 12345 for? I ADDED ASk-30*Point for stop loss
        if(ticket1>0)
           {
           if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES))
           Print("BUY order opened : ",OrderOpenPrice());
           }
        }
     return(0);
     }

  return(0);
  }

 
niko wrote >>

Hey guys/gals, any help with the above bit of code (not the pseudo code but the code code) while i'm learning how to do it the proper way.

I had a brainstorm today, I think maybe the error is there is one function counting the orders, and instead maybe I need 2 functions separately, one counts the buys and another sells. I programmed the code below (changed a few things to account for this), you can see below. But still it was all messed up as I am still a complete beginner. Any ideas on this one?

Hi Niko

With reference to the pseudo code

From the code:
*** Question ***
What time periods do you want to use for your moving averages? Are the periods Minutes, Hours, Days, Weeks, Months or do they need to be variable?
***Answer *** Excellent question. It will be 5 minute periods for all moving averages, they do not need to be variable.

Then I would suggest adding to the pseudo code in the configuration parameters area a statement something like

"Use 5 Minute Time Frame for Indicators"


You said:
***I was unclear on this before, my apologies. There should be no block as this in the code.

I will exit positions manually when needed through the trade/terminal window

(I assume that’s possible and won’t mess up the EA execution, right).

So actually I don’t know if we need to code this flexibility or not? What do you think


I then suggest that you just delete this block from the pseudo code.

There should be no problem coding the EA so that you can use the terminal to close a position manually without messing anything up.

You added these lines:

ELSE If there is already 1 open BUY position for this currency pair DO NOT OPEN BUY Position for this currency pair.

ELSE If there is already 1 sell position open for this currency pair Do not open SELL Positions for this currency pair


*** Question *** Why do you need to specifically state what you DONT want the program to do after the previous lines have just

stated what you DO want the program to do. In my opinion the pseudo code is the place to carefully think through your logic. Otherwise you are likely to

get into a mess when you start actual coding.


You said:
PS: I will often use just 1 direction on a currency pair (eg: just short, for eurusd for 2 days for instance). How could we build this into the code. My assumption is I could just put ‘//’ infront of the parts of the code I don’t want to use (Eg: infront of buy or sell orders within each currency pair) and then remove them when I need to use that part of code next time. Will this work with a code structured in this way?


Commenting out code as you suggest is not a very elegant way to go. Further more, i f you comment out say the "LONG" section of the code you would comment out the function for opening long positions for all currency pairs, unless you produce code with a lot of duplication. This is also not a very elegant way to go and easily leads to errors.

Over a given period you may want to be long EURUSD, short GBPUSD and not trade USDCHF at all. This is quite easily accomplished but I'd like to see you have a go at trying to work out to how to describe it yourself in the pseudo code.


Here are a couple of hints as to one possible way of going about it.

1. have a look at how your levels are set up and employed

2. The levels are represented as decimal numbers. For go/no go situations is is a common practice to use another type of representation known as a boolean or flag. Would it be possible to use flags to accomplish what you want to do?


You said:

**I can’t really think of anything you left out on purpose, to be honest. Unless it’s to check if there are sufficient funds for the trade, but that’s not necessary yet.

Maybe in future versions of the strategy.


No its not about checking for sufficient funds. You specifically said no money management except of course for specifiying lot size. It is something much more fundamental.

What items of information do you need to place an order? Where and how are each of these items described and handled in the psuedo code?


You said:

Where do we go from here? Whilst we are creating this 'proper' code, I am still trying to solve the puzzle of the code above. It doesn't follow our pseudo code as it is an old patched up code, but I am still quite puzzled with it as it should work, at least for 1 currency. Key thing is building it in this unorganised haphazard way is teaching me the actual coding elements (like how brackets affect the script, where to declare variables, etc). Any ideas on why that code doesn't work as it is now?


I appreciate your frustration. I had a very quick look at the old code but frankly what you are trying to do doesn't make much sense logiically. I could rewrite this piece of code in about 15 minutes and it would do what you want it to, but that is not the point is it?. You said you wanted to learn to write code. I am endeavouring to instruct you how to do it for yourself.


Nico, I dont think you are too far away from starting to write some (new) proper code but it is important to get as much of the "what you want to do" accurately nailed down in the pseudo code first.

It will make the coding "sooo much easier" as you will soon see.


Keep up the good work

Cheers

Tim

 

Hey Tim,


As always I really appreciate your time with me. I will go ahead and modify the pseudo code so it reflects your comments. About real code, no worries, let's do it properly (i'm just a bit impatient at times :), as learning is more powerful than anything.

 

Hey Tim,


I went through the pseudo code again and did as much as my newbie brain could figure out at this point. Please find it attached, I look forward to your comments!

Files:
 
niko wrote >>

Hey Tim,

I went through the pseudo code again and did as much as my newbie brain could figure out at this point. Please find it attached, I look forward to your comments!

Hi Nick

That is all looking good.

I went through your latest psuedo code and answered some questions etc.

I think we are just about ready to start coding.


As a very rough rule of thumb they say 1/3 rd of programming time should be spent on specification, 1/3rd on coding and 1/3rd on debugging and testing.


So we can say that we are now roughly 1/3 of the way through this little project!

Now lets talk about the MT4 language.


All computer languages have specific ways ( a format) in which the language must be written so that the computer can understand what you are trying to instruct it to do. You find out about this and other language specific information from the documentation. The documentation for MT4 ia available on line on this forum. If you have not already done so I would suggest you spend some time having a look at the sort of information that is availaible in the documentation. Then when you need information about some specific topic or function you will know where to look for it.


To make life a bit easier, many modern compter languages including MT4 use templates which set up a basic program layout for you. These template programs are often refered to as "wizards". You will find the EA wizard under the "New" menu item in the MetatEditor with the green cross on it.


If you open this you will find a template for "Expert Advisor". Select this and you will be prompted for an EA name, author details etc and parameters. The parameters can be added or changed later but at this stage you might want to enter the parameters from the configuration block.

You will be required to give each parameter a name, a type and an intial value


Parameter names should always be descriptive of what they are


for example EURUSD_TopFilterLevel is a meaningful name ex1 IS NOT. The use of meaningful data names make the program much easier to understand debug or modify later on. Note that in MT4, data names are case sensitive. LOTSize is not the same as LotSize. You need to be careful and consistent with data names.


You will also be required to specify a data type (type) for each parameter. MT4 uses just 4 data types Integer Number (int), Decimal Number (double), Text (string) and True / False or Flag (bool) .


Again we need to be careful with data types and to not mix them up unknowingly or we can introduce subtle bugs.


Once you have created your template with the wizard I would suggest you start "structuring" you program . The basic unit of structure in MT4 is the FUNCTION.


The simplest basic function,which as it is written here, actually does nothing is -



void MyBasicFunction()

{

}


The template generated by the metatrader wizard already has three empty functions in it.



int init()

{

return(0)

}



init deinit()

{

return(0)

}



int start()

{

return(0)

}


The init function is called every time the EA starts when you drag it onto a chart and click the Expert advisor button ON

The deinit function is called every time the EA stops when you drag it off a chart or click the Expert advisor button OFF


The start function is called on EVERY NEW TICK received by the chart that it is attached to. This means it can be called several times or more a minute depending on how busy the market is. This is where we will do most of our work from.


For the time being I suggest that you put all the pseudo code from the main block into the start function and then comment out the pseudo code with // on each line.


Then create a new function of your own for the entry rules block and comment out the pseudo code in it the same way.


Leave the init and deint functions empty and as they are for the time being.


You should be able to compile this code without errors but of course at this stage it will do nothing.


Keep up the good work!


Regards

Tim

(PS we can talk on skype if needs be but lets just see how we go with the forum for the time being)

 

Hey Tim,

As always your help and time is highly invaluable! As a gesture of thanks I would like you to send you a very nice bottle of champaigne once we finished with the coding process.

Yep, I been through the mql book quite a few times, the difficulty was putting the theory into practice. I also printed out about 10 or so EA's posted on this website, to see hwo things are coded and put together, to try and understand them. And I spend a lot of time in simulator trader on MT4 (ie backtesting and learning already existing EA's).

I'l start the coding process, and make blocks very clear and separate to help both of us see what's going on. And send them to you via forum.

1 Question: You said brokerages put in things to stop aggressive pipping. 1. What do you define aggressive pipping? 2. What things do they put in place?

I used to manually scalp eurusd on 1min timeframes (with a spreadbetting method), this was going really well until the brockerage clicked in and started to put delays for execution (entry and exit). So now there's no point at all doing this with this brokerage, even though it's illegal, delays will still happen and mess up the whole day (like if you typically scalp just for 1 hour, 2 delays and you lost the valuable time). I got all money back for delayed trades after aggressive threatening of court action.

2 Question: Do you use different brokers? What brokers would you recommend? (if you are allowed to mention names here)

thanks,

nick

Reason: