Saturday, March 7, 2009

Sample MQ4 Code

Here is a bit of code that demonstrates what I've been blogging about. It's a simple way to manage pending position requests.

The idea is that a signal his been raised but that this code will let you wait for an improvement in the price before actually opening a position.


// *******************************************************************
// Name: AMS_PEND.MQ4
// Date: 01-Mar-2009
// Prog: Rookie
//
// Desc: A pending position queue manager. Pending positions can be
// held until a period of time or some favorable conditions.
// *******************************************************************
#property copyright "Copyright © 2009, Rookie"
#property link "http://trying-forex.blogspot.com/"

static double pendlask[4096]; // Ask price for a pending long
static int pendltime[4096]; // Time used to calculate pending time
static int pendlqty = 0; // How many items in pendlask queue

static double pendsbid[4096]; // Bid price for a pending short
static int pendstime[4096]; // Time used to calculate pending time
static int pendsqty = 0; // How many items in pendsbid queue

// ****************************************************************************
// Add a pending position entry to the long list
// ****************************************************************************
void addpendl(double ask)
{
static double last = 0;

if ( Time[0] > last )
{
pendltime[pendlqty] = Time[0];
pendlask[pendlqty] = Ask;
pendlqty++;
}
last = Time[0];
}

// ****************************************************************************
// Add a pending position entry to the short list
// ****************************************************************************
void addpends(double bid)
{
static double last = 0;

if ( Time[0] > last )
{
pendstime[pendsqty] = Time[0];
pendsbid[pendsqty] = Bid;
pendsqty++;
}
last = Time[0];
}

// ****************************************************************************
// A simple way to determining if a pending long position should be
// activated.
// ****************************************************************************
int dopendl_simple()
{
int i;
static int this=0;

// Dead simple "drop" style pending manager
// ----------------------------------------
for (i=0; i {
if ( pendltime[i]>0 )
{
if ( pendlask[i] > Ask )
Print("Pending L #",i," ask was ",pendlask[i]," at ",pendltime[i]," now ",Ask);

// Terminate items that don't fire
// -------------------------------
if ( Time[0] - pendltime[i] > 3000 )
{
Print("Pending L #",i," from ",pendltime[i]," cleared due to time ",Time[0]);
pendltime[i]=0;
continue;
}

// Fire first pending item more than N points down
// -----------------------------------------------
if ( pendlask[i] - Ask > 5.0*Point )
{
if ( Bid < iMA(this,this,5,0,MODE_SMA,PRICE_TYPICAL,0) )
{
Print("Pending L #",i," firing");
pendltime[i] = 0;
pendlask[i] = 0.0;
return(1);
}
}
}
}

return(0);
}

// ****************************************************************************
// A simple way to determining if a pending short position should be
// activated.
// ****************************************************************************
int dopends_simple()
{
int i;
static int this=0;

// Dead simple "drop" style pending manager
// ----------------------------------------
for (i=0; i {
if ( pendstime[i]>0 )
{
if ( pendsbid[i] < Bid )
Print("Pending S #",i," bid was ",pendsbid[i]," at ",pendstime[i]," now ",Bid);

// Terminate items that don't fire
// -------------------------------
if ( Time[0] - pendstime[i] > 3000 )
{
Print("Pending S #",i," from ",pendstime[i]," cleared due to time ",Time[0]);
pendstime[i]=0;
continue;
}

// Fire first pending item more than N points up
// ---------------------------------------------
if ( Bid - pendsbid[i] > 5.0*Point )
{
if ( Bid > iMA(this,this,5,0,MODE_SMA,PRICE_TYPICAL,0) )
{
Print("Pending S #",i," firing");
pendstime[i] = 0;
pendsbid[i] = 0.0;
return(1);
}
}
}
}

return(0);
}

// ****************************************************************************
// Algorithm selection point for long position pend processing
// ****************************************************************************
int dopendl()
{
return(dopendl_simple());
}

// ****************************************************************************
// Algorithm selection point for short position pend processing
// ****************************************************************************
int dopends()
{
return(dopends_simple());
}

You'd use code like the following in the start function to see whether or not to add a pending position and whether or not to open a position based on a prior pending position.

int openl;

// See if you have an "open long" signal. If so, add it to the
// queue.
// ------------------------------------------------------------
openl = ...;
if ( openl>0 )
{
addpendl(Ask);
Print("Pending LONG at ask ",Ask);
}

// Now, see if the queue manager suggests it's time to open a
// long position from within the queue.
// ----------------------------------------------------------
openl = dopendl();

// Open a position here if necessary
// ---------------------------------
if ( openl > 0 )
{
...
}

Notice that this design allows you to simply comment out all the pending code and open positions directly from your original signal. This makes it easy to integrate into your own code in case you want to try this in your own testing platform and see how affects your existing EA.

2 comments:

jmot said...

hi Rookie Forex Trader, i am back to programming EAs as well. hope to exchange pointers with you

Anonymous said...

Hello jmot and others of course.... I too am reasonably new to forex trading... I am looking for someone to create an EA out of an indicator I have been working with for a some time and I think it owrks well manually... Can you give me directions for help..
Jack US/Texas