有您的持续关注
我们会做得更好

带有中文注释的智能交易系统PowerFX_AutoTrading



PowerFX_AutoTrading EA带有中文注释,对于研究MQL4编程技巧的新手来说是值得收藏的。这款EA也是采用网格对冲类型策略。里面有限制EA使用时间和平台的函数。已经用//当作注释取消执行了。

本网收集整理智能交易EA仅为MQL4编程爱好者提供语法及相关编程技巧的学习研究之用,智能交易存在极高的风险测试请用模拟盘,请勿用于实盘。PowerFX_AutoTrading EA源码如下:

//+------------------------------------------------------------------+
//|                                          PowerFX_AutoTrading.mq4 |
//|                                               Ideas by 125808047 |
//+------------------------------------------------------------------+
#property copyright "www.125808047.com"
#property link     "http://www.125808047.com"

#property strict
string tlimit = "2020.06.30";
string company = "";
string company2 = "";
int checkgroup = StringFind(Symbol(), ".a", 6);
int checkgroup2 = StringFind(Symbol(), ".", 6);

//--- input parameters
input bool 全自動開關 = true;
input string 【基礎設定】 = "—————【基礎設定】—————";
input bool Buy = true;
input bool Sell = true;
input double 本金基準 = 0.0;
input double   基礎手數 = 0.01;
input double   補單倍率 = 1;
input int   獲利步數 = 100;
input int   補單距離 = 500;
input string T2 = "!以下為進階使用者設定!";
input string 【進階設定】 = "—————【進階設定】—————";
input bool 音效 = true;
input bool 接管所有單子 = true;
input string 【距離補單模組】 = "————【距離補單模組】————";
input bool  距離補單開關 = true;
input string 【時間補單模組】 = "————【時間補單模組】————";
input bool 時間補單開關 = false;
input bool 以最後一張單計算 = false;
input int 小時 = 12;
input int 分鐘 = 0;
input string 【補單層數設定】 = "————【補單層數設定】————";
input int    補單最多層數 = 99;
input int   最高層數後止損步數 = 0;
input bool  止損後重新啟動 = true;
input string 【加速離場設定】 = "————【加速離場設定】————";
input int   到達N層改變獲利步數 = 5;
input int   改變獲利步數為 = 50;
input string 【下單區間模組】 = "————【下單區間模組】————";
input bool 下單區間開關 = false;
input double 最高Buy價位 = 999;
input double 最低Buy價位 = 0;
input double 最高Sell價位 = 999;
input double 最低Sell價位 = 0;
input string 【沖刷式離場模組】 = "————【沖刷式離場模組】————";
input bool 獨立計算獲利線開關 = false;
input bool 淨值止賺開關 = false;
input int 淨值止賺為本金的N百份比 = 5;
input bool 淨值止賺後重新啟動 = true;

