I'm trying to piece together an EA based on the cashbackforex website.
I have got the basics running, but am having a problem trying to add the trailing stop and money management codes.
Can someone take a look and tell me why the features won't work? If I scratch out the MM codes, the at least the EA works, but the trailing stop still doesn't function. With the MM code active, it won't even open any trades.
Thanks in advance.
I have got the basics running, but am having a problem trying to add the trailing stop and money management codes.
Can someone take a look and tell me why the features won't work? If I scratch out the MM codes, the at least the EA works, but the trailing stop still doesn't function. With the MM code active, it won't even open any trades.
Thanks in advance.
Inserted Code
// Section 1:
// Preprocessor Directives, External & Internal Variables
#property copyright "Copyright © 2008-2010, ForexRazor.Com"
#property link "http://www.forexrazor.com/"
//----------------------- EA PARAMETER
extern string Expert_Name = "---------- STOCHcross";
extern double MagicNumber = 59483;
extern string MM_Parameters = "---------- Money Management";
extern double Lots = 1;
extern bool MM = false, //Use Money Management or not
FromBalance = false; //Use Balance instead of Margin
extern int Risk = 10; //10%
extern string Order_Setting = "---------- Order Setting";
extern bool OppositeClose = true;
extern bool EnterOpenBar = true;
extern double TakeProfit =100;
extern double StopLoss = 75;
extern string TrailingStop_Setting = "---------- Trailing Stop Setting";
extern double TrailingStart = 50;
extern double TrailingStop = 25;
extern double TrailingStep = 5;
extern string Indicator_Setting = "---------- Indicator Setting";
extern int KPeriod = 14,
DPeriod = 4,
Slowing = 5;
extern double UnderSoldLevel = 30,
OverBoughtLevel = 70;
extern int Slippage = 5;
// Global Variables
int Counter, vSlippage;
double ticket, number, vPoint;
double sl, tp;
double kline[3], dline[3];
bool crossedup, crosseddown;
int longtkt, shorttkt, cnt, t;
// Section 2: Initialization
int init(){
crossedup = false;
crosseddown = false;
t = 0;
longtkt = -1;
shorttkt = -1;
cnt = 0;
if(Digits==3 || Digits==5)
{ vPoint=Point*10; vSlippage=Slippage*10; }
else{ vPoint=Point; vSlippage=Slippage; }
return(0); }
//-------------------------------------------------------
// Section 3: Start
int start()
{
crossedup = false;
crosseddown = false;
if(Bars<100) { Print("Bars less than 100");
return(0); }
//--------------------------------------------------------
// Section 3A: Define ShortCuts to Common Functions
int Total, OType=-1, Ticket;
double Price, SL, TP, Lot;
bool CloseBuy=false, CloseSell=false, OpenBuy=false, OpenSell=false;
for(int Counter=1; Counter<=OrdersTotal(); Counter++)
{
if (OrderSelect(Counter-1,SELECT_BY_POS)==true)
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
Ticket=OrderTicket();
OType =OrderType();
Price =OrderOpenPrice();
SL =OrderStopLoss();
TP =OrderTakeProfit();
Lot =OrderLots();
}
}
//----------------------------------------------------
// Section 3B: Indicator Calling
for(int i = 0;i < 3;i++)
{
kline[i] = iStochastic(NULL, Period(), KPeriod, DPeriod, Slowing, MODE_SMA, 0, MODE_MAIN, i);
dline[i] = iStochastic(NULL, Period(), KPeriod, DPeriod, Slowing, MODE_SMA, 0, MODE_SIGNAL, i);
}
if ((kline[1] < dline[1]) && (kline[2] > dline[2])) crosseddown = true;
else if ((kline[1] > dline[1]) && (kline[2] < dline[2])) crossedup = true;
else return(0);
//------------------------------------------------
// Section 3C: Entry Conditions
bool OpenBar=true;
if(EnterOpenBar) if(iVolume(NULL,0,0)>1) OpenBar=false;
if (crossedup && (kline[1] < UnderSoldLevel)
&& OpenBar){
OpenBuy=true;
if (OppositeClose) CloseSell=true;
}
if (crosseddown && (kline[1] > OverBoughtLevel)
&& OpenBar){
OpenSell=true;
if (OppositeClose) CloseBuy=true;
}
//-------------------------------------------------
// Section 3D: Close Conditions
while(true)
{
if (OType==0 && CloseBuy==true)
{
close (OP_BUY); // Close Buy
return;
}
if (OType==1 && CloseSell==true)
{
close (OP_SELL); // Close Sell
return;
}
break;
}
//--------------------------------------------------
// Section 3E: Order Placement
while(true)
{
if (OrdersTotalMagicOpen()==0 && OpenBuy==true)
{
if(StopLoss>0){SL=Ask - StopLoss*vPoint;}else{SL=0;} if(TakeProfit>0){TP=Ask+TakeProfit*vPoint;}else{TP=0;}
ticket=0;number=0;
while(ticket<=0 && number<100){
RefreshRates();
ticket = OrderSend(Symbol(),OP_BUY, Lots, Ask,vSlippage,SL,TP,Expert_Name, MagicNumber, 0, Green);
return (ticket);
}}
if (OrdersTotalMagicOpen()==0 && OpenSell==true)
{
if(StopLoss>0){SL=Bid + StopLoss*vPoint;}else{SL=0;} if(TakeProfit>0){TP=Bid-TakeProfit*vPoint;}else{TP=0;}
ticket=0;number=0;
while(ticket<=0 && number<100){
RefreshRates();
ticket= OrderSend(Symbol(),OP_SELL, Lots, Bid,vSlippage,SL,TP, Expert_Name, MagicNumber, 0, Red);
return (ticket);
}}
break;
}
//---------------------------------------------------------
return; // End of start()
}
// Section 4A: Close Function
void OnTick()
{
if(MM)CalculateMM();
}
void close(int type){
if(OrdersTotal()>0){
for(Counter=OrdersTotal()-1;Counter>=0;Counter--){
OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES);
if(type==OP_BUY && OrderType()==OP_BUY){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) {
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits), vSlippage);
} }
if(type==OP_SELL && OrderType()==OP_SELL){
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) {
RefreshRates(); OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),vSlippage);
}}
}}}
// Section 4B: OrdersTotalMagicOpen Function
int OrdersTotalMagicOpen() {
int l_count_0 = 0;
for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_SELL || OrderType() == OP_BUY) l_count_0++;
}
return (l_count_0);
}
void TrailOrder(double Trailingstart,double Trailingstop){
int ticket = 0;
double tStopLoss = NormalizeDouble(OrderStopLoss(), Digits); // Stop Loss
int cnt,vPoint,vSlippage;
double sl = OrderStopLoss(); // Stop Loss
RefreshRates();
if(OrdersTotal()>0){
for(cnt=OrdersTotal();cnt>=0;cnt--){
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()
&& OrderMagicNumber()==MagicNumber){
if(OrderType()==OP_BUY){
if(Ask> NormalizeDouble(OrderOpenPrice()+TrailingStart* vPoint,Digits)
&& tStopLoss < NormalizeDouble(Bid-(TrailingStop+TrailingStep)*vPoint,Digits)){
tStopLoss = NormalizeDouble(Bid-TrailingStop*vPoint,Digits);
ticket = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLoss,OrderTakeProfit(),0,Blue);
if (ticket > 0){
Print ("TrailingStop #2 Activated: ", OrderSymbol(), ": SL", tStopLoss, ": Bid", Bid);
return(0);
}}}
if (OrderType()==OP_SELL) {
if (Bid < NormalizeDouble(OrderOpenPrice()-TrailingStart*vPoint,Digits)
&& (sl >(NormalizeDouble(Ask+(TrailingStop+TrailingStep)*vPoint,Digits)))
|| (OrderStopLoss()==0)){
tStopLoss = NormalizeDouble(Ask+TrailingStop*vPoint,Digits);
ticket = OrderModify(OrderTicket(),OrderOpenPrice(),tStopLoss,OrderTakeProfit(),0,Red);
if (ticket > 0){
Print ("Trailing #2 Activated: ", OrderSymbol(), ": SL ",tStopLoss, ": Ask ", Ask);
return(0);
}}}
}}}}
//----------------------- MONEY MANAGEMENT FUNCTION
void CalculateMM()
{
double MinLots=MarketInfo(Symbol(),MODE_MINLOT);
double MaxLots=MarketInfo(Symbol(),MODE_MAXLOT);
{
if(FromBalance==false) Lots=AccountFreeMargin()/100000*Risk;
else if(FromBalance==true) Lots=AccountBalance()/100000*Risk;
}
Lots=MathMin(MaxLots,MathMax(MinLots,Lots));
if(MinLots<0.1)Lots=NormalizeDouble(Lots,2);
else
{
if(MinLots<1)Lots=NormalizeDouble(Lots,1);
else Lots=NormalizeDouble(Lots,0);
}
if(Lots<MinLots)Lots=MinLots;
if(Lots>MaxLots)Lots=MaxLots;
} Attached File(s)