Grid Trading: Grape and Frame

 

Hi all,

This is my first contributory post, but hopefully it will not be the last.

This thread is about grid trading and about my grid EAs. I know that many of you abhor grid trading and see it as a red herring; I understand your perspective but I ask you to please keep pessimistic comments like that out of this thread.

First, a brief explanation of grid trading (as I see it). Those of you who are already familiar with the idea can skip this part:

Whereas trend/breakout trading attempts to profit on deviations from the current price (the trend), grid trading attempts to profit on the idea that the price will almost always return to any value that it has previously visited.

Typically, grid trading requires very small take-profits and very large (or non-existent) stop-losses. In the simplest example, instead of closing your order when it is beginning to lose the grid trader simply ignores it and continue to trade. He will continue earning a profit on all his winning orders but his losing orders will not close and will become "hanging orders." If a grid trader allows this to happen, it is because he believes that the price will eventually return and close the hanging orders with a profit. However, if the the price does not return to close the order in time, the equity will fall to the point of bankruptcy and a stop-out will occur.

There are a million variations on this strategy this strategy that attempt to deal with the negative repercussions of hanging orders in their own way.

I will now describe my own efforts in this arena:

The first EA I produced (called "Grape", for grid rape) seems to function extraordinarily in back-testing during 2000-2005 on the EURUSD . Unfortunately it turns to garbage in late 2006-2010. I am not sure if this is due to poor tick-data quality or if it represents a change in market dynamics between those periods, but I am leaning towards the prior.

This is how "Grape" functions:

1. Before entering the market it uses a number of indicators (RSI, Stoch, ADX) to enter the market only when no trending is occurring (sideways movement).

2. Upon entering the market, a Buy Limit and a Sell Limit are opened around the current price.

3. As it moves towards one direction or another it continues opening Buy Limits and Sell Limits around the current price *as long as all of the indicators still say that the market is sideways.*

4. The moment it makes any profit whatsoever it closes all open and pending orders to avoid betting against myself or creating hanging orders. (This function helped enormously)

5. Start again from 1.

Here is the backtest image from 2000-2005 on EURUSD (M1):

Looks quite good from 2000 - 2005 doesn't it? Now look at 2007:

Different story: stop out in November of 2007 and major draw-downs before that.

Most of the inputs are self-explanatory except for the following:

Spread: Distance between current price and buy/sell limit.

Cutoff: Amount of negative profit (loss) an order has to sustain until new limits are opened. Set this to -1 as it doesn't seem to make matters better. This function is likely bugged.

Equity Cutoff: Redundant function (I think). Works the same way as "Cutoff" but on equity instead of on individual orders.

Max_Profit: The maximum amount the equity can increase before all orders are closed.

Lot adjustment: A value of "1" here means to increase lots in proportion to balance (when balance = equity). A value of "0" would mean to not use any lot incrementation.

All of the other variables below Equity Cutoff relate to my indicators. If you are going to change them I suggest looking at the code first.

Unfortunately I was disorganized while making this EA and I am not sure that I have attached my most recent version. I will continue looking around for a newer one that I've made as this one likely has some major bugs.

What I surmised from the failure of this EA during the 2007-Now time-frame is that the oscillating price causes hanging orders to form on both sides. I created another EA in an attempt to avoid this; I called it "Frame."

This is how "Frame" functions:

1. The EA enters the market only when its indicators show that the market is flat. For this EA I use the indicator "Alligator."

2. When the EA enters the market, it draws a "frame." The upper limit of this frame is x% above the price and the lower limit is x% below the price. The percentage is specified in the EA's inputs. I use somewhere between 0.5% and 1.5%. Trading can only occur within the frame and not outside it.

3. Once the frame is established the EA will open buy/sell limits in a fashion similar to Grape as long as the Alligator indicator shows that the market is still flat.

4. Also like Grape, this EA will close all open orders if the current equity is greater than the last-equity-when-equity-was-equal-to-balance. This is to help avoid hanging orders within the frame. This feature can be turned on and off in the EA's input.

5. If the price leaves the frame, one of the two following situations occur:

1. If the EA made an overall profit in the frame, all orders inside the frame are closed and we take a loss on the currently open orders.