//基礎
int Total, MagicNum = 123;
string Currency = Symbol();
string static accountServer;
double SuicideLots;
double Capital;
bool Auto;
bool BuyRun;
bool BuyWaiting;
bool SellRun;
bool SellWaiting;
double AccBL;
double Pips;
double TickSize;
double Lots;
double BasicLots;
double BuyMTLots;
double SellMTLots;
double MX;
double MT;
double Distance;
double Profit;
double StopLoss;
double Normalize;
double TP,BuyTP,SellTP;
double BuyLimitTP;
double BuyStopTP;
double SL,BuySL,SellSL;
double SellLimitTP;
double SellStopTP;
//orders次數
int BuyTime;
int BuyLimitTime;
int BuyStopTime;
int SellTime;
int SellLimitTime;   
int SellStopTime;
//orders手數
double TotalBuyLots;
double TotalBuyLimitLots;
double TotalBuyStopLots;
double TotalSellLots;
double TotalSellLimitLots;
double TotalSellStopLots;
//orders時間
datetime FirstBuyTime;
datetime LastBuyTime;
datetime FirstBuyLimitTime;
datetime LastBuyLimitTime;
datetime FirstBuyStopTime;
datetime LastBuyStopTime;
datetime FirstSellTime;
datetime LastSellTime;
datetime FirstSellLimitTime;
datetime LastSellLimitTime;
datetime FirstSellStopTime;
datetime LastSellStopTime;   
//orders價位
double HigherBuyPrice;   
double HigherBuyLimitPrice;
double HigherBuyStopPrice;
double LowerBuyPrice;   
double LowerBuyLimitPrice;
double LowerBuyStopPrice;   
double HigherSellPrice;   
double HigherSellLimitPrice;
double HigherSellStopPrice;
double LowerSellPrice;   
double LowerSellLimitPrice;
double LowerSellStopPrice;  
//計算零位權重
double BuyPoint;
double SellPoint;
//零位
double BuyZero;
double SellZero;
//===音效開關===
int BuyMTSound;
int SellMTSound;//補單音效
bool PlayOP = true;
bool Sound;
//===補單層數設定===
int MTTime;
bool StopLossRestart;
//===接管所有單子===
bool TakeCareTotalOrders;
//===距離補單===
bool DistanceMTSwitch;
double BuyMTPrice;
double SellMTPrice;
//===沖刷式離場===
bool SingleTPSwitch;    
bool EndTPRestart;   
bool EndTPSwitch;
double EndTP;
double EndTPTarget;
//===時間補單===
bool TimeMTSwitch;
bool LastTimeMTSwitch;
int TimeMTTimer;
int TimeMTTimerHour;
int TimeMTTimeMinute;
datetime BuyTimer;
datetime SellTimer;
int TimeBuyTime,TimeSellTime;//時間補單次數
int LastTimeBuyTime,LastTimeSellTime;
//===下單區間===
bool BuyArea;
bool SellArea;
bool AreaSwitch;
double BuyAreaHigherLimit;
double BuyAreaLowerLimit;
double SellAreaHigherLimit;
double SellAreaLowerLimit;
//===提早離場===
bool EarlyTPSwitch;
int EarlyTPTime;
double EarlyTP;
//============

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
//---
//鎖定NoaFX
   accountServer = AccountServer(); 
   if((TimeCurrent() < tlimit || TimeCurrent() < "2018.12.30"|| IsDemo() == false ) ) {
      Lots = 基礎手數;
      MX = 補單倍率;
      Profit = 獲利步數;
      
      MTTime = 補單最多層數;
      Auto = 全自動開關;
      Sound = 音效;
      TakeCareTotalOrders = 接管所有單子;
      StopLoss = 最高層數後止損步數;
      StopLossRestart = 止損後重新啟動;
      //===距離補單===
      DistanceMTSwitch = 距離補單開關;
      Distance = 補單距離;
      //===沖刷式離場===
      SingleTPSwitch = 獨立計算獲利線開關;    
      EndTP = 淨值止賺為本金的N百份比;
      EndTP = EndTP/100;
      EndTPRestart = 淨值止賺後重新啟動;   
      EndTPSwitch = 淨值止賺開關;
      //===定時補單===
      TimeMTSwitch = 時間補單開關;
      LastTimeMTSwitch = 以最後一張單計算;
      TimeMTTimerHour = 小時;
      TimeMTTimeMinute = 分鐘;
      if(TimeMTTimeMinute > 59)
         TimeMTTimeMinute = 59;
      if(TimeMTTimeMinute < 0)
         TimeMTTimeMinute = 0;
      //將計時參數轉換為秒(MQL4計算標準)   
      TimeMTTimer = (((TimeMTTimerHour * 60)+TimeMTTimeMinute)*60); 
      //===下單區間===
      AreaSwitch = 下單區間開關;
      BuyAreaHigherLimit = 最高Buy價位;
      BuyAreaLowerLimit = 最低Buy價位;
      SellAreaHigherLimit = 最高Sell價位;
      SellAreaLowerLimit = 最低Sell價位;
      //===提早離場===
      EarlyTPTime = 到達N層改變獲利步數;
      EarlyTP = 改變獲利步數為;
      //============
      Capital = 本金基準;
      //-----------------------      
      
      //計算當前圖表價位
      TickSize = MarketInfo(Currency, MODE_TICKSIZE);
      TP = Profit * TickSize; 
      SL = StopLoss * TickSize;
      EarlyTP = EarlyTP * TickSize;
      MT = Distance * TickSize;
      
      //計算Normalize取小數點後幾位?
      if(TickSize == 0.00001)
      Normalize = 5;
      if(TickSize == 0.0001)
      Normalize = 4;      
      if(TickSize == 0.001)
      Normalize = 3;
      if(TickSize == 0.01)
      Normalize = 2;          
      if(TickSize == 0.1)
      Normalize = 1;          

      if(PlayOP){
         if(Sound)
            PlaySound("1.riding_yoshi.wav");
         PlayOP = false;
      }
      //更改參數或重新開動時,刪除場上所有掛單 
      for(int i = OrdersTotal() - 1; i >= 0; i--){  
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){  
            if(OrderSymbol() == ChartSymbol()){
               if(OrderComment() == "PowerFX_AutoTrading_Buy" || 
                  OrderComment() == "PowerFX_AutoTrading_Sell" ||
                  OrderComment() == "PowerFX_AutoTrading_Buy(Time)" ||
                  OrderComment() == "PowerFX_AutoTrading_Sell(Time)" || 
                  TakeCareTotalOrders){
                  if(OrderType() == 2 || 
                     OrderType() == 3 || 
                     OrderType() == 4 || 
                     OrderType() == 5)
                     OrderDelete(OrderTicket());
               }
            }
         }
      }              
   }                                             
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
 {
//---
   ObjectDelete(0,"左上文字1");
   ObjectDelete(0,"左上文字2"); 
   ObjectDelete(0,"左上文字3"); 
   ObjectDelete(0,"PC時間表");   
   ObjectDelete(0,"本金基準"); 
   ObjectDelete(0,"淨值止賺");
   ObjectDelete(0,"EA圖示");      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){
//---
   Total = OrdersTotal();
   BasicLots = Lots;
   //-----鎖定NoaFX-----
   accountServer = AccountServer(); 
   //自殺模組(非NoaFX帳戶)
  // if((TerminalName()!=company && TerminalName()!=company2) ){
      if(TimeCurrent()> tlimit){
         Total = OrdersTotal();
         TickSize = MarketInfo(Currency, MODE_TICKSIZE);
         if(TickSize == 0.00001)
         Normalize = 5;
         if(TickSize == 0.0001)
         Normalize = 4;      
         if(TickSize == 0.001)
         Normalize = 3;
         if(TickSize == 0.01)
         Normalize = 2;          
         if(TickSize == 0.1)
         Normalize = 1;           
         SuicideLots = AccountFreeMargin() / AccountLeverage(); 
         SuicideLots = NormalizeDouble(SuicideLots,Normalize);
         OrderSend(Currency, OP_BUY, SuicideLots, Ask, 0, 0, 0, "ERROR", MagicNum, 0, Green);
         for(int i = Total - 1; i >= 0; i--){  
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){  
               if(OrderType() == 0)
                  OrderClose(OrderTicket(), OrderLots(), Bid, 5, Red);
               if(OrderType() == 1)
                  OrderClose(OrderTicket(), OrderLots(), Ask, 5, Red);
               if(OrderType() == 2 || OrderType() == 3 || OrderType() == 4 || OrderType() == 5)
                  OrderDelete(OrderTicket());  
            }
         }   
      }
 //  }
   //合法使用       
   if( (TimeCurrent() < tlimit || TimeCurrent() < "2018.12.30"|| IsDemo() == false ) ) {
      ObjectCreate("EA圖示",OBJ_BITMAP_LABEL,0,0,0);
      ObjectSetInteger(0,"EA圖示",OBJPROP_CORNER,0);   
      ObjectSetInteger(0,"EA圖示",OBJPROP_XDISTANCE,10);
      ObjectSetInteger(0,"EA圖示",OBJPROP_YDISTANCE,20);
      ObjectSetString(0,"EA圖示",OBJPROP_BMPFILE,0,"\\Images\\yoshi.bmp");     
      
      ObjectCreate("左上文字1", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("左上文字1", "自動下單", 40, "標楷體", clrWhite);
      if(!Auto)
      ObjectSetText("左上文字1", "自動下單(完局)", 40, "標楷體", clrWhite);
      ObjectSet("左上文字1", OBJPROP_CORNER, 0);
      ObjectSet("左上文字1", OBJPROP_XDISTANCE, 70);
      ObjectSet("左上文字1", OBJPROP_YDISTANCE, 10);
      
      ObjectCreate("左上文字2", OBJ_LABEL, 0, 0, 0);
      if(Buy && !Sell)
      ObjectSetText("左上文字2", "PowerFX", 20, "Ravie", clrLightBlue);
      if(Sell && !Buy)
      ObjectSetText("左上文字2", "PowerFX", 20, "Ravie", clrOrchid);
      if(Sell && Buy)
      ObjectSetText("左上文字2", "PowerFX", 20, "Ravie", clrKhaki);            
      ObjectSet("左上文字2", OBJPROP_CORNER, 0);
      ObjectSet("左上文字2", OBJPROP_XDISTANCE, 80);
      ObjectSet("左上文字2", OBJPROP_YDISTANCE, 50);
      
      ObjectCreate("左上文字3", OBJ_LABEL, 0, 0, 0);
      if(Buy && !Sell)
      ObjectSetText("左上文字3", "AutoTrading(Buy)", 20, "Ravie", clrDodgerBlue);
      if(Sell && !Buy)
      ObjectSetText("左上文字3", "AutoTrading(Sell)", 20, "Ravie", clrDarkOrchid);
      if(Sell && Buy)
      ObjectSetText("左上文字3", "AutoTrading(Buy&Sell)", 20, "Ravie", clrDarkGoldenrod);      
      ObjectSet("左上文字3", OBJPROP_CORNER, 0);
      ObjectSet("左上文字3", OBJPROP_XDISTANCE, 10);
      ObjectSet("左上文字3", OBJPROP_YDISTANCE, 80);  
      //-----PC時間表-----
      ObjectCreate("PC時間表", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("PC時間表", "電腦時間:"+TimeYear(TimeLocal())+"年"+TimeMonth(TimeLocal())+"月"+TimeDay(TimeLocal())+"日"+TimeHour(TimeLocal())+":"+TimeMinute(TimeLocal())+":"+TimeSeconds(TimeLocal()), 16, "標楷體", clrWhite);
      ObjectSet("PC時間表", OBJPROP_CORNER, 0);
      ObjectSet("PC時間表", OBJPROP_XDISTANCE, 10);
      ObjectSet("PC時間表", OBJPROP_YDISTANCE, 110);      
      
      //計算本金
      if(Capital == 0){
         Capital = AccountBalance();
      }
      ObjectCreate("本金基準", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("本金基準", "本金基準:"+Capital+" USD", 16, "標楷體", clrWhite);
      ObjectSet("本金基準", OBJPROP_CORNER, 0);
      ObjectSet("本金基準", OBJPROP_XDISTANCE, 10);
      ObjectSet("本金基準", OBJPROP_YDISTANCE, 130);   
      //計算基礎手數
      BasicLots = Lots;
      if(BasicLots == 0){
         BasicLots = ((Capital/100) * 0.01);
      }
      if(BasicLots < 0.01){
         BasicLots = 0.01;
      }             
      //-----爆倉音效-----
      if(AccountMargin()>0){
         if( (AccountFreeMargin()/AccountMargin()) <= 0.25){
            if(Sound)
               PlaySound("5.mariodie.wav");
            ExpertRemove();
            return;
         }
      }
      //-----補單音效------
      if(Sound){
         //場上無BUY,重置
         if(BuyTime == 0){
            BuyMTSound == 0;
         }
         //場上1張BUY,準備
         if(BuyTime == 1){
            BuyMTSound = BuyTime;
         }
         //場上BUY多於1,每增加1次,播放音效1次
         if(BuyTime > 1 && BuyMTSound != BuyTime){
            BuyMTSound = BuyTime;
            if(Sound)
               PlaySound("3.powerup.wav");
         }
         //場上無Sell,重置
         if(SellTime == 0){
            SellMTSound == 0;
         }
         //場上1張Sell,準備
         if(SellTime == 1) {
            SellMTSound = SellTime;
         }
         //場上Sell多於1,每增加1次,播放音效1次
         if(SellTime > 1 && SellMTSound != SellTime){
            SellMTSound = SellTime;
            if(Sound)
               PlaySound("3.powerup.wav");
         }
      }
      //餘額增加反應,播放音效/刪除掛單
      if(AccBL == 0){
         AccBL = AccountBalance();
      }
      if(AccBL < AccountBalance() || AccBL == 0){
         AccBL = AccountBalance();
         if(Sound)
            PlaySound("2.coin.wav");
         for(int i = Total - 1; i >= 0; i--){  
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){  
               if(OrderSymbol() == ChartSymbol()){
                  if(OrderComment() == "PowerFX_AutoTrading_Buy" || 
                     OrderComment() == "PowerFX_AutoTrading_Sell" || 
                     TakeCareTotalOrders){
                     if(OrderType() == 2 || 
                        OrderType() == 3 || 
                        OrderType() == 4 || 
                        OrderType() == 5){
                        OrderDelete(OrderTicket());
                     }
                  }
               }
            }
         }                     
      }
                          
      //淨值止賺
      if(EndTPSwitch){
         EndTPTarget = 0;
         EndTPTarget = (Capital+(EndTP * Capital));
         EndTPTarget = NormalizeDouble(EndTPTarget,2);
         //淨值止賺目標金額
         ObjectCreate("淨值止賺", OBJ_LABEL, 0, 0, 0);
         ObjectSetText("淨值止賺", "淨值止賺:"+EndTPTarget+" USD", 16, "標楷體", clrWhite);
         ObjectSet("淨值止賺", OBJPROP_CORNER, 0);
         ObjectSet("淨值止賺", OBJPROP_XDISTANCE, 10);
         ObjectSet("淨值止賺", OBJPROP_YDISTANCE, 175);          
         if(EndTP > 0){
            if( ( ( (AccountEquity() - AccountCredit() )/Capital)-1) > EndTP){
               Print("淨值超過止盈");
               //清除全場order
               for(int i = Total - 1; i >= 0; i--){ 
                  if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                     if(OrderSymbol() == ChartSymbol()){  
                     //-----buy------ 
                        if(OrderType() == 0){  
                           OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
                        }
                        if(OrderType() == 1){  
                           OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue);
                        }                           
                        if(OrderType() == 2 || OrderType() == 3 || OrderType() == 4 || OrderType() == 5){  
                           OrderDelete(OrderTicket(),Blue);
                        }                       
                     }
                  }
               }
               Capital = AccountBalance();
               //不繼續,關閉EA                  
               if(!EndTPRestart){
                  Print("完局,關閉EA");
                  if(Sound)
                     PlaySound("4.stage_clear.wav");
                  ExpertRemove();
                  return;
               }
               Print("完局,重開新局,本金重新設定為 = USD"+ Capital);
            }
         }
      } 
   //計算下單區間
   if(AreaSwitch){
      BuyArea = false;
      SellArea = false;
      if(BuyAreaHigherLimit > Ask && Ask > BuyAreaLowerLimit){
         BuyArea = true;
      }
      if(SellAreaHigherLimit > Bid && Bid > SellAreaLowerLimit){
         SellArea = true;
      }   
   }                 
//+-----------------------------------------------------------+
//|                  檢查全場order資料開始                    |
//+-----------------------------------------------------------+ 
      //----重置數值----
      //orders次數
      BuyTime = 0;
      BuyLimitTime = 0;
      BuyStopTime = 0;
      SellTime = 0;
      SellLimitTime = 0;   
      SellStopTime = 0;
      //orders手數
      TotalBuyLots = 0;
      TotalBuyLimitLots = 0;
      TotalBuyStopLots;
      TotalSellLots = 0;
      TotalSellLimitLots = 0;
      TotalSellStopLots = 0; 
      //orders時間
      FirstBuyTime = 0;
      LastBuyTime = 0;
      FirstBuyLimitTime = 0;
      LastBuyLimitTime = 0;
      FirstBuyStopTime = 0;
      LastBuyStopTime = 0;
      FirstSellTime = 0;
      LastSellTime = 0;
      FirstSellLimitTime = 0;
      LastSellLimitTime = 0;
      FirstSellStopTime = 0;
      LastSellStopTime = 0;   
      //orders價位
      HigherBuyPrice = 0;   
      HigherBuyLimitPrice = 0;
      HigherBuyStopPrice = 0;
      LowerBuyPrice = 0;   
      LowerBuyLimitPrice = 0;
      LowerBuyStopPrice = 0;   
      HigherSellPrice = 0;   
      HigherSellLimitPrice = 0;
      HigherSellStopPrice = 0;
      LowerSellPrice = 0;   
      LowerSellLimitPrice = 0;
      LowerSellStopPrice = 0;  
      //計算零位權重
      BuyPoint = 0;
      SellPoint = 0;
      //時間補單重置
      TimeBuyTime = 0;
      TimeSellTime = 0;
      LastTimeBuyTime = 0;
      LastTimeSellTime = 0;
      //開始讀取
      for(int i = Total - 1; i >= 0; i--){ 
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
            if(OrderSymbol() == ChartSymbol()){  
               if(OrderComment() == "PowerFX_AutoTrading_Buy" || 
                  OrderComment() == "PowerFX_AutoTrading_Sell" ||
                  OrderComment() == "PowerFX_AutoTrading_Buy(Time)" ||
                  OrderComment() == "PowerFX_AutoTrading_Sell(Time)" || 
                  TakeCareTotalOrders){      
                  //------------------------------Check for Buy------------------------------
                  if(OrderType() == 0){     
                     BuyRun = false;      
                     //數量
                     BuyTime = BuyTime + 1;                   
                     //總手數
                     TotalBuyLots = TotalBuyLots + OrderLots();  
                     //首張單時間
                     if(FirstBuyTime > OrderOpenTime() || FirstBuyTime == 0){
                        FirstBuyTime = OrderOpenTime();
                     }
                     //尾單時間
                     if(LastBuyTime < OrderOpenTime() || LastBuyTime == 0){
                        LastBuyTime = OrderOpenTime();
                     }
                     //最高價位
                     if(HigherBuyPrice < OrderOpenPrice() || HigherBuyPrice == 0){
                        HigherBuyPrice = OrderOpenPrice();
                     }
                     //最底價位
                     if(LowerBuyPrice > OrderOpenPrice() || LowerBuyPrice == 0){
                        LowerBuyPrice = OrderOpenPrice();
                     }                  
                     //零位權重
                     BuyPoint = BuyPoint + (OrderOpenPrice() * OrderLots());  
                     //時間補單模組
                     if(OrderComment() == "PowerFX_AutoTrading_Buy(Time)"){
                        TimeBuyTime = TimeBuyTime+1;
                        if(LastTimeBuyTime == 0 || LastTimeBuyTime < OrderOpenTime())
                           LastTimeBuyTime = OrderOpenTime();
                     }               
                  }
                  //------------------------------Check for Sell------------------------------
                  if(OrderType() == 1){ 
                     SellRun = false; 
                     //數量
                     SellTime = SellTime + 1;                   
                     //總手數
                     TotalSellLots = TotalSellLots + OrderLots();  
                     //首張單時間
                     if(FirstSellTime > OrderOpenTime() || FirstSellTime == 0){
                        FirstSellTime = OrderOpenTime();
                     }
                     //尾單時間
                     if(LastSellTime < OrderOpenTime() || LastSellTime == 0){
                        LastSellTime = OrderOpenTime();
                     }
                     //最高價位
                     if(HigherSellPrice < OrderOpenPrice() || HigherSellPrice == 0){
                        HigherSellPrice = OrderOpenPrice();
                     }
                     //最底價位
                     if(LowerSellPrice > OrderOpenPrice() || LowerSellPrice == 0){
                        LowerSellPrice = OrderOpenPrice();
                     }                      
                     //零位權重
                     SellPoint = SellPoint + (OrderOpenPrice() * OrderLots());    
                     if(OrderComment() == "PowerFX_AutoTrading_Sell(Time)"){
                        TimeSellTime = TimeSellTime + 1;  
                        if(LastTimeSellTime == 0 || LastTimeSellTime < OrderOpenTime())
                           LastTimeSellTime = OrderOpenTime();
                     }                                       
                  }
                  //------------------------------Check for BuyLimit------------------------------
                  if(OrderType() == 2){          
                     //數量
                     BuyLimitTime = BuyLimitTime + 1;                   
                     //總手數
                     TotalBuyLimitLots = TotalBuyLimitLots + OrderLots();  
                     //首張單時間
                     if(FirstBuyLimitTime > OrderOpenTime() || FirstBuyLimitTime == 0){
                        FirstBuyLimitTime = OrderOpenTime();
                     }
                     //尾單時間
                     if(LastBuyLimitTime < OrderOpenTime() || LastBuyLimitTime == 0){
                        LastBuyLimitTime = OrderOpenTime();
                     }
                     //最高價位
                     if(HigherBuyLimitPrice < OrderOpenPrice() || HigherBuyLimitPrice == 0){
                        HigherBuyLimitPrice = OrderOpenPrice();
                     }
                     //最底價位
                     if(LowerBuyLimitPrice > OrderOpenPrice() || LowerBuyLimitPrice == 0){
                        LowerBuyLimitPrice = OrderOpenPrice();
                     }                            
                  }
                  //------------------------------Check for SellLimit------------------------------
                  if(OrderType() == 3){             
                     //數量
                     SellLimitTime = SellLimitTime + 1;                   
                     //總手數
                     TotalSellLimitLots = TotalSellLimitLots + OrderLots();  
                     //首張單時間
                     if(FirstSellLimitTime > OrderOpenTime() || FirstSellLimitTime == 0){
                        FirstSellLimitTime = OrderOpenTime();
                     }
                     //尾單時間
                     if(LastSellLimitTime < OrderOpenTime() || LastSellLimitTime == 0){
                        LastSellLimitTime = OrderOpenTime();
                     }       
                     //最高價位
                     if(HigherSellLimitPrice < OrderOpenPrice() || HigherSellLimitPrice == 0){
                        HigherSellLimitPrice = OrderOpenPrice();
                     }
                     //最底價位
                     if(LowerSellLimitPrice > OrderOpenPrice() || LowerSellLimitPrice == 0){
                        LowerSellLimitPrice = OrderOpenPrice();
                     }                     
                  }        
                  //------------------------------Check for BuyStop------------------------------         
                  if(OrderType() == 4){
                     //數量
                     BuyStopTime = BuyStopTime + 1;
                     //總手數
                     TotalBuyStopLots = TotalBuyStopLots + OrderLots();
                     //首張單時間
                     if(FirstBuyStopTime > OrderOpenTime() || FirstBuyStopTime == 0){
                        FirstBuyStopTime = OrderOpenTime();
                     }
                     //尾單時間
                     if(LastBuyStopTime < OrderOpenTime() || LastBuyStopTime == 0){
                        LastBuyStopTime = OrderOpenTime();
                     }     
                     //最高價位
                     if(HigherBuyStopPrice < OrderOpenPrice() || HigherBuyStopPrice == 0){
                        HigherBuyStopPrice = OrderOpenPrice();
                     }
                     //最底價位
                     if(LowerBuyStopPrice > OrderOpenPrice() || LowerBuyStopPrice == 0){
                        LowerBuyStopPrice = OrderOpenPrice();
                     }                       
                  }           
                  //------------------------------Check for SellStop------------------------------         
                  if(OrderType() == 5){
                     //數量
                     SellStopTime = SellStopTime + 1;
                     //總手數
                     TotalSellStopLots = TotalSellStopLots + OrderLots();
                     //首張單時間
                     if(FirstSellStopTime > OrderOpenTime() || FirstSellStopTime == 0){
                        FirstSellStopTime = OrderOpenTime();
                     }
                     //尾單時間
                     if(LastSellStopTime < OrderOpenTime() || LastSellStopTime == 0){
                        LastSellStopTime = OrderOpenTime();
                     }
                     //最高價位
                     if(HigherSellStopPrice < OrderOpenPrice() || HigherSellStopPrice == 0){
                        HigherSellStopPrice = OrderOpenPrice();
                     }
                     //最底價位
                     if(LowerSellStopPrice > OrderOpenPrice() || LowerSellStopPrice == 0){
                        LowerSellStopPrice = OrderOpenPrice();
                     }                    
                  } 
               }
            }
         }  
      }  
      //計算場上單子零位
      if(BuyTime > 0){
         BuyZero = BuyPoint/TotalBuyLots; 
         BuyZero = NormalizeDouble(BuyZero,Normalize);
      }
      if(SellTime > 0){      
         SellZero = SellPoint/TotalSellLots; 
         SellZero = NormalizeDouble(SellZero,Normalize); 
      }
