Here is an update. Enclosed you’ll find basic library functions that I’ve written.
Expert Advisor combining with indicator
- Moving Average
- psar
- cci
-macd
- volume
- accelerator
Important functions:
- get last order
- time: return time of candle, startHr, endHr
- trial version
- extended error checking
Order management
- closing or deleting buy, sell, selllimit, buylimit, sellstop, buystop
- lot increment (martingale style)
extern double lots=0.01;
extern int takeprofit=40;
extern int stoploss=40;
extern int magic=33454;
//checkMA
extern int period_MA=25;
//PSAR
extern double Step=0.02;
extern double Maximum=0.2;
extern double TimeFrame=0;
//cci
extern int period_cci=30;
//rsi
extern int period_rsi=9;
//bb
extern int period_bb=20;
extern int deviation_bb=2;
extern int shift_bb=2;
extern int apply_bb=PRICE_CLOSE;
//timer
extern int startHr=4;
extern int endHr=20;
extern double g=10;
bool check=true;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
//checkMA
bool check;
check=checkMA();
if(check==true) Alert("place buy");
else if(check==false) Alert("place sell");
//psar
bool check2;
check2=psar();
if(check==true)
{
closesell();
openbuy();
}
else if(check==false)
{
closebuy();
opensell();
}
//CCI
bool bill=cci();
if(bill==true && OrdersTotal()<=1)
{
closesell();
openbuy();
}
else if(bill==false && OrdersTotal()<=1)
{
closebuy();
opensell();
}
//macd
bool check3;
check=macd();
if(check==true)
{
closesell();
openbuy();
}
else if(check==false)
{
closebuy();
opensell();
}
//rsi
bill=rsi();
if(bill==true)
{
closebuy();
openbuy();
}
else if(bill==false)
{
closebuy();
opensell();
}
//volume
bool check4=volume();
if(check4==true)
{
openbuy();
}
else if(check==false)
{
opensell();
}
//accelator
bool c=accelator();
if(c==false && OrdersTotal()<1)
{
closesell();
openbuy();
}
else if(c==true && OrdersTotal()<1)
{
closebuy();
opensell();
}
//getlastorder
bool b=getlastorder();
if(b==true)
{
double lots=1.2; //if loss, then increase lotSize
//openbuy( lot);
}
else if(b==false) //previous closed order was in profit
{
lots=1;
//openbuy(lots);
}
//check time
bool t=checktime();
if(OrdersTotal()==0)
{
if(t==true && check==true)
{
openbuystop(0,0);
opensellstop(0,0);
check=false; //flag check, meaning: next hour_candle this code will execute again.
}
else if(t==false)
{
closebuy();
closesell();
deletepending();
check=true;
}
}
else if(OrdersTotal()==1)
{
deletepending();
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//checkMA
bool checkMA()
{
double ma=0;
ma=iMA(Symbol(),0,period_MA,0,MODE_SMA,PRICE_CLOSE,0);
if(Ask>ma) return(true);
else if(Ask<ma) return (false);
else return (EMPTY_VALUE);
}
//psar
bool psar()
{
double sar=iSAR(NULL,TimeFrame,Step,Maximum,0);
if(Ask>sar) return(true);
if(Ask<sar) return(false);
else return (EMPTY_VALUE);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool cci()
{
double point;
point=iCCI(NULL,0,period_cci,PRICE_TYPICAL,0);
if(point>0) return(true);
else if(point<0) return(false);
else return(EMPTY_VALUE);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool macd()
{
double ma;
ma=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
if(ma>0) return(true);
else if(ma<0) return(false);
else return(EMPTY_VALUE);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool rsi()
{
double r1,r2;
r1=iRSI(NULL,0,period_rsi,PRICE_CLOSE,0);
r2=iRSI(NULL,0,period_rsi,PRICE_CLOSE,1);
if(r2>70&&r1<70) return(false);
if(r2<30&&r1>30) return(true);
else return (EMPTY_VALUE);
}
//bolinger band
bool bb()
{
double b,c;
b=iBands(NULL, 0,period_bb,deviation_bb,shift_bb,apply_bb,MODE_LOWER,0);
c=iBands(NULL, 0, period_bb, deviation_bb, shift_bb, apply_bb, MODE_UPPER,0);
if(Close[0]<b && Open[0]>=b && b>Close[0])
{
return(false);
}
else if(Close[0]>c && Open[0]<=c && c<=Close[0])
{
return (true);
}
else return (EMPTY_VALUE);
}
//volume
bool volume()
{
int vol=iVolume(NULL,0,0);
if(vol>300) return (true);
else if(vol<300) return (false);
else return(EMPTY_VALUE);
}
//accelator
bool accelator()
{
double ac,ac1;
ac=iAC(NULL,0,0);
ac1=iAC(NULL,0,1);
if(ac1>0.000&&ac<0.000) return(true);
else if(ac1<0.000&&ac>0.000) return(false);
else return(EMPTY_VALUE);
}
//+------------------------------------------------------------------+
void closebuy()
{
for(int i=0; i<OrdersTotal();i++)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY)
{
int j=0;
while(j!=5)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Pink);
j++;
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void closesell()
{
for(int i=0; i<OrdersTotal();i++)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_SELL)
{
int j=0;
while(j!=5)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Pink);
j++;
}
}
}
}
//+------------------------------------------------------------------+
void deletebuy()
{
for(int i=0;i<OrdersTotal();i++)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUYSTOP)
{
OrderDelete(OrderTicket());
}
}
}
//+------------------------------------------------------------------+
void opensell()
{
OrderSend(Symbol(),OP_SELLLIMIT,lots,Bid,3,Bid+stoploss*Point,Bid-takeprofit*Point,NULL,magic,Red);
}
//+------------------------------------------------------------------+
void openbuy()
{
OrderSend(Symbol(),OP_BUY,lots,Ask,3,Ask-stoploss*Point,Ask+takeprofit*Point,NULL,magic,Red);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void openbuystop(double price,double stop)
{
if(OrderSend(Symbol(),OP_BUYSTOP,lots,Ask+g*Point,3,Ask+g*Point-stoploss*Point,Ask+g*Point+takeprofit*Point,NULL,magic,Pink)==true)
{
Print("Order send");
}
else fun_error(GetLastError());
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void opensellstop(double price,double stop)
{
OrderSend(Symbol(),OP_SELLSTOP,lots,price,3,stop,price-takeprofit*Point,NULL,magic,Red);
}
//+------------------------------------------------------------------+
bool deletepending()
{
for(int w=0;w<OrdersTotal();w++)
{
OrderSelect(w,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)
{
OrderDelete(OrderTicket());
}
}
return(0);
}
//getlastorder, for example, if last buy order was a loss, then double lot (martingale)
bool getlastorder()
{
for(int i=OrdersTotal();i<0;i--)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderType()==OP_BUY || OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderProfit()<0) return(true);
else if(OrderType()==OP_BUY || OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderProfit()>0)
return(false); //if previous closed order made profit, then false.
else return(EMPTY_VALUE);
}
return(0); //should I return 0 or EMPTY_VALUE again?
}
//+------------------------------------------------------------------+
double lotIncrement()
{
double lots=0;
for(int i=0;i<158800;i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
lots+=OrderLots();
}
}
Comment("total number of lots traded: ",lots);
return(lots);
}
//+------------------------------------------------------------------+
double Time() //return time of current candle
{
int hour=TimeHour(Time[0]);
int minute=TimeMinute(Time[0]);
int second=TimeSeconds(Time[0]);
Alert("Time of this candle is: ",hour,":",minute,":",second);
return(0);
}
//+------------------------------------------------------------------+
bool checktime()
{
int i=0;
int hourtime=TimeHour(Time[i]);
if(hourtime==startHr)return(true);
else if(hourtime==endHr) return (false);
else return (EMPTY_VALUE);
}
//+------------------------------------------------------------------+
int fun_error(int Error)
{
switch(Error)
{
case 4: Print("Trade server is busy");
Sleep(3000);
return(1);
default: Print("error",Error);
return(0);
}
}
//+------------------------------------------------------------------+
bool trial_version()
{ //TimeCurrent is the servers time, it cannot be 'hacked' by changing the client's time on his computer
datetime starts=D'24.02.2014';
datetime ends=D'5.03.2014';
if(TimeCurrent()>=starts && TimeCurrent()<=ends)
{
Print("EA is still in trial_mode");
}
else { Print("Trial mode expired"); }
return(0);
}
//+------------------------------------------------------------------+