//+------------------------------------------------------------------+ //| RSI & CCI.mq4 | //| | //| | //+------------------------------------------------------------------+ #define MAGICMA 777 extern int cci_period = 14; extern int rsi_period = 14; extern int cci_applied_price = 0; extern int rsi_applied_price = 0; extern double Lots = 0.1; extern double MaximumRisk = 0.02; extern double DecreaseFactor = 3; extern double MovingShift = 6; //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //---- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ double LotsOptimized() { double lot=Lots; int orders=HistoryTotal(); // history orders total int losses=0; // number of losses orders without a break //---- select lot size lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //---- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for(int i=orders-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- if(OrderProfit()>0) break; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); } //---- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double cci_curr=iCCI(NULL,0,cci_period,cci_applied_price,0); double cci_prev=iCCI(NULL,0,cci_period,cci_applied_price,1); double rsi_curr=iCCI(NULL,0,cci_period,rsi_applied_price,0); double rsi_prev=iCCI(NULL,0,cci_period,rsi_applied_price,1); int res; //---- buy conditions if(cci_curr > 0 && cci_prev <= 0 && rsi_curr > 50 && rsi_prev <=50) { res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"Volume: "+Volume[0]+"Time: "+TimeCurrent(),MAGICMA,0,Green); return; } //---- sell conditions if(cci_curr < 0 && cci_prev >= 0 && rsi_curr < 50 && rsi_prev >=50) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"Volume: "+Volume[0]+"Time: "+TimeCurrent(),MAGICMA,0,Red); return; } //---- } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { double cci_curr=iCCI(NULL,0,cci_period,cci_applied_price,0); double cci_prev=iCCI(NULL,0,cci_period,cci_applied_price,1); double rsi_curr=iCCI(NULL,0,cci_period,rsi_applied_price,0); double rsi_prev=iCCI(NULL,0,cci_period,rsi_applied_price,1); for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue; //---- check order type if(OrderType()==OP_BUY) { if(cci_curr < 0 && cci_prev >= 0 && rsi_curr < 50 && rsi_prev >=50) OrderClose(OrderTicket(),OrderLots(),Bid,3,White); break; } if(OrderType()==OP_SELL) { if(cci_curr > 0 && cci_prev <= 0 && rsi_curr > 50 && rsi_prev <=50) OrderClose(OrderTicket(),OrderLots(),Ask,3,White); break; } } //---- } //+------------------------------------------------------------------+ //| Start function | //+------------------------------------------------------------------+ void start() { //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //---- calculate open orders by current symbol if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else CheckForClose(); //---- } //+------------------------------------------------------------------+
--
Paul Ruffalo
Thanks for the code..
how about the code
open entry at Mom(12) cross up at 100 and close trade by cross CCi(12) at cross at 0
sell if Mom(12) cross down at 100 and close at CCi(12) cross 0
15M time frame
all price at HL/3
Thanks for the code..
how about the code
open entry at Mom(12) cross up at 100 and close trade by cross CCi(12) at cross at 0
sell if Mom(12) cross down at 100 and close at CCi(12) cross 0
15M time frame
all price at HL/3
How about ... A bottle of champing, gour-mae dinners, concert tickets and heck while we're at it, red sports car and a hot date. lol...
Thanks for the code
--
Paul Ruffalo
you're welcome.
ernst
Thanks for the code..
how about the code
open entry at Mom(12) cross up at 100 and close trade by cross CCi(12) at cross at 0
sell if Mom(12) cross down at 100 and close at CCi(12) cross 0
15M time frame
all price at HL/3
//+------------------------------------------------------------------+ //| Momentum.mq4 | //| | //| | //+------------------------------------------------------------------+ #define BuyOrder 111 #define SellOrder 222 extern int mom_period = 12; extern int cci_period = 12; extern int mom_applied_price = 5; // HLC/3 extern int cci_applied_price = 5; extern int TimeFrame = 15; //PERIOD_M15 extern double Lots = 1.0; extern double MaximumRisk = 0.02; extern double DecreaseFactor = 3; extern double IncreaseFactor = 3; //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==BuyOrder || OrderMagicNumber()==SellOrder) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //---- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ double LotsOptimized() { double lot=Lots; int orders=HistoryTotal(); // history orders total int losses=0,gains=0; // number of losses orders without a break //---- select lot size lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //---- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for(int i=orders-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- if(OrderProfit()>0) gains++; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); if(gains>1) lot=NormalizeDouble(lot+lot*gains/IncreaseFactor,1); } //---- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double mom_curr=iMomentum(NULL,TimeFrame,mom_period,mom_applied_price,0); double mom_prev=iMomentum(NULL,TimeFrame,mom_period,mom_applied_price,1); int res; //---- buy conditions if(mom_curr > 100 && mom_prev <= 100) { res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"Volume: "+Volume[0]+"Time: "+TimeCurrent(),BuyOrder,0,Green); return; } //---- sell conditions if(mom_curr < 100 && mom_prev >= 100) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"Volume: "+Volume[0]+"Time: "+TimeCurrent(),SellOrder,0,Red); return; } //---- } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { double cci_curr=iCCI(NULL,TimeFrame,cci_period,cci_applied_price,0); double cci_prev=iCCI(NULL,TimeFrame,cci_period,cci_applied_price,1); for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=BuyOrder && OrderMagicNumber()!=SellOrder || OrderSymbol()!=Symbol()) continue; //---- check order type if(OrderType()==OP_BUY) { if(cci_curr < 0 && cci_prev >= 0) OrderClose(OrderTicket(),OrderLots(),Bid,3,White); break; } if(OrderType()==OP_SELL) { if(cci_curr > 0 && cci_prev <= 0) OrderClose(OrderTicket(),OrderLots(),Ask,3,White); break; } } //---- } //+------------------------------------------------------------------+ //| Start function | //+------------------------------------------------------------------+ void start() { //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //---- calculate open orders by current symbol if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else CheckForClose(); //---- } //+------------------------------------------------------------------+
//| Momentum.mq4 |
//| i make some modification and please help to fine tune |
//| curently back test is profitable |
//+------------------------------------------------------------------+
#define BuyOrder 111
#define SellOrder 222
extern int mom_period = 12;
extern int cci_period = 12;
extern int mom_applied_price = 5; // HLC/3
extern int cci_applied_price = 5;
extern int TimeFrame = 15; //PERIOD_M15
extern double Lots = 1.0;
extern double MaximumRisk = 0.02;
extern double DecreaseFactor = 3;
extern double IncreaseFactor = 3;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==BuyOrder || OrderMagicNumber()==SellOrder)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0,gains=0; // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) gains++;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
if(gains>1) lot=NormalizeDouble(lot+lot*gains/IncreaseFactor,1);
}
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double mom_curr=iMomentum(NULL,TimeFrame,5,0,0);
double mom_prev=iMomentum(NULL,TimeFrame,5,0,1);
double cci_curr5=iCCI(NULL,TimeFrame,5,cci_applied_price,0);
double cci_curr17=iCCI(NULL,TimeFrame,17,cci_applied_price,0);
int res;
//---- buy conditions
if((mom_curr > 100 && mom_prev < 100) && (cci_curr5 > cci_curr17) && cci_curr5 > 0)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"Volume: "+Volume[0]+"Time: "+TimeCurrent(),BuyOrder,0,Green);
return;
}
//---- sell conditions
if((mom_curr < 100 && mom_prev > 100) && (cci_curr17 > cci_curr5) && cci_curr5 < 0)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"Volume: "+Volume[0]+"Time: "+TimeCurrent(),SellOrder,0,Red);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
// double cci_curr=iCCI(NULL,TimeFrame,cci_period,cci_applied_price,0);
// double cci_prev=iCCI(NULL,TimeFrame,cci_period,cci_applied_price,1);
double rsi_curr=iRSI(NULL,TimeFrame,5,0,0);
double rsi_prev=iRSI(NULL,TimeFrame,5,0,1);
double mom_curr=iMomentum(NULL,TimeFrame,125,0,0);
double mom_prev=iMomentum(NULL,TimeFrame,125,0,1);
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=BuyOrder && OrderMagicNumber()!=SellOrder || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if(rsi_curr < 70 && rsi_prev >=70 && mom_curr <=100 && mom_prev > 100) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
if(rsi_curr > 35 && rsi_prev <=35 && mom_curr >=100 && mom_prev < 100 ) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Momentum.mq4 |
//| i make some modification and please help to fine tune |
//| curently back test is profitable |
//+------------------------------------------------------------------+
just a suggestion: no need to use heaps of oscillators. one should do the trick.
extern int mom_period = 5; extern int fast_cci_period = 5; extern int slow_cci_period = 17; extern int rsi_period = 5; extern int mom_close_period = 125; extern int mom_applied_price = 5; // HLC/3 extern int cci_applied_price = 5; extern int TimeFrame = 15; //PERIOD_M15 extern double Lots = 1.0; extern double MaximumRisk = 0.02; extern double DecreaseFactor = 2; extern double IncreaseFactor = 2; extern int Expert_ID = 1234; int _MagicNumber = 0; double LotsOptimized() { double lot=Lots; int orders=HistoryTotal(); // history orders total int losses=0,gains=0; // number of losses orders without a break //---- select lot size lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //---- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for( int i=orders-1;i>=0;i-- ) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- if(OrderProfit()>0) gains++; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); if(gains>1) lot=NormalizeDouble(lot+lot*gains/IncreaseFactor,1); } //---- return lot size if(lot<0.1) lot=0.1; return(lot); } int init() { int Period_ID = 0; switch ( Period() ) { case PERIOD_MN1: Period_ID = 9; break; case PERIOD_W1: Period_ID = 8; break; case PERIOD_D1: Period_ID = 7; break; case PERIOD_H4: Period_ID = 6; break; case PERIOD_H1: Period_ID = 5; break; case PERIOD_M30: Period_ID = 4; break; case PERIOD_M15: Period_ID = 3; break; case PERIOD_M5: Period_ID = 2; break; case PERIOD_M1: Period_ID = 1; break; } _MagicNumber = Expert_ID * 10 + Period_ID; return(0); } int start() { double mom_curr=iMomentum(NULL,TimeFrame,mom_period,mom_applied_price,0); double mom_prev=iMomentum(NULL,TimeFrame,mom_period,mom_applied_price,1); double fast_cci_curr=iCCI(NULL,TimeFrame,fast_cci_period,cci_applied_price,0); double slow_cci_curr=iCCI(NULL,TimeFrame,slow_cci_period,cci_applied_price,0); double mom_close_curr=iMomentum(NULL,TimeFrame,mom_close_period,mom_applied_price,0); double mom_close_prev=iMomentum(NULL,TimeFrame,mom_close_period,mom_applied_price,1); double rsi_curr=iRSI(NULL,TimeFrame,rsi_period,mom_applied_price,0); double rsi_prev=iRSI(NULL,TimeFrame,rsi_period,mom_applied_price,1); double momentum_level=100; double cci_level=0; double rsi_long_level=70; double rsi_short_level=35; int _GetLastError = 0, _OrdersTotal = OrdersTotal(); //---- search in all open positions for ( int z = _OrdersTotal - 1; z >= 0; z -- ) { //---- if an error occurs when searching for a position, go to the next one if ( !OrderSelect( z, SELECT_BY_POS ) ) { _GetLastError = GetLastError(); Print( "OrderSelect( ", z, ", SELECT_BY_POS ) - Error #", _GetLastError ); continue; } //---- if a position is opened not for the current symbol, skip it if ( OrderSymbol() != Symbol() ) continue; //---- if the MagicNumber is not equal to _MagicNumber, skip this position if ( OrderMagicNumber() != _MagicNumber ) continue; //---- if a BUY position is opened, if ( OrderType() == OP_BUY ) { if ( rsi_curr < rsi_long_level && rsi_prev >= rsi_long_level && mom_curr <= momentum_level && mom_prev > momentum_level) { //---- close the position if ( !OrderClose( OrderTicket(), OrderLots(), Bid, 5, RoyalBlue ) ) { _GetLastError = GetLastError(); Alert( "Error OrderClose # ", _GetLastError ); return(-1); } } //---- if the alert has not been changed, quit: it is too early to open a new position else return(0); } //---- if a SELL position is opened, if ( OrderType() == OP_SELL ) { if ( rsi_curr > rsi_short_level && rsi_prev <= rsi_short_level && mom_curr >= momentum_level && mom_prev < momentum_level ) { //---- close the position if ( !OrderClose( OrderTicket(), OrderLots(), Ask, 5, LimeGreen ) ) { _GetLastError = GetLastError(); Alert( "Error OrderClose # ", _GetLastError ); return(-1); } } //---- if the alert has not changed, quit: it is too early to open a new position else return(0); } } //+------------------------------------------------------------------+ //| if execution reached this point, there is no an open position | //| check whether it is still possible to open a position | //+------------------------------------------------------------------+ if ( mom_curr > momentum_level && mom_prev <= momentum_level && fast_cci_curr > slow_cci_curr && fast_cci_curr > cci_level ) { //---- open a BUY position if ( OrderSend( Symbol(), OP_BUY, LotsOptimized(), Ask, 3, 0.0, 0.0, "Volume: "+Volume[0]+"Time: "+TimeCurrent(),_MagicNumber, 0, RoyalBlue ) < 0 ) { _GetLastError = GetLastError(); Alert( "Error OrderSend # ", _GetLastError ); return(-1); } return(0); } if ( mom_curr < momentum_level && mom_prev >= momentum_level && fast_cci_curr < slow_cci_curr && fast_cci_curr < cci_level ) { //---- open a SELL position if ( OrderSend( Symbol(), OP_SELL, LotsOptimized(), Bid, 3, 0.0, 0.0, "Volume: "+Volume[0]+"Time: "+TimeCurrent(),_MagicNumber, 0, LimeGreen ) < 0 ) { _GetLastError = GetLastError(); Alert( "Error OrderSend # ", _GetLastError ); return(-1); } return(0); } return(0); }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
EXAMPLE: IF CCI(14) > 0 AND RSI(14)>50 THEN ENTRY BUY
IF CCI(14)<=0 THEN CLOSE BUY
IF CCI(14)< 0 AND RSI(14)<50 THEN ENTRY SELL
IF RSI(14)>=50 THEN CLOSE SELL
YOUR TRADING WILL BE SIMPLE AND PROFITABLE...