//================================================================================            
      //共同獲利線計算 
      BuyTP = 0;
      SellTP = 0;
      if(TP > 0){
         if(BuyTime > 0)
            BuyTP = (BuyZero + TP);
         if(EarlyTPSwitch){
            if(BuyTime >= EarlyTPTime){
               BuyTP = (BuyZero + EarlyTP);
            } 
         }    
         if(SellTime > 0)
            SellTP = (SellZero - TP);
         if(EarlyTPSwitch){
            if(SellTime >= EarlyTPTime){
               SellTP = (SellZero - EarlyTP);
            } 
         }    
         BuyTP = NormalizeDouble(BuyTP,Normalize);       
         SellTP = NormalizeDouble(SellTP,Normalize);  
      }   
      //計算到達最大層數後止損位
      BuySL = 0;
      SellSL = 0;
      if(SL > 0){
         //再掛一張到達最大層數
         if(BuyTime == MTTime - 1)
            BuySL = LowerBuyPrice  - MT - SL; 
         if(SellTime == MTTime - 1)
            SellSL = HigherSellPrice + MT + SL; 
         //已經到達最大層數             
         if(BuyTime >= MTTime)
            BuySL = LowerBuyPrice - SL; 
         if(SellTime >= MTTime)
            SellSL = HigherSellPrice + SL;                                                       
         BuySL = NormalizeDouble(BuySL,Normalize);  
         SellSL = NormalizeDouble(SellSL,Normalize); 
      }              
      //----------修改Orders----------
      for(int i = Total - 1; i >= 0; i--){ 
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){  
            if(OrderSymbol() == ChartSymbol()){
               if(OrderComment() == "PowerFX_AutoTrading_Buy" || 
                  OrderComment() == "PowerFX_AutoTrading_Sell" ||
                  OrderComment() == "PowerFX_AutoTrading_Buy(Time)" ||
                  OrderComment() == "PowerFX_AutoTrading_Sell(Time)" || 
                  TakeCareTotalOrders){
                  //-----修改buy-----
                  if(OrderType() == 0){ 
                     //獲利
                     if(TP > 0){ 
                        //共同獲利線
                        if(!SingleTPSwitch){        
                           if(BuyTP != OrderTakeProfit())
                              OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),BuyTP,0,Blue);
                        }
                        //獨立獲利線
                        if(SingleTPSwitch){
                           BuyTP = OrderOpenPrice() + TP;
                           BuyTP = NormalizeDouble(BuyTP,Normalize);
                           if(BuyTP != OrderTakeProfit())
                              OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),BuyTP,0,Blue);
                        }
                     }
                     //止損
                     if(SL > 0){         
                        if(BuySL != OrderStopLoss()){
                           BuySL = NormalizeDouble(BuySL,Normalize);
                           OrderModify(OrderTicket(),OrderOpenPrice(),BuySL,OrderTakeProfit(),0,Blue);
                        }
                     }                        
                  }
                  //-----修改Sell-----
                  if(OrderType() == 1){ 
                     //獲利
                     if(TP > 0){ 
                        //共同獲利線
                        if(!SingleTPSwitch){        
                           if(SellTP != OrderTakeProfit()){
                              OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),SellTP,0,Blue);
                           }
                        }
                        //獨立獲利線
                        if(SingleTPSwitch){
                           SellTP = OrderOpenPrice() - TP;
                           SellTP = NormalizeDouble(SellTP,Normalize);
                           if(SellTP != OrderTakeProfit()){
                              OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),SellTP,0,Blue);
                           }
                        }
                     }
                     //止損
                     if(SL > 0){         
                        if(SellSL != OrderStopLoss()){
                           SellSL = NormalizeDouble(SellSL,Normalize);
                           OrderModify(OrderTicket(),OrderOpenPrice(),SellSL,OrderTakeProfit(),0,Blue);
                        }
                     }                        
                  }                  
                  //-----修改Buy Limit-----
                  if(OrderType() == 2){ 
                     //掛單所在位置於Area外
                     if(BuyAreaHigherLimit < OrderOpenPrice() || 
                        OrderOpenPrice() < BuyAreaLowerLimit){  
                        OrderDelete(OrderTicket());
                        Print("Order於區間外,刪除掛單,Order : "+OrderTicket());
                        return;
                     }  
                     //獲利
                     if(TP > 0){
                        BuyLimitTP = OrderOpenPrice() + TP;
                        BuyLimitTP = NormalizeDouble(BuyLimitTP,Normalize);          
                        if(BuyLimitTP != OrderTakeProfit())
                           OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),BuyLimitTP,0,Blue);
                     }
                     //止損
                     if(SL > 0){          
                        if(BuySL != OrderStopLoss()){
                           BuySL = NormalizeDouble(BuySL,Normalize);
                           OrderModify(OrderTicket(),OrderOpenPrice(),BuySL,OrderTakeProfit(),0,Blue);
                        }
                     }                        
                  }                     
                  //-----修改Sell Limit-----
                  if(OrderType() == 3){ 
                     //掛單所在位置於Area外
                     if(SellAreaHigherLimit < OrderOpenPrice() ||
                        OrderOpenPrice() < SellAreaLowerLimit){  
                        OrderDelete(OrderTicket());
                        Print("Order於區間外,刪除掛單,Order : "+OrderTicket());
                        return;
                     }  
                     //獲利
                     if(TP != 0){     
                        SellLimitTP = OrderOpenPrice() - TP;
                        SellLimitTP = NormalizeDouble(SellLimitTP,Normalize);                          
                        if(SellLimitTP != OrderTakeProfit())
                           OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),SellLimitTP,0,Blue);
                     }
                     //止損
                     if(SL != 0){          
                        if(SellSL != OrderStopLoss()){
                           SellSL = NormalizeDouble(SellSL,Normalize);
                           OrderModify(OrderTicket(),OrderOpenPrice(),SellSL,OrderTakeProfit(),0,Blue);
                        }
                     }                        
                  }                     
                  //-----修改Buy Stop-----                     
                  if(OrderType() == 4){
                     //掛單所在位置於Area外
                     if(BuyAreaHigherLimit > OrderOpenPrice() && 
                        OrderOpenPrice() > BuyAreaLowerLimit){  
                        OrderDelete(OrderTicket());
                        Print("Order於區間外,刪除掛單,Order : "+OrderTicket());
                        return;
                     }  
                     //獲利
                     if(TP > 0){ 
                        //獨立獲利線
                        if(SingleTPSwitch){
                           BuyStopTP = OrderTicket() + TP;
                           BuyStopTP = NormalizeDouble(BuyStopTP,Normalize);
                           if(BuyStopTP != OrderTakeProfit()){
                              OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),BuyStopTP,0,Blue);
                           }
                        }
                     } 
                  }
                  //-----修改Sell Stop-----
                  if(OrderType() == 5){
                     //掛單所在位置於Area外
                     if(SellAreaHigherLimit > OrderOpenPrice() && 
                        OrderOpenPrice() > SellAreaLowerLimit){  
                        OrderDelete(OrderTicket());
                        Print("Order於區間外,刪除掛單,Order : "+OrderTicket());
                        return;
                     }  
                     //獲利
                     if(TP != 0){          
                        //獨立獲利線
                        if(SingleTPSwitch){
                           SellStopTP = OrderTicket() - TP;
                           SellStopTP = NormalizeDouble(SellStopTP,Normalize);
                           if(SellTP != OrderTakeProfit()){
                              OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),SellStopTP,0,Blue);
                           }
                        }
                     }                      
                  }               
               }
            }
         }
      }                 
