Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
//----------------------- MAIN PROGRAM LOOP
int start()
{
int total=0, cnt, dir=0, ticket=0;
double Stopper=0, LastPrice=0, AveragePrice=0, PriceTarget=0;
double SMA=iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0);
if(SMA*1.02<Bid && ((dir==-1 && Bid>=LastPrice+PipStep*Point)||total<1)) // Go SHORT -> Only sell if >= 30 pips above previous position entry
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),0,0,HotPink);
if(SMA*0.98>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total<1)) // Go LONG -> Only buy if >= 30 pips below previous position entry
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),0,0,Lime);
//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE
PriceTarget=AveragePrice+TakeProfit*Point*dir;
Stopper=AveragePrice-Stoploss*Point*dir;
//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET
for(cnt=0;cnt<OrderTotal;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()) continue;
OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels
}
}
//----------------------- MAIN PROGRAM LOOP int start() { int total=0, cnt, dir=0, ticket=0; double Stopper=0, LastPrice=0, AveragePrice=0, PriceTarget=0; double SMA=iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0);
if(SMA*1.02<Bid && ((dir==-1 && Bid>=LastPrice+PipStep*Point)||total<1)) // Go SHORT -> Only sell if >= 30 pips above previous position entry ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),0,0,HotPink);
if(SMA*0.98>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total<1)) // Go LONG -> Only buy if >= 30 pips below previous position entry ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),0,0,Lime);
//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE PriceTarget=AveragePrice+TakeProfit*Point*dir; Stopper=AveragePrice-Stoploss*Point*dir;
//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET for(cnt=0;cnt<OrderTotal;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderSymbol()!=Symbol()) continue; OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels } }
Thank you when I compile I get an error. Had multiple errors before I added the ; on the pipstep line then now the only error I get is;
'OrderTotal' - variable not defined C:\Documents and Settings\!.mq4 (61, 18)
also can I add an 'extern double Percent = 0.3;' line
then change the trade code to look like;
Code:
if(SMA*(1+Percent/100))>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total<1)) // Go LONG -> Only buy if >= 30 pips below previous position entry
instead of;
Code:
if(SMA*0.98>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total<1)) // Go LONG -> Only buy if >= 30 pips below previous position entry
Thanks and WOW you pretty much changed the whole EA
also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order.
Thanks again
Last edited by matrixebiz; 09-15-2007 at 01:55 AM.
Sorry for the two mistakes, I am in hollyday and cannot check the code from here.
I made two modifications :
1) the EA use now only the positions of the pair on which he is attached.
2) the EA keep the direction of the first position (of that pair). This is needed is some TP / SL / entry configuration where the EA may open buys and sells simultaneously; then the average price, new TP and SL have no meanings anymore. It is possible to write an EA working in both direction at the same time but it's much more complex. The simplest way to do that is to work with two EAs with different MagicNumbers and only long / only short. If you need it, tell me if you want any help.
The other modifs are only the right way to achieve the same goal. For example, one cannot assume that the last order is indexed by OrderTotal(): first it should be OrderTotal()-1, but this may also be wrong because it depends of the sorted collumn of the "trade" tab of the terminal or if you have opened positions on other pairs.
I tryed to keep most of your code, and I keep also most of it's lack, for example, don't play with manual pendings on the same pair: all will be wrong...
It will be possible to improve the security if you find that this basic EA is profitable.
"also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order."
Now the SL is based on the average price (as I understood your code). If you do not want to have a general SL, just don't modify them. Then the "modify" line should only modify the TP :
Sorry for the two mistakes, I am in hollyday and cannot check the code from here.
I made two modifications :
1) the EA use now only the positions of the pair on which he is attached.
2) the EA keep the direction of the first position (of that pair). This is needed is some TP / SL / entry configuration where the EA may open buys and sells simultaneously; then the average price, new TP and SL have no meanings anymore. It is possible to write an EA working in both direction at the same time but it's much more complex. The simplest way to do that is to work with two EAs with different MagicNumbers and only long / only short. If you need it, tell me if you want any help.
The other modifs are only the right way to achieve the same goal. For example, one cannot assume that the last order is indexed by OrderTotal(): first it should be OrderTotal()-1, but this may also be wrong because it depends of the sorted collumn of the "trade" tab of the terminal or if you have opened positions on other pairs.
I tryed to keep most of your code, and I keep also most of it's lack, for example, don't play with manual pendings on the same pair: all will be wrong...
It will be possible to improve the security if you find that this basic EA is profitable.
"also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order."
Now the SL is based on the average price (as I understood your code). If you do not want to have a general SL, just don't modify them. Then the "modify" line should only modify the TP :
BTW, I am not sure that "0" as OpenPrice() is correct in the curent line.
Ok, not sure what you said about the SL ? is it for each order?
so are you also saying that the EA cannot place a buy and sell with the same currency at the same time untill one of them is closed? can that be fixed? also, how do you add a little bullet (graphic) on the chart that shows when the order was placed?
Thanks
Ok, not sure what you said about the SL ? is it for each order?
so are you also saying that the EA cannot place a buy and sell with the same currency at the same time untill one of them is closed? can that be fixed? also, how do you add a little bullet (graphic) on the chart that shows when the order was placed?
Thanks
About the SL, both ideas may work:
if you want a general SL, ie the same level for all positions so they are all closing at the same time, use this line (This was the way your original code was intended to work):
In this second case, each order may hit its own SL separetaly; but keep in mind that the EA should then be more complex to work properly: if a position is closed by SL, the average price becomes wrong, so you have to recompute it at each tick and check if the other positions need to be modified.
About to buy and sell at the same time, you must understand that the average price, the SL and TP as they were defined in your code have no meaning if the directions are mixed. Just look at this example:
situation 1: you have a buy at 1.200 and a sell at 1.220;
situation 2: a buy at 1.200 and a buy at 1.220;
Your original code makes no distinction between both situations, but they are really different...
That's why I suggest you to add a MagicNumber to the EA and to work on one chart with the EA to open only the buys and on an other chart with the same EA but with another MagicNumber to open only the sells.
About the SL, both ideas may work:
if you want a general SL, ie the same level for all positions so they are all closing at the same time, use this line (This was the way your original code was intended to work):
In this second case, each order may hit its own SL separetaly; but keep in mind that the EA should then be more complex to work properly: if a position is closed by SL, the average price becomes wrong, so you have to recompute it at each tick and check if the other positions need to be modified.
About to buy and sell at the same time, you must understand that the average price, the SL and TP as they were defined in your code have no meaning if the directions are mixed. Just look at this example:
situation 1: you have a buy at 1.200 and a sell at 1.220;
situation 2: a buy at 1.200 and a buy at 1.220;
Your original code makes no distinction between both situations, but they are really different...
That's why I suggest you to add a MagicNumber to the EA and to work on one chart with the EA to open only the buys and on an other chart with the same EA but with another MagicNumber to open only the sells.
Ok, I don't mind then to only have one order per currency open at one time. So in effect there was no need to add MaxOpenOrders correct? since the EA will only open one order at a time. or will it open two or more orders but only in one direction (sells or buys) until all uni-positions are closed?
If you have time can you fix it so that it will recompute when I change the SL code as you mentioned plus work with multiple direction trades without using a magic number.
Thanks alot
Last edited by matrixebiz; 09-17-2007 at 01:51 AM.