ตอนนี้ได้ประมาณนี้ครับ คิดไม่ออกจะใช้คำสั่งแบบไหน
//+------------------------------------------------------------------+
//| O_BuyStop&O_SellStop.mq4 |
//| Akalak |
//|
https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Akalak"
#property link "
https://www.mql5.com"
#property version "3.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
extern int MagicNumber = 1234;
extern double Lotsize = 0.01;
extern int SL = 200;
extern int TP = 1000;
extern bool UseMoveToBreakevent = True;
extern int WhenTomoveToBe = 110;
extern int PipsToLockIn = 30;
extern bool UseTraillingStop = True;
extern int WhenToTrail = 110;
extern int TrailAmount = 110;
extern int MaxOrder = 0;
extern int Distance = 110;
extern int OrderDistand = 110;
//--------------------------------------------------------------------------------
double Fma,Mma,Sma,FmaSh,MmaSh,SmaSh;
void GetMA(){
Fma = iMA(Symbol(),PERIOD_H4,5,0,MODE_EMA,PRICE_WEIGHTED,0);
// Mma = iMA(Symbol(),0,20,0,MODE_EMA,PRICE_WEIGHTED,0);
Sma = iMA(Symbol(),PERIOD_H4,20,0,MODE_EMA,PRICE_WEIGHTED,0);
// FmaSh = iMA(Symbol(),0,5,0,MODE_EMA,PRICE_WEIGHTED,1);
// MmaSh = iMA(Symbol(),0,20,0,MODE_EMA,PRICE_WEIGHTED,1);
// SmaSh = iMA(Symbol(),0,50,0,MODE_EMA,PRICE_WEIGHTED,1);
}
//-------------------------------------------------------------------------
void OpenBuy(){
OrderSend(Symbol(), OP_BUYSTOP,Lotsize , Ask+Distance*Point, 3, Ask+Distance*Point-SL*Point,Ask+Distance*Point+TP*Point, "", MagicNumber, 0, Blue);
}
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
void OpenSell(){
OrderSend(Symbol(), OP_SELLSTOP, Lotsize, Bid-Distance*Point, 3, Bid- Distance*Point+SL*Point,Bid-Distance*Point-TP*Point, "", MagicNumber, 0, Red);
}
//-----------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
void DeleteOrder(int type){
for(int i= OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS)==True){
if(OrderMagicNumber() == MagicNumber && OrderSymbol()== Symbol()){
OrderDelete(OrderTicket());
}
}
}
}
//----------------------------------------------------------------------------------------------------------
void CloseOrderAll(int type){
for(int i= OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS)==True){
if(OrderMagicNumber() == MagicNumber && OrderSymbol()== Symbol()){
switch(type){
case OP_BUY:
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0,0);
break;
case OP_SELL:
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0,0);
break;
}
}
}
}
}
//----------------------------------------------------------------------------------------------------------
void CloseOrderBuy(int type){
for(int i= OrdersTotal()-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType() ==OP_BUY && OrderSymbol()== Symbol()){
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),0);
}
}
}
//----------------------------------------------------------------------------------------------------------
void CloseOrderSell(int type){
for(int i= OrdersTotal()-1;i>=0;i--){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType() ==OP_SELL && OrderSymbol()== Symbol()){
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),0);
}
}
}
//-----------------------------------------------------------------------------------------------------
int CountOrder(int type){
int CntOrder = 0;
for(int i=MaxOrder;i<OrdersTotal();i++){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderType() == type && OrderMagicNumber() == MagicNumber){
if(OrderSymbol() == Symbol()){
CntOrder++;
}
}
}
return(CntOrder);
}
//--------------------------------------------------------------------------------------
void MoveToBreakevent(){
for(int i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderMagicNumber() == MagicNumber){
if(OrderSymbol() == Symbol()){
if(OrderType() == OP_BUY){
if(Bid-OrderOpenPrice() > WhenTomoveToBe*Point ){
if(OrderOpenPrice() > OrderStopLoss()){
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+PipsToLockIn*Point,OrderTakeProfit(),0,Green);
}
}
}
if(OrderType() == OP_SELL){
if(OrderOpenPrice()-Ask > WhenTomoveToBe*Point){
if(OrderOpenPrice() < OrderStopLoss()){
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-PipsToLockIn*Point,OrderTakeProfit(),0,Red);
}
}
}
}
}
}
}
}
//-----------------------------------------------------------------------------------------------------------
void MoveToOrderOpenPrice(){
for(int i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderMagicNumber() == MagicNumber){
if(OrderSymbol() == Symbol()){
if(OrderType() == OP_BUYSTOP){
if(Bid-OrderOpenPrice() > OrderDistand*Point ){
if(OrderOpenPrice() > OrderStopLoss()){
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+PipsToLockIn*Point,OrderTakeProfit(),0,Green);
}
}
}
if(OrderType() == OP_SELLSTOP){
if(OrderOpenPrice()-Ask > OrderDistand*Point){
if(OrderOpenPrice() < OrderStopLoss()){
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-PipsToLockIn*Point,OrderTakeProfit(),0,Red);
}
}
}
}
}
}
}
}
//------------------------------------------------------------------------------------------
void AdjustTrail(){
for(int i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(OrderMagicNumber() == MagicNumber){
if(OrderSymbol() == Symbol()){
if(OrderType() == OP_BUY){
if(Bid-OrderOpenPrice() > WhenToTrail*Point){
if(OrderStopLoss()<Bid-TrailAmount*Point){
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailAmount*Point,OrderTakeProfit(),0,0);
}
}
}
if(OrderType() == OP_SELL){
if(OrderOpenPrice()- Ask > WhenToTrail*Point){
if(OrderStopLoss()>Ask+TrailAmount*Point){
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailAmount*Point,OrderTakeProfit(),0,0);
}
}
}
}
}
}
}
}
//-----------------------------------------------------------------------------------------------------------
void OnTick(){
if(UseMoveToBreakevent)MoveToBreakevent();
if(UseTraillingStop)AdjustTrail();
GetMA();
if(CountOrder(OP_BUYSTOP)==0 ){
if(Fma > Sma ){
OpenBuy();
// MoveToOrderOpenPrice();
// DeleteOrder(OP_SELLSTOP);
CloseOrderSell(OP_SELL);
}
}
if(CountOrder(OP_SELLSTOP)==0 ){
if(Fma < Sma ){
OpenSell();
// MoveToOrderOpenPrice();
// DeleteOrder(OP_BUYSTOP);
CloseOrderBuy(OP_BUY);
}
}
}
//+-------------------------------------------------------------------------------------