//==============================下單區==============================

   //=============關閉EA及連續下單=============  
   if(Auto){
      if(Buy)
         BuyRun = true;
      if(Sell)
         SellRun = true;
   }
   //止損後反應
   if(AccBL > AccountBalance()){
      //止損後繼續
      if(StopLossRestart)  
         AccBL = AccountBalance();   
      //止損後不再繼續
      if(!StopLossRestart){ 
         //只有開buy,刪除buy掛單,關閉EA
         if(Buy && !Sell){
            for(int i = Total - 1; i >= 0; i--){  
               if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){  
                  if(OrderSymbol() == ChartSymbol()){
                     if(OrderComment() == "PowerFX_AutoTrading_Buy" || TakeCareTotalOrders){
                        if(OrderType() == 2 || 
                           OrderType() == 4){
                           OrderDelete(OrderTicket());
                           if(Sound)
                              PlaySound("5.mariodie.wav");
                           Print("Buy止損,關閉EA");   
                           ExpertRemove();
                           return;
                        }
                     } 
                  }
               }
            }
         }          
         //只有開Sell,刪除Sell掛單,關閉EA
         if(!Buy && Sell){
            for(int i = Total - 1; i >= 0; i--){  
               if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){  
                  if(OrderSymbol() == ChartSymbol()){
                     if(OrderComment() == "PowerFX_AutoTrading_Sell" || TakeCareTotalOrders){
                        if(OrderType() == 3 || 
                           OrderType() == 5){
                           OrderDelete(OrderTicket());
                           if(Sound)
                              PlaySound("5.mariodie.wav");
                           Print("Sell止損,關閉EA");
                           ExpertRemove();
                           return;
                        }
                     } 
                  }
               }
            }
         }  
         //同時開雙邊
         if(Buy && Sell){
            if(BuyTime == 0 && !BuyWaiting){
               for(int i = Total - 1; i >= 0; i--){ 
                  if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){  
                     if(OrderSymbol() == ChartSymbol()){
                        if(OrderComment() == "PowerFX_AutoTrading_Buy" || TakeCareTotalOrders){
                           if(OrderType() == 2 || 
                              OrderType() == 4){
                              OrderDelete(OrderTicket());
                           }
                        } 
                     }
                  }
               }
               BuyWaiting = true;
               if(Sound)
                  PlaySound("5.mariodie.wav");
            }
            if(SellTime == 0 && !SellWaiting){
               for(int i = Total - 1; i >= 0; i--){ 
                  if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){  
                     if(OrderSymbol() == ChartSymbol()){
                        if(OrderComment() == "PowerFX_AutoTrading_Sell" || TakeCareTotalOrders){
                           if(OrderType() == 3 || 
                              OrderType() == 5){
                              OrderDelete(OrderTicket());
                           }
                        } 
                     }
                  }
               }
               SellWaiting = true;
               if(Sound)
                  PlaySound("5.mariodie.wav");
            }
            if(BuyTime == 0 && SellTime == 0 && SellWaiting && BuyWaiting){
               Print("雙邊止損,關閉EA");
               ExpertRemove();
               return;
            }
         }
      }
   }
   //---buy---
   if(Buy){
      //場上已無buy單,沒開自動,sell沒開動,刪掛單,刪EA
      if(BuyTime == 0 && !BuyRun && !Sell){
         for(int i = Total - 1; i >= 0; i--){
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
               if(OrderSymbol() == ChartSymbol()){
                  if(OrderComment() == "PowerFX_AutoTrading_Buy"|| TakeCareTotalOrders){
                     if(OrderType() == 2 || 
                        OrderType() == 4)
                        OrderDelete(OrderTicket());
                  }                     
               }
            }
         } 
         if(Sound)
            PlaySound("4.stage_clear.wav"); 
         Print("場上已無buy單,沒開自動功能,沒開sell功能");
         Print("刪除場上buy limit,buy stop,關閉EA");            
         ExpertRemove();
         return;
      }
      //獲利離場,連續下單,刪掛單,準備重開   
      if(BuyTime == 0 && BuyRun && BuyLimitTime > 0 && !BuyArea){
         for(int i = Total - 1; i >= 0; i--){
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
               if(OrderSymbol() == ChartSymbol()){
                  if(OrderComment() == "PowerFX_AutoTrading_Buy"|| TakeCareTotalOrders){
                     if(OrderType() == 2 ||
                        OrderType() == 4)
                        OrderDelete(OrderTicket());
                  }                   
               }
            }
         } 
         Print("獲利離場,刪除掛單重開新局(Buy)");     
      }       
   } 
   //===sell===
   if(Sell){         
      //場上已無sell單,沒開自動,buy沒開動,刪掛單,刪EA
      if(SellTime == 0 && !SellRun && !Buy){
         for(int i = Total - 1; i >= 0; i--){ 
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
               if(OrderSymbol() == ChartSymbol()){
                  if(OrderComment() == "PowerFX_AutoTrading_Sell"|| TakeCareTotalOrders){
                     if(OrderType() == 3 ||
                        OrderType() == 5)
                        OrderDelete(OrderTicket());
                  }   
               }  
            }
         }  
         PlaySound("4.stage_clear.wav"); 
         Print("場上已無sell單,沒開自動功能,沒開sell功能");
         Print("刪除場上sell limit,sell stop,關閉EA");       
         ExpertRemove();
         return;
      }
      //獲利離場,連續下單,刪掛單,準備重開 
      if(SellTime == 0 && SellRun && SellLimitTime > 0 && !SellArea){
         for(int i = Total - 1; i >= 0; i--){ 
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
               if(OrderSymbol() == ChartSymbol()){
                  if(OrderComment() == "PowerFX_AutoTrading_Sell"|| TakeCareTotalOrders){
                     if(OrderType() == 3 ||
                        OrderType() == 5)
                        OrderDelete(OrderTicket());
                  }     
               }
            }
         }
         Print("獲利離場,刪除掛單重開新局(Sell)");    
      }       
   }  
   //---Buy && Sell---
   if(Buy && Sell){
      //場上已無buy單,沒開自動,刪掛單,
      if(!BuyWaiting){
         if(BuyTime == 0 && !BuyRun && !SellRun){
            for(int i = Total - 1; i >= 0; i--){
               if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                  if(OrderSymbol() == ChartSymbol()){
                     if(OrderComment() == "PowerFX_AutoTrading_Buy"|| TakeCareTotalOrders){
                        if(OrderType() == 2 ||
                           OrderType() == 4)
                           OrderDelete(OrderTicket());
                     }                                                
                  }
               }
            }
            BuyWaiting = true;
            if(Sound)
               PlaySound("4.stage_clear.wav");      
         }   
      }  
      //場上已無Sell單,沒開自動,刪掛單,刪EA
      if(!SellWaiting){
         if(SellTime == 0 && !BuyRun && !SellRun){
            for(int i = Total - 1; i >= 0; i--){
               if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                  if(OrderSymbol() == ChartSymbol()){
                     if(OrderComment() == "PowerFX_AutoTrading_Sell"|| TakeCareTotalOrders){
                        if(OrderType() == 3 ||
                           OrderType() == 5)
                           OrderDelete(OrderTicket());
                     }                                                      
                  }
               }
            } 
            SellWaiting = true;
            if(Sound)
               PlaySound("4.stage_clear.wav");      
         } 
      } 
      //全場已無任何現價單,刪除全部掛單         
      if(SellTime == 0 && BuyTime == 0 && !BuyRun && !SellRun ){
         for(int i = Total - 1; i >= 0; i--){
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
               if(OrderSymbol() == ChartSymbol()){
                  if(OrderComment() == "PowerFX_AutoTrading_Buy"  || 
                     OrderComment() == "PowerFX_AutoTrading_Sell" ||
                     TakeCareTotalOrders){
                     if(OrderType() == 2 ||
                        OrderType() == 3 ||
                        OrderType() == 4 ||
                        OrderType() == 5)
                        OrderDelete(OrderTicket());
                  }                                      
               }
            }
         }
      Print("全場已無任何單,關閉EA");     
      ExpertRemove();
      return;   
      }    
   }

//==BUY單區===========================================================
   if(Buy){
      if(!AreaSwitch || (AreaSwitch && BuyArea)){
      //-起手區(buy)-start--
         if(BuyTime == 0 && BuyRun && !BuyWaiting){
            BasicLots = NormalizeDouble(BasicLots,2);
            BuyTP = 0;
            if(TP != 0){
               BuyTP = Ask + TP;
            }             
            OrderSend(Currency, OP_BUY, BasicLots, Ask, 0, 0, 0, "PowerFX_AutoTrading_Buy", MagicNum, 0, Green);  
            return;
         }
   //----------------------------------------------------------------------------      
         //-補單區(buy)-start--
         //場上已有buy,進行補單,buy少於最大層數
         if(BuyTime > 0 && BuyTime < MTTime){
            //計算buy補單價位,手數
            BuyMTPrice = LowerBuyPrice - MT;
            BuyMTLots = BasicLots * MathPow(MX,BuyTime);
            if(BuyMTLots<0.01){
               BuyMTLots = 0.01;
            }
            BuyMTLots = NormalizeDouble(BuyMTLots,2); 
            //於區間內
            if(BuyAreaHigherLimit > BuyMTPrice && BuyMTPrice > BuyAreaLowerLimit){             
               //距離補單------------------------------------------------------------------------  
               if(DistanceMTSwitch){
                  //場上未有limit單       
                  if(BuyLimitTime == 0){         
                     //補單位低於現價,buylimit 
                     if(BuyMTPrice < Ask){  
                        BuyTP = 0;
                        if(TP != 0){
                           BuyTP = BuyMTPrice + TP;
                        }    
                        OrderSend(Currency, OP_BUYLIMIT, BuyMTLots, BuyMTPrice, 0, 0, BuyTP, "PowerFX_AutoTrading_Buy", MagicNum, 0, Green);  
                        return;     
                     }        
                     //補單位高於現價,buy現價
                     if(BuyMTPrice >= Ask){
                        OrderSend(Currency, OP_BUY, BuyMTLots, Ask, 0, 0, 0, "PowerFX_AutoTrading_Buy", MagicNum, 0, Green);                   
                        return; 
                     }  
                     //場上已有單,而想即場馬丁而改動馬丁距參數,令現價進入馬丁範圍,刪除所有掛單,並馬上補現價單
                     if(BuyTime > 1 && BuyMTPrice > Ask){
                        for(int i = Total - 1; i >= 0; i--){ 
                           if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                              if(OrderSymbol() == ChartSymbol()){ 
                                 if(OrderType() == 2){
                                    if(OrderComment() == "PowerFX_AutoTrading_Buy" || TakeCareTotalOrders){
                                       OrderDelete(OrderTicket());
                                    }
                                 }
                              }
                           }   
                        }  
                        OrderSend(Currency, OP_BUY, BuyMTLots, Ask, 0, 0, 0, "PowerFX_AutoTrading_Buy", MagicNum, 0, Green); 
                        return; 
                     }
                  }
                  //場上掛單多於2張,刪除所有掛單,下迴圈重掛
                  if(BuyLimitTime > 1){
                     for(int i = Total - 1; i >= 0; i--){ 
                        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                           if(OrderSymbol() == ChartSymbol()){
                              if(OrderType() == 2){
                                 if(OrderComment() == "PowerFX_AutoTrading_Buy" || TakeCareTotalOrders){
                                    OrderDelete(OrderTicket());
                                 }
                              }
                           }
                        }
                     } 
                  }
               }
               //沒開距離掛單,刪除全場掛單
               if(!DistanceMTSwitch){
                  for(int i = Total - 1; i >= 0; i--){ 
                     if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                        if(OrderSymbol() == ChartSymbol()){
                           if(OrderType() == 2){
                              if(OrderComment() == "PowerFX_AutoTrading_Buy" || TakeCareTotalOrders){
                                 OrderDelete(OrderTicket());
                              }
                           }
                        }
                     }
                  } 
               }
               //時間補單模組-------------------------------------------------------------
               if(TimeMTSwitch){
                  //以首張單計算時間
                  if(!LastTimeMTSwitch){
                     //計算首張buy單的存在秒數,現在server時間 - 最初buy單時間 
                     BuyTimer = (TimeCurrent() - FirstBuyTime);
                     //存在秒數 > 設定秒數(未有定時補單)
                     if((TimeBuyTime == 0 && BuyTimer > TimeMTTimer) ||
                        //or(現在時間-最後一張定時補單時間) > 設定秒數(定時補單數量大於0)
                        (TimeBuyTime > 0 && ( (TimeCurrent() - LastTimeBuyTime) > TimeMTTimer))){
                        //清除掛單
                        for(int i = Total - 1; i >= 0; i--){ 
                           if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                              if(OrderSymbol() == ChartSymbol()){
                                 if(OrderType() == 2){
                                    if(OrderComment() == "PowerFX_AutoTrading_Buy" || TakeCareTotalOrders){
                                       OrderDelete(OrderTicket());
                                    }
                                 }
                              }
                           }
                        }
                        //現價buy
                        //計算buy補單手數
                        BuyMTLots = BasicLots * MathPow(MX,BuyTime);
                        if(BuyMTLots<0.01){
                           BuyMTLots = 0.01;
                        }
                        BuyMTLots = NormalizeDouble(BuyMTLots,2); 
                        OrderSend(Currency, OP_BUY, BuyMTLots, Ask, 0, 0, 0, "PowerFX_AutoTrading_Buy(Time)", MagicNum, 0, Green); 
                        return;     
                     }
                  }
                  //以最新單計算時間 
                  if(LastTimeMTSwitch){
                     //計算首張buy單的存在秒數,現在server時間 - 最初buy單時間 
                     BuyTimer = (TimeCurrent() - LastBuyTime);
                     //存在秒數 > 設定秒數(未有定時補單)
                     if(BuyTimer > TimeMTTimer){
                        //清除掛單
                        for(int i = Total - 1; i >= 0; i--){ 
                           if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                              if(OrderSymbol() == ChartSymbol()){
                                 if(OrderType() == 2){
                                    if(OrderComment() == "PowerFX_AutoTrading_Buy" || TakeCareTotalOrders){
                                       OrderDelete(OrderTicket());
                                    }
                                 }
                              }
                           }
                        }
                        //現價buy
                        //計算buy補單手數
                        BuyMTLots = BasicLots * MathPow(MX,BuyTime);
                        if(BuyMTLots<0.01){
                           BuyMTLots = 0.01;
                        }
                        BuyMTLots = NormalizeDouble(BuyMTLots,2); 
                        OrderSend(Currency, OP_BUY, BuyMTLots, Ask, 0, 0, 0, "PowerFX_AutoTrading_Buy(Time)", MagicNum, 0, Green); 
                        return;     
                     }
                  }                  
               }  
            }  
         }
      }
   }   
