New to coding - Issues with a crossing Moving Average

 

Hello Everyone.

I am new to coding, however have been trading a while. I bought and read the "Expert Advisor Programming" book by Andrew Young. I found this very helpful and am currently making a VERY simple EA, however as I said I am new to coding - but want to get into it and give it a go.

A couple of questions firstly.

I have modified code out of the book (mainly just removed excess to make it alot simpler). I am attempting to make an EA that trades on the cross of 2 Moving Averages, but am running into some difficulties.

Firstly, I have selected the "Every Tick" model, and my EA is set to "ExecuteOncePerBar". What determines when the trade actually takes place? I would have thought that it would be the open price of the bar (or atleast close to it), however sometimes it is quite alot away from the open price, when it places a trade in the stratey tester. Even when I choose the model "Open prices Only" in the strategy tester, it still comes up with numbers that are not the Open Price.

Also, for some reason, it seems to miss trades on a couple of crosses. Why would it trade on some, and not others?

Also, does anyone know of a provider that allows an EA to actually trade on a Demo Account, is this even possible?

I have attached the include file, and pasted the code for the EA below. Prehaps someone could look at it and see what they think?

I have gone through a 24 day time period, of 2 currencies, and following the method of trading on the cross have come up with a positive return, this is using the open prices of the bar as soon as the MA's cross, I did this on 'paper' as such.

Thanks for your time.

//+------------------------------------------------------------------+
//| Test 01.mq4 |
//| |
//| |
//+------------------------------------------------------------------+

// Preprocessor
#property copyright "000"
#include <IncludeTest01.mqh>


// External variables
extern int Slippage = 10;
extern int MagicNumber = 123;

extern int FastMAPeriod = 12;
extern int SlowMAPeriod = 26;

extern bool CheckOncePerBar = true;


// Global variables
int BuyTicket;
int SellTicket;

double UsePoint;
int LotSize = 1;
int UseSlippage;

datetime CurrentTimeStamp;


// Init function
int init()
{
UsePoint = PipPoint(Symbol());
UseSlippage = GetSlippage(Symbol(),Slippage);
}

// Start function
int start()
{

// Execute on bar open
if(CheckOncePerBar == true)
{
int BarShift = 1;
if(CurrentTimeStamp != Time[0])
{
CurrentTimeStamp = Time[0];
bool NewBar = true;
}
else NewBar = false;
}
else
{
NewBar = true;
BarShift = 0;
}


// Moving averages
double FastMA = iMA(NULL,0,FastMAPeriod,0,0,0,0);
double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);

double LastFastMA = iMA(NULL,0,FastMAPeriod,0,0,0,BarShift);
double LastSlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,BarShift);


// Begin trade block
if(NewBar == true)
{

// Buy order
if(FastMA > SlowMA && LastFastMA <= LastSlowMA && BuyMarketCount(Symbol(),MagicNumber) == 0)
{
// Close sell orders
if(SellMarketCount(Symbol(),MagicNumber) > 0)
{
CloseAllSellOrders(Symbol(),MagicNumber,Slippage);
}

// Open buy order
BuyTicket = OpenBuyOrder(Symbol(),LotSize,UseSlippage,MagicNumber);
}


// Sell Order
if(FastMA < SlowMA && LastFastMA >= LastSlowMA && SellMarketCount(Symbol(),MagicNumber) == 0)
{
if(BuyMarketCount(Symbol(),MagicNumber) > 0)
{
CloseAllBuyOrders(Symbol(),MagicNumber,Slippage);
}

SellTicket = OpenSellOrder(Symbol(),LotSize,UseSlippage,MagicNumber);
}

} // End trade block


return(0);
}

Files:
 
NewCoder47:
Firstly, I have selected the "Every Tick" model, and my EA is set to "ExecuteOncePerBar".
  1. int start(){
    :
    // Execute on bar open
    if(CheckOncePerBar == true)
    :
    else{
       NewBar = true;
       BarShift = 0;
    }
    // Moving averages
    double FastMA = iMA(NULL,0,FastMAPeriod,0,0,0,0);
    double SlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,0);
    double LastFastMA = iMA(NULL,0,FastMAPeriod,0,0,0,BarShift);
    double LastSlowMA = iMA(NULL,0,SlowMAPeriod,0,0,0,BarShift);
    If you set CheckOncePerBar to false, BarShift will be 0 and you then compare the SAME values, as FastMA will equal LastFastMA...

 

Oh, Yes, the memorable Andrew Young! Its available from my Public Library.

My advice - simply follow from front to back. If anything is not clear, note it down or like I did, dog-ear the page, just SKIP IT and continue to the next codes into the Editor,etc. The book and terminal MUST never spearate and side-by-side all the time, then only this will work. By the time halfway, or not even 1/4 - you WILL realise you're able to answer most of your own previous questions. 1 month or even less ...;-)

They have the PDF version, which is much easier than flipping pages.

 
I really need to get this book myself as I struggle through it all LOL

Where is the pdf version I would like to buy it also ?
 
Agent86:
I really need to get this book myself as I struggle through it all LOL

Where is the pdf version I would like to buy it also ?

I remembered the caregiver, Rosh, was promoting and helping to sell author's work. Just search the author in this mql site.

Or visit your public librarian and request for it.


------

Update: Yes, its still here: https://www.mql5.com/en/forum/124567

Super Tip: Master exactly 1 chapter, 1 day. Don't rush. You dont know, JUST SKIP IT first, but still do the examples & coding diligently. There're only 9-10 chapters, 2 hundred pages only. in weeks, you're done!


 
WHRoeder:
  1. If you set CheckOncePerBar to false, BarShift will be false and you then compare the SAME values, as FastMA will equal LastFastMA...


If I am wanting the EA to operate once per bar, i.e, at the start of every new bar, it will not do this if I set CheckOncePerBar to false. However, even if I was to set it to false, what would the code achieve by comparing FastMA and LastFastMA if they were both equal?

Sorry if these questions sound dumb, but im just trying to get my head around it

 
Agent86:
I really need to get this book myself as I struggle through it all LOL

Where is the pdf version I would like to buy it also ?

You can get the PDF from expertadvisorbook.com
 
diostar:

Oh, Yes, the memorable Andrew Young! Its available from my Public Library.

My advice - simply follow from front to back. If anything is not clear, note it down or like I did, dog-ear the page, just SKIP IT and continue to the next codes into the Editor,etc. The book and terminal MUST never spearate and side-by-side all the time, then only this will work. By the time halfway, or not even 1/4 - you WILL realise you're able to answer most of your own previous questions. 1 month or even less ...;-)

They have the PDF version, which is much easier than flipping pages.



Yes I really liked the book also.

I have read it through, yet still have the questions as above. Any ideas? Particulary on when the trade is placed? I would have thought that in execution of once per bar, and it found the conditions to be correct and traded, it would trade on or fairly close to the open price of that bar.

 
NewCoder47:


Yes I really liked the book also.

I have read it through, yet still have the questions as above. Any ideas? Particulary on when the trade is placed? I would have thought that in execution of once per bar, and it found the conditions to be correct and traded, it would trade on or fairly close to the open price of that bar.

It's what goes into your MA logic, hwoever they're not what you expect. This the issue, is it not?

In this situation, use the Strategy tester on Visual Mode, best is to test either longs trade or shorts trade. You WILL get to see exactly how that logic performs on every open bar. HTH

Reason: