adventures of a newbie - page 3

 
niko:

Thank you cloudbreaker! This is starting to make a bit more fun, I didn't know that you can call a function within a return (even though it's probably in the mql book, just like everything else theoretical). Nice looking helicopter EC 155! Why 'thankfully', you don't want your own chopper?

Question Aero HC2:


I have 3 scripts for my strategy this far. 2 are uni-directional, 1 is bidirectional (which is the one that we are working on here).

From your experience with MT4, should I expect serious problems if I attach different scripts to 5 or so pairs on the screen (1 script per pair). It will be easier for me than to program or change 1 large script and make that one pick the pair I want (as that would require daily changing in the code)?

 
niko:

Question Aero HC2:


I have 3 scripts for my strategy this far. 2 are uni-directional, 1 is bidirectional (which is the one that we are working on here).

From your experience with MT4, should I expect serious problems if I attach different scripts to 5 or so pairs on the screen (1 script per pair). It will be easier for me than to program or change 1 large script and make that one pick the pair I want (as that would require daily changing in the code)?

Assuming here that we are talking about a single trading strategy across currency pairs:-

I prefer to make my EAs pair-agnostic - ie. the same EX4 can be attached to any pair.

-That way we have only one codebase to maintain.

-The logic remains as simple as possible.

 
cloudbreaker:

Assuming here that we are talking about a single trading strategy across currency pairs:-

I prefer to make my EAs pair-agnostic - ie. the same EX4 can be attached to any pair.

-That way we have only one codebase to maintain.

-The logic remains as simple as possible.

Yep, that would be the ideal, but I set a manual filter for each currency each day, not indicator based, plus it turns out to be 3 strategies although idea is the same. This would make the code really big in my understanding and cumbersome to adjust on a daily basis if we were to put everything together. For the near future I'l program the filter to cut my work time, but there are always things you can't program (90% to be programmed, the rest is what the brain is for I think. Although I did chase the 'holy grail' before trying to program everything and come up with the perfect model haha, looking in the wrong direction).


I'm launching the 2 strategies live this Monday as the code on those is done. They have been manually executed by my father in law and myself a little, coding should bring higher results. For the Combined strategy I can launch 2 EA's I guess on the same chart (one for long one for short), but I still want to learn, so will resume coding process. So expect lots of questions to come very soon!


which country are you based in? I'm in London, UK.

 

Hey cloudbreaker and other mt4 gurus here!


Question: I have 2 scripts (one is for buying another is for shorting). I wanted to use both today on 3 pairs. Now, because only 1 EA can be attached to a chart, I opened 6 charts (2 per pair), and attached the scripts to them. However, only 1 script was executed. I have no idea why. Any help please?


So to sum up: 2 EAs, one just does buying another just does shorting, very basic rules (golden cross on emas and thats it), profit take 20 pips, no stop loss. Why is only 1 executed? I checked that Expert Advisors was activated, and all the scripts had smiling faces next to them in the charts.

 
niko:

Hey cloudbreaker and other mt4 gurus here!


Question: I have 2 scripts (one is for buying another is for shorting). I wanted to use both today on 3 pairs. Now, because only 1 EA can be attached to a chart, I opened 6 charts (2 per pair), and attached the scripts to them. However, only 1 script was executed. I have no idea why. Any help please?


So to sum up: 2 EAs, one just does buying another just does shorting, very basic rules (golden cross on emas and thats it), profit take 20 pips, no stop loss. Why is only 1 executed? I checked that Expert Advisors was activated, and all the scripts had smiling faces next to them in the charts.



What do the logfiles say? ie. the one in the logs directory and the one in the experts directory?

Are you getting any "Trade Context Busy" messages? If so, then I'd recommend adding some logic making use of the IsTradeAllowed() function.

If not, then I'd recommend littering your EAs with Print() statements.

 
cloudbreaker wrote >>

What do the logfiles say? ie. the one in the logs directory and the one in the experts directory?

Are you getting any "Trade Context Busy" messages? If so, then I'd recommend adding some logic making use of the IsTradeAllowed() function.

If not, then I'd recommend littering your EAs with Print() statements.

hey cloudbreaker! thank you for coming to rescue!

1. What do you mean log directory? How do I get to both of those? (I tried to open it via C>ProgramFiles>MT4>Experts>Logs but the folder shows empty).

 
niko wrote >>

hey cloudbreaker! thank you for coming to rescue!

1. What do you mean log directory? How do I get to both of those? (I tried to open it via C>ProgramFiles>MT4>Experts>Logs but the folder shows empty).

Hey cloudbreaker!

I have been through your comments on coding a lot of the times already (I have them all posted on my wall), trying to understand and patch it all together, but still completely lost. As far as I got was to copy the MyOrderCount function from another EA but still no idea on how to integrate that into the code. I'm out of my depth completely, but it doesn't mean we can't progress. If you could help me build the code bit by bit (and direct me towards maybe more specific things that I could build myself as well) then we will get the code done and I will learn much faster.

I attached the code as far as I got with it, and also the Map of Program Design the way I view it. Don't worry about the minor things like brackets in the code, i'l double check them with notepad++ as we go along.

Key priority, is if you could help me step by step integrate the MyOrderCount code into the remainder, and please explain everything bit by bit as we go along. Or you can also send me to specific parts of book or anything like that if you feel it's appropriate. This way we'l progress much faster.

Thank you!

ps: let me know if the program map didn't attach

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

extern double    Lots=0.01;
extern double    TakeProfit=20;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
  return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
  return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
if(Bars<75)
     {
     Print("Bars less than 100");
     return(0);
     }
  //-------------------Declaring All Variables and Conditions

double ema1,ema2,ema3,closeup, e1over2, e2over3,e1under2,e2under3;

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;
//--------------------declaration end

//-------------order counting code
int CalculateNumberOfOrders(string Symbol)
{
int long=0,short=0;
for(int i=0;i<OrdersTotal();i++)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)break;
      if(OrderType()==OP_BUY)long++;
      if(OrderType()==OP_SELL)sell++;
     {
  //---return orders Volume
  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.


//------------------EURUSD Block-------------------------
//check order type, if it doesn't equal to buy already then buy
if(OrderType()
     {
    static int ticket;
      // deleted if(OrdersTotal()==0)
     if(e1under2 && e2under3)     // short function
       {
        ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,Bid-TakeProfit*Point,"Short
Order ",0,0,Red);
        if(ticket>0)
              {
              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SHORT order opened : ",OrderOpenPrice());
              }
              //return(0);
       }

      //  -------------------------------------------------------------------------------------------

      {
      static int ticket1;
            // deleted if(OrdersTotal()==0)
        if(e1over2 && e2over3) //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);   //the problem is with this return not being
in the right place i think
        }
      }
return(0);
}
}

ps: please ignore the random // comments in the code, its from old notes

 
niko wrote >>

Hey cloudbreaker!

I have been through your comments on coding a lot of the times already (I have them all posted on my wall), trying to understand and patch it all together, but still completely lost. As far as I got was to copy the MyOrderCount function from another EA but still no idea on how to integrate that into the code. I'm out of my depth completely, but it doesn't mean we can't progress. If you could help me build the code bit by bit (and direct me towards maybe more specific things that I could build myself as well) then we will get the code done and I will learn much faster.

I attached the code as far as I got with it, and also the Map of Program Design the way I view it. Don't worry about the minor things like brackets in the code, i'l double check them with notepad++ as we go along.

Key priority, is if you could help me step by step integrate the MyOrderCount code into the remainder, and please explain everything bit by bit as we go along. Or you can also send me to specific parts of book or anything like that if you feel it's appropriate. This way we'l progress much faster.

Thank you!

ps: let me know if the program map didn't attach

ps: please ignore the random // comments in the code, its from old notes

Hi Niko

May I add my two pence worth to this discussion,

As an old, virtually antique :), programmer with a little bit of teaching and training experience, I have come across quite a lot of bad code in my time. One common mistake that leads to poorly written code is lack of planning. It may be stating the obvious but you really do need to specify what you want to do before you start trying to code it. This may seem like extra work but it will save you heaps of time in the long run and produce, more reliable more understandable, and more easily maintained code.

One very useful technique for specifying computer code is known as Pseudo Coding. Basically it is just a logical set of computer like instructions written in plain english. There is no particular format required but you can use typical computer program jargon that is found in some form or another in just about every computer programming language.

Here is an example of some Pseudo Code for cooking cheese and macaroni. I grabbed it from an article "Intro to Programming by Patrick Nouvion" on the IBFX site. The entire article is probably worth a read. http://www.ibfx.com/ibfxu/catalog/programming/pro1010.aspx

START
Find Milk and Butter and Kraft box
If found continue
else send kid to the store

Find Pot in cup board

If found continue else ask wife to help find it

Find Stove
If found continue
else order pizza

Check for an available burner
If available continue
else make avail

Turn Stove on to high
Find Water
If found continue
else order pizza