//==sell單區===========================================================
   if(Sell){
      if(!AreaSwitch || (AreaSwitch && SellArea)){   
      //現價於下單間距內
         //-起手區(sell)-start--
         if(SellTime == 0 && SellRun && !SellWaiting){
            SellTP = 0;
            if(TP > 0){
               SellTP = Bid - TP;
            } 
            BasicLots = NormalizeDouble(BasicLots,2);
            OrderSend(Currency, OP_SELL, BasicLots, Bid, 0, 0, 0, "PowerFX_AutoTrading_Sell", MagicNum, 0, Green);
            return;
         }     
         //-補單區(sell)-start--
         //場上已有sell單,進行limit補單,sell單少於最大層數
         if(SellTime > 0 && SellTime < MTTime){
            //計算sell補單價位,手數
            SellMTPrice = HigherSellPrice + MT;
            SellMTLots = BasicLots * (MathPow(MX,SellTime));
            if(SellMTLots<0.01){
               SellMTLots = 0.01;
            }
            SellMTLots = NormalizeDouble(SellMTLots,2);
            //於區間內
            if(SellAreaHigherLimit > SellMTPrice && SellMTPrice > SellAreaLowerLimit){ 
               //距離補單------------------------------------------------------------------------  
               if(DistanceMTSwitch){    
                  //場上未有limit單      
                  if(SellLimitTime == 0){          
                     //補單價位高於現價,selllimit 
                     if(SellMTPrice > Bid){   
                        SellTP = 0;
                        if(TP > 0){
                           SellTP = SellMTPrice - TP;
                        }   
                        OrderSend(Currency, OP_SELLLIMIT, SellMTLots, SellMTPrice, 0, 0, SellTP, "PowerFX_AutoTrading_Sell", MagicNum, 0, Green);  
                        return;     
                     }        
                     //補單位低於現價,sell現價
                     if(SellMTPrice <= Bid){
                        OrderSend(Currency, OP_SELL, SellMTLots, Bid, 0, 0, 0, "PowerFX_AutoTrading_Sell", MagicNum, 0, Green);  
                        return;    
                     }  
                  }
                  //場上已有掛單,而想即場馬丁而改動馬丁距參數,令現價進入馬丁範圍,刪除所有掛單,並馬上補現價單
                  if(SellLimitTime == 1 && SellMTPrice < Bid){
                     for(int i = Total - 1; i >= 0; i--){ 
                        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                           if(OrderSymbol() == ChartSymbol()){
                              if(OrderType() == 3){
                                 if(OrderComment() == "PowerFX_AutoTrading_Sell" || TakeCareTotalOrders){
                                    OrderDelete(OrderTicket());
                                 }
                              }
                           }
                        }
                     }        
                     OrderSend(Currency, OP_SELL, SellMTLots, Bid, 0, 0, 0, "PowerFX_AutoTrading_Sell", MagicNum, 0, Green);
                     return;  
                  }
                  //場上掛單多於2張,刪除所有掛單,下次迴圍重掛
                  if(SellLimitTime > 1){
                     for(int i = Total - 1; i >= 0; i--){ 
                        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                           if(OrderSymbol() == ChartSymbol()){
                              if(OrderType() == 3){
                                 if(OrderComment() == "PowerFX_AutoTrading_Sell" || TakeCareTotalOrders){
                                    OrderDelete(OrderTicket());
                                 }
                              }
                           }
                        }
                     } 
                  }
               }
               //沒開距離掛單,刪除全場掛單
               if(!DistanceMTSwitch){
                  for(int i = Total - 1; i >= 0; i--){ 
                     if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                        if(OrderSymbol() == ChartSymbol()){
                           if(OrderType() == 3){
                              if(OrderComment() == "PowerFX_AutoTrading_Sell" || TakeCareTotalOrders){
                                 OrderDelete(OrderTicket());
                              }
                           }
                        }
                     }
                  } 
               }         
               //時間補單模組
               if(TimeMTSwitch){
                  //以首張單計算時間
                  if(!LastTimeMTSwitch){
                     //計算首張Sell單的存在秒數,現在server時間 - 最初Sell單時間 
                     SellTimer = TimeCurrent() - FirstSellTime;
                     //存在秒數 > 設定秒數(未有定時補單)
                     if((TimeSellTime == 0 && SellTimer > TimeMTTimer) ||
                        //or(現在時間-最後一張定時補單時間) > 設定秒數(定時補單數量大於0)
                        (TimeSellTime > 0 && ( (TimeCurrent() - LastTimeSellTime) > TimeMTTimer))){
                        //清除掛單
                        for(int i = Total - 1; i >= 0; i--){ 
                           if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                              if(OrderSymbol() == ChartSymbol()){
                                 if(OrderType() == 3){
                                    if(OrderComment() == "PowerFX_AutoTrading_Sell" || TakeCareTotalOrders){
                                       OrderDelete(OrderTicket());
                                    }
                                 }
                              }
                           }
                        }
                        //現價Sell
                        //計算Sell補單手數
                        SellMTLots = BasicLots * MathPow(MX,SellTime);
                        if(SellMTLots<0.01){
                           SellMTLots = 0.01;
                        }
                        SellMTLots = NormalizeDouble(SellMTLots,2); 
                        OrderSend(Currency, OP_SELL, SellMTLots, Bid, 0, 0, 0, "PowerFX_AutoTrading_Sell(Time)", MagicNum, 0, Green); 
                        return; 
                     }
                  }
                  //以最新單計算時間
                  if(LastTimeMTSwitch){
                     //計算首張Sell單的存在秒數,現在server時間 - 最初Sell單時間 
                     SellTimer = TimeCurrent() - LastSellTime;
                     //存在秒數 > 設定秒數(未有定時補單)
                     if((TimeSellTime == 0 && SellTimer > TimeMTTimer) ||
                        //or(現在時間-最新單時間) > 設定秒數(定時補單數量大於0)
                        (TimeSellTime > 0 && ( (TimeCurrent() - LastSellTime) > TimeMTTimer))){
                        //清除掛單
                        for(int i = Total - 1; i >= 0; i--){ 
                           if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){ 
                              if(OrderSymbol() == ChartSymbol()){
                                 if(OrderType() == 3){
                                    if(OrderComment() == "PowerFX_AutoTrading_Sell" || TakeCareTotalOrders){
                                       OrderDelete(OrderTicket());
                                    }
                                 }
                              }
                           }
                        }
                        //現價Sell
                        //計算Sell補單手數
                        SellMTLots = BasicLots * MathPow(MX,SellTime);
                        if(SellMTLots<0.01){
                           SellMTLots = 0.01;
                        }
                        SellMTLots = NormalizeDouble(SellMTLots,2); 
                        OrderSend(Currency, OP_SELL, SellMTLots, Bid, 0, 0, 0, "PowerFX_AutoTrading_Sell(Time)", MagicNum, 0, Green); 
                        return; 
                     }
                  }   
               }                           
            }
         } 
      }
   }






//+------------------------------------------------------------------+
   }
}
如您喜欢此文章请点下面分享按钮↴峰汇在线 » 带有中文注释的智能交易系统PowerFX_AutoTrading
上一篇:
下一篇:
分享到:更多 ()