extern int Expert_ID = 333;//
extern double Lot = 0.01; // deposit 1000 usd lot1.0 protit 1411 usd trade 18 times currency EURUSD M30 time 2015-11-01 to 2015-12-16 (46 days)
//extern double MAperiod = 10; // for Exness M30 EURUSD
//extern double K = 20;
//extern double D =4;
//extern double Slow = 4;
//extern int Movingback200 = 200; // old 320
//extern int Movingback100 = 100;
//extern int BBback = 180;
//extern int Movingbackvaryopen = 100;
//extern int Movingbackvaryclose = 100;
//extern int TP =180;
//extern int SL = 60;
extern double TrailingStop =0;// for time frame M1
extern double Shiftbackopenorder=26;
// close all code //
//extern int ProfitTarget =100000; // closes all orders once Float hits this $ amount 800 usd lot 1.0 deposit 1000 usd protit 1411 usd time 2015-11-01 to 2015-12-16 (46 days)
//extern int LossTarget = -1000; // must be -400 only stop loss USD
extern bool CloseAllNow = false; // closes all orders now//
//extern bool CloseProfitableTradesOnly = false; // closes only profitable trades
extern double ProftableTradeAmount = 1; // Only trades above this amount close out
//extern bool ClosePendingOnly = false; // closes pending orders only
//extern bool UseAlerts = false;
void CloseAll()
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
bool result = false;
if ( OrderType() == OP_BUY) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
if ( OrderType() == OP_SELL) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
if ( OrderType()== OP_BUYSTOP) result = OrderDelete( OrderTicket() );
if ( OrderType()== OP_SELLSTOP) result = OrderDelete( OrderTicket() );
// if (UseAlerts) PlaySound("alert.wav");
}
return;
}
//+------------------------------------------------------------------------+
//| cancels all orders that are in profit
//+------------------------------------------------------------------------+
void CloseAllinProfit()
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
bool result = false;
if ( OrderType() == OP_BUY && OrderProfit()+OrderSwap()>ProftableTradeAmount) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
if ( OrderType() == OP_SELL && OrderProfit()+OrderSwap()>ProftableTradeAmount) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
// if (UseAlerts) PlaySound("alert.wav");
}
return;
}
//+------------------------------------------------------------------------+
//| cancels all pending orders
//+------------------------------------------------------------------------+
void ClosePendingOrdersOnly()
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
bool result = false;
if ( OrderType()== OP_BUYSTOP) result = OrderDelete( OrderTicket() );
if ( OrderType()== OP_SELLSTOP) result = OrderDelete( OrderTicket() );
}
return;
}
// close all code //
int start()
{
// close all code //
int OrdersBUY;
int OrdersSELL;
double BuyLots, SellLots, BuyProfit, SellProfit;
//+------------------------------------------------------------------+
// Determine last order price |
//-------------------------------------------------------------------+
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY) OrdersBUY++;
if(OrderType()==OP_SELL) OrdersSELL++;
if(OrderType()==OP_BUY) BuyLots += OrderLots();
if(OrderType()==OP_SELL) SellLots += OrderLots();
if(OrderType() == OP_BUY) BuyProfit += OrderProfit() + OrderCommission() + OrderSwap();
if(OrderType() == OP_SELL) SellProfit += OrderProfit() + OrderCommission() + OrderSwap();
}
if(CloseAllNow ) CloseAll();
//closed all time 13.00-20.00
// if( Hour()==20 && Minute()==40 && DayOfWeek()==5 ) CloseAll();
// Print ( "Close all = every friday 20:40 ");
// if( Hour()>20 || ( Hour() >=0 && Hour() < 6 ) || ( Hour() >9 && Hour()<12 ) ) CloseAll();
// if(CloseProfitableTradesOnly) CloseAllinProfit();
// if(BuyProfit+SellProfit >= ProfitTarget ) CloseAll();
// if(BuyProfit+SellProfit <= LossTarget ) CloseAll();
// if(ClosePendingOnly) ClosePendingOrdersOnly();
// Print ( "ProfitTarget = ",ProfitTarget);
//Print ( "LossTarget = ",LossTarget);
// Print ( "Ordertotal = ",OrdersTotal());
// Print ( "Minute = ",Minute());
///// close all code///
//---- Remember the indicator's values for further analysis
//---- Note that we use the 1st and the 2nd bar. This allows a 1-bar delay
//---- (i.e., the signal will appear later), but protects against repeated opening and closing
//---- positions within a bar
double MACD_1 = iMACD( Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1 );
double EMA20_1=iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,1);
double Ma1201=iMA(NULL,0,12,0,MODE_SMA,PRICE_CLOSE,1);
double Ma1226=iMA(NULL,0,12,0,MODE_SMA,PRICE_CLOSE,26);
double Ma1252=iMA(NULL,0,12,0,MODE_SMA,PRICE_CLOSE,52);
double Ma12=iMA(NULL,0,12,0,MODE_SMA,PRICE_CLOSE,0);
double Ma26=iMA(NULL,0,26,0,MODE_SMA,PRICE_CLOSE,0);
int BarHighest60 = iHighest(NULL,0,MODE_HIGH,60,30);
double PHighest60=iHigh(NULL,0,BarHighest60);
int BarLowest60 = iLowest(NULL,0,MODE_LOW,60,30);
double PLowest60=iLow(NULL,0,BarLowest60);
int BarHighest26 = iHighest(NULL,0,MODE_HIGH,26,0);
double PHighest26=iHigh(NULL,0,BarHighest26);
int BarLowest26 = iLowest(NULL,0,MODE_LOW,26,0);
double PLowest26=iLow(NULL,0,BarLowest26);
int BarHighest30 = iHighest(NULL,0,MODE_HIGH,30,0);
double PHighest30=iHigh(NULL,0,BarHighest30);
int BarLowest30 = iLowest(NULL,0,MODE_LOW,30,0);
double PLowest30=iLow(NULL,0,BarLowest30);
//RSI highest
double RSIHi60=iRSI(NULL,0,12,PRICE_CLOSE,BarHighest60);
double RSIHi30=iRSI(NULL,0,12,PRICE_CLOSE,BarHighest30);
//RSI LOwest
double RSILow60=iRSI(NULL,0,12,PRICE_CLOSE, BarLowest60);
double RSILow30=iRSI(NULL,0,12,PRICE_CLOSE,BarLowest30);
double PClose15=iClose(NULL,0,15);
double PClose30=iClose(NULL,0,30);
double PClose60=iClose(NULL,0,60);
double PClose0=iClose(NULL,0,0);
double PClose1=iClose(NULL,0,1);
double PClose2=iClose(NULL,0,2);
double PClose3=iClose(NULL,0,3);
double POpen1=iOpen(NULL,0,1);
double POpen2=iOpen(NULL,0,2);
double POpen3=iOpen(NULL,0,3);
double PLow1= iLow(NULL,0,1);
double PLow2= iLow(NULL,0,2);
double PLow3= iLow(NULL,0,3);
double PHigh1= iHigh(NULL,0,1);
double PHigh2= iHigh(NULL,0,2);
double PHigh3= iHigh(NULL,0,3);
int BarHighest = iHighest(NULL,0,MODE_HIGH,Shiftbackopenorder,12);
int BariLowest = iLowest(NULL,0,MODE_LOW,Shiftbackopenorder,12);
double PLowest=iLow(NULL,0,BariLowest);
double PHighest=iHigh(NULL,0,BarHighest);
int BarHighest240 = iHighest(NULL,0,MODE_HIGH,240,0);
int BarLowest240 = iLowest(NULL,0,MODE_LOW,240,0);
double PLowest240=iLow(NULL,0,BarLowest240);
double PHighest240=iHigh(NULL,0,BarHighest240);
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 closed not for the current symbol, skip it
if ( OrderSymbol() != Symbol() ) continue;
//---- if the MagicNumber is not equal to the Expert_ID, skip this position
if ( OrderMagicNumber() != Expert_ID ) continue;
//---- if a BUY position is opened,
if ( OrderType() == OP_BUY )
{
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
//---- close buy position
// if(Ask==0)
if( PClose0 < EMA20_1 && MACD_1 < 0 && BuyProfit+SellProfit >= 0)
// if (( PClose0 <= PLowest && Ma12 < Ma26 && Ma1201 < Ma1226 && Ma1226 < Ma1252) || ( ( PClose0 >= PHighest26 && Ma12 > Ma26 ) && ( ( Ma1252 < Ma1226 && Ma1226 > Ma1201) || (Ma1252 > Ma1226 && Ma1226 < Ma1201) ) ))// open buy for side way
{
//---- close the position
if ( !OrderClose( OrderTicket(), OrderLots(), Bid, 5, Green ) )
{
_GetLastError = GetLastError();
// Alert( "Error OrderClose # ", _GetLastError );
return(-1);
}
}
//---- if the alert has not changed, exit: it is too early to open a new position
else
{ return(0); }
}
//---- if a SELL position is opened,
if ( OrderType() == OP_SELL )
{
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
//---- close sell position
// if(Ask==0)
// if ( ( PClose0 >= PHighest && Ma12 > Ma26 && Ma1201 > Ma1226 && Ma1226 > Ma1252 ) || (( PClose0 <= PLowest26 && Ma12 < Ma26) && ( ( Ma1252 < Ma1226 && Ma1226 > Ma1201) || (Ma1252 > Ma1226 && Ma1226 < Ma1201))) ) //open sell for side way
if ( PClose0 > EMA20_1 && MACD_1 > 0 && BuyProfit+SellProfit >= 0)
// if(Ma12 > Ma26)
// if(PClose0 >= PHighestdays)
// if (Pricelow1 <= Bolingerlower1 && PClose0 >= Bolingerlower )
//if (PLow0 <= PLowest )
// if (PClose0 <= Bolingerlower )
// if ( PClose0 > Ma1 && PClose1 > Ma1 && Stomain1 < 20 /*&& Ma1 > Mabackopen && (Hour()>=11 && Hour()<=17)*/ )
// if ( Ask==0 /*(Stomain1 < 20 && Stomain > 21 && Ma1 > Mabackclose )|| (Stomain1 < 80 && Stomain > 81 && Ma1 > Mabackclose )*/ )
// if ( Stomain1 < 19 && Stomain > 20 )
// if ( (Stomain1 < 20 && Stomain > 21 && Ma1 > Maback200 && Maback100 > Maback200 ) || (Stomain1 < 80 && Stomain > 81 && Ma1 > Maback200 && Maback100 > Maback200 ))
{
//---- close the position
if ( !OrderClose( OrderTicket(), OrderLots(), Ask, 5, Red ) )
{
_GetLastError = GetLastError();
// Alert( "Error OrderClose № ", _GetLastError );
return(-1);
}
}
//---- if the alert has not changed, exit: it is too early to open a new position
else return(0);
}
}
/////////// //---- open a BUY positiont/////////////
if
(
PClose0 > EMA20_1 && MACD_1 > 0 && BuyProfit+SellProfit >= 0
)
// if ( ( (PHigh15 >= PHighest && PClose0 >PHighest ) || ( PLow15 <= PLowest && PClose0 > PLowest) ) && ( (Hour()>=12 && Hour()<=20) ) )
{
// double slb=Ask-SL*Point;
// double tpb=Ask+TP*Point;
double stoploss=Ask*0.97; // SL percentage
double takeprofit=Ask*1.06; // TP percentage
double sl=Ask-(PHighest-PLowest)/2;
double tp=Ask+(PHighest-PLowest)*1.236 ;
//---- open a BUY positiont
if ( OrderSend( Symbol(), OP_BUY, Lot, Ask, 5,stoploss , takeprofit , "forex candle",
Expert_ID, 0, Green ) < 0 )
{
_GetLastError = GetLastError();
// Alert( "Error OrderSend # ", _GetLastError );
return(-1);
}
return(0);
}
//---- open a SELL position
if
(
PClose0 < EMA20_1 && MACD_1 < 0 && BuyProfit+SellProfit >= 0
)
// if ( ((PLow15 <= PLowest && PClose0 < PLowest) || ( PHigh15 >= PHighest && PClose0 < PHighest ) ) && (Hour()>=12 && Hour()<=20) )
{
// double sls=Bid+SL*Point;
// double tps=Bid-TP*Point;
double stop=Bid*1.03;
double take=Bid*0.94;
double s=Bid+(PHighest-PLowest)/2;
double t=Bid-(PHighest-PLowest)*1.236;
//---- open a SELL position
if ( OrderSend( Symbol(), OP_SELL, Lot, Bid, 5,stop ,take, "forex candle",
Expert_ID, 0, Red ) < 0 )
{
_GetLastError = GetLastError();
// Alert( "Error OrderSend # ", _GetLastError );
return(-1);
}
return(0);
}
return(0);
}
MAGIC NUMBER คือ
ตัวเลขที่เรากำหนดขึ้นมา จะเป็นเลขอะไรก็ได้เช่น 1111 หรือ 2222 เพื่อใช้เป็นตัวกำหนดค่าของ EA เวลาที่เราใช้คำสั่งในการเปิดออร์เดอร์ เพื่อให้รู้ว่าออร์ที่เปิดนั้นเปิดมาจากคู่เงินไหนหรือไทม์เฟรมไหน ทำให้ง่ายเวลาในการตรวจเช็คเพื่อปิดออร์เดอร์
การเขียน EA ต้องมี MAGIC NUMBER ไหม
จริงๆแล้วควรมีฟังก์ชั่น Magic Number เพราะในการเทรดเรามีโอกาสที่จะเทรดหลายคู่เงิน หรือหลายไทม์เฟรม ยิ่งคนมีเทคนิคเยอะก็อยากจะเทรดหลายคู่เงินพร้อมกัน ถ้าหากไม่มีฟังก์ชั่นกำหนด Magic Number คงยุ่งน่าดูในการตรวจเช็คเงื่อนไขการปิดออร์เดอร์ แต่สำหรับมือใหม่หัดเขียนอีเออาจจะไม่ต้องกำหนด Magic Number ก็ได้เพื่อลดขั้นตอนการเขียน ซึ่งจะค่อยเพิ่มทีหลังเอาก็ได้
http://onlinemakeemoney.blogspot.com/2015/08/magic-number.html
วิธีการกำหนดค่า MAGIC NUMBER
เราสามารถกำหนดได้จากการใส่ตัวเลขทุกครั้งที่เราเพิ่มอีเอลงในชาร์ท โดยการเขียนโค๊ดตามตัวอย่าง
extern int MagicNumber = 1234;
extern int MagicNumber = 1234;
int OnInit()
{
MagicNumber = subGenerateMagicNumber(MagicNumber, Symbol(), TimeFrame);
return(INIT_SUCCEEDED);
}
int start()
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,
StopLoss, // Stop loss price.
TP, // Take profit price.
TicketComment,MagicNumber,0,Green);
}
int subGenerateMagicNumber(int MagicNumber, string symbol, int timeFrame)
{
int isymbol = 0;
if (symbol == "EURUSD") isymbol = 1;
else if (symbol == "GBPUSD") isymbol = 2;
else if (symbol == "USDJPY") isymbol = 3;
else if (symbol == "USDCHF") isymbol = 4;
else if (symbol == "AUDUSD") isymbol = 5;
else if (symbol == "USDCAD") isymbol = 6;
else if (symbol == "EURGBP") isymbol = 7;
else if (symbol == "EURJPY") isymbol = 8;
else if (symbol == "EURCHF") isymbol = 9;
else if (symbol == "EURAUD") isymbol = 10;
else if (symbol == "EURCAD") isymbol = 11;
else if (symbol == "GBPUSD") isymbol = 12;
else if (symbol == "GBPJPY") isymbol = 13;
else if (symbol == "GBPCHF") isymbol = 14;
else if (symbol == "GBPAUD") isymbol = 15;
else if (symbol == "GBPCAD") isymbol = 16;
else isymbol = 17;
if(isymbol<10 data-blogger-escaped-10="" data-blogger-escaped-agicnumber="" data-blogger-escaped-div="" data-blogger-escaped-isymbol="" data-blogger-escaped-magicnumber="MagicNumber" data-blogger-escaped-return="" data-blogger-escaped-timeframe="" data-blogger-escaped-tringconcatenate="" data-blogger-escaped-trtointeger="">
คำสั่ง Break กับคำสั่ง Continue
คำสั่ง break นั้นจะเป็นคำสั่งเพื่อออกจาก loop ส่วนคำสั่ง continue นั้นจะเป็นคำสั่งเพื่อกระโดดไปยังต้น loop
คำสั่ง break
คำสั่ง break เป็นคำสั่งที่ให้โปรแกรมออกจาก loop ทันที โดยไม่ทำคำสั่งที่เหลือต่อ ส่วนมาก ก่อนจะใช้คำสั่งนี้ ก็จะมีการตรวจสอบอะไรซักอย่างเสียก่อน ซึ่งคำสั่ง break นี้ สามารถใช้ได้กับ loop หลาย ๆ loop ไม่ว่าจะเป็น while, do while, for, switch และอื่น ๆ เรามาดูตัวอย่างกัน ด้านล่างครับ
ตัวอย่างที่ 1
public static void main(String[] args){
int i = 1;
while(i < 2000){
if(i == 9) break;
System.out.print(i + " "); i++;
}
}
ผลลัพธ์ที่ได้คือ 1 2 3 4 5 6 7 8
จากตัวอย่างจะเห็นว่า เรากำหนดค่า i = 1; แล้วนำมาเข้า loop while แล้วเช็คว่า เมื่อ i = 9 ให้ทำการ break นั้นคือเมื่อถึง 9 แล้วจะออกจาก loop ทันที
ตัวอย่างที่ 2
public static void main(String[] args){
for(int i = 0; i < 2000; i++){
if(i == 9) break;
System.out.print(i + " ");
}
}
ผลลัพธ์ที่ได้คือ 0 1 2 3 4 5 6 7 8
การทำงานของ break ใน loop for ก็มีลักษณะเดียวกับ while loop
คำสั่ง continue
คำสั่ง continue เป็นคำสั่งที่ให้โปรแกรม กลับไปทำงานที่ต้น loop โดยไม่ทำคำสั่งที่เหลือต่อ ส่วนมาก ก่อนจะใช้คำสั่งนี้ก็จะต้องมีการตรวจสอบอะไรซักอย่างเสียก่อน คล้าย ๆ กับคำสั่ง break ส่วน loop ที่ใช้นั้น ก็เหมือนกับคำสั่ง break เลยครับ
ตัวอย่างที่ 3
public static void main(String[] args){
int i = 0;
while(i < 20){
i++;
if(i == 9) continue;
System.out.print(i + " ");
}
}
ผลลัพธ์ที่ได้คือ 1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19 20
จากตัวอย่างจะเห็นว่า เรากำหนดค่า i = 0; แล้วนำมาเข้า loop while ก่อนอื่นก็ทำการบวก i ขึ้นก่อนหนึ่งค่ำ แล้วเช็คว่า เมื่อ i = 9 ให้ทำการ continue นั้นคือเมื่อถึง 9 แล้วจะกลับไปทำงานที่ต้น loop ทันที ทำให้ผลลัพธ์ที่ออกมา ไม่มีเลข 9 อยู่
ตัวอย่างที่ 4
public static void main(String[] args){
for(int i = 0; i < 20; i++){
if(i == 9) break;
System.out.print(i + " ");
}
}
ผลลัพธ์ที่ได้คือ 0 1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19
การทำงานของ continue ใน loop for ก็มีลักษณะเดียวกับ while loop