2. If the EA did NOT make an overall profit in the frame, all open orders are "hedged" with what I refer to as a "freeze order." This "freeze order" is an order in the opposite direction of the currently open orders. Ex: If there are 3 open BUY orders and 4 open SELL orders within the frame. The price breaks through the upper frame limit,and we did not make an overall profit within the frame. The "freeze order" is initiated for 1 BUY order to counteract (or "freeze") the net 1 SELL order (4 SELL - 3 BUY = 1 SELL).

The rationale behind "freezing" the losing orders instead of just closing them is this: If you leave the orders open then they can become hanging orders that exhaust your equity leading to a stop-out. If you close the orders you are guaranteed to take a loss. However if you "freeze" them as I described above, the price has a chance to return to those losing orders and make them into winners. Of course, in order for this to happen the "freeze order" must be closed before it starts losing money itself.

Following this rationale, the "freeze order" can have no "take-profit" but it must have a "stop-loss." Currently there are two "stop-loss" options implemented:

1. If you set the "FreezeOrder_SL" (stop-loss) input value to a non-zero value, the "stop-loss" will be that many pips away from the "freeze-order." 2. If you set the "FreezeOrder_SL" input value to 0, the closest open order's "take-profit" (within the Frame) will be set as the "stop-loss."

6. A new frame is again opened when the indicators show a sideways market and the whole thing is repeated from #1.

Here is the back-test image from 2000-2005 on EURUSD (M1):

Again - Looks quite good from 2000 - 2005 doesn't it? Now look at 2007:

Once again a different story: stop out in June of 2007 and major draw-downs before that.

The following is a description of Frame's less obvious inputs:

TimeRestriction: Restricts trading only to specific hours

LotAdjustment_Type: Increments lots if the balance is increasing together with the equity. Please check the code for more information. Set it to 0 if you do not want this feature, or to 3 if you want it to work in a similar matter to Grape's lot adjustment. Check the code for more information.

Spread: Distance between current price and buy/sell limit.

FreezeOrder_SL: The "stop-loss" (in pips) for the "freeze order." This is described above in more detail.

TrailingStop: This function is currently inactive. Leave it at "0."

FreezeFactor: The lot size of the "freeze order" is multiplied by this factor.

Max_Profit: The maximum amount the equity can increase before all orders are closed.

Max_Frame_Profit: Same as above, but pertains only to orders within the current frame. This function may not work properly.

Max_Lots_In_Frame : This is the maximum net amount of lots that can be invested in a frame. This function does NOT work properly. Please set this number to something very high (ex: 40000) when testing the EA.

FrameSize: Size of frame (in percent of current price)

GatorDistance: Max. difference between the two extreme lines for Alligator

CloseByEquity: When this variable is set to "true," all orders will close when we have made a profit overall.

CloseByFrame: When this variable is set to "true," all orders will close when we have made a profit within the frame. This function may not work. Leave it "false."

DoubleGator: This function does not work. Leave it "false."

Compensation: This function does not work. Leave it "false."

UseStoch: Use stochastic indicator to determine when market is sideways.

UseRSI: Use RSI indicator to determine when market is sideways.

UseIncGator: Check to see if the Alligator indicator has been decreasing for 3 consecutive periods.

GatorPeriods: Increases the amount of consecutive periods that the Alligator indicator must be decreasing for to count as a "go."

Files:
 

I would love to hear any input on either of these (admittedly similar) systems. I am not a professional programmer, so I'll listen any relevant programming advice as well.

My code is kind of messy and lacks comments. If it is too hard to read please tell me so and I will re-upload it with comments and nicer TAB structure.

Both EAs use a couple of functions that I myself did not create. For those functions I thank "komposter," and "fai."

 

Very good trading idea.

I'm going to have a try on it. Now I have skill to judge if the current market is trend or shock. I need some EAs to help me trading in different market structure. These two EAs seems to be very helpful.

 

Hi

Can you tell us which folders to use for indicators etc.

Thanks.

 
pmacel:
Hi

Can you tell us which folders to use for indicators etc.

Thanks.

Indicators? These are both expert advisors and I am pretty sure neither of them use any custom indicators. Place them in yourMetatrader 4/experts folder.

 

Oh right I completely forgot.... if you want to recompile it then place the following files in your Metatrader 4/experts/include folder:b-SharingDoW.mqh, Events.mq4,Timer.mq4,more_functions.mqh.

There are a bunch of bugs that I fixed in more recent versions. If there is any interest I will post them.

Reason: