12-01-2008, 11:56 AM
Junior Member
Join Date: Nov 2008
Posts: 4
Help needed to this powerful system.
Hi I have tried to make an EA for my system, let me try to explain:
I use the following indicators:
MACD(12,26,9)
Stoch(40,1,7)
EMA(60) applied to the Stoch-value
The buy signal is when:
The current MACD bar has a higher value than the previous bar and the EMA(60) is crossing the Stoch.
The sell signal is the opposite of the buy signal.
I have tried to code the EA so when the buy signal is showing it's supposed to close all sell-positions, but it's not working probably... The only way I could find the EMA(60) of the stoch-values was to make a indicator, which I called "Stoch_MA".
Can anyone please look at my code and see what's wrong? - and possibly improve it?
Thanks
The EA:
Code:
//+------------------------------------------------------------------+
//| davdkh v3.mq4 |
//| davdkh Copyright © 2008 |
//| |
//+------------------------------------------------------------------+
#property copyright "davdkh Copyright © 2008"
#property link ""
extern double TakeProfit=10;
extern double Lots=0.2;
extern double StopLoss=30;
extern double Risk=0.05;
string NameExpert="davdkh is thinking for you!";
int klartilstart;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Comment("davdkh is thinking for you!");
klartilstart=0;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
string ResultSignal=VerifySignal();
if(ResultSignal!="")
{
if(klartilstart==0)
{
Comment("davdkh has found a entry... is proceding");
if(ResultSignal=="buy")
{
OpenBuy();
ExitSell();
}
else if (ResultSignal=="sell")
{
OpenSell();
ExitBuy();
}
}
else Comment("davdkh is analyzing");
}
else
{
klartilstart=0;
Comment("davdkh is analyzing");
Modify();
}
//----
return(0);
}
//+------------------------------------------------------------------+
string VerifySignal()
{
double rsi[];
for(int i=0 ; i<=60 ; i++)
{
rsi[i]=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,i);
}
double MA=iCustom(Symbol(),0,"Stoch_MA",1,0);
double Stoch=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,0);
double MACDc=iMACD(Symbol(),0,12,26,9,0,MODE_MAIN,0);
double MACDp=iMACD(Symbol(),0,12,26,9,0,MODE_MAIN,2);
if(MA==Stoch && MACDc>MACDp)
return("buy");
if(MA==Stoch && MACDc<MACDp)
return("sell");
return("");
}
//+------------------------------------------------------------------+
void OpenBuy()
{
while(true)
{
RefreshRates();
OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, NameExpert,0,0,Blue);
break;
}
klartilstart++;
}
//+------------------------------------------------------------------+
void OpenSell()
{
while(true)
{
RefreshRates();
OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, NameExpert,0,0,Red);
break;
}
klartilstart++;
}
//+------------------------------------------------------------------+
double size()
{
double mr = (MarketInfo(Symbol(), MODE_MARGINREQUIRED)/10);
double mRisk = MathFloor((AccountBalance() / mr) * Risk);
double ls = mRisk * 0.1;
return(ls);
}
//+------------------------------------------------------------------+
void Modify()
{
RefreshRates();
bool result;
double stop_loss, point, TP;
int cmd,total,error;
//----
total=OrdersTotal();
point=MarketInfo(Symbol(),MODE_POINT);
//----
for(int i=0; i<total; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
//---- print selected order
OrderPrint();
cmd=OrderType();
TP=OrderTakeProfit();
//---- buy or sell orders are considered
if(cmd==OP_BUY || cmd==OP_SELL)
{
//---- modify first market order
while(true)
{
if(cmd==OP_BUY) stop_loss=OrderOpenPrice();
else stop_loss=OrderOpenPrice();
result=OrderModify(OrderTicket(),OrderOpenPrice(),stop_loss+5*Point,OrderOpenPrice()+TakeProfit*Point,0,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
//---- print modified order (it still selected after modify)
OrderPrint();
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
//----
return(0);
//----
}
void ExitBuy()
{
while(true)
{
RefreshRates();
bool result2;
double point2;
int cmd2,total2,error2;
//----
total2=OrdersTotal();
point2=MarketInfo(Symbol(),MODE_POINT);
//----
for(int e=0; e<total2; e++)
{
if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
{
//---- print selected order
OrderPrint();
cmd2=OrderType();
//---- buy or sell orders are considered
if(cmd2==OP_BUY || cmd2==OP_SELL)
{
//---- modify first market order
while(true)
{
if(cmd2==OP_BUY)
{
result2=OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
if(result2!=TRUE) { error2=GetLastError(); Print("LastError = ",error2); }
else error2=0;
if(error2==135) RefreshRates();
else break;
}
}
//---- print modified order (it still selected after modify)
OrderPrint();
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
klartilstart--;
}
}
void ExitSell()
{
while(true)
{
RefreshRates();
bool result2;
double point2;
int cmd2,total2,error2;
//----
total2=OrdersTotal();
point2=MarketInfo(Symbol(),MODE_POINT);
//----
for(int e=0; e<total2; e++)
{
if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
{
//---- print selected order
OrderPrint();
cmd2=OrderType();
//---- buy or sell orders are considered
if(cmd2==OP_BUY || cmd2==OP_SELL)
{
//---- modify first market order
while(true)
{
if(cmd2==OP_BUY)
{
result2=OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
if(result2!=TRUE) { error2=GetLastError(); Print("LastError = ",error2); }
else error2=0;
if(error2==135) RefreshRates();
else break;
}
}
//---- print modified order (it still selected after modify)
OrderPrint();
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
}
klartilstart--;
}
12-01-2008, 11:57 AM
Junior Member
Join Date: Nov 2008
Posts: 4
and the indicator:
And the indicator:
Code:
//+------------------------------------------------------------------+
//| Stoch_MA.mq4 |
//| davdkh Copyright © 2008 |
//| |
//+------------------------------------------------------------------+
#property copyright "davdkh Copyright © 2008"
#property link ""
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- buffers
double ExtMapBuffer1[1000];
double ExtMapBuffer2[1000];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorDigits(2);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
string short_name = "Stoch_MA is running";
IndicatorShortName(short_name);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
int bar, limit;
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-IndicatorCounted();
for(bar=0; bar<limit; bar++)
ExtMapBuffer1[bar]=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,bar);
for(bar=0; bar<limit; bar++)
ExtMapBuffer2[bar]=iMAOnArray(ExtMapBuffer1,Bars,60,0,MODE_EMA,bar);
//----
return(0);
}
//+------------------------------------------------------------------+
12-04-2008, 02:20 AM
Senior Member
Join Date: May 2006
Posts: 837
Does anyone have an EA that uses a bar indicator, to buy and sell when the bar changes colour>
__________________
New to Forex? Get all you need for Free at my site
Click Here You can also get my Hot Forex System (scroll to the bottom of the page):
Click Here
12-04-2008, 02:51 AM
Senior Member
Join Date: Aug 2006
Posts: 456
Very interesting!
Quote:
Originally Posted by
davdkh
Hi I have tried to make an EA for my system, let me try to explain:
I use the following indicators:
MACD(12,26,9)
Stoch(40,1,7)
EMA(60) applied to the Stoch-value
The buy signal is when:
The current MACD bar has a higher value than the previous bar and the EMA(60) is crossing the Stoch.
The sell signal is the opposite of the buy signal.
I have tried to code the EA so when the buy signal is showing it's supposed to close all sell-positions, but it's not working probably... The only way I could find the EMA(60) of the stoch-values was to make a indicator, which I called "Stoch_MA".
Can anyone please look at my code and see what's wrong? - and possibly improve it?
Thanks
The EA:
Code:
//+------------------------------------------------------------------+
//| davdkh v3.mq4 |
//| davdkh Copyright © 2008 |
//| |
//+------------------------------------------------------------------+
#property copyright "davdkh Copyright © 2008"
#property link ""
extern double TakeProfit=10;
extern double Lots=0.2;
extern double StopLoss=30;
extern double Risk=0.05;
string NameExpert="davdkh is thinking for you!";
int klartilstart;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Comment("davdkh is thinking for you!");
klartilstart=0;
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
string ResultSignal=VerifySignal();
if(ResultSignal!="")
{
if(klartilstart==0)
{
Comment("davdkh has found a entry... is proceding");
if(ResultSignal=="buy")
{
OpenBuy();
ExitSell();
}
else if (ResultSignal=="sell")
{
OpenSell();
ExitBuy();
}
}
else Comment("davdkh is analyzing");
}
else
{
klartilstart=0;
Comment("davdkh is analyzing");
Modify();
}
//----
return(0);
}
//+------------------------------------------------------------------+
string VerifySignal()
{
double rsi[];
for(int i=0 ; i<=60 ; i++)
{
rsi[i]=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,i);
}
double MA=iCustom(Symbol(),0,"Stoch_MA",1,0);
double Stoch=iStochastic(Symbol(),0,40,1,7,MODE_EMA,1,MODE_MAIN,0);
double MACDc=iMACD(Symbol(),0,12,26,9,0,MODE_MAIN,0);
double MACDp=iMACD(Symbol(),0,12,26,9,0,MODE_MAIN,2);
if(MA==Stoch && MACDc>MACDp)
return("buy");
if(MA==Stoch && MACDc<MACDp)
return("sell");
return("");
}
//+------------------------------------------------------------------+
void OpenBuy()
{
while(true)
{
RefreshRates();
OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, NameExpert,0,0,Blue);
break;
}
klartilstart++;
}
//+------------------------------------------------------------------+
void OpenSell()
{
while(true)
{
RefreshRates();
OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, NameExpert,0,0,Red);
break;
}
klartilstart++;
}
//+------------------------------------------------------------------+
double size()
{
double mr = (MarketInfo(Symbol(), MODE_MARGINREQUIRED)/10);
double mRisk = MathFloor((AccountBalance() / mr) * Risk);
double ls = mRisk * 0.1;
return(ls);
}
//+------------------------------------------------------------------+
void Modify()
{
RefreshRates();
bool result;
double stop_loss, point, TP;
int cmd,total,error;
//----
total=OrdersTotal();
point=MarketInfo(Symbol(),MODE_POINT);
//----
for(int i=0; i<total; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
//---- print selected order
OrderPrint();
cmd=OrderType();
TP=OrderTakeProfit();
//---- buy or sell orders are considered
if(cmd==OP_BUY || cmd==OP_SELL)
{
//---- modify first market order
while(true)
{
if(cmd==OP_BUY) stop_loss=OrderOpenPrice();
else stop_loss=OrderOpenPrice();
result=OrderModify(OrderTicket(),OrderOpenPrice(),stop_loss+5*Point,OrderOpenPrice()+TakeProfit*Point,0,CLR_NONE);
if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
else error=0;
if(error==135) RefreshRates();
else break;
}
//---- print modified order (it still selected after modify)
OrderPrint();
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
//----
return(0);
//----
}
void ExitBuy()
{
while(true)
{
RefreshRates();
bool result2;
double point2;
int cmd2,total2,error2;
//----
total2=OrdersTotal();
point2=MarketInfo(Symbol(),MODE_POINT);
//----
for(int e=0; e<total2; e++)
{
if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
{
//---- print selected order
OrderPrint();
cmd2=OrderType();
//---- buy or sell orders are considered
if(cmd2==OP_BUY || cmd2==OP_SELL)
{
//---- modify first market order
while(true)
{
if(cmd2==OP_BUY)
{
result2=OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
if(result2!=TRUE) { error2=GetLastError(); Print("LastError = ",error2); }
else error2=0;
if(error2==135) RefreshRates();
else break;
}
}
//---- print modified order (it still selected after modify)
OrderPrint();
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
klartilstart--;
}
}
void ExitSell()
{
while(true)
{
RefreshRates();
bool result2;
double point2;
int cmd2,total2,error2;
//----
total2=OrdersTotal();
point2=MarketInfo(Symbol(),MODE_POINT);
//----
for(int e=0; e<total2; e++)
{
if(OrderSelect(e,SELECT_BY_POS,MODE_TRADES))
{
//---- print selected order
OrderPrint();
cmd2=OrderType();
//---- buy or sell orders are considered
if(cmd2==OP_BUY || cmd2==OP_SELL)
{
//---- modify first market order
while(true)
{
if(cmd2==OP_BUY)
{
result2=OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE);
if(result2!=TRUE) { error2=GetLastError(); Print("LastError = ",error2); }
else error2=0;
if(error2==135) RefreshRates();
else break;
}
}
//---- print modified order (it still selected after modify)
OrderPrint();
break;
}
}
else { Print( "Error when order select ", GetLastError()); break; }
}
}
klartilstart--;
}
Are the stochastic settings %K..40 %D..1 slowing..7 ?
What timeframe do you use this on?
12-08-2008, 01:34 PM
Junior Member
Join Date: Nov 2008
Posts: 4
I use it on timeframe h1
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT. The time now is 09:00 PM .