Put Water in pot
Make sure that the stove is hot
if hot continue
else check that it is plugged in
else call repair man and order pizza

Place pot on burner
if you have salt add a pinch of salt
if not continue
if you have pepper add a pinch of pepper
if not continue
while water is not boiling wait
open Kraft box
add the macaroni from box to pot
wait 8 minutes
while waiting, if pot over boils turn heat down and blow on it
Find colander
Drain macaroni in colander
return to pot
add contents of cheese packet
add butter
add 1 Tablespoon milk
stir to combine
if too thick add 1 Tablespoon milk; if not, eat
END

If I was writting this pseudo code myself I would refine it a bit more by breaking it up into functional blocks and using a bit of indenting to make it "Look Pretty". For example

START BLOCK - Prepare Boiling Water

Put Water in pot

Place pot on burner
if you have salt add a pinch of salt
if not continue
if you have pepper add a pinch of pepper
if not continue
while water is not boiling wait

END BLOCK - Prepare Boling Water

and so on....

Niko, I commend you for wanting to learn to write your own code. In my experience, the quickest and easiest way to learn is to do what you are doing and just have a go. What I would suggest though is that you take a brief step backwards and write out what you want to achieve in pseudo code. This should help you to get your thinking clear about exactly what it is you are trying to achieve.

Once you have done that you may want to post it back here for me or someone else to have a look at. Once you have the pseudo code right, then you can start converting it to MT4 code. Trying to do it all in one go is too big a jump, especially when you are a beginner.

By the way, my pseudo code is never wasted. Most of it finishes up as comments in my finished code.

Good Luck!

Tim Wilson

 
niko:

Hey cloudbreaker!

I have been through your comments on coding a lot of the times already (I have them all posted on my wall), trying to understand and patch it all together, but still completely lost. As far as I got was to copy the MyOrderCount function from another EA but still no idea on how to integrate that into the code. I'm out of my depth completely, but it doesn't mean we can't progress. If you could help me build the code bit by bit (and direct me towards maybe more specific things that I could build myself as well) then we will get the code done and I will learn much faster.

I attached the code as far as I got with it, and also the Map of Program Design the way I view it. Don't worry about the minor things like brackets in the code, i'l double check them with notepad++ as we go along.

Key priority, is if you could help me step by step integrate the MyOrderCount code into the remainder, and please explain everything bit by bit as we go along. Or you can also send me to specific parts of book or anything like that if you feel it's appropriate. This way we'l progress much faster.

Thank you!

ps: let me know if the program map didn't attach

ps: please ignore the random // comments in the code, its from old notes

A quick look tells me that you are making a fundamental mistake.

You seem to be attempting to declare the function MyOrderCount() within the codeblock of the start() function.

Think of your code as a series of functions init(), start(), yourfunction1(), yourfunction2() ... yourfunctionN().

The init() and start() functions are special functions. The init() function is run once when you first initialize the EA. The start() function is run each time an incoming tick arrives.

So the start() function forms the root from which all your logic will be called once the EA is up and running.

As an example, each time a tick arrives, your start() function will run and do this:

- check some conditions

- if conditions are met, perform an order

One of the conditions you need is to make sure you don't already have an order of the type (say buy) before you make a buy order.

So from within the start() function, you'll need to work out what buy orders exist by a CALL to your buy order counting function (the codeblock of which sits OUTSIDE the start() function).

Your order counting function will return the number of buy orders and you will resume execution of the start() function at the line after your call to the buy order counting function.

Then you will use the value returned from the buy order counting function to determine whether or not to execute your buy order.

Actual execution of the buy order may be a call to ANOTHER function you create for yourself (again, declared outside of the start() function).

Hope this makes sense. You may structure it completely differently, but I'm just trying to get your head around the concept of functions and calling.


CB

 

Hey TSWilson and CB!

You guys are great! Thank you so much.

Completely spot on regarding the pseudo code. I did this when I did very basic programming in pro**realtime language, but there it was so simple, no declarations, no blocks, etc (the whole code was just 'if...then' haha). I started the pseudo code this morning (got to work an hour early just to do it) and wrote a visual diagram on how and what blocks to have. I will finish this later today and post it here.

Once this is done, the rest of the blocks and the specifics of the code I can figure out with your help guys. So back to the drawing board for now!

I am happy to work through the night today and tomorow to finish the code asap, as I'm off on holiday next week and want to have all ready before I go.

So keep watching this thread today, I will post the pseudo code and the diagram!

And as always, thank you the Universe for people like you!

Reason: