This script is designed to automatically trade on the Forex market using a strategy based on the Commodity Channel Index (CCI) indicator. Here's a breakdown of its logic:
-
Setup:
- The script starts by defining some settings that the user can adjust, such as the CCI period (how many past data points to consider for CCI calculation), the CCI levels for triggering buy or sell signals, stop loss and take profit values, the lot size for each trade, and a magic number to identify the trades opened by this script.
-
Trading Logic:
- The script continuously monitors the CCI value.
- It uses two key CCI levels (an upper and a lower threshold) to identify potential buying or selling opportunities.
- If the CCI goes below the lower threshold (meaning the price is potentially oversold), the script remembers that it's looking for a buy opportunity.
- If the CCI then rises back above the lower threshold, the script executes a buy order (if trading time restrictions are fulfilled). In this case the script also tries to close all sell positions.
- Conversely, if the CCI goes above the upper threshold (meaning the price is potentially overbought), the script remembers that it's looking for a sell opportunity.
- If the CCI then falls back below the upper threshold, the script executes a sell order (if trading time restrictions are fulfilled). In this case the script also tries to close all buy positions.
- This ensures trades are opened when the CCI crosses back from extreme overbought or oversold areas.
- The script manages open trades based on the CCI indicator.
-
Order Execution:
- When a buy or sell signal is triggered, the script sends an order to the broker to open a position. It includes the desired lot size, stop loss (if defined), and take profit (if defined).
-
Time Restrictions:
- The script includes optional time-based restrictions. It can be configured to only trade during specific hours of the day.
-
Position Management:
- The script can close all existing buy or sell positions when a new signal in the opposite direction is generated. This function is controlled by the signals generated by the CCI.
-
Additional Functions:
- It has functions to check if there are already open positions of a certain type (buy or sell).
- It can calculate the total profit or loss of all open positions of a specific type.
//+------------------------------------------------------------------+
//| FT_CCI.mq4 |
//| FORTRADER.RU, Þðèé, ftyuriy@gmail.com |
//| http://FORTRADER.RU, Ôðàêòàëû + Ñðåäíèå |
//+------------------------------------------------------------------+
#property copyright "FORTRADER.RU, Þðèé, ftyuriy@gmail.com"
#property link "http://FORTRADER.RU, CCI"
/*Ðàçðàáîòàíî äëÿ 50 âûïóñêà æóðíàëà FORTRADER.Ru. Ñèñòåìà ïî CCI.
Îò÷åòû: http://finfile.ru/index.php/files/get/4LvxA6u0UY/ft-cci-result2.zip
Ñåò ôàéëû:http://finfile.ru/index.php/files/get/Kz2LK5Gnj6/eurusdh1.set
Îáñóæäåíèå: http://fxnow.ru/group_discussion_view.php?group_id=49&grouptopic_id=409&grouppost_id=3439#post_3439
Àðõèâ æóðíàëà: http://www.fortrader.ru/arhiv.php
50 âûïóñê: http://www.fortrader.ru/
*/
extern int cciper=14;
extern int ccUPur=200;
extern int ccDWur=-200;
extern int SL=0;
extern int TP=0;
extern int mn=1;
int err;
extern int MG=564651;
extern double Lots=0.1;
int start()
{
OpenPattern();//îòêðûâàåì ñäåëêè ïðè ìåðåñå÷åíèè
return(0);
}
int okbuy,oksell;
void OpenPattern()
{
double op,sl,tp;
double CCICurr=iCCI(Symbol(),0,cciper,PRICE_TYPICAL,1);
if(CCICurr<ccDWur) {okbuy=1;}
if(CCICurr>ccDWur && okbuy==1 ) {okbuy=0;
if(timecontrol()==1)
{
op=Ask;if(SL>0){sl=Ask-SL*Point*mn;}if(TP>0){tp=Ask+TP*Point*mn;}
err=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"6 FORTRADER.RU",MG,0,Red);
if(err<0){Print("OrderSend()- Îøèáêà OP_BUY. op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());return(-1);}
}
CloseAllPos(0);
}
if(CCICurr>ccUPur) {oksell=1;}
if(CCICurr<ccUPur && oksell==1) {oksell=0;
if(timecontrol()==1)
{
op=Bid;if(SL>0){sl=Bid+SL*Point*mn;}if(TP>0){tp=Bid-TP*Point*mn;}
err=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(op,Digits),3,NormalizeDouble(sl,Digits),NormalizeDouble(tp,Digits),"4 FORTRADER.RU",MG,0,Red);
if(err<0){Print("OrderSend()- Îøèáêà OP_SELL. op "+op+" sl "+sl+" tp "+tp+" "+GetLastError());return(-1);}
}
CloseAllPos(1);
}
}
//Çàêðûâàåì âñå ïîçèöèè ïî òèïó
int CloseAllPos(int type)
{//Îïèñàíèå ôóíêöèè: http://fxnow.ru/blog.php?user=Yuriy&blogentry_id=72
int buy=1; int sell=1;
int i,b=0;int ordertiket;
if(type==1)
{
while(buy==1)
{
buy=0;
for( i=0;i<OrdersTotal();i++)
{
if(true==OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() ){buy=1; OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);}
}else{buy=0;}
}
if(buy==0){return(0);}
}
}
if(type==0 )
{
while(sell==1)
{
sell=0;
for( i=0;i<OrdersTotal();i++)
{
if(true==OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() ){sell=1;OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); }
}else{sell=0;}
}
if(sell==0){return(0);}
}
}
return(0);
}
//ïðîâåðÿåò åñòü ëè îòêðûòûå îðäåðà
int ChPos(int type)
{//ïîäðîáíîå îïèñàíèå: http://fxnow.ru/blog.php?user=Yuriy&blogentry_id=100
int i;int col;
for( i=1; i<=OrdersTotal(); i++)
{
if(OrderSelect(i-1,SELECT_BY_POS)==true)
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && type==1&& OrderMagicNumber()==MG){col=1;}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && type==0&& OrderMagicNumber()==MG){col=1;}
}
}
return(col);
}
//ñóììèðóåò ðåçóëüòàò ïîçèöèé ïî òèïó
int SummPos(int type)
{//ïîäðîáíîå îïèñàíèå: http://fxnow.ru/blog.php?user=Yuriy&blogentry_id=100
int i;double summ;
for( i=1; i<=OrdersTotal(); i++)
{
if(OrderSelect(i-1,SELECT_BY_POS)==true)
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && type==1&& OrderMagicNumber()==MG){summ=summ+OrderProfit();}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && type==0&& OrderMagicNumber()==MG){summ=summ+OrderProfit();}
}
}
return(summ);
}
extern int time=0; //1 - âêëþ÷åíî, 0 - âûêëþ÷åíî.
extern int starttime = 7;
extern int stoptime = 17;
//Îãðàíè÷åíèå ïî âðåìåíè
int timecontrol()
{// Ïîäðîáíîå îïèñàíèå http://fxnow.ru/blog.php?user=Yuriy&blogentry_id=1
if ( ( (Hour()>=0 && Hour()<=stoptime-1) || (Hour()>=starttime && Hour()<=23)) && starttime>stoptime)
{
return(1);
}
if ( (Hour()>=starttime && Hour()<=stoptime-1) && starttime<stoptime)
{
return(1);
}
if(time==0){ return(1);}
return(0);
}
Comments