Created
March 22, 2016 13:55
-
-
Save APPEA/6644c30d97421c2e48c9 to your computer and use it in GitHub Desktop.
拆彈部隊
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| AllNewXBOMB.mq4 | | |
//| APP Wargame-Hydra | | |
//| http://www.facebook.com/groups/moneywargame/ | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import "kernel32.dll" | |
ulong GetVersion(void); | |
#import | |
#define CHT 1 | |
//#define ENG 1 | |
#define ZERO 0.000001 | |
#define MODE_BUY 1 | |
//#define MODE_SELL 1 | |
//#define MODE_DEMO 1 | |
//#define MODE_AREA51 1 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_CHECKTIMEIN 0 | |
#define MODE_TIMESTP 1 | |
#define MODE_TIMESSL 2 | |
#define MODE_DAILYTP 3 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_FIRSTTRADE 99 | |
#define MODE_MARTIN 100 | |
#define OP_ALL 101 | |
#define MODE_SLTP_BYPOINT 102 | |
#define MODE_SLTP_BYPRICE 103 | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int id_1 = 3333333; | |
int ErrorCode = 0; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
int BarTime = 0; | |
double NextTime=0.5; | |
double point = 0.0; | |
double iLots = 0.0; | |
double Start_Balance = 0; | |
double iPending; | |
datetime H1BarTime = 0; | |
datetime FirstTradeBar = 0; | |
datetime Start_P = 0; | |
datetime EARunTime = 0; | |
datetime AvoidTime[1] = {0}; | |
datetime EGStartTime = TimeCurrent(); | |
bool EGFlag = true; | |
bool NewGetSW = false; | |
bool AuthorGetSW = false; | |
bool TradeSW = true; | |
int Last_Time[1]={0}; | |
int Status[1]={0}; | |
double Last_Price[1]={-1.0}; | |
double MinPriceShot = 0.0; | |
string AvoidSymbol[1] = {""}; | |
string LogText[4] = {"小虧為贏" , "次止盈" , "次止損" , "日止盈"}; | |
string Label_arr[6] = | |
{ | |
"TimesProfitPer", | |
"Start_Balance", | |
"TimePast", | |
"Orders", | |
"TodayProfitPer", | |
"MaxLoss" | |
}; | |
string Label_arr1[6] = | |
{ | |
"TimesProfitPer0", | |
"Start_Balance0", | |
"TimePast0", | |
"Orders0", | |
"TodayProfitPer0", | |
"MaxLoss0" | |
}; | |
string Start_P_str = "Start_P"; | |
string Start_Balance_str = "Start_Balance"; | |
string MaxEquity_str = "MaxEquity"; | |
string NextSW_str = "NextSW"; | |
string EARUNTIME_str = "EARUNTIME"; | |
string Light = "LightStatus"; | |
string Run = "RunStatus"; | |
string Margin = "Margin"; | |
string FloatLoss = "Float"; | |
string NewsInfo[2] = {"NewsInfo0", "NewsInfo1"}; | |
string Light_str[2] = {"Light","Light2"}; | |
#ifdef CHT | |
string NewsEngage = "狂派情資已接收完全.....全面開戰!"; | |
#endif | |
#ifdef ENG | |
string NewsEngage = "News retrieval completed .....automated system initiated"; | |
#endif | |
//+------------------------------------------------------------------+ | |
//|Ender's Game Par | | |
//+------------------------------------------------------------------+ | |
datetime ticket_time = 0; | |
int OutCntTemp = 0; | |
#ifdef CHT | |
input bool AutoRepairSW = false; //自動回復停損停利 | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //ESP戰艦穩定煞車系統 | |
int pSeconds=14; | |
input double Energy=4; //能量偵測靈敏度 | |
input int SleepTime=15; //能量引擎啟動時間(分鐘) | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //預警雷達控制模組 | |
input int MinBefore = 30; //前幾分鐘閃避 | |
input int MinAfter = 30; //後幾分鐘閃避 | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //保證金比例控制模組 | |
input double MarginLimit = 2000; //保證金低於%數暫停 | |
input bool FloatEq = false; //浮虧控制模組 | |
input double FloatAm = -4000; //虧損(-金額)暫停 | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //開始時間,格式為HH | |
input int DayStopHour = 00; //結束時間,格式為HH | |
input string WeekDay="12345"; //週一執行,鍵入1;週一週二鍵入12 | |
input bool WeekStartOverSW = true; //一個禮拜頭尾不間斷 | |
input int WeekStartHour = 8; //第一天幾點開始 | |
input int WeekStopHour = 17; //最後一天幾點結束 | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //止盈計算本金基準 | |
input bool TakeProfitPercentSW = true; //次止盈開關 | |
input double TakeProfitPercent = 5; //次止盈,每次最多獲利%,立即平倉歸零重啟新局 | |
input bool TakeProfitPercentDaySW = true; //日止盈開關 | |
input double TakeProfitPercentDay = 1000; //日止盈,每日最多獲利%,立即停止交易,隔日重新啟動 | |
input bool StopLossPercentSW = true; //浮虧止損開關 | |
input double StopLossPercent = 1000; //現時帳戶浮虧止損,止損基準值:帳戶餘額 | |
input bool SleepLoss = true; //止損後當日不再出兵 | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //小虧為贏開關 | |
input int MaxTime = 150; //每局最長時間(分鐘) | |
input int MaxIn = 10; //每局最多兵力 | |
input double MaxP = 0.5; //達成以上條件小虧為贏 | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //掛單價格 | |
input bool SingleOut = false; //單進單出 | |
#endif | |
input bool MartinSW = true; //馬丁開關 | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //第幾張馬丁開始加倍 | |
#endif | |
input double LotMultiplikator = 1.382; //馬丁倍率 | |
input double Step = 30; //馬丁間距 | |
input int MaxTrades = 5; //最多單數 | |
input double MM = 1.0; //資金管理%數 | |
input double Lots = 0.01; //手數 | |
input double TakeProfit = 10.0; //獲利點數 | |
input double StopLoss = 10.0; //停損點數 | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
#ifdef ENG | |
input bool AutoRepairSW = false; //Auto Repair SL&TP | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //BrakeSystem | |
int pSeconds=14; | |
input double Energy=4; //Braking System Sensitivity Control | |
input int SleepTime=15; //Braking System Sleeping Time | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //Braking System Sleeping Time | |
input int MinBefore = 30; //Stop trading few minutes before news alert | |
input int MinAfter = 30; //Stop trading few minutes after news alert | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //Margin Level Control Module | |
input double MarginLimit = 2000; //Margin level % limit to stop trading | |
input bool FloatEq = false; //Floating Loss Control Module | |
input double FloatAm = -4000; //Floating loss (-$amount) limit to stop trading | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //Starting Time, in HH format | |
input int DayStopHour = 00; //Ending Time, in HH format | |
input string WeekDay="12345"; //Star on Monday=1, Monday and Tuesday=12 | |
input bool WeekStartOverSW = true; //Trade everyday without stop | |
input int WeekStartHour = 8; //First Day Staring Time | |
input int WeekStopHour = 17; //Last Day Ending Time | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //Capital setting for Profit/Loss termination | |
input bool TakeProfitPercentSW = true; //Switch of Secondary Take Profit | |
input double TakeProfitPercent = 1; //Secondary Take Profit, max. % of profit to close trade, and reset | |
input bool TakeProfitPercentDaySW = true; //Switch of Daily Take Profit | |
input double TakeProfitPercentDay = 1000; //Daily Take Profit, max. % of profit to close trade, reset on the next day | |
input bool StopLossPercentSW = true; //Switch of stopping Floating Loss | |
input double StopLossPercent = 1000; //Stop Loss of Current Account Max. Floating Loss (Based on Balance) | |
input bool SleepLoss = true; //Stop trading when reaching stop loss the same day | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //Switch of Small Loss To Win | |
input int MaxTime = 150; //Max. Time Per Set (Minutes) | |
input int MaxIn = 10; //Max. Trades Per Set | |
input double MaxP = 0.5; //Criteria of Small Loss for Win | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //Pending Order at Price | |
input bool SingleOut = false; //Take Profit Single Out | |
#endif | |
input bool MartinSW = true; //Start Martingale | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //Number to start martingale multiplication | |
#endif | |
input double LotMultiplikator = 1.382; //Martingale Multiplier | |
input double Step = 30; //Martingale Distance | |
input int MaxTrades = 5; //Max. Number of Orders | |
input double MM = 5.0; //Fund Management % | |
input double Lots = 0.01; //Order Size_lot | |
input double TakeProfit = 10.0; //Take Profit | |
input double StopLoss = 10.0; //Stop Loss | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
string Newsfile = "ffcal_week_this.xml"; | |
string EventFile = "NewsEvent.csv"; | |
string AuthorFile = "58426f6d625f313530333235"; | |
int newverMT4=1; | |
int accountMode=1; | |
string accountID="307383675471044242004002478351777461355442714470680287367714305481330815525625712384844860686545066036787326516602236575060168253182382223384704564023840278267055768862740384331027464044486413846161126644733334767107807263270163337825300231566058026024280"; | |
string EndDate="773007153340217643304083503643662664135084287337732519558536277033134667745660522246521343428004006023880442472680006145250813282575063624287537184878081645870548647881741716731701678285887276610110026947142762324384327364030733857683026770853863520111866"; | |
int OnInit() | |
{ | |
//--- | |
CheckOSVersion(); | |
//+------------------------------------------------------------------+ | |
//|Get Point By Symbol | | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
iLots = OpLots(); | |
ticket_time = TimeCurrent(); | |
//+------------------------------------------------------------------+ | |
//|Random Minute | | |
//+------------------------------------------------------------------+ | |
MathSrand(GetTickCount()); | |
DownloadMin = 1 + ((59 * MathRand()) / 32768); | |
#ifdef MODE_AREA51 | |
TimesStopConfig(); | |
//+------------------------------------------------------------------+ | |
//|Update Files | | |
//+------------------------------------------------------------------+ | |
iPending = Pending; | |
#endif | |
UpdateFiles(MODE_INIT); | |
//--- | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
GlobalVariableSet("DNSW",0); | |
DeleteObjects(); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
#ifdef MODE_BUY | |
if (NonStop==false) EndersGameModule(Symbol(),OP_BUY,id_1,EGFlag); | |
#endif | |
#ifdef MODE_SELL | |
if (NonStop==false) EndersGameModule(Symbol(),OP_SELL,id_1,EGFlag); | |
#endif | |
UpdateFiles(MODE_TICK); | |
if (!EAControl_Enders()) return; | |
#ifdef MODE_AREA51 | |
double TimesProfit = 0.0; | |
double TodayProfit = 0.0; | |
if (!CloseAllTrades()) return; | |
if (!TimesStop(TimesProfit,TodayProfit)) return; | |
Displayprofit(TimesProfit,TodayProfit); | |
CheckTimeIn(TimesProfit); | |
//+------------------------------------------------------------------+ | |
//|Shutdown EA | | |
//+------------------------------------------------------------------+ | |
if (!EAControl()) return; | |
BrakeSystemFun(); | |
#endif | |
#ifdef MODE_BUY | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_BUY,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_BUY); | |
Martin(OP_BUY); | |
} | |
#endif | |
#ifdef MODE_SELL | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_SELL,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_SELL); | |
Martin(OP_SELL); | |
} | |
#endif | |
} | |
//+------------------------------------------------------------------+ | |
//|Determine Martingale TakeProfi | | |
//+------------------------------------------------------------------+ | |
double Cal_TP(int Type,double iPrice,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double AllOrderLots = 0.0; | |
double TP_Goal; | |
double FirstOrderLots = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == Type && OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
FirstOrderLots = OrderLots(); | |
TotalLots += OrderLots(); | |
if (Type == OP_SELL) | |
TotalDisProduct += (iPrice - OrderOpenPrice()) / point * OrderLots(); | |
if (Type == OP_BUY) | |
TotalDisProduct += (OrderOpenPrice() - iPrice) / point * OrderLots(); | |
} | |
} | |
if (EqualZero(TotalLots)) return(0.0); | |
TP_Goal = ((TPPoint * FirstOrderLots) + TotalDisProduct) / TotalLots; | |
if (Type == OP_SELL) | |
return(iPrice - (TP_Goal * point)); | |
if (Type == OP_BUY) | |
return(iPrice + (TP_Goal * point)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|OrderSend Function | | |
//+------------------------------------------------------------------+ | |
int SendOrders(const int Type ,const string iComment) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double InPrice = 0.0; | |
switch (Type) | |
{ | |
case OP_BUY: | |
InPrice = Ask; | |
break; | |
case OP_SELL: | |
InPrice = Bid; | |
break; | |
} | |
double SendOrders_Lots = MartinLots(Type); | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
int Ticket = OrderSend(Symbol(),Type,SendOrders_Lots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,Type == OP_BUY ? clrLime : clrRed); | |
if (Ticket < 0) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(ErrorCode)); | |
} | |
return(Ticket); | |
} | |
void Set_SL_TP(const int Mode,const int Type,double iSL,double iTP) | |
{ | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++)//執行N次篩選(N=訂單總數) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1)//挑選出同貨幣對 && BUY單 && MagicNumber=本程式 的訂單 | |
{ | |
if (Mode == MODE_SLTP_BYPOINT) | |
{ | |
if (Type == OP_BUY || Type == OP_BUYSTOP || Type == OP_BUYLIMIT) | |
{ | |
TP = OrderOpenPrice() + iTP * point; | |
SL = OrderOpenPrice() - iSL * point; | |
} | |
if (Type == OP_SELL || Type == OP_SELLSTOP || Type == OP_SELLLIMIT) | |
{ | |
TP = OrderOpenPrice() - iTP * point; | |
SL = OrderOpenPrice() + iSL * point; | |
} | |
if (EqualZero(iTP)) TP = 0; | |
if (EqualZero(iSL)) SL = 0; | |
}else | |
{ | |
SL = iSL; | |
TP = iTP; | |
} | |
if (iTP < 0) | |
{ | |
TP = OrderTakeProfit(); | |
} | |
if (iSL < 0) | |
{ | |
SL = OrderStopLoss(); | |
} | |
if ((CompareDouble(OrderStopLoss(),NormalizeDouble(SL,Digits))) && (CompareDouble(OrderTakeProfit(),NormalizeDouble(TP,Digits)))) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_SL_TP " + IntegerToString(OrderTicket()) + " Error: " + ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= ZERO) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= ZERO && A >= -ZERO) return(true); | |
return(false); | |
} | |
void Martin(const int Type) | |
{ | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (MartinSW) | |
{ | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (BarTime == Bars && MyOrderTotal(Type) < MaxTrades) | |
{ | |
if (SendOrders(Type,"XBOMB" + IntegerToString(MyOrderTotal(Type) + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_MARTIN); | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_BUY && MyOrderTotal(OP_BUY) > 0) | |
{ | |
if (Ask <= MinPrice - Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_SELL && MyOrderTotal(OP_SELL) > 0) | |
{ | |
if (Bid >= MaxPrice + Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (MyOrderTotal(Type) > 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_MARTIN); | |
} | |
} | |
void FirstTrade(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
if (Type == OP_BUY && (MyOrderTotal(OP_BUYSTOP) > 0 || MyOrderTotal(OP_BUYLIMIT) > 0)) return; | |
if (Type == OP_SELL && (MyOrderTotal(OP_SELLSTOP) > 0 || MyOrderTotal(OP_SELLLIMIT) > 0)) return; | |
if (Cnt == 0 && FirstTradeBar != Time[0]) | |
{ | |
if (SendOrders(Type,"XBOMB(" + IntegerToString(Type) + ")." + IntegerToString(Cnt + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_FIRSTTRADE); | |
FirstTradeBar = Time[0]; | |
} | |
} | |
if (Cnt == 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_FIRSTTRADE); | |
} | |
double GetLastTciketINFO(const int Type,double &MaxPrice,double &MinPrice) | |
{ | |
double iMaxPrice = 0.0; | |
double iMinPrice = 5000.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol() && OrderType() == Type) | |
{ | |
if (OrderOpenPrice() >= iMaxPrice) | |
iMaxPrice = OrderOpenPrice(); | |
if (OrderOpenPrice() <= iMinPrice && !EqualZero(iMinPrice)) | |
iMinPrice = OrderOpenPrice(); | |
} | |
} | |
MaxPrice = iMaxPrice; | |
MinPrice = iMinPrice; | |
return(0.0); | |
} | |
int MyOrderTotal(int Type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL) | |
{ | |
x++; | |
continue; | |
} | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
void iCloseOrders(int type) | |
{ | |
int C0_cnt; | |
if (type == OP_BUYSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUYSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELLSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELLSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrLime)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
}else { | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selecttickets() | |
{ | |
int cnt_tickets = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)) | |
#endif | |
{ | |
ticket_time=OrderOpenTime(); | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selectticketsfrhistory() | |
{ | |
int cnt_tickets = 0; | |
for (int i=0;i<OrdersHistoryTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && ticket_time<=OrderOpenTime()) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && ticket_time<=OrderOpenTime()) | |
#endif | |
{ | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double MartinLots(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
#ifdef MODE_AREA51 | |
int StartM = MartinMStart; | |
if (StartM <= 0) StartM = 2; | |
#else | |
int StartM = 2; | |
#endif | |
if (Cnt <= 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,0),2)); | |
if (Cnt > 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,(double)(Cnt - 1) - (StartM - 2)),2)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|Optimize Lots | | |
//+------------------------------------------------------------------+ | |
double OpLots() | |
{ | |
double Out = Lots; | |
if (!EqualZero(MM) && MM > 0) | |
{ | |
Out = (AccountBalance() * MM ) / (1000 * TakeProfit); | |
} | |
return(Out); | |
} | |
#ifdef MODE_AREA51 | |
void PendingTrade(const int Type,const string iComment) | |
{ | |
double InPrice = Pending; | |
int Ticket = 0; | |
int iType = -1; | |
if (Type == OP_BUY) | |
{ | |
if (InPrice > Ask) | |
iType = OP_BUYSTOP; | |
if (InPrice < Ask) | |
iType = OP_BUYLIMIT; | |
if (MyOrderTotal(OP_BUYSTOP) >= 1 || MyOrderTotal(OP_BUYLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrLime); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (InPrice < Bid) | |
iType = OP_SELLSTOP; | |
if (InPrice > Bid) | |
iType = OP_SELLLIMIT; | |
if (MyOrderTotal(OP_SELLSTOP) >= 1 || MyOrderTotal(OP_SELLLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrRed); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
return; | |
} | |
bool CloseAllTrades() | |
{ | |
if (GlobalVariableGet(NextSW_str) == 1) | |
{ | |
#ifdef MODE_BUY | |
iCloseOrders(OP_BUY); | |
#endif | |
#ifdef MODE_SELL | |
iCloseOrders(OP_SELL); | |
#endif | |
Sleep(10000); | |
if (MyOrderTotal(OP_ALL) > 0) | |
{ | |
#ifdef CHT | |
Alert("水雷未掃除成功"); | |
#endif | |
#ifdef ENG | |
Alert("Submarine mine sweeper not completed"); | |
#endif | |
return(false); | |
}else | |
{ | |
Sleep((int)(NextTime * 1000 * 60)); | |
} | |
} | |
return(true); | |
} | |
void CheckTimeIn(const double TimesProfit) | |
{ | |
datetime RunTime = (TimeCurrent() - Start_P) / 60; | |
if (SmallLossWinSW && RunTime >= MaxTime && OrdersTotal() >= MaxIn && TimesProfit >= MaxP) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_CHECKTIMEIN,""); | |
} | |
} | |
void WriteLog(const int Mode,const string in) | |
{ | |
double TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
string str = WindowExpertName() + "," + Symbol() + "," + TimeToString(TimeCurrent(),TIME_DATE|TIME_DATE|TIME_SECONDS) + | |
": " + LogText[Mode] + " , " + in + | |
"," + Start_Balance_str + ": " + DoubleToString(Start_Balance,2) + | |
"," + Start_P_str + ": " + IntegerToString(Start_P) + | |
",TimeProfit: " + DoubleToString(TimesProfit,2) + | |
"| Set: 餘:" + DoubleToString(iBalance,2) + | |
",次:" + DoubleToString(TakeProfitPercent,2) + | |
",日:" + DoubleToString(TakeProfitPercentDay,2) + | |
",損:" + DoubleToStr(StopLossPercent ,2) + | |
",時:" + DoubleToString(MaxTime,2) + | |
",單:" + DoubleToString(MaxIn,2) + | |
",虧:" + DoubleToString(MaxP,2) + | |
"\n" | |
; | |
int h1 = FileOpen("Logs.txt",FILE_READ|FILE_WRITE|FILE_TXT); | |
if (h1 < 0) Print("Open Log Failed"); | |
FileSeek(h1,0,SEEK_END); | |
int byte = FileWriteString(h1,str,StringLen(str)); | |
if (byte <= 0) Print("Write Log Failed"); | |
FileClose(h1); | |
} | |
double GetProfitPeriod(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType() == OP_BUY || OrderType() == OP_SELL) | |
{ | |
if (today < OrderCloseTime()) | |
profit += OrderProfit() + OrderCommission() + OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
void Displayprofit(const double TimesProfit, const double TodayProfit) | |
{ | |
int Pos = 50; | |
string Label_Text = ""; | |
string Label_Text1 = ""; | |
if (ObjectFind(Run) < 0) ObjectCreate(0,Run,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Run, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Run, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Run, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Run, OBJPROP_YDISTANCE, Pos); | |
ObjectSetInteger(0,Run, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Run, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetString(0,Run, OBJPROP_FONT, "微軟正黑體"); | |
for (int i=0;i<6;i++) | |
{ | |
if(ObjectFind(Label_arr[i]) < 0) ObjectCreate(0,Label_arr[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_XDISTANCE,120); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr[i],OBJPROP_FONT,"微軟正黑體"); | |
if(ObjectFind(Label_arr1[i]) < 0) ObjectCreate(0,Label_arr1[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_FONT,"微軟正黑體"); | |
switch(i) | |
{ | |
#ifdef CHT | |
case 0: | |
Label_Text = "單次擄獲狂派戰俘: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "進場起始兵力: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "進場經過時間: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " 分鐘"; | |
break; | |
case 3: | |
Label_Text = "戰場兵力: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "今日擄獲狂派戰俘累積至: " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "最大暫時被擄穫兵力: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
#ifdef ENG | |
case 0: | |
Label_Text = "Profit/Loss: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "Start Balance: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "Time Elapsed: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " Minutes"; | |
break; | |
case 3: | |
Label_Text = "Orders: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "Profit/Loss (Today): " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "Maximum Floating Loss: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
} | |
ObjectSetString(0,Label_arr[i],OBJPROP_TEXT,Label_Text); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_TEXT,Label_Text1); | |
} | |
for (int i=0;i<ArraySize(NewsInfo);i++) | |
{ | |
if (ObjectFind(NewsInfo[i]) < 0) ObjectCreate(0,NewsInfo[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_YDISTANCE, Pos + 150 + i * 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,NewsInfo[i], OBJPROP_FONT, "微軟正黑體"); | |
} | |
ObjectCreate(0,Margin,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Margin, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Margin, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Margin, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Margin, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Margin, OBJPROP_YDISTANCE, Pos+195); | |
ObjectSetInteger(0,Margin, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,Margin, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
ObjectCreate(0,FloatLoss,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_YDISTANCE, Pos+215); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,FloatLoss, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
if (ObjectFind(Light) < 0) | |
ObjectCreate(0,Light,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Light,OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Light, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Light,OBJPROP_COLOR,clrGold); | |
ObjectSetInteger(0,Light, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Light,OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Light,OBJPROP_YDISTANCE,Pos+235); | |
ObjectSetString(0,Light, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Light,OBJPROP_TEXT,""); | |
} | |
bool EAControl() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!Margin()) | |
{ | |
str = "Margin"; | |
Out = false; | |
} | |
if (!Float()) | |
{ | |
str = "Float"; | |
Out = false; | |
} | |
if (TimeCurrent() <= EARunTime) | |
{ | |
str = "SLSLeep"; | |
Out = false; | |
} | |
if (!WeekStartOver()) | |
{ | |
str = "Time"; | |
Out = false; | |
} | |
if (!AvoidNews()) | |
{ | |
str = "News"; | |
Out = false; | |
} | |
/*if (!Margin() || !Float() || TimeCurrent() <= EARunTime || !WeekStartOver() || !AvoidNews()) | |
{ | |
Comment("EAControl 1"); | |
return(false); | |
}*/ | |
Comment(str); | |
return(Out); | |
} | |
bool iTimeControl() | |
{ | |
bool EA_Valid = false; | |
if (DayStopHour == DayStartHour) EA_Valid = true; | |
if (DayStopHour < DayStartHour) | |
{ | |
if (TimeHour(TimeLocal()) >= DayStartHour || TimeHour(TimeLocal()) < DayStopHour) | |
EA_Valid = true; | |
} | |
if (DayStopHour > DayStartHour) | |
{ | |
if ((TimeHour(TimeLocal()) >= DayStartHour && TimeHour(TimeLocal()) < DayStopHour)) | |
EA_Valid = true; | |
} | |
return(EA_Valid); | |
} | |
bool WeekStartOver() | |
{ | |
bool EA_Valid = true; | |
int Dayweek = TimeDayOfWeek(TimeLocal()); | |
if (WeekStartOverSW) | |
{ | |
if ( | |
Dayweek < StrToInteger(StringSubstr(WeekDay,0,1)) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,0,1)) && TimeHour(TimeLocal()) < WeekStartHour) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) && TimeHour(TimeLocal()) >= WeekStopHour ) || | |
Dayweek > StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) | |
) | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
if (iTimeControl()) | |
{ | |
if (StringFind(WeekDay,DoubleToStr(Dayweek,0),0) >= 0) | |
{ | |
EA_Valid = true; | |
}else | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
EA_Valid = false; | |
} | |
} | |
#ifdef CHT | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"未在設定時區內,現有戰役達成後將離開戰場"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"已進入作戰時區內,全面啟動執行..."); | |
return(true); | |
} | |
#endif | |
#ifdef ENG | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Not in trading time zone, stop trading after current set"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Entered trading time zone, automated system initiated"); | |
return(true); | |
} | |
#endif | |
} | |
int NewsConvert() | |
{ | |
string maindata[][7]; | |
string sBuffer = ""; | |
if (!FileIsExist(Newsfile)) | |
{ | |
return(0); | |
} | |
int h1=FileOpen(Newsfile,FILE_SHARE_READ|FILE_CSV); | |
int i = 0 , j = 0; | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(sBuffer,"<title>") >= 0) | |
{ | |
ArrayResize(maindata,j+1); | |
maindata[j][0] = sBuffer; | |
for (i=1;i<7;i++) | |
{ | |
maindata[j][i] = FileReadString(h1); | |
} | |
j++; | |
} | |
} | |
FileClose(h1); | |
int count = ArrayRange(maindata,0); | |
string sTags[7] = { "<title>", "<country>", "<date><![CDATA[", "<time><![CDATA[", "<impact><![CDATA[", "<forecast><![CDATA[", "<previous><![CDATA[" }; | |
string eTags[7] = { "</title>", "</country>", "]]></date>", "]]></time>", "]]></impact>", "]]></forecast>", "]]></previous>" }; | |
for (i=0;i<count;i++) | |
{ | |
for (j=0;j<7;j++) | |
{ | |
maindata[i][j]=StringSubstr(maindata[i][j],StringFind(maindata[i][j],sTags[j],0)+StringLen(sTags[j]),StringFind(maindata[i][j],eTags[j],0)-StringFind(maindata[i][j],sTags[j],0)-StringLen(sTags[j])); | |
} | |
} | |
int h2 = FileOpen("NewsEvent.csv",FILE_SHARE_READ|FILE_WRITE|FILE_CSV,';'); | |
int GMT = 0; | |
string time_str = ""; | |
string prev_time_str = ""; | |
for (i=0;i<count;i++) | |
{ | |
if (maindata[i][4] == "High") | |
{ | |
GMT = -1 * (TimeGMTOffset()/3600); | |
time_str = TimeToString( | |
StringToTime(MakeDateTime(maindata[i][2],maindata[i][3])) + GMT * 3600, | |
TIME_DATE|TIME_MINUTES|TIME_SECONDS | |
); | |
if (time_str == prev_time_str) continue; | |
FileWrite(h2,maindata[i][1],time_str); | |
prev_time_str = time_str; | |
} | |
} | |
FileClose(h2); | |
return(count); | |
} | |
string MakeDateTime(string strDate, string strTime) | |
{ | |
// Print("Converting Forex Factory Time into Metatrader time..."); //added by MN | |
// Converts forexfactory time & date into yyyy.mm.dd hh:mm | |
int n1stDash = StringFind(strDate, "-"); | |
int n2ndDash = StringFind(strDate, "-", n1stDash+1); | |
string strMonth = StringSubstr(strDate, 0, 2); | |
string strDay = StringSubstr(strDate, 3, 2); | |
string strYear = StringSubstr(strDate, 6, 4); | |
// strYear = "20" + strYear; | |
int nTimeColonPos = StringFind(strTime, ":"); | |
string strHour = StringSubstr(strTime, 0, nTimeColonPos); | |
string strMinute = StringSubstr(strTime, nTimeColonPos+1, 2); | |
string strAM_PM = StringSubstr(strTime, StringLen(strTime)-2); | |
int nHour24 = StrToInteger(strHour); | |
if ((strAM_PM == "pm" || strAM_PM == "PM") && nHour24 != 12) | |
{ | |
nHour24 += 12; | |
} | |
if ((strAM_PM == "am" || strAM_PM == "AM") && nHour24 == 12) | |
{ | |
nHour24 = 0; | |
} | |
string strHourPad = ""; | |
if (nHour24 < 10) | |
strHourPad = "0"; | |
return(StringConcatenate(strYear, ".", strMonth, ".", strDay, " ", strHourPad, nHour24, ":", strMinute)); | |
} | |
void RebuildNewsArray() | |
{ | |
NewsConvert(); | |
string sBuffer = ""; | |
int i = 0; | |
int h1 = FileOpen(EventFile,FILE_SHARE_READ|FILE_CSV); | |
if (h1 < 0) | |
{ | |
ArrayResize(AvoidTime,1); | |
ArrayResize(AvoidSymbol,1); | |
return; | |
} | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(Symbol(),sBuffer) >= 0) | |
{ | |
ArrayResize(AvoidSymbol,i+2); | |
AvoidSymbol[i+1] = sBuffer; | |
ArrayResize(AvoidTime,i+2); | |
sBuffer = FileReadString(h1); | |
AvoidTime[i+1] = StrToTime(sBuffer); | |
i++; | |
} | |
} | |
FileClose(h1); | |
} | |
bool AvoidNews() | |
{ | |
#ifdef CHT | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "預警雷達已偵測到 " + AvoidSymbol[i] + " 戰場於 " + StringSubstr(TimeToStr(AvoidTime[i]),11,5) + " 可能將有大量狂派埋伏"; | |
str[1] = "超級達摩戰艦於 " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5) + " 啟動防禦系統進入一級警戒"; | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "正在接收狂派情資..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "最後狂派活動時間: " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "已無其他狂派活動或情資已過期,即將休兵......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
#ifdef ENG | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "News Alert on " + AvoidSymbol[i] + " at time " + StringSubstr(TimeToStr(AvoidTime[i]),11,5); | |
str[1] = "Red alert! Braking System Standing By at " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5); | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "News retrieving..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "Latest News at : " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "No trading info available......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
} | |
bool Margin() | |
{ | |
if (MarginSleep) | |
{ | |
double Margin_R = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); | |
if (EqualZero(Margin_R)) return(true); | |
if (MarginLimit >= Margin_R) | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,"Margin level lower than " + DoubleToString(MarginLimit,2) + " automated system suspended"); | |
return(false); | |
}else | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool Float() | |
{ | |
if (FloatEq) | |
{ | |
double Profit = AccountProfit(); | |
if (Profit <= FloatAm) | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,"Floating loss greater than "+DoubleToString(FloatAm,2)+" automated system suspended"); | |
return(0); | |
}else | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool TimesStopConfig() | |
{ | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(NextSW_str,0); | |
#ifdef GLOBAL_FILE | |
if (!GlobalFileRead()) | |
{ | |
GlobalVars.G_EARunTime = 0; | |
GlobalVars.G_MaxEquity = (AccountProfit() / AccountBalance()) * 100; | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalFileWrite(); | |
GlobalFileRead(); | |
} | |
Print("EARuntime: " + TimeToString(GlobalVars.G_EARunTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("MaxLossEquity: " + DoubleToString(GlobalVars.G_MaxEquity,2)); | |
Print("Start_P: " + TimeToString(GlobalVars.G_Start_P,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("Start_Balance: " + DoubleToString(GlobalVars.G_Start_Balance,2)); | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
#endif | |
return(true); | |
} | |
bool TimesStop(double &TimesProfit, double &TodayProfit) | |
{ | |
#ifdef GLOBAL_FILE | |
if (OrdersTotal()==0) | |
{ | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Status | | |
//+------------------------------------------------------------------+ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVars.G_Start_Balance; | |
else | |
Start_Balance = iBalance; | |
Start_P = GlobalVars.G_Start_P; | |
EARunTime = GlobalVars.G_EARunTime; | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVars.G_MaxEquity) | |
{ | |
GlobalVars.G_MaxEquity = (AccountProfit() / Start_Balance) * 100; | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Status | | |
//+------------------------------------------------------------------+ | |
GlobalVars.G_EARunTime = EARunTime; | |
GlobalVars.G_Start_Balance = Start_Balance; | |
GlobalVars.G_Start_P = Start_P; | |
GlobalFileWrite(); | |
#else | |
if (OrdersTotal()==0) | |
{ | |
GlobalVariableSet(Start_Balance_str,AccountBalance()); | |
GlobalVariableSet(Start_P_str,TimeCurrent()); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Balance | | |
//+------------------------------------------------------------------+ | |
if (GlobalVariableCheck(Start_Balance_str)) | |
{ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVariableGet(Start_Balance_str); | |
else | |
Start_Balance = iBalance; | |
}else | |
{ | |
Start_Balance = AccountBalance(); | |
} | |
if (GlobalVariableCheck(Start_P_str)) | |
{ | |
Start_P = (datetime)GlobalVariableGet(Start_P_str); | |
}else | |
{ | |
Start_P = TimeCurrent(); | |
} | |
if (GlobalVariableCheck(EARUNTIME_str)) | |
{ | |
EARunTime = (datetime)GlobalVariableGet(EARUNTIME_str); | |
} | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVariableGet(MaxEquity_str)) | |
{ | |
GlobalVariableSet(MaxEquity_str,(AccountProfit() / Start_Balance)*100); | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Balnace | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(EARUNTIME_str,EARunTime); | |
GlobalVariableSet(Start_P_str,(double)Start_P); | |
GlobalVariableSet(Start_Balance_str,Start_Balance); | |
#endif | |
return(true); | |
} | |
#endif | |
void DeleteObjects() | |
{ | |
for (int i=5;i>=0;i--) | |
{ | |
if(ObjectFind(Label_arr[i]) >= 0) ObjectDelete(0,Label_arr[i]); | |
if(ObjectFind(Label_arr1[i]) >= 0) ObjectDelete(0,Label_arr1[i]); | |
if (i < 2) | |
{ | |
if (ObjectFind(NewsInfo[i]) >= 0) ObjectDelete(0,NewsInfo[i]); | |
if (ObjectFind(Light_str[i]) >= 0) ObjectDelete(0,Light_str[i]); | |
} | |
} | |
if(ObjectFind(Run) >= 0) ObjectDelete(0,Run); | |
if(ObjectFind(FloatLoss) >= 0) ObjectDelete(FloatLoss); | |
if(ObjectFind(Margin) >= 0) ObjectDelete(Margin); | |
if(ObjectFind(Light) >= 0) ObjectDelete(Light); | |
//if (FileIsExist(EventFile)) FileDelete(EventFile); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
if (UninitializeReason() != 3) | |
{ | |
#ifdef MODE_AREA51 | |
if (ArraySize(AvoidTime) <= 1) NewGetSW = true; | |
else | |
{ | |
if (TimeLocal() >= AvoidTime[ArraySize(AvoidTime)-1] + MinAfter) | |
{ | |
NewGetSW = true; | |
} | |
} | |
#endif | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if ( | |
(TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) || | |
NewGetSW | |
) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
if (!NewGetSW) | |
{ | |
Sleep(1000); | |
} | |
Sleep(1000); | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
GlobalVariableSet("DNSW0",0); | |
NewGetSW = false; | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
void AutoRepairSLTP(const bool flag,const int Type,const int Mode) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (!flag) return; | |
int Cnt = MyOrderTotal(Type); | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (Mode == MODE_FIRSTTRADE) | |
{ | |
TP = TakeProfit; | |
SL = StopLoss; | |
if (MartinSW) | |
SL = 0.0; | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,SL,TP); | |
} | |
if (Mode == MODE_MARTIN) | |
{ | |
switch (Type) | |
{ | |
case OP_BUY: | |
TP = Cal_TP(OP_BUY,MinPrice,TakeProfit); | |
SL = 0; | |
break; | |
case OP_SELL: | |
TP = Cal_TP(OP_SELL,MaxPrice,TakeProfit); | |
SL = 0; | |
break; | |
} | |
if (Cnt > 1) | |
{ | |
#ifdef MODE_AREA51 | |
if (SingleOut) | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,0,TakeProfit); | |
else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#endif | |
} | |
} | |
} | |
string FileCharArrayToString(const string str_temp) | |
{ | |
uchar End_arr[]; | |
int j = 0; | |
for(int i=0;i<StringLen(str_temp);i+=2) | |
{ | |
ArrayResize(End_arr,j+1); | |
End_arr[j] = (char)StringToInteger(StringSubstr(str_temp,i,2)); | |
j++; | |
} | |
return(CharArrayToString(End_arr,0,WHOLE_ARRAY,CP_ACP)); | |
} | |
void CheckOSVersion() | |
{ | |
ulong dwVersion = GetVersion(); | |
uint dwMajorVersion = (uchar)(((ushort)(dwVersion & 0xffff)) & 0xff); | |
printf("Major Version Of OS: %d",dwMajorVersion); | |
if (dwMajorVersion < 6) | |
{ | |
#ifdef CHT | |
Alert("請注意!使用Windows XP(Server 2003)之系統將無法保證交易能正常執行!"); | |
#endif | |
#ifdef ENG | |
Alert("Attention! Not guarantee to run well in Windows XP(Server 2003)"); | |
#endif | |
} | |
} | |
void SetKbar(double& Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
#ifdef MODE_AREA51 | |
void BrakeSystemFun() | |
{ | |
int Light_Arrow[2] = {108,161}; | |
if (BrakeSystem) | |
{ | |
int Pos = 50; | |
double Position=20; | |
#ifdef CHT | |
string Active = "煞車系統啟動中"; | |
string Deactive = "煞車系統待命中"; | |
#endif | |
#ifdef ENG | |
string Active = "Braking System Initiating"; | |
string Deactive = "Braking System Stand By"; | |
#endif | |
double Position_L = Bid + Position * point; | |
for (int i=0;i<ArraySize(Light_str);i++) | |
{ | |
if (ObjectFind(Light_str[i]) < 0) | |
ObjectCreate(0,Light_str[i],OBJ_ARROW,0,0,0); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_ARROWCODE,Light_Arrow[i]); | |
if (i == 0) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLime); | |
if (i == 1) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_WIDTH,5); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSetDouble(0,Light_str[i],OBJPROP_PRICE1,Position_L); | |
} | |
int EnergyBall=Ball(Status,SleepStop,Last_Time,Last_Price,SleepTime,(int)TimeCurrent(),point,Energy,Close[0]); | |
if (EnergyBall<0) | |
{ | |
Alert("Energy Brake Error"); | |
TradeSW = false; | |
return; | |
} | |
if (TimeCurrent()<=SleepStop[0]) | |
{ | |
TradeSW = false; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrRed); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrCrimson); | |
#ifdef CHT | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" 分鐘後啟動"); | |
#endif | |
#ifdef ENG | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", initiate after"+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" minutes"); | |
#endif | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrLime); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetString(0,Light,OBJPROP_TEXT,Deactive); | |
} | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectDelete(Light_str[0]); | |
ObjectDelete(Light_str[1]); | |
} | |
} | |
#endif | |
bool Lots_Valid(const double iLot) | |
{ | |
double ValuePerLot = SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE); | |
if ((iLot * ValuePerLot) > AccountInfoDouble(ACCOUNT_FREEMARGIN)) return(false); | |
return(true); | |
} | |
int GetTicket(int &Ticket_arr[]) | |
{ | |
int Cnt = 0; | |
for (int i=0;i<ArraySize(Ticket_arr);i++) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (Ticket_arr[i] == OrderTicket()) | |
{ | |
Cnt++; | |
break; | |
} | |
} | |
} | |
return(Cnt); | |
} | |
int MyOrderTotal_Enders(const string symbol,const int Type,const int ID) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL && OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
continue; | |
} | |
if ((Type == OP_BUY && ((OrderType() & 1) == 1 ? false : true)) || (Type == OP_SELL && ((OrderType() & 1) == 1 ? true : false))) | |
{ | |
if (OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
} | |
} | |
} | |
return(x); | |
} | |
bool EAControl_Enders() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
Comment(str); | |
return(Out); | |
} | |
int EndersGamgeCnt = 0; | |
void EndersGameModule(const string symbol, const int Type, int id, bool &flag) | |
{ | |
if (MyOrderTotal_Enders(symbol,Type,id) == 0) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (OrderCloseTime() > EGStartTime) | |
{ | |
EndersGamgeCnt++; | |
Alert(Symbol() + " 戰場已完成第" + IntegerToString(EndersGamgeCnt) + " 次戰役"); | |
EGStartTime = TimeCurrent(); | |
break; | |
} | |
} | |
} | |
if (EndersGamgeCnt >= OutCnt) | |
{ | |
Alert("已達成設定局數,即將離開 " + Symbol() + " 戰場"); | |
flag = false; | |
ExpertRemove(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| AllNewXBOMB.mq4 | | |
//| APP Wargame-Hydra | | |
//| http://www.facebook.com/groups/moneywargame/ | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import "kernel32.dll" | |
ulong GetVersion(void); | |
#import | |
#define CHT 1 | |
//#define ENG 1 | |
#define ZERO 0.000001 | |
#define MODE_BUY 1 | |
//#define MODE_SELL 1 | |
//#define MODE_DEMO 1 | |
//#define MODE_AREA51 1 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_CHECKTIMEIN 0 | |
#define MODE_TIMESTP 1 | |
#define MODE_TIMESSL 2 | |
#define MODE_DAILYTP 3 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_FIRSTTRADE 99 | |
#define MODE_MARTIN 100 | |
#define OP_ALL 101 | |
#define MODE_SLTP_BYPOINT 102 | |
#define MODE_SLTP_BYPRICE 103 | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int id_1 = 3333333; | |
int ErrorCode = 0; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
int BarTime = 0; | |
double NextTime=0.5; | |
double point = 0.0; | |
double iLots = 0.0; | |
double Start_Balance = 0; | |
double iPending; | |
datetime H1BarTime = 0; | |
datetime FirstTradeBar = 0; | |
datetime Start_P = 0; | |
datetime EARunTime = 0; | |
datetime AvoidTime[1] = {0}; | |
datetime EGStartTime = TimeCurrent(); | |
bool EGFlag = true; | |
bool NewGetSW = false; | |
bool AuthorGetSW = false; | |
bool TradeSW = true; | |
int Last_Time[1]={0}; | |
int Status[1]={0}; | |
double Last_Price[1]={-1.0}; | |
double MinPriceShot = 0.0; | |
string AvoidSymbol[1] = {""}; | |
string LogText[4] = {"小虧為贏" , "次止盈" , "次止損" , "日止盈"}; | |
string Label_arr[6] = | |
{ | |
"TimesProfitPer", | |
"Start_Balance", | |
"TimePast", | |
"Orders", | |
"TodayProfitPer", | |
"MaxLoss" | |
}; | |
string Label_arr1[6] = | |
{ | |
"TimesProfitPer0", | |
"Start_Balance0", | |
"TimePast0", | |
"Orders0", | |
"TodayProfitPer0", | |
"MaxLoss0" | |
}; | |
string Start_P_str = "Start_P"; | |
string Start_Balance_str = "Start_Balance"; | |
string MaxEquity_str = "MaxEquity"; | |
string NextSW_str = "NextSW"; | |
string EARUNTIME_str = "EARUNTIME"; | |
string Light = "LightStatus"; | |
string Run = "RunStatus"; | |
string Margin = "Margin"; | |
string FloatLoss = "Float"; | |
string NewsInfo[2] = {"NewsInfo0", "NewsInfo1"}; | |
string Light_str[2] = {"Light","Light2"}; | |
#ifdef CHT | |
string NewsEngage = "狂派情資已接收完全.....全面開戰!"; | |
#endif | |
#ifdef ENG | |
string NewsEngage = "News retrieval completed .....automated system initiated"; | |
#endif | |
//+------------------------------------------------------------------+ | |
//|Ender's Game Par | | |
//+------------------------------------------------------------------+ | |
datetime ticket_time = 0; | |
int OutCntTemp = 0; | |
#ifdef CHT | |
input bool AutoRepairSW = false; //自動回復停損停利 | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //ESP戰艦穩定煞車系統 | |
int pSeconds=14; | |
input double Energy=4; //能量偵測靈敏度 | |
input int SleepTime=15; //能量引擎啟動時間(分鐘) | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //預警雷達控制模組 | |
input int MinBefore = 30; //前幾分鐘閃避 | |
input int MinAfter = 30; //後幾分鐘閃避 | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //保證金比例控制模組 | |
input double MarginLimit = 2000; //保證金低於%數暫停 | |
input bool FloatEq = false; //浮虧控制模組 | |
input double FloatAm = -4000; //虧損(-金額)暫停 | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //開始時間,格式為HH | |
input int DayStopHour = 00; //結束時間,格式為HH | |
input string WeekDay="12345"; //週一執行,鍵入1;週一週二鍵入12 | |
input bool WeekStartOverSW = true; //一個禮拜頭尾不間斷 | |
input int WeekStartHour = 8; //第一天幾點開始 | |
input int WeekStopHour = 17; //最後一天幾點結束 | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //止盈計算本金基準 | |
input bool TakeProfitPercentSW = true; //次止盈開關 | |
input double TakeProfitPercent = 5; //次止盈,每次最多獲利%,立即平倉歸零重啟新局 | |
input bool TakeProfitPercentDaySW = true; //日止盈開關 | |
input double TakeProfitPercentDay = 1000; //日止盈,每日最多獲利%,立即停止交易,隔日重新啟動 | |
input bool StopLossPercentSW = true; //浮虧止損開關 | |
input double StopLossPercent = 1000; //現時帳戶浮虧止損,止損基準值:帳戶餘額 | |
input bool SleepLoss = true; //止損後當日不再出兵 | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //小虧為贏開關 | |
input int MaxTime = 150; //每局最長時間(分鐘) | |
input int MaxIn = 10; //每局最多兵力 | |
input double MaxP = 0.5; //達成以上條件小虧為贏 | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //掛單價格 | |
input bool SingleOut = false; //單進單出 | |
#endif | |
input bool MartinSW = true; //馬丁開關 | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //第幾張馬丁開始加倍 | |
#endif | |
input double LotMultiplikator = 1.382; //馬丁倍率 | |
input double Step = 30; //馬丁間距 | |
input int MaxTrades = 5; //最多單數 | |
input double MM = 5.0; //資金管理%數 | |
input double Lots = 0.01; //手數 | |
input double TakeProfit = 10.0; //獲利點數 | |
input double StopLoss = 10.0; //停損點數 | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
#ifdef ENG | |
input bool AutoRepairSW = false; //Auto Repair SL&TP | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //BrakeSystem | |
int pSeconds=14; | |
input double Energy=4; //Braking System Sensitivity Control | |
input int SleepTime=15; //Braking System Sleeping Time | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //Braking System Sleeping Time | |
input int MinBefore = 30; //Stop trading few minutes before news alert | |
input int MinAfter = 30; //Stop trading few minutes after news alert | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //Margin Level Control Module | |
input double MarginLimit = 2000; //Margin level % limit to stop trading | |
input bool FloatEq = false; //Floating Loss Control Module | |
input double FloatAm = -4000; //Floating loss (-$amount) limit to stop trading | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //Starting Time, in HH format | |
input int DayStopHour = 00; //Ending Time, in HH format | |
input string WeekDay="12345"; //Star on Monday=1, Monday and Tuesday=12 | |
input bool WeekStartOverSW = true; //Trade everyday without stop | |
input int WeekStartHour = 8; //First Day Staring Time | |
input int WeekStopHour = 17; //Last Day Ending Time | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //Capital setting for Profit/Loss termination | |
input bool TakeProfitPercentSW = true; //Switch of Secondary Take Profit | |
input double TakeProfitPercent = 1; //Secondary Take Profit, max. % of profit to close trade, and reset | |
input bool TakeProfitPercentDaySW = true; //Switch of Daily Take Profit | |
input double TakeProfitPercentDay = 1000; //Daily Take Profit, max. % of profit to close trade, reset on the next day | |
input bool StopLossPercentSW = true; //Switch of stopping Floating Loss | |
input double StopLossPercent = 1000; //Stop Loss of Current Account Max. Floating Loss (Based on Balance) | |
input bool SleepLoss = true; //Stop trading when reaching stop loss the same day | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //Switch of Small Loss To Win | |
input int MaxTime = 150; //Max. Time Per Set (Minutes) | |
input int MaxIn = 10; //Max. Trades Per Set | |
input double MaxP = 0.5; //Criteria of Small Loss for Win | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //Pending Order at Price | |
input bool SingleOut = false; //Take Profit Single Out | |
#endif | |
input bool MartinSW = true; //Start Martingale | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //Number to start martingale multiplication | |
#endif | |
input double LotMultiplikator = 1.382; //Martingale Multiplier | |
input double Step = 30; //Martingale Distance | |
input int MaxTrades = 5; //Max. Number of Orders | |
input double MM = 5.0; //Fund Management % | |
input double Lots = 0.01; //Order Size_lot | |
input double TakeProfit = 10.0; //Take Profit | |
input double StopLoss = 10.0; //Stop Loss | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
string Newsfile = "ffcal_week_this.xml"; | |
string EventFile = "NewsEvent.csv"; | |
string AuthorFile = "58426f6d625f313530333235"; | |
int newverMT4=1; | |
int accountMode=1; | |
string accountID="307383675471044242004002478351777461355442714470680287367714305481330815525625712384844860686545066036787326516602236575060168253182382223384704564023840278267055768862740384331027464044486413846161126644733334767107807263270163337825300231566058026024280"; | |
string EndDate="773007153340217643304083503643662664135084287337732519558536277033134667745660522246521343428004006023880442472680006145250813282575063624287537184878081645870548647881741716731701678285887276610110026947142762324384327364030733857683026770853863520111866"; | |
int OnInit() | |
{ | |
//--- | |
CheckOSVersion(); | |
//+------------------------------------------------------------------+ | |
//|Get Point By Symbol | | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
iLots = OpLots(); | |
ticket_time = TimeCurrent(); | |
//+------------------------------------------------------------------+ | |
//|Random Minute | | |
//+------------------------------------------------------------------+ | |
MathSrand(GetTickCount()); | |
DownloadMin = 1 + ((59 * MathRand()) / 32768); | |
#ifdef MODE_AREA51 | |
TimesStopConfig(); | |
//+------------------------------------------------------------------+ | |
//|Update Files | | |
//+------------------------------------------------------------------+ | |
iPending = Pending; | |
#endif | |
UpdateFiles(MODE_INIT); | |
//--- | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
GlobalVariableSet("DNSW",0); | |
DeleteObjects(); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
#ifdef MODE_BUY | |
if (NonStop==false) EndersGameModule(Symbol(),OP_BUY,id_1,EGFlag); | |
#endif | |
#ifdef MODE_SELL | |
if (NonStop==false) EndersGameModule(Symbol(),OP_SELL,id_1,EGFlag); | |
#endif | |
UpdateFiles(MODE_TICK); | |
if (!EAControl_Enders()) return; | |
#ifdef MODE_AREA51 | |
double TimesProfit = 0.0; | |
double TodayProfit = 0.0; | |
if (!CloseAllTrades()) return; | |
if (!TimesStop(TimesProfit,TodayProfit)) return; | |
Displayprofit(TimesProfit,TodayProfit); | |
CheckTimeIn(TimesProfit); | |
//+------------------------------------------------------------------+ | |
//|Shutdown EA | | |
//+------------------------------------------------------------------+ | |
if (!EAControl()) return; | |
BrakeSystemFun(); | |
#endif | |
#ifdef MODE_BUY | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_BUY,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_BUY); | |
Martin(OP_BUY); | |
} | |
#endif | |
#ifdef MODE_SELL | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_SELL,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_SELL); | |
Martin(OP_SELL); | |
} | |
#endif | |
} | |
//+------------------------------------------------------------------+ | |
//|Determine Martingale TakeProfi | | |
//+------------------------------------------------------------------+ | |
double Cal_TP(int Type,double iPrice,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double AllOrderLots = 0.0; | |
double TP_Goal; | |
double FirstOrderLots = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == Type && OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
FirstOrderLots = OrderLots(); | |
TotalLots += OrderLots(); | |
if (Type == OP_SELL) | |
TotalDisProduct += (iPrice - OrderOpenPrice()) / point * OrderLots(); | |
if (Type == OP_BUY) | |
TotalDisProduct += (OrderOpenPrice() - iPrice) / point * OrderLots(); | |
} | |
} | |
if (EqualZero(TotalLots)) return(0.0); | |
TP_Goal = ((TPPoint * FirstOrderLots) + TotalDisProduct) / TotalLots; | |
if (Type == OP_SELL) | |
return(iPrice - (TP_Goal * point)); | |
if (Type == OP_BUY) | |
return(iPrice + (TP_Goal * point)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|OrderSend Function | | |
//+------------------------------------------------------------------+ | |
int SendOrders(const int Type ,const string iComment) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double InPrice = 0.0; | |
switch (Type) | |
{ | |
case OP_BUY: | |
InPrice = Ask; | |
break; | |
case OP_SELL: | |
InPrice = Bid; | |
break; | |
} | |
double SendOrders_Lots = MartinLots(Type); | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
int Ticket = OrderSend(Symbol(),Type,SendOrders_Lots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,Type == OP_BUY ? clrLime : clrRed); | |
if (Ticket < 0) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(ErrorCode)); | |
} | |
return(Ticket); | |
} | |
void Set_SL_TP(const int Mode,const int Type,double iSL,double iTP) | |
{ | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++)//執行N次篩選(N=訂單總數) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1)//挑選出同貨幣對 && BUY單 && MagicNumber=本程式 的訂單 | |
{ | |
if (Mode == MODE_SLTP_BYPOINT) | |
{ | |
if (Type == OP_BUY || Type == OP_BUYSTOP || Type == OP_BUYLIMIT) | |
{ | |
TP = OrderOpenPrice() + iTP * point; | |
SL = OrderOpenPrice() - iSL * point; | |
} | |
if (Type == OP_SELL || Type == OP_SELLSTOP || Type == OP_SELLLIMIT) | |
{ | |
TP = OrderOpenPrice() - iTP * point; | |
SL = OrderOpenPrice() + iSL * point; | |
} | |
if (EqualZero(iTP)) TP = 0; | |
if (EqualZero(iSL)) SL = 0; | |
}else | |
{ | |
SL = iSL; | |
TP = iTP; | |
} | |
if (iTP < 0) | |
{ | |
TP = OrderTakeProfit(); | |
} | |
if (iSL < 0) | |
{ | |
SL = OrderStopLoss(); | |
} | |
if ((CompareDouble(OrderStopLoss(),NormalizeDouble(SL,Digits))) && (CompareDouble(OrderTakeProfit(),NormalizeDouble(TP,Digits)))) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_SL_TP " + IntegerToString(OrderTicket()) + " Error: " + ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= ZERO) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= ZERO && A >= -ZERO) return(true); | |
return(false); | |
} | |
void Martin(const int Type) | |
{ | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (MartinSW) | |
{ | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (BarTime == Bars && MyOrderTotal(Type) < MaxTrades) | |
{ | |
if (SendOrders(Type,"XBOMB" + IntegerToString(MyOrderTotal(Type) + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_MARTIN); | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_BUY && MyOrderTotal(OP_BUY) > 0) | |
{ | |
if (Ask <= MinPrice - Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_SELL && MyOrderTotal(OP_SELL) > 0) | |
{ | |
if (Bid >= MaxPrice + Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (MyOrderTotal(Type) > 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_MARTIN); | |
} | |
} | |
void FirstTrade(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
if (Type == OP_BUY && (MyOrderTotal(OP_BUYSTOP) > 0 || MyOrderTotal(OP_BUYLIMIT) > 0)) return; | |
if (Type == OP_SELL && (MyOrderTotal(OP_SELLSTOP) > 0 || MyOrderTotal(OP_SELLLIMIT) > 0)) return; | |
if (Cnt == 0 && FirstTradeBar != Time[0]) | |
{ | |
if (SendOrders(Type,"XBOMB(" + IntegerToString(Type) + ")." + IntegerToString(Cnt + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_FIRSTTRADE); | |
FirstTradeBar = Time[0]; | |
} | |
} | |
if (Cnt == 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_FIRSTTRADE); | |
} | |
double GetLastTciketINFO(const int Type,double &MaxPrice,double &MinPrice) | |
{ | |
double iMaxPrice = 0.0; | |
double iMinPrice = 5000.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol() && OrderType() == Type) | |
{ | |
if (OrderOpenPrice() >= iMaxPrice) | |
iMaxPrice = OrderOpenPrice(); | |
if (OrderOpenPrice() <= iMinPrice && !EqualZero(iMinPrice)) | |
iMinPrice = OrderOpenPrice(); | |
} | |
} | |
MaxPrice = iMaxPrice; | |
MinPrice = iMinPrice; | |
return(0.0); | |
} | |
int MyOrderTotal(int Type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL) | |
{ | |
x++; | |
continue; | |
} | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
void iCloseOrders(int type) | |
{ | |
int C0_cnt; | |
if (type == OP_BUYSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUYSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELLSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELLSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrLime)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
}else { | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selecttickets() | |
{ | |
int cnt_tickets = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)) | |
#endif | |
{ | |
ticket_time=OrderOpenTime(); | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selectticketsfrhistory() | |
{ | |
int cnt_tickets = 0; | |
for (int i=0;i<OrdersHistoryTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && ticket_time<=OrderOpenTime()) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && ticket_time<=OrderOpenTime()) | |
#endif | |
{ | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double MartinLots(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
#ifdef MODE_AREA51 | |
int StartM = MartinMStart; | |
if (StartM <= 0) StartM = 2; | |
#else | |
int StartM = 2; | |
#endif | |
if (Cnt <= 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,0),2)); | |
if (Cnt > 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,(double)(Cnt - 1) - (StartM - 2)),2)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|Optimize Lots | | |
//+------------------------------------------------------------------+ | |
double OpLots() | |
{ | |
double Out = Lots; | |
if (!EqualZero(MM) && MM > 0) | |
{ | |
Out = (AccountBalance() * MM ) / (1000 * TakeProfit); | |
} | |
return(Out); | |
} | |
#ifdef MODE_AREA51 | |
void PendingTrade(const int Type,const string iComment) | |
{ | |
double InPrice = Pending; | |
int Ticket = 0; | |
int iType = -1; | |
if (Type == OP_BUY) | |
{ | |
if (InPrice > Ask) | |
iType = OP_BUYSTOP; | |
if (InPrice < Ask) | |
iType = OP_BUYLIMIT; | |
if (MyOrderTotal(OP_BUYSTOP) >= 1 || MyOrderTotal(OP_BUYLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrLime); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (InPrice < Bid) | |
iType = OP_SELLSTOP; | |
if (InPrice > Bid) | |
iType = OP_SELLLIMIT; | |
if (MyOrderTotal(OP_SELLSTOP) >= 1 || MyOrderTotal(OP_SELLLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrRed); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
return; | |
} | |
bool CloseAllTrades() | |
{ | |
if (GlobalVariableGet(NextSW_str) == 1) | |
{ | |
#ifdef MODE_BUY | |
iCloseOrders(OP_BUY); | |
#endif | |
#ifdef MODE_SELL | |
iCloseOrders(OP_SELL); | |
#endif | |
Sleep(10000); | |
if (MyOrderTotal(OP_ALL) > 0) | |
{ | |
#ifdef CHT | |
Alert("水雷未掃除成功"); | |
#endif | |
#ifdef ENG | |
Alert("Submarine mine sweeper not completed"); | |
#endif | |
return(false); | |
}else | |
{ | |
Sleep((int)(NextTime * 1000 * 60)); | |
} | |
} | |
return(true); | |
} | |
void CheckTimeIn(const double TimesProfit) | |
{ | |
datetime RunTime = (TimeCurrent() - Start_P) / 60; | |
if (SmallLossWinSW && RunTime >= MaxTime && OrdersTotal() >= MaxIn && TimesProfit >= MaxP) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_CHECKTIMEIN,""); | |
} | |
} | |
void WriteLog(const int Mode,const string in) | |
{ | |
double TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
string str = WindowExpertName() + "," + Symbol() + "," + TimeToString(TimeCurrent(),TIME_DATE|TIME_DATE|TIME_SECONDS) + | |
": " + LogText[Mode] + " , " + in + | |
"," + Start_Balance_str + ": " + DoubleToString(Start_Balance,2) + | |
"," + Start_P_str + ": " + IntegerToString(Start_P) + | |
",TimeProfit: " + DoubleToString(TimesProfit,2) + | |
"| Set: 餘:" + DoubleToString(iBalance,2) + | |
",次:" + DoubleToString(TakeProfitPercent,2) + | |
",日:" + DoubleToString(TakeProfitPercentDay,2) + | |
",損:" + DoubleToStr(StopLossPercent ,2) + | |
",時:" + DoubleToString(MaxTime,2) + | |
",單:" + DoubleToString(MaxIn,2) + | |
",虧:" + DoubleToString(MaxP,2) + | |
"\n" | |
; | |
int h1 = FileOpen("Logs.txt",FILE_READ|FILE_WRITE|FILE_TXT); | |
if (h1 < 0) Print("Open Log Failed"); | |
FileSeek(h1,0,SEEK_END); | |
int byte = FileWriteString(h1,str,StringLen(str)); | |
if (byte <= 0) Print("Write Log Failed"); | |
FileClose(h1); | |
} | |
double GetProfitPeriod(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType() == OP_BUY || OrderType() == OP_SELL) | |
{ | |
if (today < OrderCloseTime()) | |
profit += OrderProfit() + OrderCommission() + OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
void Displayprofit(const double TimesProfit, const double TodayProfit) | |
{ | |
int Pos = 50; | |
string Label_Text = ""; | |
string Label_Text1 = ""; | |
if (ObjectFind(Run) < 0) ObjectCreate(0,Run,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Run, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Run, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Run, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Run, OBJPROP_YDISTANCE, Pos); | |
ObjectSetInteger(0,Run, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Run, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetString(0,Run, OBJPROP_FONT, "微軟正黑體"); | |
for (int i=0;i<6;i++) | |
{ | |
if(ObjectFind(Label_arr[i]) < 0) ObjectCreate(0,Label_arr[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_XDISTANCE,120); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr[i],OBJPROP_FONT,"微軟正黑體"); | |
if(ObjectFind(Label_arr1[i]) < 0) ObjectCreate(0,Label_arr1[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_FONT,"微軟正黑體"); | |
switch(i) | |
{ | |
#ifdef CHT | |
case 0: | |
Label_Text = "單次擄獲狂派戰俘: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "進場起始兵力: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "進場經過時間: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " 分鐘"; | |
break; | |
case 3: | |
Label_Text = "戰場兵力: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "今日擄獲狂派戰俘累積至: " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "最大暫時被擄穫兵力: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
#ifdef ENG | |
case 0: | |
Label_Text = "Profit/Loss: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "Start Balance: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "Time Elapsed: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " Minutes"; | |
break; | |
case 3: | |
Label_Text = "Orders: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "Profit/Loss (Today): " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "Maximum Floating Loss: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
} | |
ObjectSetString(0,Label_arr[i],OBJPROP_TEXT,Label_Text); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_TEXT,Label_Text1); | |
} | |
for (int i=0;i<ArraySize(NewsInfo);i++) | |
{ | |
if (ObjectFind(NewsInfo[i]) < 0) ObjectCreate(0,NewsInfo[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_YDISTANCE, Pos + 150 + i * 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,NewsInfo[i], OBJPROP_FONT, "微軟正黑體"); | |
} | |
ObjectCreate(0,Margin,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Margin, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Margin, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Margin, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Margin, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Margin, OBJPROP_YDISTANCE, Pos+195); | |
ObjectSetInteger(0,Margin, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,Margin, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
ObjectCreate(0,FloatLoss,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_YDISTANCE, Pos+215); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,FloatLoss, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
if (ObjectFind(Light) < 0) | |
ObjectCreate(0,Light,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Light,OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Light, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Light,OBJPROP_COLOR,clrGold); | |
ObjectSetInteger(0,Light, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Light,OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Light,OBJPROP_YDISTANCE,Pos+235); | |
ObjectSetString(0,Light, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Light,OBJPROP_TEXT,""); | |
} | |
bool EAControl() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!Margin()) | |
{ | |
str = "Margin"; | |
Out = false; | |
} | |
if (!Float()) | |
{ | |
str = "Float"; | |
Out = false; | |
} | |
if (TimeCurrent() <= EARunTime) | |
{ | |
str = "SLSLeep"; | |
Out = false; | |
} | |
if (!WeekStartOver()) | |
{ | |
str = "Time"; | |
Out = false; | |
} | |
if (!AvoidNews()) | |
{ | |
str = "News"; | |
Out = false; | |
} | |
/*if (!Margin() || !Float() || TimeCurrent() <= EARunTime || !WeekStartOver() || !AvoidNews()) | |
{ | |
Comment("EAControl 1"); | |
return(false); | |
}*/ | |
Comment(str); | |
return(Out); | |
} | |
bool iTimeControl() | |
{ | |
bool EA_Valid = false; | |
if (DayStopHour == DayStartHour) EA_Valid = true; | |
if (DayStopHour < DayStartHour) | |
{ | |
if (TimeHour(TimeLocal()) >= DayStartHour || TimeHour(TimeLocal()) < DayStopHour) | |
EA_Valid = true; | |
} | |
if (DayStopHour > DayStartHour) | |
{ | |
if ((TimeHour(TimeLocal()) >= DayStartHour && TimeHour(TimeLocal()) < DayStopHour)) | |
EA_Valid = true; | |
} | |
return(EA_Valid); | |
} | |
bool WeekStartOver() | |
{ | |
bool EA_Valid = true; | |
int Dayweek = TimeDayOfWeek(TimeLocal()); | |
if (WeekStartOverSW) | |
{ | |
if ( | |
Dayweek < StrToInteger(StringSubstr(WeekDay,0,1)) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,0,1)) && TimeHour(TimeLocal()) < WeekStartHour) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) && TimeHour(TimeLocal()) >= WeekStopHour ) || | |
Dayweek > StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) | |
) | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
if (iTimeControl()) | |
{ | |
if (StringFind(WeekDay,DoubleToStr(Dayweek,0),0) >= 0) | |
{ | |
EA_Valid = true; | |
}else | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
EA_Valid = false; | |
} | |
} | |
#ifdef CHT | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"未在設定時區內,現有戰役達成後將離開戰場"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"已進入作戰時區內,全面啟動執行..."); | |
return(true); | |
} | |
#endif | |
#ifdef ENG | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Not in trading time zone, stop trading after current set"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Entered trading time zone, automated system initiated"); | |
return(true); | |
} | |
#endif | |
} | |
int NewsConvert() | |
{ | |
string maindata[][7]; | |
string sBuffer = ""; | |
if (!FileIsExist(Newsfile)) | |
{ | |
return(0); | |
} | |
int h1=FileOpen(Newsfile,FILE_SHARE_READ|FILE_CSV); | |
int i = 0 , j = 0; | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(sBuffer,"<title>") >= 0) | |
{ | |
ArrayResize(maindata,j+1); | |
maindata[j][0] = sBuffer; | |
for (i=1;i<7;i++) | |
{ | |
maindata[j][i] = FileReadString(h1); | |
} | |
j++; | |
} | |
} | |
FileClose(h1); | |
int count = ArrayRange(maindata,0); | |
string sTags[7] = { "<title>", "<country>", "<date><![CDATA[", "<time><![CDATA[", "<impact><![CDATA[", "<forecast><![CDATA[", "<previous><![CDATA[" }; | |
string eTags[7] = { "</title>", "</country>", "]]></date>", "]]></time>", "]]></impact>", "]]></forecast>", "]]></previous>" }; | |
for (i=0;i<count;i++) | |
{ | |
for (j=0;j<7;j++) | |
{ | |
maindata[i][j]=StringSubstr(maindata[i][j],StringFind(maindata[i][j],sTags[j],0)+StringLen(sTags[j]),StringFind(maindata[i][j],eTags[j],0)-StringFind(maindata[i][j],sTags[j],0)-StringLen(sTags[j])); | |
} | |
} | |
int h2 = FileOpen("NewsEvent.csv",FILE_SHARE_READ|FILE_WRITE|FILE_CSV,';'); | |
int GMT = 0; | |
string time_str = ""; | |
string prev_time_str = ""; | |
for (i=0;i<count;i++) | |
{ | |
if (maindata[i][4] == "High") | |
{ | |
GMT = -1 * (TimeGMTOffset()/3600); | |
time_str = TimeToString( | |
StringToTime(MakeDateTime(maindata[i][2],maindata[i][3])) + GMT * 3600, | |
TIME_DATE|TIME_MINUTES|TIME_SECONDS | |
); | |
if (time_str == prev_time_str) continue; | |
FileWrite(h2,maindata[i][1],time_str); | |
prev_time_str = time_str; | |
} | |
} | |
FileClose(h2); | |
return(count); | |
} | |
string MakeDateTime(string strDate, string strTime) | |
{ | |
// Print("Converting Forex Factory Time into Metatrader time..."); //added by MN | |
// Converts forexfactory time & date into yyyy.mm.dd hh:mm | |
int n1stDash = StringFind(strDate, "-"); | |
int n2ndDash = StringFind(strDate, "-", n1stDash+1); | |
string strMonth = StringSubstr(strDate, 0, 2); | |
string strDay = StringSubstr(strDate, 3, 2); | |
string strYear = StringSubstr(strDate, 6, 4); | |
// strYear = "20" + strYear; | |
int nTimeColonPos = StringFind(strTime, ":"); | |
string strHour = StringSubstr(strTime, 0, nTimeColonPos); | |
string strMinute = StringSubstr(strTime, nTimeColonPos+1, 2); | |
string strAM_PM = StringSubstr(strTime, StringLen(strTime)-2); | |
int nHour24 = StrToInteger(strHour); | |
if ((strAM_PM == "pm" || strAM_PM == "PM") && nHour24 != 12) | |
{ | |
nHour24 += 12; | |
} | |
if ((strAM_PM == "am" || strAM_PM == "AM") && nHour24 == 12) | |
{ | |
nHour24 = 0; | |
} | |
string strHourPad = ""; | |
if (nHour24 < 10) | |
strHourPad = "0"; | |
return(StringConcatenate(strYear, ".", strMonth, ".", strDay, " ", strHourPad, nHour24, ":", strMinute)); | |
} | |
void RebuildNewsArray() | |
{ | |
NewsConvert(); | |
string sBuffer = ""; | |
int i = 0; | |
int h1 = FileOpen(EventFile,FILE_SHARE_READ|FILE_CSV); | |
if (h1 < 0) | |
{ | |
ArrayResize(AvoidTime,1); | |
ArrayResize(AvoidSymbol,1); | |
return; | |
} | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(Symbol(),sBuffer) >= 0) | |
{ | |
ArrayResize(AvoidSymbol,i+2); | |
AvoidSymbol[i+1] = sBuffer; | |
ArrayResize(AvoidTime,i+2); | |
sBuffer = FileReadString(h1); | |
AvoidTime[i+1] = StrToTime(sBuffer); | |
i++; | |
} | |
} | |
FileClose(h1); | |
} | |
bool AvoidNews() | |
{ | |
#ifdef CHT | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "預警雷達已偵測到 " + AvoidSymbol[i] + " 戰場於 " + StringSubstr(TimeToStr(AvoidTime[i]),11,5) + " 可能將有大量狂派埋伏"; | |
str[1] = "超級達摩戰艦於 " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5) + " 啟動防禦系統進入一級警戒"; | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "正在接收狂派情資..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "最後狂派活動時間: " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "已無其他狂派活動或情資已過期,即將休兵......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
#ifdef ENG | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "News Alert on " + AvoidSymbol[i] + " at time " + StringSubstr(TimeToStr(AvoidTime[i]),11,5); | |
str[1] = "Red alert! Braking System Standing By at " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5); | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "News retrieving..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "Latest News at : " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "No trading info available......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
} | |
bool Margin() | |
{ | |
if (MarginSleep) | |
{ | |
double Margin_R = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); | |
if (EqualZero(Margin_R)) return(true); | |
if (MarginLimit >= Margin_R) | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,"Margin level lower than " + DoubleToString(MarginLimit,2) + " automated system suspended"); | |
return(false); | |
}else | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool Float() | |
{ | |
if (FloatEq) | |
{ | |
double Profit = AccountProfit(); | |
if (Profit <= FloatAm) | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,"Floating loss greater than "+DoubleToString(FloatAm,2)+" automated system suspended"); | |
return(0); | |
}else | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool TimesStopConfig() | |
{ | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(NextSW_str,0); | |
#ifdef GLOBAL_FILE | |
if (!GlobalFileRead()) | |
{ | |
GlobalVars.G_EARunTime = 0; | |
GlobalVars.G_MaxEquity = (AccountProfit() / AccountBalance()) * 100; | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalFileWrite(); | |
GlobalFileRead(); | |
} | |
Print("EARuntime: " + TimeToString(GlobalVars.G_EARunTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("MaxLossEquity: " + DoubleToString(GlobalVars.G_MaxEquity,2)); | |
Print("Start_P: " + TimeToString(GlobalVars.G_Start_P,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("Start_Balance: " + DoubleToString(GlobalVars.G_Start_Balance,2)); | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
#endif | |
return(true); | |
} | |
bool TimesStop(double &TimesProfit, double &TodayProfit) | |
{ | |
#ifdef GLOBAL_FILE | |
if (OrdersTotal()==0) | |
{ | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Status | | |
//+------------------------------------------------------------------+ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVars.G_Start_Balance; | |
else | |
Start_Balance = iBalance; | |
Start_P = GlobalVars.G_Start_P; | |
EARunTime = GlobalVars.G_EARunTime; | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVars.G_MaxEquity) | |
{ | |
GlobalVars.G_MaxEquity = (AccountProfit() / Start_Balance) * 100; | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Status | | |
//+------------------------------------------------------------------+ | |
GlobalVars.G_EARunTime = EARunTime; | |
GlobalVars.G_Start_Balance = Start_Balance; | |
GlobalVars.G_Start_P = Start_P; | |
GlobalFileWrite(); | |
#else | |
if (OrdersTotal()==0) | |
{ | |
GlobalVariableSet(Start_Balance_str,AccountBalance()); | |
GlobalVariableSet(Start_P_str,TimeCurrent()); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Balance | | |
//+------------------------------------------------------------------+ | |
if (GlobalVariableCheck(Start_Balance_str)) | |
{ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVariableGet(Start_Balance_str); | |
else | |
Start_Balance = iBalance; | |
}else | |
{ | |
Start_Balance = AccountBalance(); | |
} | |
if (GlobalVariableCheck(Start_P_str)) | |
{ | |
Start_P = (datetime)GlobalVariableGet(Start_P_str); | |
}else | |
{ | |
Start_P = TimeCurrent(); | |
} | |
if (GlobalVariableCheck(EARUNTIME_str)) | |
{ | |
EARunTime = (datetime)GlobalVariableGet(EARUNTIME_str); | |
} | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVariableGet(MaxEquity_str)) | |
{ | |
GlobalVariableSet(MaxEquity_str,(AccountProfit() / Start_Balance)*100); | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Balnace | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(EARUNTIME_str,EARunTime); | |
GlobalVariableSet(Start_P_str,(double)Start_P); | |
GlobalVariableSet(Start_Balance_str,Start_Balance); | |
#endif | |
return(true); | |
} | |
#endif | |
void DeleteObjects() | |
{ | |
for (int i=5;i>=0;i--) | |
{ | |
if(ObjectFind(Label_arr[i]) >= 0) ObjectDelete(0,Label_arr[i]); | |
if(ObjectFind(Label_arr1[i]) >= 0) ObjectDelete(0,Label_arr1[i]); | |
if (i < 2) | |
{ | |
if (ObjectFind(NewsInfo[i]) >= 0) ObjectDelete(0,NewsInfo[i]); | |
if (ObjectFind(Light_str[i]) >= 0) ObjectDelete(0,Light_str[i]); | |
} | |
} | |
if(ObjectFind(Run) >= 0) ObjectDelete(0,Run); | |
if(ObjectFind(FloatLoss) >= 0) ObjectDelete(FloatLoss); | |
if(ObjectFind(Margin) >= 0) ObjectDelete(Margin); | |
if(ObjectFind(Light) >= 0) ObjectDelete(Light); | |
//if (FileIsExist(EventFile)) FileDelete(EventFile); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
if (UninitializeReason() != 3) | |
{ | |
#ifdef MODE_AREA51 | |
if (ArraySize(AvoidTime) <= 1) NewGetSW = true; | |
else | |
{ | |
if (TimeLocal() >= AvoidTime[ArraySize(AvoidTime)-1] + MinAfter) | |
{ | |
NewGetSW = true; | |
} | |
} | |
#endif | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if ( | |
(TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) || | |
NewGetSW | |
) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
if (!NewGetSW) | |
{ | |
Sleep(1000); | |
} | |
Sleep(1000); | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
GlobalVariableSet("DNSW0",0); | |
NewGetSW = false; | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
void AutoRepairSLTP(const bool flag,const int Type,const int Mode) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (!flag) return; | |
int Cnt = MyOrderTotal(Type); | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (Mode == MODE_FIRSTTRADE) | |
{ | |
TP = TakeProfit; | |
SL = StopLoss; | |
if (MartinSW) | |
SL = 0.0; | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,SL,TP); | |
} | |
if (Mode == MODE_MARTIN) | |
{ | |
switch (Type) | |
{ | |
case OP_BUY: | |
TP = Cal_TP(OP_BUY,MinPrice,TakeProfit); | |
SL = 0; | |
break; | |
case OP_SELL: | |
TP = Cal_TP(OP_SELL,MaxPrice,TakeProfit); | |
SL = 0; | |
break; | |
} | |
if (Cnt > 1) | |
{ | |
#ifdef MODE_AREA51 | |
if (SingleOut) | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,0,TakeProfit); | |
else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#endif | |
} | |
} | |
} | |
string FileCharArrayToString(const string str_temp) | |
{ | |
uchar End_arr[]; | |
int j = 0; | |
for(int i=0;i<StringLen(str_temp);i+=2) | |
{ | |
ArrayResize(End_arr,j+1); | |
End_arr[j] = (char)StringToInteger(StringSubstr(str_temp,i,2)); | |
j++; | |
} | |
return(CharArrayToString(End_arr,0,WHOLE_ARRAY,CP_ACP)); | |
} | |
void CheckOSVersion() | |
{ | |
ulong dwVersion = GetVersion(); | |
uint dwMajorVersion = (uchar)(((ushort)(dwVersion & 0xffff)) & 0xff); | |
printf("Major Version Of OS: %d",dwMajorVersion); | |
if (dwMajorVersion < 6) | |
{ | |
#ifdef CHT | |
Alert("請注意!使用Windows XP(Server 2003)之系統將無法保證交易能正常執行!"); | |
#endif | |
#ifdef ENG | |
Alert("Attention! Not guarantee to run well in Windows XP(Server 2003)"); | |
#endif | |
} | |
} | |
void SetKbar(double& Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
#ifdef MODE_AREA51 | |
void BrakeSystemFun() | |
{ | |
int Light_Arrow[2] = {108,161}; | |
if (BrakeSystem) | |
{ | |
int Pos = 50; | |
double Position=20; | |
#ifdef CHT | |
string Active = "煞車系統啟動中"; | |
string Deactive = "煞車系統待命中"; | |
#endif | |
#ifdef ENG | |
string Active = "Braking System Initiating"; | |
string Deactive = "Braking System Stand By"; | |
#endif | |
double Position_L = Bid + Position * point; | |
for (int i=0;i<ArraySize(Light_str);i++) | |
{ | |
if (ObjectFind(Light_str[i]) < 0) | |
ObjectCreate(0,Light_str[i],OBJ_ARROW,0,0,0); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_ARROWCODE,Light_Arrow[i]); | |
if (i == 0) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLime); | |
if (i == 1) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_WIDTH,5); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSetDouble(0,Light_str[i],OBJPROP_PRICE1,Position_L); | |
} | |
int EnergyBall=Ball(Status,SleepStop,Last_Time,Last_Price,SleepTime,(int)TimeCurrent(),point,Energy,Close[0]); | |
if (EnergyBall<0) | |
{ | |
Alert("Energy Brake Error"); | |
TradeSW = false; | |
return; | |
} | |
if (TimeCurrent()<=SleepStop[0]) | |
{ | |
TradeSW = false; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrRed); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrCrimson); | |
#ifdef CHT | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" 分鐘後啟動"); | |
#endif | |
#ifdef ENG | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", initiate after"+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" minutes"); | |
#endif | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrLime); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetString(0,Light,OBJPROP_TEXT,Deactive); | |
} | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectDelete(Light_str[0]); | |
ObjectDelete(Light_str[1]); | |
} | |
} | |
#endif | |
bool Lots_Valid(const double iLot) | |
{ | |
double ValuePerLot = SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE); | |
if ((iLot * ValuePerLot) > AccountInfoDouble(ACCOUNT_FREEMARGIN)) return(false); | |
return(true); | |
} | |
int GetTicket(int &Ticket_arr[]) | |
{ | |
int Cnt = 0; | |
for (int i=0;i<ArraySize(Ticket_arr);i++) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (Ticket_arr[i] == OrderTicket()) | |
{ | |
Cnt++; | |
break; | |
} | |
} | |
} | |
return(Cnt); | |
} | |
int MyOrderTotal_Enders(const string symbol,const int Type,const int ID) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL && OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
continue; | |
} | |
if ((Type == OP_BUY && ((OrderType() & 1) == 1 ? false : true)) || (Type == OP_SELL && ((OrderType() & 1) == 1 ? true : false))) | |
{ | |
if (OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
} | |
} | |
} | |
return(x); | |
} | |
bool EAControl_Enders() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
Comment(str); | |
return(Out); | |
} | |
int EndersGamgeCnt = 0; | |
void EndersGameModule(const string symbol, const int Type, int id, bool &flag) | |
{ | |
if (MyOrderTotal_Enders(symbol,Type,id) == 0) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (OrderCloseTime() > EGStartTime) | |
{ | |
EndersGamgeCnt++; | |
Alert(Symbol() + " 戰場已完成第" + IntegerToString(EndersGamgeCnt) + " 次戰役"); | |
EGStartTime = TimeCurrent(); | |
break; | |
} | |
} | |
} | |
if (EndersGamgeCnt >= OutCnt) | |
{ | |
Alert("已達成設定局數,即將離開 " + Symbol() + " 戰場"); | |
flag = false; | |
ExpertRemove(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| AllNewXBOMB.mq4 | | |
//| APP Wargame-Hydra | | |
//| http://www.facebook.com/groups/moneywargame/ | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import "kernel32.dll" | |
ulong GetVersion(void); | |
#import | |
#define CHT 1 | |
//#define ENG 1 | |
#define ZERO 0.000001 | |
#define MODE_BUY 1 | |
//#define MODE_SELL 1 | |
#define MODE_AREA51 1 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_CHECKTIMEIN 0 | |
#define MODE_TIMESTP 1 | |
#define MODE_TIMESSL 2 | |
#define MODE_DAILYTP 3 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_FIRSTTRADE 99 | |
#define MODE_MARTIN 100 | |
#define OP_ALL 101 | |
#define MODE_SLTP_BYPOINT 102 | |
#define MODE_SLTP_BYPRICE 103 | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int id_1 = 3333333; | |
int ErrorCode = 0; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
int BarTime = 0; | |
double NextTime=0.5; | |
double point = 0.0; | |
double iLots = 0.0; | |
double Start_Balance = 0; | |
double iPending; | |
datetime H1BarTime = 0; | |
datetime FirstTradeBar = 0; | |
datetime Start_P = 0; | |
datetime EARunTime = 0; | |
datetime AvoidTime[1] = {0}; | |
datetime EGStartTime = TimeCurrent(); | |
bool EGFlag = true; | |
bool NewGetSW = false; | |
bool AuthorGetSW = false; | |
bool TradeSW = true; | |
int Last_Time[1]={0}; | |
int Status[1]={0}; | |
double Last_Price[1]={-1.0}; | |
double MinPriceShot = 0.0; | |
string AvoidSymbol[1] = {""}; | |
string LogText[4] = {"小虧為贏" , "次止盈" , "次止損" , "日止盈"}; | |
string Label_arr[6] = | |
{ | |
"TimesProfitPer", | |
"Start_Balance", | |
"TimePast", | |
"Orders", | |
"TodayProfitPer", | |
"MaxLoss" | |
}; | |
string Label_arr1[6] = | |
{ | |
"TimesProfitPer0", | |
"Start_Balance0", | |
"TimePast0", | |
"Orders0", | |
"TodayProfitPer0", | |
"MaxLoss0" | |
}; | |
string Start_P_str = "Start_P"; | |
string Start_Balance_str = "Start_Balance"; | |
string MaxEquity_str = "MaxEquity"; | |
string NextSW_str = "NextSW"; | |
string EARUNTIME_str = "EARUNTIME"; | |
string Light = "LightStatus"; | |
string Run = "RunStatus"; | |
string Margin = "Margin"; | |
string FloatLoss = "Float"; | |
string NewsInfo[2] = {"NewsInfo0", "NewsInfo1"}; | |
string Light_str[2] = {"Light","Light2"}; | |
#ifdef CHT | |
string NewsEngage = "狂派情資已接收完全.....全面開戰!"; | |
#endif | |
#ifdef ENG | |
string NewsEngage = "News retrieval completed .....automated system initiated"; | |
#endif | |
//+------------------------------------------------------------------+ | |
//|Ender's Game Par | | |
//+------------------------------------------------------------------+ | |
datetime ticket_time = 0; | |
int OutCntTemp = 0; | |
#ifdef CHT | |
input bool AutoRepairSW = false; //自動回復停損停利 | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //ESP戰艦穩定煞車系統 | |
int pSeconds=14; | |
input double Energy=4; //能量偵測靈敏度 | |
input int SleepTime=15; //能量引擎啟動時間(分鐘) | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //預警雷達控制模組 | |
input int MinBefore = 30; //前幾分鐘閃避 | |
input int MinAfter = 30; //後幾分鐘閃避 | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //保證金比例控制模組 | |
input double MarginLimit = 2000; //保證金低於%數暫停 | |
input bool FloatEq = false; //浮虧控制模組 | |
input double FloatAm = -4000; //虧損(-金額)暫停 | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //開始時間,格式為HH | |
input int DayStopHour = 00; //結束時間,格式為HH | |
input string WeekDay="12345"; //週一執行,鍵入1;週一週二鍵入12 | |
input bool WeekStartOverSW = true; //一個禮拜頭尾不間斷 | |
input int WeekStartHour = 8; //第一天幾點開始 | |
input int WeekStopHour = 17; //最後一天幾點結束 | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //止盈計算本金基準 | |
input bool TakeProfitPercentSW = true; //次止盈開關 | |
input double TakeProfitPercent = 1; //次止盈,每次最多獲利%,立即平倉歸零重啟新局 | |
input bool TakeProfitPercentDaySW = true; //日止盈開關 | |
input double TakeProfitPercentDay = 1000; //日止盈,每日最多獲利%,立即停止交易,隔日重新啟動 | |
input bool StopLossPercentSW = true; //浮虧止損開關 | |
input double StopLossPercent = 1000; //現時帳戶浮虧止損,止損基準值:帳戶餘額 | |
input bool SleepLoss = true; //止損後當日不再出兵 | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //小虧為贏開關 | |
input int MaxTime = 150; //每局最長時間(分鐘) | |
input int MaxIn = 10; //每局最多兵力 | |
input double MaxP = 0.5; //達成以上條件小虧為贏 | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //掛單價格 | |
input bool SingleOut = false; //單進單出 | |
#endif | |
input bool MartinSW = true; //馬丁開關 | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //第幾張馬丁開始加倍 | |
#endif | |
input double LotMultiplikator = 1.382; //馬丁倍率 | |
input double Step = 30; //馬丁間距 | |
input int MaxTrades = 5; //最多單數 | |
input double MM = 0.0; //資金管理%數 | |
input double Lots = 0.01; //手數 | |
input double TakeProfit = 10.0; //獲利點數 | |
input double StopLoss = 10.0; //停損點數 | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
#ifdef ENG | |
input bool AutoRepairSW = false; //Auto Repair SL&TP | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //BrakeSystem | |
int pSeconds=14; | |
input double Energy=4; //Braking System Sensitivity Control | |
input int SleepTime=15; //Braking System Sleeping Time | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //Braking System Sleeping Time | |
input int MinBefore = 30; //Stop trading few minutes before news alert | |
input int MinAfter = 30; //Stop trading few minutes after news alert | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //Margin Level Control Module | |
input double MarginLimit = 2000; //Margin level % limit to stop trading | |
input bool FloatEq = false; //Floating Loss Control Module | |
input double FloatAm = -4000; //Floating loss (-$amount) limit to stop trading | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //Starting Time, in HH format | |
input int DayStopHour = 00; //Ending Time, in HH format | |
input string WeekDay="12345"; //Star on Monday=1, Monday and Tuesday=12 | |
input bool WeekStartOverSW = true; //Trade everyday without stop | |
input int WeekStartHour = 8; //First Day Staring Time | |
input int WeekStopHour = 17; //Last Day Ending Time | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //Capital setting for Profit/Loss termination | |
input bool TakeProfitPercentSW = true; //Switch of Secondary Take Profit | |
input double TakeProfitPercent = 1; //Secondary Take Profit, max. % of profit to close trade, and reset | |
input bool TakeProfitPercentDaySW = true; //Switch of Daily Take Profit | |
input double TakeProfitPercentDay = 1000; //Daily Take Profit, max. % of profit to close trade, reset on the next day | |
input bool StopLossPercentSW = true; //Switch of stopping Floating Loss | |
input double StopLossPercent = 1000; //Stop Loss of Current Account Max. Floating Loss (Based on Balance) | |
input bool SleepLoss = true; //Stop trading when reaching stop loss the same day | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //Switch of Small Loss To Win | |
input int MaxTime = 150; //Max. Time Per Set (Minutes) | |
input int MaxIn = 10; //Max. Trades Per Set | |
input double MaxP = 0.5; //Criteria of Small Loss for Win | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //Pending Order at Price | |
input bool SingleOut = false; //Take Profit Single Out | |
#endif | |
input bool MartinSW = true; //Start Martingale | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //Number to start martingale multiplication | |
#endif | |
input double LotMultiplikator = 1.382; //Martingale Multiplier | |
input double Step = 30; //Martingale Distance | |
input int MaxTrades = 5; //Max. Number of Orders | |
input double MM = 0.0; //Fund Management % | |
input double Lots = 0.01; //Order Size_lot | |
input double TakeProfit = 10.0; //Take Profit | |
input double StopLoss = 10.0; //Stop Loss | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
string Newsfile = "ffcal_week_this.xml"; | |
string EventFile = "NewsEvent.csv"; | |
int OnInit() | |
{ | |
//--- | |
CheckOSVersion(); | |
//+------------------------------------------------------------------+ | |
//|Get Point By Symbol | | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
iLots = OpLots(); | |
ticket_time = TimeCurrent(); | |
//+------------------------------------------------------------------+ | |
//|Random Minute | | |
//+------------------------------------------------------------------+ | |
MathSrand(GetTickCount()); | |
DownloadMin = 1 + ((59 * MathRand()) / 32768); | |
#ifdef MODE_AREA51 | |
TimesStopConfig(); | |
//+------------------------------------------------------------------+ | |
//|Update Files | | |
//+------------------------------------------------------------------+ | |
iPending = Pending; | |
#endif | |
UpdateFiles(MODE_INIT); | |
//--- | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
GlobalVariableSet("DNSW",0); | |
DeleteObjects(); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
UpdateFiles(MODE_TICK); | |
#ifdef MODE_AREA51 | |
double TimesProfit = 0.0; | |
double TodayProfit = 0.0; | |
if (!CloseAllTrades()) return; | |
if (!TimesStop(TimesProfit,TodayProfit)) return; | |
Displayprofit(TimesProfit,TodayProfit); | |
CheckTimeIn(TimesProfit); | |
#endif | |
#ifdef MODE_BUY | |
if (NonStop==false) EndersGameModule(Symbol(),OP_BUY,id_1,EGFlag); | |
#endif | |
#ifdef MODE_SELL | |
if (NonStop==false) EndersGameModule(Symbol(),OP_SELL,id_1,EGFlag); | |
#endif | |
#ifdef MODE_AREA51 | |
//+------------------------------------------------------------------+ | |
//|Shutdown EA | | |
//+------------------------------------------------------------------+ | |
if (!EAControl()) return; | |
BrakeSystemFun(); | |
#endif | |
#ifdef MODE_BUY | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_BUY,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_BUY); | |
Martin(OP_BUY); | |
} | |
#endif | |
#ifdef MODE_SELL | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_SELL,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_SELL); | |
Martin(OP_SELL); | |
} | |
#endif | |
} | |
//+------------------------------------------------------------------+ | |
//|Determine Martingale TakeProfi | | |
//+------------------------------------------------------------------+ | |
double Cal_TP(int Type,double iPrice,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double AllOrderLots = 0.0; | |
double TP_Goal; | |
double FirstOrderLots = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == Type && OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
FirstOrderLots = OrderLots(); | |
TotalLots += OrderLots(); | |
if (Type == OP_SELL) | |
TotalDisProduct += (iPrice - OrderOpenPrice()) / point * OrderLots(); | |
if (Type == OP_BUY) | |
TotalDisProduct += (OrderOpenPrice() - iPrice) / point * OrderLots(); | |
} | |
} | |
if (EqualZero(TotalLots)) return(0.0); | |
TP_Goal = ((TPPoint * FirstOrderLots) + TotalDisProduct) / TotalLots; | |
if (Type == OP_SELL) | |
return(iPrice - (TP_Goal * point)); | |
if (Type == OP_BUY) | |
return(iPrice + (TP_Goal * point)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|OrderSend Function | | |
//+------------------------------------------------------------------+ | |
int SendOrders(const int Type ,const string iComment) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double InPrice = 0.0; | |
switch (Type) | |
{ | |
case OP_BUY: | |
InPrice = Ask; | |
break; | |
case OP_SELL: | |
InPrice = Bid; | |
break; | |
} | |
double SendOrders_Lots = MartinLots(Type); | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
int Ticket = OrderSend(Symbol(),Type,SendOrders_Lots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,Type == OP_BUY ? clrLime : clrRed); | |
if (Ticket < 0) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(ErrorCode)); | |
} | |
return(Ticket); | |
} | |
void Set_SL_TP(const int Mode,const int Type,double iSL,double iTP) | |
{ | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++)//執行N次篩選(N=訂單總數) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1)//挑選出同貨幣對 && BUY單 && MagicNumber=本程式 的訂單 | |
{ | |
if (Mode == MODE_SLTP_BYPOINT) | |
{ | |
if (Type == OP_BUY || Type == OP_BUYSTOP || Type == OP_BUYLIMIT) | |
{ | |
TP = OrderOpenPrice() + iTP * point; | |
SL = OrderOpenPrice() - iSL * point; | |
} | |
if (Type == OP_SELL || Type == OP_SELLSTOP || Type == OP_SELLLIMIT) | |
{ | |
TP = OrderOpenPrice() - iTP * point; | |
SL = OrderOpenPrice() + iSL * point; | |
} | |
if (EqualZero(iTP)) TP = 0; | |
if (EqualZero(iSL)) SL = 0; | |
}else | |
{ | |
SL = iSL; | |
TP = iTP; | |
} | |
if (iTP < 0) | |
{ | |
TP = OrderTakeProfit(); | |
} | |
if (iSL < 0) | |
{ | |
SL = OrderStopLoss(); | |
} | |
if ((CompareDouble(OrderStopLoss(),NormalizeDouble(SL,Digits))) && (CompareDouble(OrderTakeProfit(),NormalizeDouble(TP,Digits)))) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_SL_TP " + IntegerToString(OrderTicket()) + " Error: " + ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= ZERO) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= ZERO && A >= -ZERO) return(true); | |
return(false); | |
} | |
void Martin(const int Type) | |
{ | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (MartinSW) | |
{ | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (BarTime == Bars && MyOrderTotal(Type) < MaxTrades) | |
{ | |
if (SendOrders(Type,"XBOMB" + IntegerToString(MyOrderTotal(Type) + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_MARTIN); | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_BUY && MyOrderTotal(OP_BUY) > 0) | |
{ | |
if (Ask <= MinPrice - Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_SELL && MyOrderTotal(OP_SELL) > 0) | |
{ | |
if (Bid >= MaxPrice + Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (MyOrderTotal(Type) > 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_MARTIN); | |
} | |
} | |
void FirstTrade(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
if (Type == OP_BUY && (MyOrderTotal(OP_BUYSTOP) > 0 || MyOrderTotal(OP_BUYLIMIT) > 0)) return; | |
if (Type == OP_SELL && (MyOrderTotal(OP_SELLSTOP) > 0 || MyOrderTotal(OP_SELLLIMIT) > 0)) return; | |
if (Cnt == 0 && FirstTradeBar != Time[0]) | |
{ | |
if (SendOrders(Type,"XBOMB(" + IntegerToString(Type) + ")." + IntegerToString(Cnt + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_FIRSTTRADE); | |
FirstTradeBar = Time[0]; | |
} | |
} | |
if (Cnt == 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_FIRSTTRADE); | |
} | |
double GetLastTciketINFO(const int Type,double &MaxPrice,double &MinPrice) | |
{ | |
double iMaxPrice = 0.0; | |
double iMinPrice = 5000.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol() && OrderType() == Type) | |
{ | |
if (OrderOpenPrice() >= iMaxPrice) | |
iMaxPrice = OrderOpenPrice(); | |
if (OrderOpenPrice() <= iMinPrice && !EqualZero(iMinPrice)) | |
iMinPrice = OrderOpenPrice(); | |
} | |
} | |
MaxPrice = iMaxPrice; | |
MinPrice = iMinPrice; | |
return(0.0); | |
} | |
int MyOrderTotal(int Type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL) | |
{ | |
x++; | |
continue; | |
} | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
void iCloseOrders(int type) | |
{ | |
int C0_cnt; | |
if (type == OP_BUYSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUYSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELLSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELLSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrLime)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
}else { | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selecttickets() | |
{ | |
int cnt_tickets = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)) | |
#endif | |
{ | |
ticket_time=OrderOpenTime(); | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selectticketsfrhistory() | |
{ | |
int cnt_tickets = 0; | |
for (int i=0;i<OrdersHistoryTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && ticket_time<=OrderOpenTime()) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && ticket_time<=OrderOpenTime()) | |
#endif | |
{ | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double MartinLots(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
#ifdef MODE_AREA51 | |
int StartM = MartinMStart; | |
if (StartM <= 0) StartM = 2; | |
#else | |
int StartM = 2; | |
#endif | |
if (Cnt <= 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,0),2)); | |
if (Cnt > 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,(double)(Cnt - 1) - (StartM - 2)),2)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|Optimize Lots | | |
//+------------------------------------------------------------------+ | |
double OpLots() | |
{ | |
double Out = Lots; | |
if (!EqualZero(MM) && MM > 0) | |
{ | |
Out = (AccountBalance() * MM ) / (1000 * TakeProfit); | |
} | |
return(Out); | |
} | |
#ifdef MODE_AREA51 | |
void PendingTrade(const int Type,const string iComment) | |
{ | |
double InPrice = Pending; | |
int Ticket = 0; | |
int iType = -1; | |
if (Type == OP_BUY) | |
{ | |
if (InPrice > Ask) | |
iType = OP_BUYSTOP; | |
if (InPrice < Ask) | |
iType = OP_BUYLIMIT; | |
if (MyOrderTotal(OP_BUYSTOP) >= 1 || MyOrderTotal(OP_BUYLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrLime); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (InPrice < Bid) | |
iType = OP_SELLSTOP; | |
if (InPrice > Bid) | |
iType = OP_SELLLIMIT; | |
if (MyOrderTotal(OP_SELLSTOP) >= 1 || MyOrderTotal(OP_SELLLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrRed); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
return; | |
} | |
bool CloseAllTrades() | |
{ | |
if (GlobalVariableGet(NextSW_str) == 1) | |
{ | |
#ifdef MODE_BUY | |
iCloseOrders(OP_BUY); | |
#endif | |
#ifdef MODE_SELL | |
iCloseOrders(OP_SELL); | |
#endif | |
Sleep(10000); | |
if (MyOrderTotal(OP_ALL) > 0) | |
{ | |
#ifdef CHT | |
Alert("水雷未掃除成功"); | |
#endif | |
#ifdef ENG | |
Alert("Submarine mine sweeper not completed"); | |
#endif | |
return(false); | |
}else | |
{ | |
Sleep((int)(NextTime * 1000 * 60)); | |
} | |
} | |
return(true); | |
} | |
void CheckTimeIn(const double TimesProfit) | |
{ | |
datetime RunTime = (TimeCurrent() - Start_P) / 60; | |
if (SmallLossWinSW && RunTime >= MaxTime && OrdersTotal() >= MaxIn && TimesProfit >= MaxP) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_CHECKTIMEIN,""); | |
} | |
} | |
void WriteLog(const int Mode,const string in) | |
{ | |
double TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
string str = WindowExpertName() + "," + Symbol() + "," + TimeToString(TimeCurrent(),TIME_DATE|TIME_DATE|TIME_SECONDS) + | |
": " + LogText[Mode] + " , " + in + | |
"," + Start_Balance_str + ": " + DoubleToString(Start_Balance,2) + | |
"," + Start_P_str + ": " + IntegerToString(Start_P) + | |
",TimeProfit: " + DoubleToString(TimesProfit,2) + | |
"| Set: 餘:" + DoubleToString(iBalance,2) + | |
",次:" + DoubleToString(TakeProfitPercent,2) + | |
",日:" + DoubleToString(TakeProfitPercentDay,2) + | |
",損:" + DoubleToStr(StopLossPercent ,2) + | |
",時:" + DoubleToString(MaxTime,2) + | |
",單:" + DoubleToString(MaxIn,2) + | |
",虧:" + DoubleToString(MaxP,2) + | |
"\n" | |
; | |
int h1 = FileOpen("Logs.txt",FILE_READ|FILE_WRITE|FILE_TXT); | |
if (h1 < 0) Print("Open Log Failed"); | |
FileSeek(h1,0,SEEK_END); | |
int byte = FileWriteString(h1,str,StringLen(str)); | |
if (byte <= 0) Print("Write Log Failed"); | |
FileClose(h1); | |
} | |
double GetProfitPeriod(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType() == OP_BUY || OrderType() == OP_SELL) | |
{ | |
if (today < OrderCloseTime()) | |
profit += OrderProfit() + OrderCommission() + OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
void Displayprofit(const double TimesProfit, const double TodayProfit) | |
{ | |
int Pos = 50; | |
string Label_Text = ""; | |
string Label_Text1 = ""; | |
if (ObjectFind(Run) < 0) ObjectCreate(0,Run,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Run, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Run, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Run, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Run, OBJPROP_YDISTANCE, Pos); | |
ObjectSetInteger(0,Run, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Run, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetString(0,Run, OBJPROP_FONT, "微軟正黑體"); | |
for (int i=0;i<6;i++) | |
{ | |
if(ObjectFind(Label_arr[i]) < 0) ObjectCreate(0,Label_arr[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_XDISTANCE,120); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr[i],OBJPROP_FONT,"微軟正黑體"); | |
if(ObjectFind(Label_arr1[i]) < 0) ObjectCreate(0,Label_arr1[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_FONT,"微軟正黑體"); | |
switch(i) | |
{ | |
#ifdef CHT | |
case 0: | |
Label_Text = "單次擄獲狂派戰俘: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "進場起始兵力: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "進場經過時間: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " 分鐘"; | |
break; | |
case 3: | |
Label_Text = "戰場兵力: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "今日擄獲狂派戰俘累積至: " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "最大暫時被擄穫兵力: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
#ifdef ENG | |
case 0: | |
Label_Text = "Profit/Loss: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "Start Balance: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "Time Elapsed: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " Minutes"; | |
break; | |
case 3: | |
Label_Text = "Orders: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "Profit/Loss (Today): " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "Maximum Floating Loss: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
} | |
ObjectSetString(0,Label_arr[i],OBJPROP_TEXT,Label_Text); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_TEXT,Label_Text1); | |
} | |
for (int i=0;i<ArraySize(NewsInfo);i++) | |
{ | |
if (ObjectFind(NewsInfo[i]) < 0) ObjectCreate(0,NewsInfo[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_YDISTANCE, Pos + 150 + i * 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,NewsInfo[i], OBJPROP_FONT, "微軟正黑體"); | |
} | |
ObjectCreate(0,Margin,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Margin, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Margin, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Margin, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Margin, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Margin, OBJPROP_YDISTANCE, Pos+195); | |
ObjectSetInteger(0,Margin, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,Margin, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
ObjectCreate(0,FloatLoss,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_YDISTANCE, Pos+215); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,FloatLoss, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
if (ObjectFind(Light) < 0) | |
ObjectCreate(0,Light,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Light,OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Light, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Light,OBJPROP_COLOR,clrGold); | |
ObjectSetInteger(0,Light, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Light,OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Light,OBJPROP_YDISTANCE,Pos+235); | |
ObjectSetString(0,Light, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Light,OBJPROP_TEXT,""); | |
} | |
bool EAControl() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!Margin()) | |
{ | |
str = "Margin"; | |
Out = false; | |
} | |
if (!Float()) | |
{ | |
str = "Float"; | |
Out = false; | |
} | |
if (TimeCurrent() <= EARunTime) | |
{ | |
str = "SLSLeep"; | |
Out = false; | |
} | |
if (!WeekStartOver()) | |
{ | |
str = "Time"; | |
Out = false; | |
} | |
if (!AvoidNews()) | |
{ | |
str = "News"; | |
Out = false; | |
} | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
/*if (!Margin() || !Float() || TimeCurrent() <= EARunTime || !WeekStartOver() || !AvoidNews()) | |
{ | |
Comment("EAControl 1"); | |
return(false); | |
}*/ | |
Comment(str); | |
return(Out); | |
} | |
bool iTimeControl() | |
{ | |
bool EA_Valid = false; | |
if (DayStopHour == DayStartHour) EA_Valid = true; | |
if (DayStopHour < DayStartHour) | |
{ | |
if (TimeHour(TimeLocal()) >= DayStartHour || TimeHour(TimeLocal()) < DayStopHour) | |
EA_Valid = true; | |
} | |
if (DayStopHour > DayStartHour) | |
{ | |
if ((TimeHour(TimeLocal()) >= DayStartHour && TimeHour(TimeLocal()) < DayStopHour)) | |
EA_Valid = true; | |
} | |
return(EA_Valid); | |
} | |
bool WeekStartOver() | |
{ | |
bool EA_Valid = true; | |
int Dayweek = TimeDayOfWeek(TimeLocal()); | |
if (WeekStartOverSW) | |
{ | |
if ( | |
Dayweek < StrToInteger(StringSubstr(WeekDay,0,1)) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,0,1)) && TimeHour(TimeLocal()) < WeekStartHour) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) && TimeHour(TimeLocal()) >= WeekStopHour ) || | |
Dayweek > StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) | |
) | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
if (iTimeControl()) | |
{ | |
if (StringFind(WeekDay,DoubleToStr(Dayweek,0),0) >= 0) | |
{ | |
EA_Valid = true; | |
}else | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
EA_Valid = false; | |
} | |
} | |
#ifdef CHT | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"未在設定時區內,現有戰役達成後將離開戰場"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"已進入作戰時區內,全面啟動執行..."); | |
return(true); | |
} | |
#endif | |
#ifdef ENG | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Not in trading time zone, stop trading after current set"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Entered trading time zone, automated system initiated"); | |
return(true); | |
} | |
#endif | |
} | |
int NewsConvert() | |
{ | |
string maindata[][7]; | |
string sBuffer = ""; | |
if (!FileIsExist(Newsfile)) | |
{ | |
return(0); | |
} | |
int h1=FileOpen(Newsfile,FILE_SHARE_READ|FILE_CSV); | |
int i = 0 , j = 0; | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(sBuffer,"<title>") >= 0) | |
{ | |
ArrayResize(maindata,j+1); | |
maindata[j][0] = sBuffer; | |
for (i=1;i<7;i++) | |
{ | |
maindata[j][i] = FileReadString(h1); | |
} | |
j++; | |
} | |
} | |
FileClose(h1); | |
int count = ArrayRange(maindata,0); | |
string sTags[7] = { "<title>", "<country>", "<date><![CDATA[", "<time><![CDATA[", "<impact><![CDATA[", "<forecast><![CDATA[", "<previous><![CDATA[" }; | |
string eTags[7] = { "</title>", "</country>", "]]></date>", "]]></time>", "]]></impact>", "]]></forecast>", "]]></previous>" }; | |
for (i=0;i<count;i++) | |
{ | |
for (j=0;j<7;j++) | |
{ | |
maindata[i][j]=StringSubstr(maindata[i][j],StringFind(maindata[i][j],sTags[j],0)+StringLen(sTags[j]),StringFind(maindata[i][j],eTags[j],0)-StringFind(maindata[i][j],sTags[j],0)-StringLen(sTags[j])); | |
} | |
} | |
int h2 = FileOpen("NewsEvent.csv",FILE_SHARE_READ|FILE_WRITE|FILE_CSV,';'); | |
int GMT = 0; | |
string time_str = ""; | |
string prev_time_str = ""; | |
for (i=0;i<count;i++) | |
{ | |
if (maindata[i][4] == "High") | |
{ | |
GMT = -1 * (TimeGMTOffset()/3600); | |
time_str = TimeToString( | |
StringToTime(MakeDateTime(maindata[i][2],maindata[i][3])) + GMT * 3600, | |
TIME_DATE|TIME_MINUTES|TIME_SECONDS | |
); | |
if (time_str == prev_time_str) continue; | |
FileWrite(h2,maindata[i][1],time_str); | |
prev_time_str = time_str; | |
} | |
} | |
FileClose(h2); | |
return(count); | |
} | |
string MakeDateTime(string strDate, string strTime) | |
{ | |
// Print("Converting Forex Factory Time into Metatrader time..."); //added by MN | |
// Converts forexfactory time & date into yyyy.mm.dd hh:mm | |
int n1stDash = StringFind(strDate, "-"); | |
int n2ndDash = StringFind(strDate, "-", n1stDash+1); | |
string strMonth = StringSubstr(strDate, 0, 2); | |
string strDay = StringSubstr(strDate, 3, 2); | |
string strYear = StringSubstr(strDate, 6, 4); | |
// strYear = "20" + strYear; | |
int nTimeColonPos = StringFind(strTime, ":"); | |
string strHour = StringSubstr(strTime, 0, nTimeColonPos); | |
string strMinute = StringSubstr(strTime, nTimeColonPos+1, 2); | |
string strAM_PM = StringSubstr(strTime, StringLen(strTime)-2); | |
int nHour24 = StrToInteger(strHour); | |
if ((strAM_PM == "pm" || strAM_PM == "PM") && nHour24 != 12) | |
{ | |
nHour24 += 12; | |
} | |
if ((strAM_PM == "am" || strAM_PM == "AM") && nHour24 == 12) | |
{ | |
nHour24 = 0; | |
} | |
string strHourPad = ""; | |
if (nHour24 < 10) | |
strHourPad = "0"; | |
return(StringConcatenate(strYear, ".", strMonth, ".", strDay, " ", strHourPad, nHour24, ":", strMinute)); | |
} | |
void RebuildNewsArray() | |
{ | |
NewsConvert(); | |
string sBuffer = ""; | |
int i = 0; | |
int h1 = FileOpen(EventFile,FILE_SHARE_READ|FILE_CSV); | |
if (h1 < 0) | |
{ | |
ArrayResize(AvoidTime,1); | |
ArrayResize(AvoidSymbol,1); | |
return; | |
} | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(Symbol(),sBuffer) >= 0) | |
{ | |
ArrayResize(AvoidSymbol,i+2); | |
AvoidSymbol[i+1] = sBuffer; | |
ArrayResize(AvoidTime,i+2); | |
sBuffer = FileReadString(h1); | |
AvoidTime[i+1] = StrToTime(sBuffer); | |
i++; | |
} | |
} | |
FileClose(h1); | |
} | |
bool AvoidNews() | |
{ | |
#ifdef CHT | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "預警雷達已偵測到 " + AvoidSymbol[i] + " 戰場於 " + StringSubstr(TimeToStr(AvoidTime[i]),11,5) + " 可能將有大量狂派埋伏"; | |
str[1] = "超級達摩戰艦於 " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5) + " 啟動防禦系統進入一級警戒"; | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "正在接收狂派情資..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "最後狂派活動時間: " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "已無其他狂派活動或情資已過期,即將休兵......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
#ifdef ENG | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "News Alert on " + AvoidSymbol[i] + " at time " + StringSubstr(TimeToStr(AvoidTime[i]),11,5); | |
str[1] = "Red alert! Braking System Standing By at " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5); | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "News retrieving..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "Latest News at : " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "No trading info available......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
} | |
bool Margin() | |
{ | |
if (MarginSleep) | |
{ | |
double Margin_R = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); | |
if (EqualZero(Margin_R)) return(true); | |
if (MarginLimit >= Margin_R) | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,"Margin level lower than " + DoubleToString(MarginLimit,2) + " automated system suspended"); | |
return(false); | |
}else | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool Float() | |
{ | |
if (FloatEq) | |
{ | |
double Profit = AccountProfit(); | |
if (Profit <= FloatAm) | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,"Floating loss greater than "+DoubleToString(FloatAm,2)+" automated system suspended"); | |
return(0); | |
}else | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool TimesStopConfig() | |
{ | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(NextSW_str,0); | |
#ifdef GLOBAL_FILE | |
if (!GlobalFileRead()) | |
{ | |
GlobalVars.G_EARunTime = 0; | |
GlobalVars.G_MaxEquity = (AccountProfit() / AccountBalance()) * 100; | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalFileWrite(); | |
GlobalFileRead(); | |
} | |
Print("EARuntime: " + TimeToString(GlobalVars.G_EARunTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("MaxLossEquity: " + DoubleToString(GlobalVars.G_MaxEquity,2)); | |
Print("Start_P: " + TimeToString(GlobalVars.G_Start_P,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("Start_Balance: " + DoubleToString(GlobalVars.G_Start_Balance,2)); | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
#endif | |
return(true); | |
} | |
bool TimesStop(double &TimesProfit, double &TodayProfit) | |
{ | |
#ifdef GLOBAL_FILE | |
if (OrdersTotal()==0) | |
{ | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Status | | |
//+------------------------------------------------------------------+ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVars.G_Start_Balance; | |
else | |
Start_Balance = iBalance; | |
Start_P = GlobalVars.G_Start_P; | |
EARunTime = GlobalVars.G_EARunTime; | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVars.G_MaxEquity) | |
{ | |
GlobalVars.G_MaxEquity = (AccountProfit() / Start_Balance) * 100; | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Status | | |
//+------------------------------------------------------------------+ | |
GlobalVars.G_EARunTime = EARunTime; | |
GlobalVars.G_Start_Balance = Start_Balance; | |
GlobalVars.G_Start_P = Start_P; | |
GlobalFileWrite(); | |
#else | |
if (OrdersTotal()==0) | |
{ | |
GlobalVariableSet(Start_Balance_str,AccountBalance()); | |
GlobalVariableSet(Start_P_str,TimeCurrent()); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Balance | | |
//+------------------------------------------------------------------+ | |
if (GlobalVariableCheck(Start_Balance_str)) | |
{ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVariableGet(Start_Balance_str); | |
else | |
Start_Balance = iBalance; | |
}else | |
{ | |
Start_Balance = AccountBalance(); | |
} | |
if (GlobalVariableCheck(Start_P_str)) | |
{ | |
Start_P = (datetime)GlobalVariableGet(Start_P_str); | |
}else | |
{ | |
Start_P = TimeCurrent(); | |
} | |
if (GlobalVariableCheck(EARUNTIME_str)) | |
{ | |
EARunTime = (datetime)GlobalVariableGet(EARUNTIME_str); | |
} | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVariableGet(MaxEquity_str)) | |
{ | |
GlobalVariableSet(MaxEquity_str,(AccountProfit() / Start_Balance)*100); | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Balnace | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(EARUNTIME_str,EARunTime); | |
GlobalVariableSet(Start_P_str,(double)Start_P); | |
GlobalVariableSet(Start_Balance_str,Start_Balance); | |
#endif | |
return(true); | |
} | |
#endif | |
void DeleteObjects() | |
{ | |
for (int i=5;i>=0;i--) | |
{ | |
if(ObjectFind(Label_arr[i]) >= 0) ObjectDelete(0,Label_arr[i]); | |
if(ObjectFind(Label_arr1[i]) >= 0) ObjectDelete(0,Label_arr1[i]); | |
if (i < 2) | |
{ | |
if (ObjectFind(NewsInfo[i]) >= 0) ObjectDelete(0,NewsInfo[i]); | |
if (ObjectFind(Light_str[i]) >= 0) ObjectDelete(0,Light_str[i]); | |
} | |
} | |
if(ObjectFind(Run) >= 0) ObjectDelete(0,Run); | |
if(ObjectFind(FloatLoss) >= 0) ObjectDelete(FloatLoss); | |
if(ObjectFind(Margin) >= 0) ObjectDelete(Margin); | |
if(ObjectFind(Light) >= 0) ObjectDelete(Light); | |
//if (FileIsExist(EventFile)) FileDelete(EventFile); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
if (UninitializeReason() != 3) | |
{ | |
#ifdef MODE_AREA51 | |
if (ArraySize(AvoidTime) <= 1) NewGetSW = true; | |
else | |
{ | |
if (TimeLocal() >= AvoidTime[ArraySize(AvoidTime)-1] + MinAfter) | |
{ | |
NewGetSW = true; | |
} | |
} | |
#endif | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if ( | |
(TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) || | |
NewGetSW | |
) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
if (!NewGetSW) | |
{ | |
Sleep(1000); | |
} | |
Sleep(1000); | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
GlobalVariableSet("DNSW0",0); | |
NewGetSW = false; | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
void AutoRepairSLTP(const bool flag,const int Type,const int Mode) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (!flag) return; | |
int Cnt = MyOrderTotal(Type); | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (Mode == MODE_FIRSTTRADE) | |
{ | |
TP = TakeProfit; | |
SL = StopLoss; | |
if (MartinSW) | |
SL = 0.0; | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,SL,TP); | |
} | |
if (Mode == MODE_MARTIN) | |
{ | |
switch (Type) | |
{ | |
case OP_BUY: | |
TP = Cal_TP(OP_BUY,MinPrice,TakeProfit); | |
SL = 0; | |
break; | |
case OP_SELL: | |
TP = Cal_TP(OP_SELL,MaxPrice,TakeProfit); | |
SL = 0; | |
break; | |
} | |
if (Cnt > 1) | |
{ | |
#ifdef MODE_AREA51 | |
if (SingleOut) | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,0,TakeProfit); | |
else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#endif | |
} | |
} | |
} | |
string FileCharArrayToString(const string str_temp) | |
{ | |
uchar End_arr[]; | |
int j = 0; | |
for(int i=0;i<StringLen(str_temp);i+=2) | |
{ | |
ArrayResize(End_arr,j+1); | |
End_arr[j] = (char)StringToInteger(StringSubstr(str_temp,i,2)); | |
j++; | |
} | |
return(CharArrayToString(End_arr,0,WHOLE_ARRAY,CP_ACP)); | |
} | |
void CheckOSVersion() | |
{ | |
ulong dwVersion = GetVersion(); | |
uint dwMajorVersion = (uchar)(((ushort)(dwVersion & 0xffff)) & 0xff); | |
printf("Major Version Of OS: %d",dwMajorVersion); | |
if (dwMajorVersion < 6) | |
{ | |
#ifdef CHT | |
Alert("請注意!使用Windows XP(Server 2003)之系統將無法保證交易能正常執行!"); | |
#endif | |
#ifdef ENG | |
Alert("Attention! Not guarantee to run well in Windows XP(Server 2003)"); | |
#endif | |
} | |
} | |
void SetKbar(double& Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
#ifdef MODE_AREA51 | |
void BrakeSystemFun() | |
{ | |
int Light_Arrow[2] = {108,161}; | |
if (BrakeSystem) | |
{ | |
int Pos = 50; | |
double Position=20; | |
#ifdef CHT | |
string Active = "煞車系統啟動中"; | |
string Deactive = "煞車系統待命中"; | |
#endif | |
#ifdef ENG | |
string Active = "Braking System Initiating"; | |
string Deactive = "Braking System Stand By"; | |
#endif | |
double Position_L = Bid + Position * point; | |
for (int i=0;i<ArraySize(Light_str);i++) | |
{ | |
if (ObjectFind(Light_str[i]) < 0) | |
ObjectCreate(0,Light_str[i],OBJ_ARROW,0,0,0); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_ARROWCODE,Light_Arrow[i]); | |
if (i == 0) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLime); | |
if (i == 1) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_WIDTH,5); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSetDouble(0,Light_str[i],OBJPROP_PRICE1,Position_L); | |
} | |
int EnergyBall=Ball(Status,SleepStop,Last_Time,Last_Price,SleepTime,(int)TimeCurrent(),point,Energy,Close[0]); | |
if (EnergyBall<0) | |
{ | |
Alert("Energy Brake Error"); | |
TradeSW = false; | |
return; | |
} | |
if (TimeCurrent()<=SleepStop[0]) | |
{ | |
TradeSW = false; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrRed); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrCrimson); | |
#ifdef CHT | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" 分鐘後啟動"); | |
#endif | |
#ifdef ENG | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", initiate after"+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" minutes"); | |
#endif | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrLime); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetString(0,Light,OBJPROP_TEXT,Deactive); | |
} | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectDelete(Light_str[0]); | |
ObjectDelete(Light_str[1]); | |
} | |
} | |
#endif | |
bool Lots_Valid(const double iLot) | |
{ | |
double ValuePerLot = SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE); | |
if ((iLot * ValuePerLot) > AccountInfoDouble(ACCOUNT_FREEMARGIN)) return(false); | |
return(true); | |
} | |
int GetTicket(int &Ticket_arr[]) | |
{ | |
int Cnt = 0; | |
for (int i=0;i<ArraySize(Ticket_arr);i++) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (Ticket_arr[i] == OrderTicket()) | |
{ | |
Cnt++; | |
break; | |
} | |
} | |
} | |
return(Cnt); | |
} | |
int MyOrderTotal_Enders(const string symbol,const int Type,const int ID) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL && OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
continue; | |
} | |
if ((Type == OP_BUY && ((OrderType() & 1) == 1 ? false : true)) || (Type == OP_SELL && ((OrderType() & 1) == 1 ? true : false))) | |
{ | |
if (OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
} | |
} | |
} | |
return(x); | |
} | |
bool EAControl_Enders() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
Comment(str); | |
return(Out); | |
} | |
int EndersGamgeCnt = 0; | |
void EndersGameModule(const string symbol, const int Type, int id, bool &flag) | |
{ | |
if (MyOrderTotal_Enders(symbol,Type,id) == 0) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (OrderCloseTime() > EGStartTime) | |
{ | |
EndersGamgeCnt++; | |
Alert(Symbol() + " 戰場已完成第" + IntegerToString(EndersGamgeCnt) + " 次戰役"); | |
EGStartTime = TimeCurrent(); | |
break; | |
} | |
} | |
} | |
if (EndersGamgeCnt >= OutCnt) | |
{ | |
Alert("已達成設定局數,即將離開 " + Symbol() + " 戰場"); | |
flag = false; | |
ExpertRemove(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| ProjectName | | |
//| Copyright 2012, CompanyName | | |
//| http://www.companyname.net | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import | |
//#define ENG 1 | |
#define CHT 1 | |
#define MODE_BUY 1 | |
//#define MODE_SELL 1 | |
#define TOLERANCE 0.000001 | |
#define OP_ALL 101 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_CHECKTIMEIN 0 | |
#define MODE_TIMESTP 1 | |
#define MODE_TIMESSL 2 | |
#define MODE_DAILYTP 3 | |
#ifdef CHT | |
string t1=""; | |
input bool AutoRepairSLTP = false; //自動回復停損停利 | |
input double Pending = 0.0; //掛單價格 | |
string T21="-------------------------------------------"; | |
input double MM = 1; //資金管理%數 | |
input double Lots = 0.01; //手數 | |
input double TakeProfit = 10.0; //獲利點數 | |
input double StopLoss = 10.0; //停損點數 | |
input string T0="-------------ESP戰艦穩定煞車系統------------"; | |
input bool BrakeSystem=true; | |
int pSeconds=14; | |
input double Energy=4; //能量偵測靈敏度 | |
input int SleepTime=15; //能量引擎啟動時間(分鐘) | |
int SleepStop[1]={0}; | |
input string T00="-------------------------------------------"; | |
input double iBalance = 0; //止盈計算本金基準 | |
input bool TakeProfitPercentSW = true; //次止盈開關 | |
input double TakeProfitPercent = 1; //次止盈,每次最多獲利%,立即平倉歸零重啟新局 | |
input bool TakeProfitPercentDaySW = true; //日止盈開關 | |
input double TakeProfitPercentDay = 1000; //日止盈,每日最多獲利%,立即停止交易,隔日重新啟動 | |
input bool StopLossPercentSW = true; //浮虧止損開關 | |
input double StopLossPercent = 1000; //現時帳戶浮虧止損,止損基準值:帳戶餘額 | |
input bool SleepLoss = true; //止損後當日不再出兵 | |
double NextTime=0.5; | |
input string T9_="-------------------小虧為贏模組-------------"; | |
input bool SmallLossWinSW = true; //小虧為贏開關 | |
input int MaxTime=150; //每局最長時間(分鐘) | |
input int MaxIn=20; //每局最多兵力 | |
input double MaxP=0.5; //達成以上條件小虧為贏 | |
input string T20="-------------------------------------------"; | |
input bool SingleOut=false; //單獨出場 | |
input double i_Multiple=1.382; //馬丁倍率 | |
input int i_MaxMartin=5; //馬丁張數 | |
input string K="馬丁距離"; | |
input double i_MartinStep_0=30; | |
input double i_MartinStep_1=40; | |
input double i_MartinStep_2=50; | |
input double i_MartinStep_3=60; | |
input double i_MartinStep_4=60; | |
input double i_MartinStep_5=60; | |
input double i_MartinStep_6=60; | |
input double i_MartinStep_7=60; | |
input double i_MartinStep_8=60; | |
input double i_MartinStep_9=60; | |
input bool MartinCalLots=true; //馬丁計算獲利手數開關,T:第一張手數 | F:總手數 | |
input double i_TPPoint=10; //馬丁計算獲利點數 | |
input int MOrders=2; //馬丁第幾張開始調整手數 | |
input bool MartinSW = true; //逆勢攤平馬丁 | |
input int M_Period = 60; //攤平馬丁時區 | |
input string Q0="-------------------------------------------"; | |
input int AutoSWAntiMartin=0; //不出場自動控制 | |
input int AntiMartinTrades=2; //不出場單數 | |
input double AntiMartinMultiple=0.5; //反馬丁倍率 | |
input double AntiMartinStep=20; //反馬丁間距 | |
input double Ai_TPPoint=0; //反馬丁獲利點設定為第一張的獲利點數 | |
input string Q5="-------------------------------------------"; | |
input bool MartinAntiMSW=true; //Counter Strike模式 | |
input double CSMultiple=0.5; //Counter Strike倍率 | |
input double CSStep=20; //Counter Strike間距 | |
input double CSTPPoint=0; //Counter Strike獲利點數 | |
input bool CSProtect = true; //Counter Strike保護裝置 | |
input string Q11="-------------------------------------------"; | |
bool NoMartininAttck=false; //執行Attack模式後,反向不下單" | |
input bool Non_Stop=false; //Enders Game | |
double Position=20; | |
input int OutCnt=1; | |
#endif | |
#ifdef ENG | |
string t1=""; | |
input bool AutoRepairSLTP = false; //Auto Repair SL&TP | |
input double Pending = 0.0; //Pending Order at Price | |
string T21="-------------------------------------------"; | |
input double MM = 1; //Fund Management % | |
input double Lots = 0.01; //Order Size_lot | |
input double TakeProfit = 10.0; //Take Profit | |
input double StopLoss = 10.0; //Stop Loss | |
input string T0="-------------BrakeSystem------------"; | |
input bool BrakeSystem=true; | |
int pSeconds=14; | |
input double Energy=4; //Braking System Sensitivity Control | |
input int SleepTime=15; //Braking System Sleeping Time | |
int SleepStop[1]={0}; | |
input string T00="-------------------------------------------"; | |
input double iBalance = 0; //Capital setting for Profit/Loss termination | |
input bool TakeProfitPercentSW = true; //Switch of Secondary Take Profit | |
input double TakeProfitPercent = 1; //Secondary Take Profit, max. % of profit to close trade, and reset | |
input bool TakeProfitPercentDaySW = true; //Switch of Daily Take Profit | |
input double TakeProfitPercentDay = 1000; //Daily Take Profit, max. % of profit to close trade, reset on the next day | |
input bool StopLossPercentSW = true; //Switch of stopping Floating Loss | |
input double StopLossPercent = 1000; // | |
input bool SleepLoss = true; //Stop trading when reaching stop loss the same day | |
double NextTime=0.5; | |
input string T9_="-------------------Small Loss for Win-------------"; | |
input bool SmallLossWinSW = true; //Switch of Small Loss To Win | |
input int MaxTime=150; //Max. Time Per Set (Minutes) | |
input int MaxIn=20; //Max. Trades Per Set | |
input double MaxP=0.5; //Criteria of Small Loss for Win | |
input string T20="-------------------------------------------"; | |
input bool SingleOut=false; //Take Profit Single Out | |
input double i_Multiple=1.382; //Martingale Multiplier | |
input int i_MaxMartin=5; //Martingale Order No. | |
input string K="Martingale Distance"; | |
input double i_MartinStep_0=30; | |
input double i_MartinStep_1=40; | |
input double i_MartinStep_2=50; | |
input double i_MartinStep_3=60; | |
input double i_MartinStep_4=60; | |
input double i_MartinStep_5=60; | |
input double i_MartinStep_6=60; | |
input double i_MartinStep_7=60; | |
input double i_MartinStep_8=60; | |
input double i_MartinStep_9=60; | |
input bool MartinCalLots=true; //Martingale Lots Profit Calculation. T: First Order Lot | F: Total Order Lot | |
input double i_TPPoint=10; //Martin Profit Calculation in Pips | |
input int MOrders=2; //Number to start martingale multiplication | |
input bool MartinSW = true; //Against Trend Martin | |
input int M_Period = 60; //Martin Order Time Frame | |
input string Q0="-------------------------------------------"; | |
input int AutoSWAntiMartin=0; //Mode Selector | |
input int AntiMartinTrades=2; //Retaining Order No. | |
input double AntiMartinMultiple=0.5; //Anti-Martingale Multiplier | |
input double AntiMartinStep=20; //Anti-Martingale Distance | |
input double Ai_TPPoint=0; //First Anti-Martingale order take profit | |
input string Q5="-------------------------------------------"; | |
input bool MartinAntiMSW=true; //Counter Strike Mode | |
input double CSMultiple=0.5; //Counter Strike Multiplier | |
input double CSStep=20; //Counter Strike Distance | |
input double CSTPPoint=0; //Counter Strike Take Profit | |
input bool CSProtect = true; //Counter Strike Protection System | |
input string Q11="-------------------------------------------"; | |
bool NoMartininAttck=false; //Stop placing opposite trend order after Attack Mode | |
input bool Non_Stop=false; //Enders Game | |
double Position=20; | |
input int OutCnt=1; | |
#endif | |
datetime ticket_time=0; | |
int Last_Time[1]={0}; | |
int OutCntTemp=0; | |
int MartinBuyBar,MartinSellBar; | |
int id_1=3333333; | |
int Status[1]={0}; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
int ErrorCode = 0; | |
bool NewGetSW = false; | |
datetime H1BarTime = 0; | |
datetime Start_P = 0; | |
datetime EARunTime = 0; | |
datetime EGStartTime = TimeCurrent(); | |
bool EGFlag = true; | |
double point; | |
double Last_Price[1]={-1.0}; | |
double FontDPI=96; | |
double MinPriceShot; | |
double iPending = 0.0; | |
double Start_Balance = 0; | |
double iLots = 0.0; | |
string Start_P_str = "Start_P"; | |
string Start_Balance_str = "Start_Balance"; | |
string MaxEquity_str = "MaxEquity"; | |
string NextSW_str = "NextSW"; | |
string EARUNTIME_str = "EARUNTIME"; | |
string B_Status,S_Status; | |
string Run = "RunStatus"; | |
string LogText[4] = {"小虧為贏" , "次止盈" , "次止損" , "日止盈"}; | |
string Label_arr[6] = | |
{ | |
"TimesProfitPer", | |
"Start_Balance", | |
"TimePast", | |
"Orders", | |
"TodayProfitPer", | |
"MaxLoss" | |
}; | |
string Label_arr1[6] = | |
{ | |
"TimesProfitPer0", | |
"Start_Balance0", | |
"TimePast0", | |
"Orders0", | |
"TodayProfitPer0", | |
"MaxLoss0" | |
}; | |
bool NewsgetSW=false; | |
bool GetAuthorSW=true; | |
bool FirstNotOut=false; | |
bool FirstIn = true; | |
bool MartinIn = true; | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void OnInit() | |
{ | |
int dc=GetDC(0); | |
FontDPI=GetDeviceCaps(dc,88); | |
if (FontDPI==0) | |
FontDPI=96; | |
ReleaseDC(0,dc); | |
//+------------------------------------------------------------------+ | |
iLots = OpLots(); | |
ticket_time=TimeCurrent(); | |
MinPriceShot=Energy; | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
TimesStopConfig(); | |
UpdateFiles(MODE_INIT); | |
iPending = Pending; | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
ObjectDelete("Lable"); | |
ObjectDelete("Lable1"); | |
ObjectDelete("percent"); | |
ObjectDelete("start_p"); | |
ObjectDelete("takepercent_day"); | |
ObjectDelete("percent0"); | |
ObjectDelete("start_p0"); | |
ObjectDelete("takepercent_day0"); | |
ObjectDelete("percent.1"); | |
ObjectDelete("start_p.1"); | |
ObjectDelete("takepercent_day.1"); | |
ObjectDelete("percent0.1"); | |
ObjectDelete("start_p0.1"); | |
ObjectDelete("takepercent_day0.1"); | |
ObjectDelete("run"); | |
ObjectDelete("Newsinfo"); | |
ObjectDelete("Newsinfo2"); | |
ObjectDelete("ERR"); | |
ObjectDelete("ERR2"); | |
ObjectDelete("Expert"); | |
ObjectDelete("Light"); | |
ObjectDelete("Light2"); | |
ObjectDelete("lightstatus"); | |
ObjectDelete("Margin"); | |
ObjectDelete("Float"); | |
ObjectDelete("LOGO"); | |
ObjectDelete("LOGO0"); | |
ObjectDelete("NAME"); | |
ObjectDelete("MSG0"); | |
ObjectDelete("MSG0.0"); | |
ObjectDelete("MSG1"); | |
ObjectDelete("MSG1.0"); | |
ObjectDelete("MSG2"); | |
ObjectDelete("MSG2.0"); | |
ObjectDelete("MSG3"); | |
GlobalVariableSet("DNSW",0); | |
DeleteObjects(); | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
string tComment="ERTYUUI"; | |
string tMComment="Martin"; | |
UpdateFiles(MODE_TICK); | |
double TimesProfit = 0.0; | |
double TodayProfit = 0.0; | |
if (!CloseAllTrades()) return; | |
if (!TimesStop(TimesProfit,TodayProfit)) return; | |
Displayprofit(TimesProfit,TodayProfit); | |
CheckTimeIn(TimesProfit); | |
//--------------------------------------------------------------海卓拉加入的片段 | |
#ifdef MODE_BUY | |
if (Non_Stop==false) EndersGameModule(Symbol(),OP_BUY,id_1,EGFlag); | |
#endif | |
#ifdef MODE_SELL | |
if (Non_Stop==false) EndersGameModule(Symbol(),OP_SELL,id_1,EGFlag); | |
#endif | |
//--------------------------------------------------------------海卓拉加入的片段 | |
//+------------------------------------------------------------------+ | |
//|Shutdown EA | | |
//+------------------------------------------------------------------+ | |
if (!EAControl()) return; | |
//+------------------------------------------------Brake_System---------------------------------------------------------------------+ | |
if (BrakeSystem) | |
{ | |
int Pos = 50; | |
#ifdef CHT | |
string Active="煞車系統啟動中"; | |
string Deactive="煞車系統待命中"; | |
#endif | |
#ifdef ENG | |
string Active="Braking System Initiating"; | |
string Deactive="Braking System Stand By"; | |
#endif | |
color Light_status=Gold; | |
double Position_L=Bid+Position*point; | |
if (ObjectFind("Light")==-1) | |
ObjectCreate("Light",OBJ_ARROW,0,0,0); | |
ObjectSet("Light",OBJPROP_ARROWCODE,108); | |
ObjectSet("Light",OBJPROP_CORNER,1); | |
ObjectSet("Light",OBJPROP_COLOR,Lime); | |
ObjectSet("Light",OBJPROP_WIDTH,5); | |
if (ObjectFind("Light2")==-1) | |
ObjectCreate("Light2",OBJ_ARROW,0,0,0); | |
ObjectSet("Light2",OBJPROP_ARROWCODE,161); | |
ObjectSet("Light2",OBJPROP_CORNER,1); | |
ObjectSet("Light2",OBJPROP_COLOR,LawnGreen); | |
ObjectSet("Light2",OBJPROP_WIDTH,5); | |
if (ObjectFind("lightstatus")==-1) | |
ObjectCreate("lightstatus",OBJ_LABEL,0,0,0); | |
ObjectSet("lightstatus",OBJPROP_CORNER,1); | |
ObjectSet("lightstatus",OBJPROP_COLOR,Gold); | |
ObjectSet("lightstatus",OBJPROP_XDISTANCE,20); | |
ObjectSet("lightstatus",OBJPROP_YDISTANCE,Pos+155); | |
ObjectSet("Light",OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSet("Light",OBJPROP_PRICE1,Position_L); | |
ObjectSet("Light2",OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSet("Light2",OBJPROP_PRICE1,Position_L); | |
int EnergyBall=Ball(Status,SleepStop,Last_Time,Last_Price,SleepTime,(int)TimeCurrent(),point,MinPriceShot,Close[0]); | |
if (EnergyBall<0) | |
{ | |
Alert("Energy Brake Error"); | |
return; | |
} | |
if (TimeCurrent()<=SleepStop[0]) | |
{ | |
FirstNotOut=true; | |
MartinIn=false; | |
ObjectSet("Light",OBJPROP_COLOR,Red); | |
ObjectSet("Light2",OBJPROP_COLOR,Crimson); | |
#ifdef CHT | |
ObjectSetText("lightstatus",Active+", "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" 分鐘後啟動",10,"微軟正黑體",Light_status); | |
#endif | |
#ifdef ENG | |
ObjectSetText("lightstatus",Active+", initiate after "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" minutes",10,"微軟正黑體",Light_status); | |
#endif} | |
else | |
{ | |
FirstNotOut=false; | |
MartinIn=true; | |
ObjectSet("Light",OBJPROP_COLOR,Lime); | |
ObjectSet("Light2",OBJPROP_COLOR,LawnGreen); | |
ObjectSetText("lightstatus",Deactive,10,"微軟正黑體",Light_status); | |
} | |
} | |
else | |
{ | |
FirstNotOut=false; | |
MartinIn=true; | |
ObjectDelete("Light"); | |
ObjectDelete("Light2"); | |
ObjectDelete("lightstatus"); | |
} | |
//+------------------------------------------------Brake_System---------------------------------------------------------------------+ | |
if (iLots<0.01 && iLots != 0.0) | |
{ | |
#ifdef CHT | |
Alert(Symbol()+" 已達最小手數(<0.01),無法下單"); | |
#endif | |
#ifdef ENG | |
Alert(Symbol()+" Minimum lot size (<0.01), trading not available"); | |
#endif | |
return; | |
} | |
#ifdef MODE_BUY | |
BuyModule(); | |
#endif | |
#ifdef MODE_SELL | |
SellModule(); | |
#endif | |
ShowMSG(); | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool profit_control(datetime i_0,double takepercent_) | |
{ | |
double today_profit; | |
today_profit=((GetProfitFromDateInCurrency(i_0)+AccountProfit())/Start_Balance)*100; | |
if (today_profit>=takepercent_) | |
{ | |
GlobalVariableSet("LastProfit",today_profit); | |
return(false); | |
} | |
return(true); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double GetProfitFromDateInCurrency(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType()==OP_BUY || OrderType()==OP_SELL) | |
{ | |
if (today<OrderCloseTime()) | |
profit+=OrderProfit()+OrderCommission()+OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void iCloseOrders(int type) | |
{ | |
int C0_cnt; | |
if (type == OP_BUYSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUYSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELLSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELLSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrLime)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
}else { | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selecttickets() | |
{ | |
int cnt_tickets = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)) | |
#endif | |
{ | |
ticket_time=OrderOpenTime(); | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selectticketsfrhistory() | |
{ | |
int cnt_tickets = 0; | |
for (int i=0;i<OrdersHistoryTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && ticket_time<=OrderOpenTime()) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && ticket_time<=OrderOpenTime()) | |
#endif | |
{ | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int OrderSendF(int type,double inLots,double SL,double TP,string Comm) | |
{ | |
int Ertemp=0; | |
double SendOrders_Lots = inLots; | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
switch (type) | |
{ | |
case OP_BUY: | |
Ertemp=OrderSend(Symbol(),OP_BUY,inLots,Ask,20,0,0,Comm,id_1,0,Lime); | |
if (Ertemp<0) | |
{ | |
return(-99); | |
} | |
Set_sl_tp_buy(Symbol(),TP,SL); | |
break; | |
case OP_SELL: | |
Ertemp=OrderSend(Symbol(),OP_SELL,inLots,Bid,20,0,0,Comm,id_1,0,Red); | |
if (Ertemp<0) | |
{ | |
return(-99); | |
} | |
Set_sl_tp_sell(Symbol(),TP,SL); | |
break; | |
} | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double NormPoint(double in) | |
{ | |
return(in * point); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void Set_sl_tp_sell(string symbol,double tp_m,double sl_m) | |
{ | |
double stoploss_m,takeprofit_m; | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==symbol && OrderType()==OP_SELL && OrderMagicNumber()==id_1) | |
{ | |
if (sl_m>0) | |
{ | |
stoploss_m=OrderOpenPrice()+sl_m*point; | |
} | |
else | |
if (sl_m==0) | |
{ | |
stoploss_m=0; | |
} | |
else | |
{ | |
stoploss_m=OrderStopLoss(); | |
} | |
if (tp_m>0) | |
{ | |
takeprofit_m=OrderOpenPrice()-tp_m*point;//宣告一個takeprofit放停利"價位" | |
} | |
else | |
if (tp_m==0) | |
{ | |
takeprofit_m=0; | |
} | |
else | |
{ | |
takeprofit_m=OrderTakeProfit(); | |
} | |
if (CompareDouble(stoploss_m,OrderStopLoss()) && CompareDouble(takeprofit_m,OrderTakeProfit())) | |
{ | |
continue; | |
} | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss_m,takeprofit_m,0,White)) | |
{ | |
Alert("Set_sl_tp_sell" + IntegerToString(GetLastError())); | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void GetAllOrders(int type,int &b_Ticket[],int &s_Ticket[]) | |
{ | |
int x=0,y=0; | |
if (type==OP_BUY) | |
{ | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
b_Ticket[x]=OrderTicket(); | |
x++; | |
} | |
} | |
} | |
if (type==OP_SELL) | |
{ | |
for (int j=0;j<OrdersTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
s_Ticket[y]=OrderTicket(); | |
y++; | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int MyOrderTotal(int Type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL) | |
{ | |
x++; | |
continue; | |
} | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void Set_sl_tp(int type,double tp_m,double sl_m) | |
{ | |
for (int z=0;z<OrdersTotal();z++)//執行N次篩選(N=訂單總數) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue;//用"流水號"找訂單 | |
if (OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1)//挑選出同貨幣對 && BUY單 && MagicNumber=本程式 的訂單 | |
{ | |
if (DoubleCutDigis(OrderStopLoss())==DoubleCutDigis(sl_m) && DoubleCutDigis(OrderTakeProfit())==DoubleCutDigis(tp_m)) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (tp_m == -1) | |
{ | |
tp_m = OrderTakeProfit(); | |
} | |
if (sl_m == -1) | |
{ | |
sl_m = OrderStopLoss(); | |
} | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(sl_m),DoubleCutDigis(tp_m),0,White)) | |
{ | |
Alert("Set_sl_tp "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void Set_sl_tp_Quota(int type,double tp_m0,double tp_m1,int WithOut) | |
{ | |
int QQ=0; | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1) | |
{ | |
QQ++; | |
if (QQ <= WithOut) continue; | |
if (QQ <= AntiMartinTrades) | |
{ | |
if (DoubleCutDigis(OrderStopLoss())==DoubleCutDigis(tp_m0) && DoubleCutDigis(OrderTakeProfit())==0) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(tp_m0),0,0,White)) | |
{ | |
Alert("Set_sl_tp Q0 "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
else | |
{ | |
if (DoubleCutDigis(OrderStopLoss()) == DoubleCutDigis(tp_m0) && DoubleCutDigis(OrderTakeProfit())==DoubleCutDigis(tp_m1)) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(tp_m0),DoubleCutDigis(tp_m1),0,White)) | |
{ | |
Alert("Set_sl_tp Q1 "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void Set_sl_tp_CS(int type,double tp_m,double sl_m,int MaxTicket_s) | |
{ | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if ((OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1 && StringFind(OrderComment(),"CS")>=0) || OrderTicket()==MaxTicket_s) | |
{ | |
if (DoubleCutDigis(OrderStopLoss())==DoubleCutDigis(sl_m) && DoubleCutDigis(OrderTakeProfit())==DoubleCutDigis(tp_m)) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (tp_m == -1) | |
{ | |
tp_m = OrderTakeProfit(); | |
} | |
if (sl_m == -1) | |
{ | |
sl_m = OrderStopLoss(); | |
} | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(sl_m),DoubleCutDigis(tp_m),0,White)) | |
{ | |
Alert("Set_sl_tp CS.0 "+IntegerToString(OrderTicket())+"|"+DoubleToStr(sl_m,Digits)+"|"+DoubleToStr(Ask,Digits)+"|"+DoubleToStr(Bid,Digits)+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
if (OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1 && StringFind(OrderComment(),"CS")<0 && OrderTicket()!=MaxTicket_s) | |
{ | |
if (/*DoubleCutDigis(OrderStopLoss()) == DoubleCutDigis(sl_m)*/OrderStopLoss()==0 && DoubleCutDigis(OrderTakeProfit())==DoubleCutDigis(tp_m)) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(OrderStopLoss()),DoubleCutDigis(tp_m),0,White)) | |
{ | |
Alert("Set_sl_tp CS.0 "+IntegerToString(OrderTicket())+"|"+DoubleToStr(sl_m,Digits)+"|"+DoubleToStr(Ask,Digits)+"|"+DoubleToStr(Bid,Digits)+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double NormPrice(double in,int dd) | |
{ | |
in=NormalizeDouble(in,dd); | |
return(in); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double Cal_TP_Sell(bool Mode,int type,double MaxPrice_s,double TPPoint,int WithOut) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
int x = 0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
x++; | |
if (x <= WithOut) continue; | |
TotalLots+=OrderLots(); | |
TotalDisProduct+= (MaxPrice_s - OrderOpenPrice())/point * OrderLots(); | |
} | |
} | |
if (Mode) | |
{ | |
TP_Goal= (TPPoint * fGetFirstLots(OP_SELL) + TotalDisProduct) / TotalLots; | |
} | |
else | |
{ | |
TP_Goal= (TPPoint * GetAllLots(OP_SELL) + TotalDisProduct) / TotalLots; | |
} | |
return(TP_Goal); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double Cal_TP_CS_SELL(int type,double MaxPrice_s,int MaxTicket_s,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol() && StringFind(OrderComment(),"CS")>=0) | |
{ | |
TotalLots+=OrderLots(); | |
TotalDisProduct+=(MaxPrice_s-OrderOpenPrice())/point*OrderLots(); | |
} | |
if (OrderTicket()==MaxTicket_s) | |
{ | |
if (OrderSelect(MaxTicket_s,SELECT_BY_TICKET,MODE_TRADES)) | |
{ | |
TotalLots+=OrderLots(); | |
TotalDisProduct+=(MaxPrice_s-OrderOpenPrice())/point*OrderLots(); | |
} | |
} | |
} | |
TP_Goal=(TPPoint*fGetFirstLots(type)+TotalDisProduct)/TotalLots; | |
return(TP_Goal); | |
} | |
double Cal_TP_CS_BUY(int type,double MinPrice_b,int MinTicket_b,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if ((OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol() && StringFind(OrderComment(),"CS")>=0) || OrderTicket()==MinTicket_b) | |
{ | |
TotalLots+=OrderLots(); | |
TotalDisProduct+=(OrderOpenPrice()-MinPrice_b)/point*OrderLots(); | |
} | |
} | |
TP_Goal=(TPPoint*fGetFirstLots(1)+TotalDisProduct)/TotalLots; | |
return(TP_Goal); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int OrderSendA(int type,double price,color tColor,string tSymbol,double inLots,double SL,double TP,string tComment) | |
{ | |
RefreshRates(); | |
double SendOrders_Lots = inLots; | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
int ticket_c=OrderSend(tSymbol,type,inLots,price,(int)(2*(point/Point)),0,0,tComment,id_1,0,tColor); //if yes send order | |
if (ticket_c < 0) | |
{ | |
Alert("OrderSend (OrderSendA) Error: "+ErrorDescription(GetLastError())); | |
return(-1); | |
} | |
if (TP == 0 && SL ==0) return(1); | |
if (!OrderModify(ticket_c,price,SL,TP,0,tColor)) | |
{ | |
Alert("OrderModify SL TP (OrderSendA) Error: "+ErrorDescription(GetLastError())); | |
return(1); | |
} | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double fGetFirstLots(int type) | |
{ | |
double fLot = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderMagicNumber()==id_1 && OrderType()==type && OrderSymbol()==Symbol()) | |
{ | |
fLot=OrderLots(); | |
break; | |
} | |
} | |
fLot=NormPrice(fLot,2); | |
return(fLot); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double GetProfit() | |
{ | |
double Profit=0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderMagicNumber()==id_1 && OrderType()==OP_SELL && OrderSymbol()==Symbol()) | |
{ | |
Profit+=OrderProfit()+OrderSwap()+OrderCommission(); | |
} | |
} | |
return(Profit); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double DoubleCutDigis(double in) | |
{ | |
return(StrToDouble(DoubleToStr(in,Digits))); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void TicketInfo_SELL(int &b_Ticket[],double &b_Price[],int &s_Ticket[],double &s_Price[],double &MaxPrice_s,int &MaxTicket_s,double &MinPrice_s,int &MinTicket_s) | |
{ | |
if (MyOrderTotal(OP_SELL)!=0) | |
{ | |
ArrayResize(s_Ticket,MyOrderTotal(OP_SELL)); | |
ArrayResize(s_Price,MyOrderTotal(OP_SELL)); | |
GetAllOrders(OP_SELL,b_Ticket,s_Ticket); | |
for (int j=0;j<ArraySize(s_Ticket);j++) | |
{ | |
if (!OrderSelect(s_Ticket[j],SELECT_BY_TICKET,MODE_TRADES)) continue; | |
s_Price[j]=OrderOpenPrice(); | |
} | |
MaxPrice_s=s_Price[ArrayMaximum(s_Price)]; | |
MaxTicket_s= s_Ticket[ArrayMaximum(s_Price)]; | |
MinPrice_s = s_Price[ArrayMinimum(s_Price)]; | |
MinTicket_s= s_Ticket[ArrayMinimum(s_Price)]; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool CheckQuota_SELL() | |
{ | |
bool Sig=false; | |
double Price[2] = {0}; | |
int x=0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (x==2) | |
break; | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==OP_SELL && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
Price[x]=OrderOpenPrice(); | |
x++; | |
} | |
} | |
if (x < 2) | |
return(false); | |
if (Price[0] < Price[1]) | |
Sig = false; | |
if (Price[0] > Price[1]) | |
Sig = true; | |
return(Sig); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int GetLastOrder(int type) | |
{ | |
int Ticket=0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
Ticket=OrderTicket(); | |
break; | |
} | |
} | |
return(Ticket); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void ShowMSG() | |
{ | |
double Scale=StrToDouble(DoubleToStr(FontDPI/96,2)); | |
double Start=55; | |
Start=(Start * Scale); | |
double Y=20; | |
Y=(Y*Scale); | |
if (ObjectFind("LOGO")==-1) | |
{ | |
//ObjectCreate("LOGO",OBJ_LABEL,0,0,0); | |
ObjectCreate(0,"LOGO",OBJ_BITMAP_LABEL,0,0,0); | |
} | |
ObjectSetString(0,"LOGO",OBJPROP_BMPFILE,"\\Images\\Axe.bmp"); | |
ObjectSetInteger(0,"LOGO",OBJPROP_XDISTANCE,8); | |
ObjectSetInteger(0,"LOGO",OBJPROP_YDISTANCE,15); | |
if (ObjectFind("LOGO0")==-1) | |
{ | |
ObjectCreate("LOGO0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("LOGO0",OBJPROP_CORNER,0); | |
ObjectSet("LOGO0",OBJPROP_XDISTANCE,60*Scale); | |
ObjectSet("LOGO0",OBJPROP_YDISTANCE,10*Scale); | |
ObjectSetText("LOGO0","登峰戰士",30,"標楷體",White); | |
if (ObjectFind("NAME")==-1) | |
{ | |
ObjectCreate("NAME",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("NAME",OBJPROP_CORNER,0); | |
ObjectSet("NAME",OBJPROP_XDISTANCE,65*Scale); | |
ObjectSet("NAME",OBJPROP_YDISTANCE,50*Scale+Y*0); | |
#ifdef MODE_BUY | |
ObjectSetText("NAME","EVEREST WARRIOR BUY",14,"Impact",GreenYellow); | |
#endif | |
#ifdef MODE_SELL | |
ObjectSetText("NAME","EVEREST WARRIOR SELL",14,"Impact",Red); | |
#endif | |
if (ObjectFind("MSG0")==-1) | |
{ | |
ObjectCreate("MSG0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG0",OBJPROP_CORNER,0); | |
ObjectSet("MSG0",OBJPROP_XDISTANCE,10*Scale); | |
ObjectSet("MSG0",OBJPROP_YDISTANCE,Start+Y*1); | |
ObjectSetText("MSG0","Martingale Multiple: ",14,"Impact",Gold); | |
if (ObjectFind("MSG0.0")==-1) | |
{ | |
ObjectCreate("MSG0.0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG0.0",OBJPROP_CORNER,0); | |
ObjectSet("MSG0.0",OBJPROP_XDISTANCE,185*Scale); | |
ObjectSet("MSG0.0",OBJPROP_YDISTANCE,Start+Y*1); | |
ObjectSetText("MSG0.0",DoubleToStr(i_Multiple,3),14,"Impact",Gold); | |
if (ObjectFind("MSG1")==-1) | |
{ | |
ObjectCreate("MSG1",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG1",OBJPROP_CORNER,0); | |
ObjectSet("MSG1",OBJPROP_XDISTANCE,10*Scale); | |
ObjectSet("MSG1",OBJPROP_YDISTANCE,Start+Y*2); | |
ObjectSetText("MSG1","Max Martingale Trades: ",14,"Impact",White); | |
if (ObjectFind("MSG1.0")==-1) | |
{ | |
ObjectCreate("MSG1.0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG1.0",OBJPROP_CORNER,0); | |
ObjectSet("MSG1.0",OBJPROP_XDISTANCE,210*Scale); | |
ObjectSet("MSG1.0",OBJPROP_YDISTANCE,Start+Y*2); | |
ObjectSetText("MSG1.0",DoubleToStr(i_MaxMartin,0),14,"Impact",White); | |
if (ObjectFind("MSG2")==-1) | |
{ | |
ObjectCreate("MSG2",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG2",OBJPROP_CORNER,0); | |
ObjectSet("MSG2",OBJPROP_XDISTANCE,10*Scale); | |
ObjectSet("MSG2",OBJPROP_YDISTANCE,Start+Y*3); | |
ObjectSetText("MSG2","Martingale Range: ",14,"Impact",CornflowerBlue); | |
if (ObjectFind("MSG2.0")==-1) | |
{ | |
ObjectCreate("MSG2.0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG2.0",OBJPROP_CORNER,0); | |
ObjectSet("MSG2.0",OBJPROP_XDISTANCE,170*Scale); | |
ObjectSet("MSG2.0",OBJPROP_YDISTANCE,Start+Y*3); | |
ObjectSetText("MSG2.0",DoubleToStr(i_MartinStep_0,0)+" | "+DoubleToStr(i_MartinStep_1,0)+" | "+DoubleToStr(i_MartinStep_2,0)+" | "+DoubleToStr(i_MartinStep_3,0)+".....",14,"Impact",CornflowerBlue); | |
if (ObjectFind("MSG3")==-1) | |
{ | |
ObjectCreate("MSG3",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG3",OBJPROP_CORNER,0); | |
ObjectSet("MSG3",OBJPROP_XDISTANCE,10*Scale); | |
ObjectSet("MSG3",OBJPROP_YDISTANCE,Start+Y*4); | |
if (FirstNotOut) | |
{ | |
ObjectSetText("MSG3","Mode: "+"ATTACK",14,"Impact",CornflowerBlue); | |
} | |
else | |
{ | |
ObjectSetText("MSG3","Mode: "+"FLAT",14,"Impact",CornflowerBlue); | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void SetKbar(double &Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool AnyCS(int type) | |
{ | |
bool Sig = false; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol() && StringFind(OrderComment(),"CS")>=0) | |
{ | |
Sig=true; | |
break; | |
} | |
} | |
return(Sig); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double GetAllLots(int type) | |
{ | |
double inLots=0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderMagicNumber()==id_1 && OrderType()==type && OrderSymbol()==Symbol()) | |
{ | |
inLots+=OrderLots(); | |
} | |
} | |
return(inLots); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool F_CSProtect(int type,int MaxTicket_s,double in_SL) | |
{ | |
bool Out = false; | |
string OutPrint = ""; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if ((OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol() && StringFind(OrderComment(),"CS")>=0) || OrderTicket() == MaxTicket_s) | |
{ | |
if (OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),(int)(2*(point/Point)),White)) | |
{ | |
Out = true; | |
OutPrint = OutPrint + DoubleToStr(OrderTicket(),0) + ","; | |
} | |
} | |
} | |
if (Out) Alert("CS Protect Active: " + OutPrint + "Ask:" +DoubleToStr(Ask,Digits) + ",SL:" + DoubleToStr(in_SL,Digits)); | |
return(Out); | |
} | |
//+------------------------------------------------------------------+ | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= TOLERANCE) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= TOLERANCE && A >= -TOLERANCE) return(true); | |
return(false); | |
} | |
void PendingTrade(const int Type,const string iComment,const double iSL,const double iTP) | |
{ | |
double InPrice = Pending; | |
int Ticket = 0; | |
int iType = -1; | |
double SL = 0.0; | |
double TP = 0.0; | |
if (Type == OP_BUY) | |
{ | |
if (InPrice > Ask) | |
iType = OP_BUYSTOP; | |
if (InPrice < Ask) | |
iType = OP_BUYLIMIT; | |
if (MyOrderTotal(OP_BUYSTOP) >= 1 || MyOrderTotal(OP_BUYLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,iLots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrLime); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
}else | |
{ | |
if (!OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) return; | |
SL = OrderOpenPrice() - iSL * point; | |
TP = OrderOpenPrice() + iTP * point; | |
if (iSL == 0 || MartinSW) SL = 0; | |
if (iTP == 0) TP = 0; | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_sl_tp "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (InPrice < Bid) | |
iType = OP_SELLSTOP; | |
if (InPrice > Bid) | |
iType = OP_SELLLIMIT; | |
if (MyOrderTotal(OP_SELLSTOP) >= 1 || MyOrderTotal(OP_SELLLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,iLots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrRed); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
}else | |
{ | |
if (!OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) return; | |
SL = OrderOpenPrice() + iSL * point; | |
TP = OrderOpenPrice() - iTP * point; | |
if (iSL == 0 || MartinSW) SL = 0; | |
if (iTP == 0) TP = 0; | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_sl_tp "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
bool CloseAllTrades() | |
{ | |
if (GlobalVariableGet(NextSW_str) == 1) | |
{ | |
#ifdef MODE_BUY | |
iCloseOrders(OP_BUY); | |
#endif | |
#ifdef MODE_SELL | |
iCloseOrders(OP_SELL); | |
#endif | |
if (MyOrderTotal(OP_ALL) > 0) | |
{ | |
#ifdef CHT | |
Alert("水雷未掃除成功"); | |
#endif | |
#ifdef ENG | |
Alert("Submarine mine sweeper not completed"); | |
#endif | |
return(false); | |
}else | |
{ | |
Sleep((int)(NextTime * 1000 * 60)); | |
} | |
} | |
return(true); | |
} | |
void Displayprofit(const double TimesProfit, const double TodayProfit) | |
{ | |
int Pos = 50; | |
string Label_Text = ""; | |
string Label_Text1 = ""; | |
if (ObjectFind(Run) < 0) ObjectCreate(0,Run,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Run, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Run, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Run, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Run, OBJPROP_YDISTANCE, Pos); | |
ObjectSetInteger(0,Run, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Run, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetString(0,Run, OBJPROP_FONT, "微軟正黑體"); | |
for (int i=0;i<6;i++) | |
{ | |
if(ObjectFind(Label_arr[i]) < 0) ObjectCreate(0,Label_arr[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_XDISTANCE,120); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr[i],OBJPROP_FONT,"微軟正黑體"); | |
if(ObjectFind(Label_arr1[i]) < 0) ObjectCreate(0,Label_arr1[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_FONT,"微軟正黑體"); | |
switch(i) | |
{ | |
#ifdef CHT | |
case 0: | |
Label_Text = "單次擄獲狂派戰俘: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "進場起始兵力: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "進場經過時間: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " 分鐘"; | |
break; | |
case 3: | |
Label_Text = "戰場兵力: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "今日擄獲狂派戰俘累積至: " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "最大暫時被擄穫兵力: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
#ifdef ENG | |
case 0: | |
Label_Text = "Profit/Loss: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "Start Balance: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "Time Elapsed: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " Minutes"; | |
break; | |
case 3: | |
Label_Text = "Orders: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "Profit/Loss (Today): " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "Maximum Floating Loss: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
} | |
ObjectSetString(0,Label_arr[i],OBJPROP_TEXT,Label_Text); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_TEXT,Label_Text1); | |
} | |
} | |
void CheckTimeIn(const double TimesProfit) | |
{ | |
datetime RunTime = (TimeCurrent() - Start_P) / 60; | |
if (SmallLossWinSW && RunTime >= MaxTime && OrdersTotal() >= MaxIn && TimesProfit >= MaxP) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_CHECKTIMEIN,""); | |
} | |
} | |
bool EAControl() | |
{ | |
bool Out = true; | |
string str =""; | |
if (TimeCurrent() <= EARunTime) | |
{ | |
str = "SLSLeep"; | |
Out = false; | |
} | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
Comment(str); | |
return(Out); | |
} | |
double OpLots() | |
{ | |
double Out = Lots; | |
if (!EqualZero(MM) && MM > 0) | |
{ | |
Out = (AccountBalance() * MM ) / (1000 * TakeProfit); | |
} | |
return(Out); | |
} | |
double GetProfitPeriod(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType() == OP_BUY || OrderType() == OP_SELL) | |
{ | |
if (today < OrderCloseTime()) | |
profit += OrderProfit() + OrderCommission() + OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
void WriteLog(const int Mode,const string in) | |
{ | |
double TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
string str = WindowExpertName() + "," + Symbol() + "," + TimeToString(TimeCurrent(),TIME_DATE|TIME_DATE|TIME_SECONDS) + | |
": " + LogText[Mode] + " , " + in + | |
"," + Start_Balance_str + ": " + DoubleToString(Start_Balance,2) + | |
"," + Start_P_str + ": " + IntegerToString(Start_P) + | |
",TimeProfit: " + DoubleToString(TimesProfit,2) + | |
"| Set: 餘:" + DoubleToString(iBalance,2) + | |
",次:" + DoubleToString(TakeProfitPercent,2) + | |
",日:" + DoubleToString(TakeProfitPercentDay,2) + | |
",損:" + DoubleToStr(StopLossPercent ,2) + | |
",時:" + DoubleToString(MaxTime,2) + | |
",單:" + DoubleToString(MaxIn,2) + | |
",虧:" + DoubleToString(MaxP,2) + | |
"\n" | |
; | |
int h1 = FileOpen("Logs.txt",FILE_READ|FILE_WRITE|FILE_TXT); | |
if (h1 < 0) Print("Open Log Failed"); | |
FileSeek(h1,0,SEEK_END); | |
int byte = FileWriteString(h1,str,StringLen(str)); | |
if (byte <= 0) Print("Write Log Failed"); | |
FileClose(h1); | |
} | |
void SellModule() | |
{ | |
string tMComment="Martin"; | |
// EveryTrade Modify | |
bool SellModifySLTP = false; | |
// Mode Selection & Determine SL TP Lots | |
double SellLots = 0.0; | |
double FirstSellTP = 0.0; | |
double FirstSellSL = 0.0; | |
double FirstSellLots = 0.0; | |
SellLots = iLots; | |
FirstSellLots = iLots; | |
FirstSellTP = TakeProfit; | |
FirstSellSL = StopLoss; | |
switch (AutoSWAntiMartin) | |
{ | |
case -1: | |
FirstNotOut=false; | |
break; | |
case 0: | |
if (MyOrderTotal(OP_SELL) > 1) | |
{ | |
if (CheckQuota_SELL()) | |
{ | |
FirstNotOut = true; | |
} | |
} | |
break; | |
case 1: | |
FirstNotOut=true; | |
break; | |
} | |
if (FirstNotOut) | |
{ | |
FirstSellTP = 0; | |
} | |
// Mode Selection & Determine SL TP Lots End | |
if (AutoRepairSLTP) | |
{ | |
if (MyOrderTotal(OP_SELL) == 1) | |
{ | |
if (MartinSW) | |
Set_sl_tp_sell(Symbol(),FirstSellTP,0); | |
else | |
Set_sl_tp_sell(Symbol(),FirstSellTP,FirstSellSL); | |
} | |
} | |
if (MyOrderTotal(OP_SELL)==0) | |
{ | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_SELL,"Pending" + IntegerToString(MyOrderTotal(OP_SELL)),FirstSellSL,FirstSellTP); | |
iPending = 0; | |
} | |
if (MyOrderTotal(OP_SELLSTOP) == 0 && MyOrderTotal(OP_SELLLIMIT) == 0) | |
{ | |
if (FirstIn) | |
{ | |
if (MartinSW) | |
OrderSendF(OP_SELL,iLots,0,FirstSellTP,Symbol()); | |
else | |
OrderSendF(OP_SELL,iLots,FirstSellSL,FirstSellTP,Symbol()); | |
} | |
} | |
} | |
double MartinStep[]; | |
ArrayResize(MartinStep,10); | |
MartinStep[0]=i_MartinStep_0; | |
MartinStep[1]=i_MartinStep_1; | |
MartinStep[2]=i_MartinStep_2; | |
MartinStep[3]=i_MartinStep_3; | |
MartinStep[4]=i_MartinStep_4; | |
MartinStep[5]=i_MartinStep_5; | |
MartinStep[6]=i_MartinStep_6; | |
MartinStep[7]=i_MartinStep_7; | |
MartinStep[8]=i_MartinStep_8; | |
MartinStep[9]=i_MartinStep_9; | |
int b_Ticket[],s_Ticket[]; | |
double b_Price[]; | |
double s_Price[]; | |
double MaxPrice_s,MinPrice_s; | |
int MaxTicket_s,MinTicket_s; | |
// FirstNotOut | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
double AntiMartinLots = 0.0; | |
int ti = 0; | |
double in_TP = 0.0; | |
double in_SL = 0.0; | |
string CommentADD=""; | |
if (FirstNotOut) | |
{ | |
if (MyOrderTotal(OP_SELL) >= 1 && MyOrderTotal(OP_SELL) < AntiMartinTrades + 1) | |
{ | |
if ( (MinPrice_s - Bid) / point > AntiMartinStep) | |
{ | |
AntiMartinLots = NormalizeDouble(SellLots * MathPow(AntiMartinMultiple,MyOrderTotal(OP_SELL)),2); | |
if (AntiMartinLots < 0.01) AntiMartinLots = 0.01; | |
if (!Lots_Valid(AntiMartinLots)) return; | |
ti = OrderSend(Symbol(),OP_SELL,AntiMartinLots,Bid,20,0,0,"FNO"+tMComment+DoubleToStr(MyOrderTotal(OP_SELL),0),id_1,0,Red) ; | |
if (ti < 0) | |
{ | |
Alert("OrderSnend Buy FNO Error: " + ErrorDescription(GetLastError())); | |
} | |
else | |
{ | |
SellModifySLTP = true; | |
} | |
} | |
} | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
if (AutoRepairSLTP || SellModifySLTP) | |
{ | |
if (CheckQuota_SELL()) | |
{ | |
in_TP = MinPrice_s - (TakeProfit * point); | |
if (in_TP > Ask && in_TP != 0) in_TP = 0; | |
in_SL = MaxPrice_s - Cal_TP_Sell(true,OP_SELL,MaxPrice_s,Ai_TPPoint,0) * point; | |
if (in_SL < Ask && in_SL != 0) in_SL = 0; | |
Set_sl_tp_Quota(OP_SELL,in_SL,in_TP,0); | |
SellModifySLTP = false; | |
} | |
} | |
} | |
//+------------------------------------------------Martin Start---------------------------------------------------------------------+ | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
double SellMartinLots_Temp = 0.0; | |
double SellMartinLots = 0.0; | |
double Step_S = 0.0; | |
if (MartinSW) | |
{ | |
if (MartinIn) | |
{ | |
if (MyOrderTotal(OP_SELL) > 0) | |
{ | |
SellMartinLots_Temp = MyOrderTotal(OP_SELL)-(MOrders-1); | |
if (SellMartinLots_Temp < 0) SellMartinLots_Temp = 0; | |
SellMartinLots=NormalizeDouble(SellLots * MathPow(i_Multiple,SellMartinLots_Temp),2); | |
if (MyOrderTotal(OP_SELL)-1 > ArraySize(MartinStep)-1) | |
{ | |
Step_S=MartinStep[ArrayMaximum(MartinStep)]; | |
}else | |
{ | |
Step_S=MartinStep[MyOrderTotal(OP_SELL)-1]; | |
} | |
if (Step_S == 0) Step_S=MartinStep[ArrayMaximum(MartinStep)]; | |
if ( (Bid - MaxPrice_s) / point > Step_S && MyOrderTotal(OP_SELL) < i_MaxMartin) | |
{ | |
if (MartinSellBar != iBars(Symbol(),M_Period)) | |
{ | |
MartinSellBar = iBars(Symbol(),M_Period) + 1; | |
} | |
else if (MartinSellBar == iBars(Symbol(),M_Period)) | |
{ | |
if (OrderSendA(OP_SELL,Bid,Red,Symbol(),SellMartinLots,0,0,Symbol()+"_"+tMComment+DoubleToStr(MyOrderTotal(OP_SELL),0)) > 0) | |
{ | |
SellModifySLTP = true; | |
MartinSellBar=0; | |
} | |
} | |
} | |
else | |
{ | |
MartinSellBar=0; | |
} | |
} | |
else | |
{ | |
MartinSellBar=0; | |
} | |
} | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
if (AutoRepairSLTP || SellModifySLTP) | |
{ | |
if (!AnyCS(OP_SELL)) | |
{ | |
if (!CheckQuota_SELL() && MyOrderTotal(OP_SELL) > 1) | |
{ | |
if (SingleOut) | |
{ | |
Set_sl_tp_sell(Symbol(),i_TPPoint,0); | |
} | |
else | |
{ | |
in_TP = MaxPrice_s - Cal_TP_Sell(MartinCalLots,OP_SELL,MaxPrice_s,i_TPPoint,0) * point; | |
if (in_TP > Ask && in_TP != 0) in_TP = 0; | |
in_SL = 0; | |
if (in_SL < Ask && in_SL != 0) in_SL = 0; | |
Set_sl_tp(OP_SELL,in_TP,in_SL); | |
} | |
SellModifySLTP = false; | |
} | |
} | |
} | |
} | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
double CSLots=0.0; | |
double LastPrice=0.0; | |
if (MartinAntiMSW && !SingleOut) | |
{ | |
if (MyOrderTotal(OP_SELL)>1) | |
{ | |
if (!CheckQuota_SELL()) | |
{ | |
if (OrderSelect(GetLastOrder(OP_SELL),SELECT_BY_TICKET,MODE_TRADES)) | |
{ | |
CSLots=NormalizeDouble(OrderLots()*CSMultiple,2); | |
if (CSLots<0.01) | |
{ | |
CSLots=0.01; | |
} | |
LastPrice=OrderOpenPrice(); | |
} | |
if ((LastPrice-Bid)/point>=CSStep /*&& (MarketInfo(Symbol(),MODE_SPREAD)) * (Point/point) <= CSStep / 2*/) | |
{ | |
if (Lots_Valid(CSLots) && OrderSend(Symbol(),OP_SELL,CSLots,Bid,20,0,0,Symbol()+"_CS_"+DoubleToStr(MyOrderTotal(OP_SELL),0),id_1,0,Red)>0) | |
{ | |
SellModifySLTP = true; | |
} | |
else | |
{ | |
Alert("CS Error: "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
if (AutoRepairSLTP || SellModifySLTP) { | |
if (AnyCS(OP_SELL)) | |
{ | |
in_TP = MaxPrice_s - Cal_TP_Sell(MartinCalLots,OP_SELL,MaxPrice_s,i_TPPoint,0) * point; | |
if (in_TP > Ask && in_TP != 0) in_TP = 0; | |
in_SL = MaxPrice_s - Cal_TP_CS_SELL(OP_SELL,MaxPrice_s,MaxTicket_s,CSTPPoint) * point; | |
if (in_SL < Ask && in_SL != 0) | |
{ | |
if (CSProtect) | |
{ | |
if (AnyCS(OP_SELL)) | |
{ | |
if (F_CSProtect(OP_SELL,MaxTicket_s,in_SL)) | |
{ | |
return; | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
} | |
} | |
} | |
in_SL = 0; | |
} | |
Set_sl_tp_CS(OP_SELL,in_TP,in_SL,MaxTicket_s); | |
SellModifySLTP = false; | |
} | |
} | |
} | |
} | |
void DeleteObjects() | |
{ | |
for (int i=5;i>=0;i--) | |
{ | |
if(ObjectFind(Label_arr[i]) >= 0) ObjectDelete(0,Label_arr[i]); | |
if(ObjectFind(Label_arr1[i]) >= 0) ObjectDelete(0,Label_arr1[i]); | |
} | |
if(ObjectFind(Run) >= 0) ObjectDelete(0,Run); | |
} | |
void BuyModule() | |
{ | |
string tMComment="Martin"; | |
// EveryTrade Modify | |
bool BuyModifySLTP = false; | |
// Mode Selection & Determine SL TP Lots | |
double BuyLots = 0.0; | |
double FirstBuyTP = 0.0; | |
double FirstBuySL = 0.0; | |
double FirstBuyLots = 0.0; | |
BuyLots = iLots; | |
FirstBuyLots = iLots; | |
FirstBuyTP = TakeProfit; | |
FirstBuySL = StopLoss; | |
switch (AutoSWAntiMartin) | |
{ | |
case -1: | |
FirstNotOut=false; | |
break; | |
case 0: | |
if (MyOrderTotal(OP_BUY) > 1) | |
{ | |
if (CheckQuota_BUY()) | |
{ | |
FirstNotOut = true; | |
} | |
} | |
break; | |
case 1: | |
FirstNotOut=true; | |
break; | |
} | |
if (FirstNotOut) | |
{ | |
FirstBuyTP = 0; | |
} | |
// Mode Selection & Determine SL TP Lots End | |
if (AutoRepairSLTP) | |
{ | |
if (MyOrderTotal(OP_BUY) == 1) | |
{ | |
if (MartinSW) | |
Set_sl_tp_buy(Symbol(),FirstBuyTP,0); | |
else | |
Set_sl_tp_buy(Symbol(),FirstBuyTP,FirstBuySL); | |
} | |
} | |
if (MyOrderTotal(OP_BUY)==0) | |
{ | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_BUY,"Pending" + IntegerToString(MyOrderTotal(OP_BUY)),FirstBuySL,FirstBuyTP); | |
iPending = 0; | |
} | |
if (MyOrderTotal(OP_BUYSTOP) == 0 && MyOrderTotal(OP_BUYLIMIT) == 0) | |
{ | |
if (FirstIn) | |
{ | |
if (MartinSW) | |
OrderSendF(OP_BUY,iLots,0,FirstBuyTP,Symbol()); | |
else | |
OrderSendF(OP_BUY,iLots,FirstBuySL,FirstBuyTP,Symbol()); | |
} | |
} | |
} | |
double MartinStep[]; | |
ArrayResize(MartinStep,10); | |
MartinStep[0]=i_MartinStep_0; | |
MartinStep[1]=i_MartinStep_1; | |
MartinStep[2]=i_MartinStep_2; | |
MartinStep[3]=i_MartinStep_3; | |
MartinStep[4]=i_MartinStep_4; | |
MartinStep[5]=i_MartinStep_5; | |
MartinStep[6]=i_MartinStep_6; | |
MartinStep[7]=i_MartinStep_7; | |
MartinStep[8]=i_MartinStep_8; | |
MartinStep[9]=i_MartinStep_9; | |
int b_Ticket[],s_Ticket[]; | |
double b_Price[]; | |
double s_Price[]; | |
double MaxPrice_b,MinPrice_b; | |
int MaxTicket_b,MinTicket_b; | |
// FirstNotOut | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
double AntiMartinLots = 0.0; | |
int ti = 0.0; | |
double in_TP = 0.0; | |
double in_SL = 0.0; | |
string CommentADD=""; | |
if (FirstNotOut) | |
{ | |
if (MyOrderTotal(OP_BUY) >= 1 && MyOrderTotal(OP_BUY) < AntiMartinTrades + 1 ) | |
{ | |
if ( (Ask - MaxPrice_b) / point > AntiMartinStep) | |
{ | |
AntiMartinLots = NormalizeDouble(BuyLots * MathPow(AntiMartinMultiple,MyOrderTotal(OP_BUY)),2); | |
if (AntiMartinLots < 0.01) AntiMartinLots = 0.01; | |
if (!Lots_Valid(AntiMartinLots)) return; | |
ti = OrderSend(Symbol(),OP_BUY,AntiMartinLots,Ask,20,0,0,"FNO"+tMComment+DoubleToStr(MyOrderTotal(OP_BUY),0),id_1,0,Lime) ; | |
if (ti < 0) | |
{ | |
Alert("OrderSnend Buy FNO Error: " + ErrorDescription(GetLastError())); | |
} | |
else | |
{ | |
BuyModifySLTP = true; | |
} | |
} | |
} | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
if (AutoRepairSLTP || BuyModifySLTP) | |
{ | |
if (CheckQuota_BUY()) | |
{ | |
in_TP = MaxPrice_b + (TakeProfit * point); | |
if (in_TP < Bid && in_TP != 0) in_TP = 0; | |
in_SL = MinPrice_b + Cal_TP_Buy(true,OP_BUY,MinPrice_b,Ai_TPPoint,0) * point; | |
if (in_SL > Bid && in_SL != 0) in_SL = 0; | |
Set_sl_tp_Quota(OP_BUY,in_SL,in_TP,0); | |
BuyModifySLTP = false; | |
} | |
} | |
} | |
//+------------------------------------------------Martin Start---------------------------------------------------------------------+ | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
double BuyMartinLots_Temp = 0.0; | |
double BuyMartinLots = 0.0; | |
double Step_B = 0.0; | |
if (MartinSW) | |
{ | |
if (MartinIn) | |
{ | |
if (MyOrderTotal(OP_BUY) > 0) | |
{ | |
BuyMartinLots_Temp = MyOrderTotal(OP_BUY)-(MOrders-1); | |
if (BuyMartinLots_Temp < 0) BuyMartinLots_Temp = 0; | |
BuyMartinLots = NormalizeDouble(BuyLots * MathPow(i_Multiple,BuyMartinLots_Temp),2); | |
if (MyOrderTotal(OP_BUY)-1 > ArraySize(MartinStep)-1) | |
{ | |
Step_B=MartinStep[ArrayMaximum(MartinStep)]; | |
}else | |
{ | |
Step_B=MartinStep[MyOrderTotal(OP_BUY)-1]; | |
} | |
if ( (MinPrice_b - Ask) / point > Step_B && MyOrderTotal(OP_BUY) < i_MaxMartin) | |
{ | |
if (MartinBuyBar != iBars(Symbol(),M_Period)) | |
{ | |
MartinBuyBar = iBars(Symbol(),M_Period) + 1; | |
} | |
else if (MartinBuyBar == iBars(Symbol(),M_Period)) | |
{ | |
if (OrderSendA(OP_BUY,Ask,Lime,Symbol(),BuyMartinLots,0,0,Symbol()+"_"+tMComment+DoubleToStr(MyOrderTotal(OP_BUY),0)) > 0) | |
{ | |
BuyModifySLTP = true; | |
MartinBuyBar=0; | |
} | |
} | |
} | |
else | |
{ | |
MartinBuyBar=0; | |
} | |
} | |
else | |
{ | |
MartinBuyBar=0; | |
} | |
} | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
if (AutoRepairSLTP || BuyModifySLTP) | |
{ | |
if (!AnyCS(OP_BUY)) | |
{ | |
if (!CheckQuota_BUY() && MyOrderTotal(OP_BUY) > 1) | |
{ | |
if (SingleOut) | |
{ | |
Set_sl_tp_buy(Symbol(),i_TPPoint,0); | |
} | |
else | |
{ | |
in_TP = MinPrice_b + Cal_TP_Buy(MartinCalLots,OP_BUY,MinPrice_b,i_TPPoint,0) * point; | |
if (in_TP < Bid && in_TP != 0) in_TP = 0; | |
in_SL = 0; | |
if (in_SL > Bid && in_SL != 0) in_SL = 0; | |
Set_sl_tp(OP_BUY,in_TP,in_SL); | |
} | |
BuyModifySLTP = false; | |
} | |
} | |
} | |
} | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
double CSLots=0.0; | |
double LastPrice=0.0; | |
if (MartinAntiMSW && !SingleOut) | |
{ | |
if (MyOrderTotal(OP_BUY)>1) | |
{ | |
if (!CheckQuota_BUY()) | |
{ | |
if (OrderSelect(GetLastOrder(OP_BUY),SELECT_BY_TICKET,MODE_TRADES)) | |
{ | |
CSLots=NormalizeDouble(OrderLots()*CSMultiple,2); | |
if (CSLots<0.01) | |
{ | |
CSLots=0.01; | |
} | |
LastPrice=OrderOpenPrice(); | |
} | |
if ((Ask-LastPrice)/point>=CSStep /*&& (MarketInfo(Symbol(),MODE_SPREAD)) * (Point/point) <= CSStep / 2*/) | |
{ | |
if (Lots_Valid(CSLots) && OrderSend(Symbol(),OP_BUY,CSLots,Ask,20,0,0,Symbol()+"_CS_"+DoubleToStr(MyOrderTotal(OP_BUY),0),id_1,0,Lime)>0) | |
{ | |
BuyModifySLTP = true; | |
} | |
else | |
{ | |
Alert("CS Error: "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
if (AutoRepairSLTP || BuyModifySLTP) | |
{ | |
if (AnyCS(OP_BUY)) | |
{ | |
in_TP = MinPrice_b + Cal_TP_Buy(MartinCalLots,OP_BUY,MinPrice_b,i_TPPoint,0) * point; | |
if (in_TP < Bid && in_TP != 0) in_TP = 0; | |
in_SL = MinPrice_b + Cal_TP_CS_BUY(OP_BUY,MinPrice_b,MinTicket_b,CSTPPoint) * point; | |
if (in_SL > Bid && in_SL != 0) | |
{ | |
if (CSProtect) | |
{ | |
if (AnyCS(OP_BUY)) | |
{ | |
if (F_CSProtect(OP_BUY,MinTicket_b,in_SL)) | |
{ | |
return; | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
} | |
} | |
} | |
in_SL = 0; | |
} | |
Set_sl_tp_CS(OP_BUY,in_TP,in_SL,MinTicket_b); | |
BuyModifySLTP = false; | |
} | |
} | |
} | |
} | |
bool CheckQuota_BUY() | |
{ | |
bool Sig=false; | |
double Price[2] = {0}; | |
int x=0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (x==2) | |
break; | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==OP_BUY && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
Price[x]=OrderOpenPrice(); | |
x++; | |
} | |
} | |
if (x < 2) | |
return(false); | |
if (Price[0] > Price[1]) | |
Sig = false; | |
if (Price[0] < Price[1]) | |
Sig = true; | |
return(Sig); | |
} | |
void Set_sl_tp_buy(string symbol,double tp_m,double sl_m) | |
{ | |
double stoploss_m,takeprofit_m; | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==symbol && OrderType()==OP_BUY && OrderMagicNumber()==id_1) | |
{ | |
if (sl_m>0) | |
{ | |
stoploss_m=OrderOpenPrice()-sl_m*point; | |
} | |
else | |
if (sl_m==0) | |
{ | |
stoploss_m=0; | |
} | |
else | |
{ | |
stoploss_m=OrderStopLoss(); | |
} | |
if (tp_m>0) | |
{ | |
takeprofit_m=OrderOpenPrice()+tp_m*point;//宣告一個takeprofit放停利"價位" | |
} | |
else | |
if (tp_m==0) | |
{ | |
takeprofit_m= 0; | |
} | |
else | |
{ | |
takeprofit_m=OrderTakeProfit(); | |
} | |
if (CompareDouble(stoploss_m,OrderStopLoss()) && CompareDouble(takeprofit_m,OrderTakeProfit())) | |
{ | |
continue; | |
} | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss_m,takeprofit_m,0,White)) | |
{ | |
Alert("Set_sl_tp_buy" + IntegerToString(GetLastError())); | |
} | |
} | |
} | |
} | |
void TicketInfo_BUY(int &b_Ticket[],double &b_Price[],int &s_Ticket[],double &s_Price[],double &MaxPrice_b,int &MaxTicket_b,double &MinPrice_b,int &MinTicket_b) | |
{ | |
if (MyOrderTotal(OP_BUY)!=0) | |
{ | |
ArrayResize(b_Ticket,MyOrderTotal(OP_BUY)); | |
ArrayResize(b_Price,MyOrderTotal(OP_BUY)); | |
GetAllOrders(OP_BUY,b_Ticket,s_Ticket); | |
for (int i=0;i<ArraySize(b_Ticket);i++) | |
{ | |
if (!OrderSelect(b_Ticket[i],SELECT_BY_TICKET,MODE_TRADES)) continue; | |
b_Price[i]=OrderOpenPrice(); | |
} | |
MaxPrice_b=b_Price[ArrayMaximum(b_Price)]; | |
MaxTicket_b= b_Ticket[ArrayMaximum(b_Price)]; | |
MinPrice_b = b_Price[ArrayMinimum(b_Price)]; | |
MinTicket_b= b_Ticket[ArrayMinimum(b_Price)]; | |
} | |
} | |
double Cal_TP_Buy(bool Mode,int type,double MinPrice_b,double TPPoint,int WithOut) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
int x = 0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
x++; | |
if (x <= WithOut) continue; | |
TotalLots+=OrderLots(); | |
TotalDisProduct+= (OrderOpenPrice() - MinPrice_b)/point * OrderLots(); | |
} | |
} | |
if (Mode) { | |
TP_Goal= (TPPoint * fGetFirstLots(OP_BUY) + TotalDisProduct) / TotalLots; | |
}else { | |
TP_Goal= (TPPoint * GetAllLots(OP_BUY) + TotalDisProduct) / TotalLots; | |
} | |
return(TP_Goal); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
if (UninitializeReason() != 3) | |
{ | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if ( | |
(TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) | |
) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
Sleep(1000); | |
GlobalVariableSet("DNSW0",0); | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
bool TimesStopConfig() | |
{ | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(NextSW_str,0); | |
#ifdef GLOBAL_FILE | |
if (!GlobalFileRead()) | |
{ | |
GlobalVars.G_EARunTime = 0; | |
GlobalVars.G_MaxEquity = (AccountProfit() / AccountBalance()) * 100; | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalFileWrite(); | |
GlobalFileRead(); | |
} | |
Print("EARuntime: " + TimeToString(GlobalVars.G_EARunTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("MaxLossEquity: " + DoubleToString(GlobalVars.G_MaxEquity,2)); | |
Print("Start_P: " + TimeToString(GlobalVars.G_Start_P,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("Start_Balance: " + DoubleToString(GlobalVars.G_Start_Balance,2)); | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
#else | |
return(true); | |
} | |
bool TimesStop(double &TimesProfit, double &TodayProfit) | |
{ | |
#ifdef GLOBAL_FILE | |
if (OrdersTotal()==0) | |
{ | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Status | | |
//+------------------------------------------------------------------+ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVars.G_Start_Balance; | |
else | |
Start_Balance = iBalance; | |
Start_P = GlobalVars.G_Start_P; | |
EARunTime = GlobalVars.G_EARunTime; | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVars.G_MaxEquity) | |
{ | |
GlobalVars.G_MaxEquity = (AccountProfit() / Start_Balance) * 100; | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Status | | |
//+------------------------------------------------------------------+ | |
GlobalVars.G_EARunTime = EARunTime; | |
GlobalVars.G_Start_Balance = Start_Balance; | |
GlobalVars.G_Start_P = Start_P; | |
GlobalFileWrite(); | |
#else | |
if (OrdersTotal()==0) | |
{ | |
GlobalVariableSet(Start_Balance_str,AccountBalance()); | |
GlobalVariableSet(Start_P_str,TimeCurrent()); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Balance | | |
//+------------------------------------------------------------------+ | |
if (GlobalVariableCheck(Start_Balance_str)) | |
{ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVariableGet(Start_Balance_str); | |
else | |
Start_Balance = iBalance; | |
}else | |
{ | |
Start_Balance = AccountBalance(); | |
} | |
if (GlobalVariableCheck(Start_P_str)) | |
{ | |
Start_P = (datetime)GlobalVariableGet(Start_P_str); | |
}else | |
{ | |
Start_P = TimeCurrent(); | |
} | |
if (GlobalVariableCheck(EARUNTIME_str)) | |
{ | |
EARunTime = (datetime)GlobalVariableGet(EARUNTIME_str); | |
} | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVariableGet(MaxEquity_str)) | |
{ | |
GlobalVariableSet(MaxEquity_str,(AccountProfit() / Start_Balance)*100); | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Balnace | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(EARUNTIME_str,EARunTime); | |
GlobalVariableSet(Start_P_str,(double)Start_P); | |
GlobalVariableSet(Start_Balance_str,Start_Balance); | |
#endif | |
return(true); | |
} | |
bool Lots_Valid(const double iLot) | |
{ | |
double ValuePerLot = SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE); | |
if ((iLot * ValuePerLot) > AccountInfoDouble(ACCOUNT_FREEMARGIN)) | |
{ | |
Alert("No Money"); | |
return(false); | |
} | |
return(true); | |
} | |
int EndersGamgeCnt = 0; | |
void EndersGameModule(const string symbol, const int Type, int id, bool &flag) | |
{ | |
if (MyOrderTotal_Enders(symbol,Type,id) == 0) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (OrderCloseTime() > EGStartTime) | |
{ | |
EndersGamgeCnt++; | |
Alert(Symbol() + " 戰場已完成第" + IntegerToString(EndersGamgeCnt) + " 次戰役"); | |
EGStartTime = TimeCurrent(); | |
break; | |
} | |
} | |
} | |
if (EndersGamgeCnt >= OutCnt) | |
{ | |
Alert("已達成設定局數,即將離開 " + Symbol() + " 戰場"); | |
flag = false; | |
ExpertRemove(); | |
} | |
} | |
int MyOrderTotal_Enders(const string symbol,const int Type,const int ID) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL && OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
continue; | |
} | |
if ((Type == OP_BUY && ((OrderType() & 1) == 1 ? false : true)) || (Type == OP_SELL && ((OrderType() & 1) == 1 ? true : false))) | |
{ | |
if (OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
} | |
} | |
} | |
return(x); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| AllNewXBOMB.mq4 | | |
//| APP Wargame-Hydra | | |
//| http://www.facebook.com/groups/moneywargame/ | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import "kernel32.dll" | |
ulong GetVersion(void); | |
#import | |
#define CHT 1 | |
//#define ENG 1 | |
#define ZERO 0.000001 | |
//#define MODE_BUY 1 | |
#define MODE_SELL 1 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_CHECKTIMEIN 0 | |
#define MODE_TIMESTP 1 | |
#define MODE_TIMESSL 2 | |
#define MODE_DAILYTP 3 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_FIRSTTRADE 99 | |
#define MODE_MARTIN 100 | |
#define OP_ALL 101 | |
#define MODE_SLTP_BYPOINT 102 | |
#define MODE_SLTP_BYPRICE 103 | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int id_1 = 3333333; | |
int ErrorCode = 0; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
int BarTime = 0; | |
double NextTime=0.5; | |
double point = 0.0; | |
double iLots = 0.0; | |
double Start_Balance = 0; | |
double iPending; | |
datetime H1BarTime = 0; | |
datetime FirstTradeBar = 0; | |
datetime Start_P = 0; | |
datetime EARunTime = 0; | |
datetime AvoidTime[1] = {0}; | |
datetime EGStartTime = TimeCurrent(); | |
bool EGFlag = true; | |
bool NewGetSW = false; | |
bool AuthorGetSW = false; | |
bool TradeSW = true; | |
int Last_Time[1]={0}; | |
int Status[1]={0}; | |
double Last_Price[1]={-1.0}; | |
double MinPriceShot = 0.0; | |
string AvoidSymbol[1] = {""}; | |
string LogText[4] = {"小虧為贏" , "次止盈" , "次止損" , "日止盈"}; | |
string Label_arr[6] = | |
{ | |
"TimesProfitPer", | |
"Start_Balance", | |
"TimePast", | |
"Orders", | |
"TodayProfitPer", | |
"MaxLoss" | |
}; | |
string Label_arr1[6] = | |
{ | |
"TimesProfitPer0", | |
"Start_Balance0", | |
"TimePast0", | |
"Orders0", | |
"TodayProfitPer0", | |
"MaxLoss0" | |
}; | |
string Start_P_str = "Start_P"; | |
string Start_Balance_str = "Start_Balance"; | |
string MaxEquity_str = "MaxEquity"; | |
string NextSW_str = "NextSW"; | |
string EARUNTIME_str = "EARUNTIME"; | |
string Light = "LightStatus"; | |
string Run = "RunStatus"; | |
string Margin = "Margin"; | |
string FloatLoss = "Float"; | |
string NewsInfo[2] = {"NewsInfo0", "NewsInfo1"}; | |
string Light_str[2] = {"Light","Light2"}; | |
#ifdef CHT | |
string NewsEngage = "狂派情資已接收完全.....全面開戰!"; | |
#endif | |
#ifdef ENG | |
string NewsEngage = "News retrieval completed .....automated system initiated"; | |
#endif | |
//+------------------------------------------------------------------+ | |
//|Ender's Game Par | | |
//+------------------------------------------------------------------+ | |
datetime ticket_time = 0; | |
int OutCntTemp = 0; | |
#ifdef CHT | |
input bool AutoRepairSW = false; //自動回復停損停利 | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //ESP戰艦穩定煞車系統 | |
int pSeconds=14; | |
input double Energy=4; //能量偵測靈敏度 | |
input int SleepTime=15; //能量引擎啟動時間(分鐘) | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //預警雷達控制模組 | |
input int MinBefore = 30; //前幾分鐘閃避 | |
input int MinAfter = 30; //後幾分鐘閃避 | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //保證金比例控制模組 | |
input double MarginLimit = 2000; //保證金低於%數暫停 | |
input bool FloatEq = false; //浮虧控制模組 | |
input double FloatAm = -4000; //虧損(-金額)暫停 | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //開始時間,格式為HH | |
input int DayStopHour = 00; //結束時間,格式為HH | |
input string WeekDay="12345"; //週一執行,鍵入1;週一週二鍵入12 | |
input bool WeekStartOverSW = true; //一個禮拜頭尾不間斷 | |
input int WeekStartHour = 8; //第一天幾點開始 | |
input int WeekStopHour = 17; //最後一天幾點結束 | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //止盈計算本金基準 | |
input bool TakeProfitPercentSW = true; //次止盈開關 | |
input double TakeProfitPercent = 1; //次止盈,每次最多獲利%,立即平倉歸零重啟新局 | |
input bool TakeProfitPercentDaySW = true; //日止盈開關 | |
input double TakeProfitPercentDay = 1000; //日止盈,每日最多獲利%,立即停止交易,隔日重新啟動 | |
input bool StopLossPercentSW = true; //浮虧止損開關 | |
input double StopLossPercent = 1000; //現時帳戶浮虧止損,止損基準值:帳戶餘額 | |
input bool SleepLoss = true; //止損後當日不再出兵 | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //小虧為贏開關 | |
input int MaxTime = 150; //每局最長時間(分鐘) | |
input int MaxIn = 10; //每局最多兵力 | |
input double MaxP = 0.5; //達成以上條件小虧為贏 | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //掛單價格 | |
input bool SingleOut = false; //單進單出 | |
#endif | |
input bool MartinSW = true; //馬丁開關 | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //第幾張馬丁開始加倍 | |
#endif | |
input double LotMultiplikator = 1.382; //馬丁倍率 | |
input double Step = 30; //馬丁間距 | |
input int MaxTrades = 5; //最多單數 | |
input double MM = 1.0; //資金管理%數 | |
input double Lots = 0.01; //手數 | |
input double TakeProfit = 10.0; //獲利點數 | |
input double StopLoss = 10.0; //停損點數 | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
#ifdef ENG | |
input bool AutoRepairSW = false; //Auto Repair SL&TP | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //BrakeSystem | |
int pSeconds=14; | |
input double Energy=4; //Braking System Sensitivity Control | |
input int SleepTime=15; //Braking System Sleeping Time | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //Braking System Sleeping Time | |
input int MinBefore = 30; //Stop trading few minutes before news alert | |
input int MinAfter = 30; //Stop trading few minutes after news alert | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //Margin Level Control Module | |
input double MarginLimit = 2000; //Margin level % limit to stop trading | |
input bool FloatEq = false; //Floating Loss Control Module | |
input double FloatAm = -4000; //Floating loss (-$amount) limit to stop trading | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //Starting Time, in HH format | |
input int DayStopHour = 00; //Ending Time, in HH format | |
input string WeekDay="12345"; //Star on Monday=1, Monday and Tuesday=12 | |
input bool WeekStartOverSW = true; //Trade everyday without stop | |
input int WeekStartHour = 8; //First Day Staring Time | |
input int WeekStopHour = 17; //Last Day Ending Time | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //Capital setting for Profit/Loss termination | |
input bool TakeProfitPercentSW = true; //Switch of Secondary Take Profit | |
input double TakeProfitPercent = 1; //Secondary Take Profit, max. % of profit to close trade, and reset | |
input bool TakeProfitPercentDaySW = true; //Switch of Daily Take Profit | |
input double TakeProfitPercentDay = 1000; //Daily Take Profit, max. % of profit to close trade, reset on the next day | |
input bool StopLossPercentSW = true; //Switch of stopping Floating Loss | |
input double StopLossPercent = 1000; //Stop Loss of Current Account Max. Floating Loss (Based on Balance) | |
input bool SleepLoss = true; //Stop trading when reaching stop loss the same day | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //Switch of Small Loss To Win | |
input int MaxTime = 150; //Max. Time Per Set (Minutes) | |
input int MaxIn = 10; //Max. Trades Per Set | |
input double MaxP = 0.5; //Criteria of Small Loss for Win | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //Pending Order at Price | |
input bool SingleOut = false; //Take Profit Single Out | |
#endif | |
input bool MartinSW = true; //Start Martingale | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //Number to start martingale multiplication | |
#endif | |
input double LotMultiplikator = 1.382; //Martingale Multiplier | |
input double Step = 30; //Martingale Distance | |
input int MaxTrades = 5; //Max. Number of Orders | |
input double MM = 1.0; //Fund Management % | |
input double Lots = 0.01; //Order Size_lot | |
input double TakeProfit = 10.0; //Take Profit | |
input double StopLoss = 10.0; //Stop Loss | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
string Newsfile = "ffcal_week_this.xml"; | |
string EventFile = "NewsEvent.csv"; | |
int OnInit() | |
{ | |
//--- | |
CheckOSVersion(); | |
//+------------------------------------------------------------------+ | |
//|Get Point By Symbol | | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
iLots = OpLots(); | |
ticket_time = TimeCurrent(); | |
//+------------------------------------------------------------------+ | |
//|Random Minute | | |
//+------------------------------------------------------------------+ | |
MathSrand(GetTickCount()); | |
DownloadMin = 1 + ((59 * MathRand()) / 32768); | |
#ifdef MODE_AREA51 | |
TimesStopConfig(); | |
//+------------------------------------------------------------------+ | |
//|Update Files | | |
//+------------------------------------------------------------------+ | |
iPending = Pending; | |
#endif | |
UpdateFiles(MODE_INIT); | |
//--- | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
GlobalVariableSet("DNSW",0); | |
DeleteObjects(); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
#ifdef MODE_BUY | |
if (NonStop==false) EndersGameModule(Symbol(),OP_BUY,id_1,EGFlag); | |
#endif | |
#ifdef MODE_SELL | |
if (NonStop==false) EndersGameModule(Symbol(),OP_SELL,id_1,EGFlag); | |
#endif | |
UpdateFiles(MODE_TICK); | |
if (!EAControl_Enders()) return; | |
#ifdef MODE_AREA51 | |
double TimesProfit = 0.0; | |
double TodayProfit = 0.0; | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return; | |
} | |
if (!CloseAllTrades()) return; | |
TimesStop(TimesProfit,TodayProfit); | |
Displayprofit(TimesProfit,TodayProfit); | |
CheckTimeIn(TimesProfit); | |
//+------------------------------------------------------------------+ | |
//|Shutdown EA | | |
//+------------------------------------------------------------------+ | |
if (!EAControl()) return; | |
BrakeSystemFun(); | |
#endif | |
#ifdef MODE_BUY | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_BUY,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_BUY); | |
Martin(OP_BUY); | |
} | |
#endif | |
#ifdef MODE_SELL | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_SELL,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_SELL); | |
Martin(OP_SELL); | |
} | |
#endif | |
} | |
//+------------------------------------------------------------------+ | |
//|Determine Martingale TakeProfi | | |
//+------------------------------------------------------------------+ | |
double Cal_TP(int Type,double iPrice,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double AllOrderLots = 0.0; | |
double TP_Goal; | |
double FirstOrderLots = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == Type && OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
FirstOrderLots = OrderLots(); | |
TotalLots += OrderLots(); | |
if (Type == OP_SELL) | |
TotalDisProduct += (iPrice - OrderOpenPrice()) / point * OrderLots(); | |
if (Type == OP_BUY) | |
TotalDisProduct += (OrderOpenPrice() - iPrice) / point * OrderLots(); | |
} | |
} | |
if (EqualZero(TotalLots)) return(0.0); | |
TP_Goal = ((TPPoint * FirstOrderLots) + TotalDisProduct) / TotalLots; | |
if (Type == OP_SELL) | |
return(iPrice - (TP_Goal * point)); | |
if (Type == OP_BUY) | |
return(iPrice + (TP_Goal * point)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|OrderSend Function | | |
//+------------------------------------------------------------------+ | |
int SendOrders(const int Type ,const string iComment) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double InPrice = 0.0; | |
switch (Type) | |
{ | |
case OP_BUY: | |
InPrice = Ask; | |
break; | |
case OP_SELL: | |
InPrice = Bid; | |
break; | |
} | |
double SendOrders_Lots = MartinLots(Type); | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
int Ticket = OrderSend(Symbol(),Type,SendOrders_Lots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,Type == OP_BUY ? clrLime : clrRed); | |
if (Ticket < 0) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(ErrorCode)); | |
} | |
return(Ticket); | |
} | |
void Set_SL_TP(const int Mode,const int Type,double iSL,double iTP) | |
{ | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++)//執行N次篩選(N=訂單總數) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1)//挑選出同貨幣對 && BUY單 && MagicNumber=本程式 的訂單 | |
{ | |
if (Mode == MODE_SLTP_BYPOINT) | |
{ | |
if (Type == OP_BUY || Type == OP_BUYSTOP || Type == OP_BUYLIMIT) | |
{ | |
TP = OrderOpenPrice() + iTP * point; | |
SL = OrderOpenPrice() - iSL * point; | |
} | |
if (Type == OP_SELL || Type == OP_SELLSTOP || Type == OP_SELLLIMIT) | |
{ | |
TP = OrderOpenPrice() - iTP * point; | |
SL = OrderOpenPrice() + iSL * point; | |
} | |
if (EqualZero(iTP)) TP = 0; | |
if (EqualZero(iSL)) SL = 0; | |
}else | |
{ | |
SL = iSL; | |
TP = iTP; | |
} | |
if (iTP < 0) | |
{ | |
TP = OrderTakeProfit(); | |
} | |
if (iSL < 0) | |
{ | |
SL = OrderStopLoss(); | |
} | |
if ((CompareDouble(OrderStopLoss(),NormalizeDouble(SL,Digits))) && (CompareDouble(OrderTakeProfit(),NormalizeDouble(TP,Digits)))) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_SL_TP " + IntegerToString(OrderTicket()) + " Error: " + ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= ZERO) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= ZERO && A >= -ZERO) return(true); | |
return(false); | |
} | |
void Martin(const int Type) | |
{ | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (MartinSW) | |
{ | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (BarTime == Bars && MyOrderTotal(Type) < MaxTrades) | |
{ | |
if (SendOrders(Type,"XBOMB" + IntegerToString(MyOrderTotal(Type) + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_MARTIN); | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_BUY && MyOrderTotal(OP_BUY) > 0) | |
{ | |
if (Ask <= MinPrice - Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_SELL && MyOrderTotal(OP_SELL) > 0) | |
{ | |
if (Bid >= MaxPrice + Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (MyOrderTotal(Type) > 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_MARTIN); | |
} | |
} | |
void FirstTrade(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
if (Type == OP_BUY && (MyOrderTotal(OP_BUYSTOP) > 0 || MyOrderTotal(OP_BUYLIMIT) > 0)) return; | |
if (Type == OP_SELL && (MyOrderTotal(OP_SELLSTOP) > 0 || MyOrderTotal(OP_SELLLIMIT) > 0)) return; | |
if (Cnt == 0 && FirstTradeBar != Time[0]) | |
{ | |
if (SendOrders(Type,"XBOMB(" + IntegerToString(Type) + ")." + IntegerToString(Cnt + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_FIRSTTRADE); | |
FirstTradeBar = Time[0]; | |
} | |
} | |
if (Cnt == 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_FIRSTTRADE); | |
} | |
double GetLastTciketINFO(const int Type,double &MaxPrice,double &MinPrice) | |
{ | |
double iMaxPrice = 0.0; | |
double iMinPrice = 5000.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol() && OrderType() == Type) | |
{ | |
if (OrderOpenPrice() >= iMaxPrice) | |
iMaxPrice = OrderOpenPrice(); | |
if (OrderOpenPrice() <= iMinPrice && !EqualZero(iMinPrice)) | |
iMinPrice = OrderOpenPrice(); | |
} | |
} | |
MaxPrice = iMaxPrice; | |
MinPrice = iMinPrice; | |
return(0.0); | |
} | |
int MyOrderTotal(int Type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL) | |
{ | |
x++; | |
continue; | |
} | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
void iCloseOrders(int type) | |
{ | |
int C0_cnt; | |
if (type == OP_BUYSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUYSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELLSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELLSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrLime)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
}else { | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selecttickets() | |
{ | |
int cnt_tickets = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)) | |
#endif | |
{ | |
ticket_time=OrderOpenTime(); | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selectticketsfrhistory() | |
{ | |
int cnt_tickets = 0; | |
for (int i=0;i<OrdersHistoryTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && ticket_time<=OrderOpenTime()) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && ticket_time<=OrderOpenTime()) | |
#endif | |
{ | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double MartinLots(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
#ifdef MODE_AREA51 | |
int StartM = MartinMStart; | |
if (StartM <= 0) StartM = 2; | |
#else | |
int StartM = 2; | |
#endif | |
if (Cnt <= 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,0),2)); | |
if (Cnt > 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,(double)(Cnt - 1) - (StartM - 2)),2)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|Optimize Lots | | |
//+------------------------------------------------------------------+ | |
double OpLots() | |
{ | |
double Out = Lots; | |
if (!EqualZero(MM) && MM > 0) | |
{ | |
Out = (AccountBalance() * MM ) / (1000 * TakeProfit); | |
} | |
return(Out); | |
} | |
#ifdef MODE_AREA51 | |
void PendingTrade(const int Type,const string iComment) | |
{ | |
double InPrice = Pending; | |
int Ticket = 0; | |
int iType = -1; | |
if (Type == OP_BUY) | |
{ | |
if (InPrice > Ask) | |
iType = OP_BUYSTOP; | |
if (InPrice < Ask) | |
iType = OP_BUYLIMIT; | |
if (MyOrderTotal(OP_BUYSTOP) >= 1 || MyOrderTotal(OP_BUYLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrLime); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (InPrice < Bid) | |
iType = OP_SELLSTOP; | |
if (InPrice > Bid) | |
iType = OP_SELLLIMIT; | |
if (MyOrderTotal(OP_SELLSTOP) >= 1 || MyOrderTotal(OP_SELLLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrRed); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
return; | |
} | |
bool CloseAllTrades() | |
{ | |
if (GlobalVariableGet(NextSW_str) == 1) | |
{ | |
#ifdef MODE_BUY | |
iCloseOrders(OP_BUY); | |
#endif | |
#ifdef MODE_SELL | |
iCloseOrders(OP_SELL); | |
#endif | |
Sleep(10000); | |
if (MyOrderTotal(OP_ALL) > 0) | |
{ | |
#ifdef CHT | |
Alert("水雷未掃除成功"); | |
#endif | |
#ifdef ENG | |
Alert("Submarine mine sweeper not completed"); | |
#endif | |
return(false); | |
}else | |
{ | |
Sleep((int)(NextTime * 1000 * 60)); | |
} | |
} | |
return(true); | |
} | |
void CheckTimeIn(const double TimesProfit) | |
{ | |
datetime RunTime = (TimeCurrent() - Start_P) / 60; | |
if (SmallLossWinSW && RunTime >= MaxTime && OrdersTotal() >= MaxIn && TimesProfit >= MaxP) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_CHECKTIMEIN,""); | |
} | |
} | |
void WriteLog(const int Mode,const string in) | |
{ | |
double TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
string str = WindowExpertName() + "," + Symbol() + "," + TimeToString(TimeCurrent(),TIME_DATE|TIME_DATE|TIME_SECONDS) + | |
": " + LogText[Mode] + " , " + in + | |
"," + Start_Balance_str + ": " + DoubleToString(Start_Balance,2) + | |
"," + Start_P_str + ": " + IntegerToString(Start_P) + | |
",TimeProfit: " + DoubleToString(TimesProfit,2) + | |
"| Set: 餘:" + DoubleToString(iBalance,2) + | |
",次:" + DoubleToString(TakeProfitPercent,2) + | |
",日:" + DoubleToString(TakeProfitPercentDay,2) + | |
",損:" + DoubleToStr(StopLossPercent ,2) + | |
",時:" + DoubleToString(MaxTime,2) + | |
",單:" + DoubleToString(MaxIn,2) + | |
",虧:" + DoubleToString(MaxP,2) + | |
"\n" | |
; | |
int h1 = FileOpen("Logs.txt",FILE_READ|FILE_WRITE|FILE_TXT); | |
if (h1 < 0) Print("Open Log Failed"); | |
FileSeek(h1,0,SEEK_END); | |
int byte = FileWriteString(h1,str,StringLen(str)); | |
if (byte <= 0) Print("Write Log Failed"); | |
FileClose(h1); | |
} | |
double GetProfitPeriod(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType() == OP_BUY || OrderType() == OP_SELL) | |
{ | |
if (today < OrderCloseTime()) | |
profit += OrderProfit() + OrderCommission() + OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
void Displayprofit(const double TimesProfit, const double TodayProfit) | |
{ | |
int Pos = 50; | |
string Label_Text = ""; | |
string Label_Text1 = ""; | |
if (ObjectFind(Run) < 0) ObjectCreate(0,Run,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Run, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Run, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Run, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Run, OBJPROP_YDISTANCE, Pos); | |
ObjectSetInteger(0,Run, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Run, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetString(0,Run, OBJPROP_FONT, "微軟正黑體"); | |
for (int i=0;i<6;i++) | |
{ | |
if(ObjectFind(Label_arr[i]) < 0) ObjectCreate(0,Label_arr[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_XDISTANCE,120); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr[i],OBJPROP_FONT,"微軟正黑體"); | |
if(ObjectFind(Label_arr1[i]) < 0) ObjectCreate(0,Label_arr1[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_FONT,"微軟正黑體"); | |
switch(i) | |
{ | |
#ifdef CHT | |
case 0: | |
Label_Text = "單次擄獲狂派戰俘: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "進場起始兵力: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "進場經過時間: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " 分鐘"; | |
break; | |
case 3: | |
Label_Text = "戰場兵力: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "今日擄獲狂派戰俘累積至: " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "最大暫時被擄穫兵力: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
#ifdef ENG | |
case 0: | |
Label_Text = "Profit/Loss: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "Start Balance: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "Time Elapsed: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " Minutes"; | |
break; | |
case 3: | |
Label_Text = "Orders: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "Profit/Loss (Today): " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "Maximum Floating Loss: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
} | |
ObjectSetString(0,Label_arr[i],OBJPROP_TEXT,Label_Text); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_TEXT,Label_Text1); | |
} | |
for (int i=0;i<ArraySize(NewsInfo);i++) | |
{ | |
if (ObjectFind(NewsInfo[i]) < 0) ObjectCreate(0,NewsInfo[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_YDISTANCE, Pos + 150 + i * 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,NewsInfo[i], OBJPROP_FONT, "微軟正黑體"); | |
} | |
ObjectCreate(0,Margin,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Margin, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Margin, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Margin, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Margin, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Margin, OBJPROP_YDISTANCE, Pos+195); | |
ObjectSetInteger(0,Margin, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,Margin, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
ObjectCreate(0,FloatLoss,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_YDISTANCE, Pos+215); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,FloatLoss, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
if (ObjectFind(Light) < 0) | |
ObjectCreate(0,Light,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Light,OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Light, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Light,OBJPROP_COLOR,clrGold); | |
ObjectSetInteger(0,Light, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Light,OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Light,OBJPROP_YDISTANCE,Pos+235); | |
ObjectSetString(0,Light, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Light,OBJPROP_TEXT,""); | |
} | |
bool EAControl() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!Margin()) | |
{ | |
str = "Margin"; | |
Out = false; | |
} | |
if (!Float()) | |
{ | |
str = "Float"; | |
Out = false; | |
} | |
if (TimeCurrent() <= EARunTime) | |
{ | |
str = "SLSLeep"; | |
Out = false; | |
} | |
if (!WeekStartOver()) | |
{ | |
str = "Time"; | |
Out = false; | |
} | |
if (!AvoidNews()) | |
{ | |
str = "News"; | |
Out = false; | |
} | |
/*if (!Margin() || !Float() || TimeCurrent() <= EARunTime || !WeekStartOver() || !AvoidNews()) | |
{ | |
Comment("EAControl 1"); | |
return(false); | |
}*/ | |
Comment(str); | |
return(Out); | |
} | |
bool iTimeControl() | |
{ | |
bool EA_Valid = false; | |
if (DayStopHour == DayStartHour) EA_Valid = true; | |
if (DayStopHour < DayStartHour) | |
{ | |
if (TimeHour(TimeLocal()) >= DayStartHour || TimeHour(TimeLocal()) < DayStopHour) | |
EA_Valid = true; | |
} | |
if (DayStopHour > DayStartHour) | |
{ | |
if ((TimeHour(TimeLocal()) >= DayStartHour && TimeHour(TimeLocal()) < DayStopHour)) | |
EA_Valid = true; | |
} | |
return(EA_Valid); | |
} | |
bool WeekStartOver() | |
{ | |
bool EA_Valid = true; | |
int Dayweek = TimeDayOfWeek(TimeLocal()); | |
if (WeekStartOverSW) | |
{ | |
if ( | |
Dayweek < StrToInteger(StringSubstr(WeekDay,0,1)) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,0,1)) && TimeHour(TimeLocal()) < WeekStartHour) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) && TimeHour(TimeLocal()) >= WeekStopHour ) || | |
Dayweek > StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) | |
) | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
if (iTimeControl()) | |
{ | |
if (StringFind(WeekDay,DoubleToStr(Dayweek,0),0) >= 0) | |
{ | |
EA_Valid = true; | |
}else | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
EA_Valid = false; | |
} | |
} | |
#ifdef CHT | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"未在設定時區內,現有戰役達成後將離開戰場"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"已進入作戰時區內,全面啟動執行..."); | |
return(true); | |
} | |
#endif | |
#ifdef ENG | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Not in trading time zone, stop trading after current set"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Entered trading time zone, automated system initiated"); | |
return(true); | |
} | |
#endif | |
} | |
int NewsConvert() | |
{ | |
string maindata[][7]; | |
string sBuffer = ""; | |
if (!FileIsExist(Newsfile)) | |
{ | |
return(0); | |
} | |
int h1=FileOpen(Newsfile,FILE_SHARE_READ|FILE_CSV); | |
int i = 0 , j = 0; | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(sBuffer,"<title>") >= 0) | |
{ | |
ArrayResize(maindata,j+1); | |
maindata[j][0] = sBuffer; | |
for (i=1;i<7;i++) | |
{ | |
maindata[j][i] = FileReadString(h1); | |
} | |
j++; | |
} | |
} | |
FileClose(h1); | |
int count = ArrayRange(maindata,0); | |
string sTags[7] = { "<title>", "<country>", "<date><![CDATA[", "<time><![CDATA[", "<impact><![CDATA[", "<forecast><![CDATA[", "<previous><![CDATA[" }; | |
string eTags[7] = { "</title>", "</country>", "]]></date>", "]]></time>", "]]></impact>", "]]></forecast>", "]]></previous>" }; | |
for (i=0;i<count;i++) | |
{ | |
for (j=0;j<7;j++) | |
{ | |
maindata[i][j]=StringSubstr(maindata[i][j],StringFind(maindata[i][j],sTags[j],0)+StringLen(sTags[j]),StringFind(maindata[i][j],eTags[j],0)-StringFind(maindata[i][j],sTags[j],0)-StringLen(sTags[j])); | |
} | |
} | |
int h2 = FileOpen("NewsEvent.csv",FILE_SHARE_READ|FILE_WRITE|FILE_CSV,';'); | |
int GMT = 0; | |
string time_str = ""; | |
string prev_time_str = ""; | |
for (i=0;i<count;i++) | |
{ | |
if (maindata[i][4] == "High") | |
{ | |
GMT = -1 * (TimeGMTOffset()/3600); | |
time_str = TimeToString( | |
StringToTime(MakeDateTime(maindata[i][2],maindata[i][3])) + GMT * 3600, | |
TIME_DATE|TIME_MINUTES|TIME_SECONDS | |
); | |
if (time_str == prev_time_str) continue; | |
FileWrite(h2,maindata[i][1],time_str); | |
prev_time_str = time_str; | |
} | |
} | |
FileClose(h2); | |
return(count); | |
} | |
string MakeDateTime(string strDate, string strTime) | |
{ | |
// Print("Converting Forex Factory Time into Metatrader time..."); //added by MN | |
// Converts forexfactory time & date into yyyy.mm.dd hh:mm | |
int n1stDash = StringFind(strDate, "-"); | |
int n2ndDash = StringFind(strDate, "-", n1stDash+1); | |
string strMonth = StringSubstr(strDate, 0, 2); | |
string strDay = StringSubstr(strDate, 3, 2); | |
string strYear = StringSubstr(strDate, 6, 4); | |
// strYear = "20" + strYear; | |
int nTimeColonPos = StringFind(strTime, ":"); | |
string strHour = StringSubstr(strTime, 0, nTimeColonPos); | |
string strMinute = StringSubstr(strTime, nTimeColonPos+1, 2); | |
string strAM_PM = StringSubstr(strTime, StringLen(strTime)-2); | |
int nHour24 = StrToInteger(strHour); | |
if ((strAM_PM == "pm" || strAM_PM == "PM") && nHour24 != 12) | |
{ | |
nHour24 += 12; | |
} | |
if ((strAM_PM == "am" || strAM_PM == "AM") && nHour24 == 12) | |
{ | |
nHour24 = 0; | |
} | |
string strHourPad = ""; | |
if (nHour24 < 10) | |
strHourPad = "0"; | |
return(StringConcatenate(strYear, ".", strMonth, ".", strDay, " ", strHourPad, nHour24, ":", strMinute)); | |
} | |
void RebuildNewsArray() | |
{ | |
NewsConvert(); | |
string sBuffer = ""; | |
int i = 0; | |
int h1 = FileOpen(EventFile,FILE_SHARE_READ|FILE_CSV); | |
if (h1 < 0) | |
{ | |
ArrayResize(AvoidTime,1); | |
ArrayResize(AvoidSymbol,1); | |
return; | |
} | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(Symbol(),sBuffer) >= 0) | |
{ | |
ArrayResize(AvoidSymbol,i+2); | |
AvoidSymbol[i+1] = sBuffer; | |
ArrayResize(AvoidTime,i+2); | |
sBuffer = FileReadString(h1); | |
AvoidTime[i+1] = StrToTime(sBuffer); | |
i++; | |
} | |
} | |
FileClose(h1); | |
} | |
bool AvoidNews() | |
{ | |
#ifdef CHT | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "預警雷達已偵測到 " + AvoidSymbol[i] + " 戰場於 " + StringSubstr(TimeToStr(AvoidTime[i]),11,5) + " 可能將有大量狂派埋伏"; | |
str[1] = "超級達摩戰艦於 " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5) + " 啟動防禦系統進入一級警戒"; | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "正在接收狂派情資..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "最後狂派活動時間: " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "已無其他狂派活動或情資已過期,即將休兵......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
#ifdef ENG | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "News Alert on " + AvoidSymbol[i] + " at time " + StringSubstr(TimeToStr(AvoidTime[i]),11,5); | |
str[1] = "Red alert! Braking System Standing By at " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5); | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "News retrieving..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "Latest News at : " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "No trading info available......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
} | |
bool Margin() | |
{ | |
if (MarginSleep) | |
{ | |
double Margin_R = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); | |
if (EqualZero(Margin_R)) return(true); | |
if (MarginLimit >= Margin_R) | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,"Margin level lower than " + DoubleToString(MarginLimit,2) + " automated system suspended"); | |
return(false); | |
}else | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool Float() | |
{ | |
if (FloatEq) | |
{ | |
double Profit = AccountProfit(); | |
if (Profit <= FloatAm) | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,"Floating loss greater than "+DoubleToString(FloatAm,2)+" automated system suspended"); | |
return(0); | |
}else | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool TimesStopConfig() | |
{ | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(NextSW_str,0); | |
#ifdef GLOBAL_FILE | |
if (!GlobalFileRead()) | |
{ | |
GlobalVars.G_EARunTime = 0; | |
GlobalVars.G_MaxEquity = (AccountProfit() / AccountBalance()) * 100; | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalFileWrite(); | |
GlobalFileRead(); | |
} | |
Print("EARuntime: " + TimeToString(GlobalVars.G_EARunTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("MaxLossEquity: " + DoubleToString(GlobalVars.G_MaxEquity,2)); | |
Print("Start_P: " + TimeToString(GlobalVars.G_Start_P,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("Start_Balance: " + DoubleToString(GlobalVars.G_Start_Balance,2)); | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
#endif | |
return(true); | |
} | |
void TimesStop(double &TimesProfit, double &TodayProfit) | |
{ | |
#ifdef GLOBAL_FILE | |
if (OrdersTotal()==0) | |
{ | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Status | | |
//+------------------------------------------------------------------+ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVars.G_Start_Balance; | |
else | |
Start_Balance = iBalance; | |
Start_P = GlobalVars.G_Start_P; | |
EARunTime = GlobalVars.G_EARunTime; | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVars.G_MaxEquity) | |
{ | |
GlobalVars.G_MaxEquity = (AccountProfit() / Start_Balance) * 100; | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Status | | |
//+------------------------------------------------------------------+ | |
GlobalVars.G_EARunTime = EARunTime; | |
GlobalVars.G_Start_Balance = Start_Balance; | |
GlobalVars.G_Start_P = Start_P; | |
GlobalFileWrite(); | |
#else | |
if (OrdersTotal()==0) | |
{ | |
GlobalVariableSet(Start_Balance_str,AccountBalance()); | |
GlobalVariableSet(Start_P_str,TimeCurrent()); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Balance | | |
//+------------------------------------------------------------------+ | |
if (GlobalVariableCheck(Start_Balance_str)) | |
{ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVariableGet(Start_Balance_str); | |
else | |
Start_Balance = iBalance; | |
}else | |
{ | |
Start_Balance = AccountBalance(); | |
} | |
if (GlobalVariableCheck(Start_P_str)) | |
{ | |
Start_P = (datetime)GlobalVariableGet(Start_P_str); | |
}else | |
{ | |
Start_P = TimeCurrent(); | |
} | |
if (GlobalVariableCheck(EARUNTIME_str)) | |
{ | |
EARunTime = (datetime)GlobalVariableGet(EARUNTIME_str); | |
} | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVariableGet(MaxEquity_str)) | |
{ | |
GlobalVariableSet(MaxEquity_str,(AccountProfit() / Start_Balance)*100); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Balnace | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(EARUNTIME_str,EARunTime); | |
GlobalVariableSet(Start_P_str,(double)Start_P); | |
GlobalVariableSet(Start_Balance_str,Start_Balance); | |
#endif | |
} | |
#endif | |
void DeleteObjects() | |
{ | |
for (int i=5;i>=0;i--) | |
{ | |
if(ObjectFind(Label_arr[i]) >= 0) ObjectDelete(0,Label_arr[i]); | |
if(ObjectFind(Label_arr1[i]) >= 0) ObjectDelete(0,Label_arr1[i]); | |
if (i < 2) | |
{ | |
if (ObjectFind(NewsInfo[i]) >= 0) ObjectDelete(0,NewsInfo[i]); | |
if (ObjectFind(Light_str[i]) >= 0) ObjectDelete(0,Light_str[i]); | |
} | |
} | |
if(ObjectFind(Run) >= 0) ObjectDelete(0,Run); | |
if(ObjectFind(FloatLoss) >= 0) ObjectDelete(FloatLoss); | |
if(ObjectFind(Margin) >= 0) ObjectDelete(Margin); | |
if(ObjectFind(Light) >= 0) ObjectDelete(Light); | |
//if (FileIsExist(EventFile)) FileDelete(EventFile); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
if (UninitializeReason() != 3) | |
{ | |
#ifdef MODE_AREA51 | |
if (ArraySize(AvoidTime) <= 1) NewGetSW = true; | |
else | |
{ | |
if (TimeLocal() >= AvoidTime[ArraySize(AvoidTime)-1] + MinAfter) | |
{ | |
NewGetSW = true; | |
} | |
} | |
#endif | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if ( | |
(TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) || | |
NewGetSW | |
) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
if (!NewGetSW) | |
{ | |
Sleep(1000); | |
} | |
Sleep(1000); | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
GlobalVariableSet("DNSW0",0); | |
NewGetSW = false; | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
void AutoRepairSLTP(const bool flag,const int Type,const int Mode) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (!flag) return; | |
int Cnt = MyOrderTotal(Type); | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (Mode == MODE_FIRSTTRADE) | |
{ | |
TP = TakeProfit; | |
SL = StopLoss; | |
if (MartinSW) | |
SL = 0.0; | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,SL,TP); | |
} | |
if (Mode == MODE_MARTIN) | |
{ | |
switch (Type) | |
{ | |
case OP_BUY: | |
TP = Cal_TP(OP_BUY,MinPrice,TakeProfit); | |
SL = 0; | |
break; | |
case OP_SELL: | |
TP = Cal_TP(OP_SELL,MaxPrice,TakeProfit); | |
SL = 0; | |
break; | |
} | |
if (Cnt > 1) | |
{ | |
#ifdef MODE_AREA51 | |
if (SingleOut) | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,0,TakeProfit); | |
else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#endif | |
} | |
} | |
} | |
string FileCharArrayToString(const string str_temp) | |
{ | |
uchar End_arr[]; | |
int j = 0; | |
for(int i=0;i<StringLen(str_temp);i+=2) | |
{ | |
ArrayResize(End_arr,j+1); | |
End_arr[j] = (char)StringToInteger(StringSubstr(str_temp,i,2)); | |
j++; | |
} | |
return(CharArrayToString(End_arr,0,WHOLE_ARRAY,CP_ACP)); | |
} | |
void CheckOSVersion() | |
{ | |
ulong dwVersion = GetVersion(); | |
uint dwMajorVersion = (uchar)(((ushort)(dwVersion & 0xffff)) & 0xff); | |
printf("Major Version Of OS: %d",dwMajorVersion); | |
if (dwMajorVersion < 6) | |
{ | |
#ifdef CHT | |
Alert("請注意!使用Windows XP(Server 2003)之系統將無法保證交易能正常執行!"); | |
#endif | |
#ifdef ENG | |
Alert("Attention! Not guarantee to run well in Windows XP(Server 2003)"); | |
#endif | |
} | |
} | |
void SetKbar(double& Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
#ifdef MODE_AREA51 | |
void BrakeSystemFun() | |
{ | |
int Light_Arrow[2] = {108,161}; | |
if (BrakeSystem) | |
{ | |
int Pos = 50; | |
double Position=20; | |
#ifdef CHT | |
string Active = "煞車系統啟動中"; | |
string Deactive = "煞車系統待命中"; | |
#endif | |
#ifdef ENG | |
string Active = "Braking System Initiating"; | |
string Deactive = "Braking System Stand By"; | |
#endif | |
double Position_L = Bid + Position * point; | |
for (int i=0;i<ArraySize(Light_str);i++) | |
{ | |
if (ObjectFind(Light_str[i]) < 0) | |
ObjectCreate(0,Light_str[i],OBJ_ARROW,0,0,0); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_ARROWCODE,Light_Arrow[i]); | |
if (i == 0) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLime); | |
if (i == 1) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_WIDTH,5); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSetDouble(0,Light_str[i],OBJPROP_PRICE1,Position_L); | |
} | |
int EnergyBall=Ball(Status,SleepStop,Last_Time,Last_Price,SleepTime,(int)TimeCurrent(),point,Energy,Close[0]); | |
if (EnergyBall<0) | |
{ | |
Alert("Energy Brake Error"); | |
TradeSW = false; | |
return; | |
} | |
if (TimeCurrent()<=SleepStop[0]) | |
{ | |
TradeSW = false; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrRed); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrCrimson); | |
#ifdef CHT | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" 分鐘後啟動"); | |
#endif | |
#ifdef ENG | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", initiate after"+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" minutes"); | |
#endif | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrLime); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetString(0,Light,OBJPROP_TEXT,Deactive); | |
} | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectDelete(Light_str[0]); | |
ObjectDelete(Light_str[1]); | |
} | |
} | |
#endif | |
bool Lots_Valid(const double iLot) | |
{ | |
double ValuePerLot = SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE); | |
if ((iLot * ValuePerLot) > AccountInfoDouble(ACCOUNT_FREEMARGIN)) return(false); | |
return(true); | |
} | |
int GetTicket(int &Ticket_arr[]) | |
{ | |
int Cnt = 0; | |
for (int i=0;i<ArraySize(Ticket_arr);i++) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (Ticket_arr[i] == OrderTicket()) | |
{ | |
Cnt++; | |
break; | |
} | |
} | |
} | |
return(Cnt); | |
} | |
int MyOrderTotal_Enders(const string symbol,const int Type,const int ID) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL && OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
continue; | |
} | |
if ((Type == OP_BUY && ((OrderType() & 1) == 1 ? false : true)) || (Type == OP_SELL && ((OrderType() & 1) == 1 ? true : false))) | |
{ | |
if (OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
} | |
} | |
} | |
return(x); | |
} | |
bool EAControl_Enders() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
Comment(str); | |
return(Out); | |
} | |
int EndersGamgeCnt = 0; | |
void EndersGameModule(const string symbol, const int Type, int id, bool &flag) | |
{ | |
if (MyOrderTotal_Enders(symbol,Type,id) == 0) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (OrderCloseTime() > EGStartTime) | |
{ | |
EndersGamgeCnt++; | |
Alert(Symbol() + " 戰場已完成第" + IntegerToString(EndersGamgeCnt) + " 次戰役"); | |
EGStartTime = TimeCurrent(); | |
break; | |
} | |
} | |
} | |
if (EndersGamgeCnt >= OutCnt) | |
{ | |
Alert("已達成設定局數,即將離開 " + Symbol() + " 戰場"); | |
flag = false; | |
ExpertRemove(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| AllNewXBOMB.mq4 | | |
//| APP Wargame-Hydra | | |
//| http://www.facebook.com/groups/moneywargame/ | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import "kernel32.dll" | |
ulong GetVersion(void); | |
#import | |
#define CHT 1 | |
//#define ENG 1 | |
#define ZERO 0.000001 | |
//#define MODE_BUY 1 | |
#define MODE_SELL 1 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_CHECKTIMEIN 0 | |
#define MODE_TIMESTP 1 | |
#define MODE_TIMESSL 2 | |
#define MODE_DAILYTP 3 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_FIRSTTRADE 99 | |
#define MODE_MARTIN 100 | |
#define OP_ALL 101 | |
#define MODE_SLTP_BYPOINT 102 | |
#define MODE_SLTP_BYPRICE 103 | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int id_1 = 3333333; | |
int ErrorCode = 0; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
int BarTime = 0; | |
double NextTime=0.5; | |
double point = 0.0; | |
double iLots = 0.0; | |
double Start_Balance = 0; | |
double iPending; | |
datetime H1BarTime = 0; | |
datetime FirstTradeBar = 0; | |
datetime Start_P = 0; | |
datetime EARunTime = 0; | |
datetime AvoidTime[1] = {0}; | |
datetime EGStartTime = TimeCurrent(); | |
bool EGFlag = true; | |
bool NewGetSW = false; | |
bool AuthorGetSW = false; | |
bool TradeSW = true; | |
int Last_Time[1]={0}; | |
int Status[1]={0}; | |
double Last_Price[1]={-1.0}; | |
double MinPriceShot = 0.0; | |
string AvoidSymbol[1] = {""}; | |
string LogText[4] = {"小虧為贏" , "次止盈" , "次止損" , "日止盈"}; | |
string Label_arr[6] = | |
{ | |
"TimesProfitPer", | |
"Start_Balance", | |
"TimePast", | |
"Orders", | |
"TodayProfitPer", | |
"MaxLoss" | |
}; | |
string Label_arr1[6] = | |
{ | |
"TimesProfitPer0", | |
"Start_Balance0", | |
"TimePast0", | |
"Orders0", | |
"TodayProfitPer0", | |
"MaxLoss0" | |
}; | |
string Start_P_str = "Start_P"; | |
string Start_Balance_str = "Start_Balance"; | |
string MaxEquity_str = "MaxEquity"; | |
string NextSW_str = "NextSW"; | |
string EARUNTIME_str = "EARUNTIME"; | |
string Light = "LightStatus"; | |
string Run = "RunStatus"; | |
string Margin = "Margin"; | |
string FloatLoss = "Float"; | |
string NewsInfo[2] = {"NewsInfo0", "NewsInfo1"}; | |
string Light_str[2] = {"Light","Light2"}; | |
#ifdef CHT | |
string NewsEngage = "狂派情資已接收完全.....全面開戰!"; | |
#endif | |
#ifdef ENG | |
string NewsEngage = "News retrieval completed .....automated system initiated"; | |
#endif | |
//+------------------------------------------------------------------+ | |
//|Ender's Game Par | | |
//+------------------------------------------------------------------+ | |
datetime ticket_time = 0; | |
int OutCntTemp = 0; | |
#ifdef CHT | |
input bool AutoRepairSW = false; //自動回復停損停利 | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //ESP戰艦穩定煞車系統 | |
int pSeconds=14; | |
input double Energy=4; //能量偵測靈敏度 | |
input int SleepTime=15; //能量引擎啟動時間(分鐘) | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //預警雷達控制模組 | |
input int MinBefore = 30; //前幾分鐘閃避 | |
input int MinAfter = 30; //後幾分鐘閃避 | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //保證金比例控制模組 | |
input double MarginLimit = 2000; //保證金低於%數暫停 | |
input bool FloatEq = false; //浮虧控制模組 | |
input double FloatAm = -4000; //虧損(-金額)暫停 | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //開始時間,格式為HH | |
input int DayStopHour = 00; //結束時間,格式為HH | |
input string WeekDay="12345"; //週一執行,鍵入1;週一週二鍵入12 | |
input bool WeekStartOverSW = true; //一個禮拜頭尾不間斷 | |
input int WeekStartHour = 8; //第一天幾點開始 | |
input int WeekStopHour = 17; //最後一天幾點結束 | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //止盈計算本金基準 | |
input bool TakeProfitPercentSW = true; //次止盈開關 | |
input double TakeProfitPercent = 5; //次止盈,每次最多獲利%,立即平倉歸零重啟新局 | |
input bool TakeProfitPercentDaySW = true; //日止盈開關 | |
input double TakeProfitPercentDay = 1000; //日止盈,每日最多獲利%,立即停止交易,隔日重新啟動 | |
input bool StopLossPercentSW = true; //浮虧止損開關 | |
input double StopLossPercent = 1000; //現時帳戶浮虧止損,止損基準值:帳戶餘額 | |
input bool SleepLoss = true; //止損後當日不再出兵 | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //小虧為贏開關 | |
input int MaxTime = 150; //每局最長時間(分鐘) | |
input int MaxIn = 10; //每局最多兵力 | |
input double MaxP = 0.5; //達成以上條件小虧為贏 | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //掛單價格 | |
input bool SingleOut = false; //單進單出 | |
#endif | |
input bool MartinSW = true; //馬丁開關 | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //第幾張馬丁開始加倍 | |
#endif | |
input double LotMultiplikator = 1.382; //馬丁倍率 | |
input double Step = 30; //馬丁間距 | |
input int MaxTrades = 5; //最多單數 | |
input double MM = 5.0; //資金管理%數 | |
input double Lots = 0.01; //手數 | |
input double TakeProfit = 10.0; //獲利點數 | |
input double StopLoss = 10.0; //停損點數 | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
#ifdef ENG | |
input bool AutoRepairSW = false; //Auto Repair SL&TP | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //BrakeSystem | |
int pSeconds=14; | |
input double Energy=4; //Braking System Sensitivity Control | |
input int SleepTime=15; //Braking System Sleeping Time | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //Braking System Sleeping Time | |
input int MinBefore = 30; //Stop trading few minutes before news alert | |
input int MinAfter = 30; //Stop trading few minutes after news alert | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //Margin Level Control Module | |
input double MarginLimit = 2000; //Margin level % limit to stop trading | |
input bool FloatEq = false; //Floating Loss Control Module | |
input double FloatAm = -4000; //Floating loss (-$amount) limit to stop trading | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //Starting Time, in HH format | |
input int DayStopHour = 00; //Ending Time, in HH format | |
input string WeekDay="12345"; //Star on Monday=1, Monday and Tuesday=12 | |
input bool WeekStartOverSW = true; //Trade everyday without stop | |
input int WeekStartHour = 8; //First Day Staring Time | |
input int WeekStopHour = 17; //Last Day Ending Time | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //Capital setting for Profit/Loss termination | |
input bool TakeProfitPercentSW = true; //Switch of Secondary Take Profit | |
input double TakeProfitPercent = 1; //Secondary Take Profit, max. % of profit to close trade, and reset | |
input bool TakeProfitPercentDaySW = true; //Switch of Daily Take Profit | |
input double TakeProfitPercentDay = 1000; //Daily Take Profit, max. % of profit to close trade, reset on the next day | |
input bool StopLossPercentSW = true; //Switch of stopping Floating Loss | |
input double StopLossPercent = 1000; //Stop Loss of Current Account Max. Floating Loss (Based on Balance) | |
input bool SleepLoss = true; //Stop trading when reaching stop loss the same day | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //Switch of Small Loss To Win | |
input int MaxTime = 150; //Max. Time Per Set (Minutes) | |
input int MaxIn = 10; //Max. Trades Per Set | |
input double MaxP = 0.5; //Criteria of Small Loss for Win | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //Pending Order at Price | |
input bool SingleOut = false; //Take Profit Single Out | |
#endif | |
input bool MartinSW = true; //Start Martingale | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //Number to start martingale multiplication | |
#endif | |
input double LotMultiplikator = 1.382; //Martingale Multiplier | |
input double Step = 30; //Martingale Distance | |
input int MaxTrades = 5; //Max. Number of Orders | |
input double MM = 5.0; //Fund Management % | |
input double Lots = 0.01; //Order Size_lot | |
input double TakeProfit = 10.0; //Take Profit | |
input double StopLoss = 10.0; //Stop Loss | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
string Newsfile = "ffcal_week_this.xml"; | |
string EventFile = "NewsEvent.csv"; | |
int OnInit() | |
{ | |
//--- | |
CheckOSVersion(); | |
//+------------------------------------------------------------------+ | |
//|Get Point By Symbol | | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
iLots = OpLots(); | |
ticket_time = TimeCurrent(); | |
//+------------------------------------------------------------------+ | |
//|Random Minute | | |
//+------------------------------------------------------------------+ | |
MathSrand(GetTickCount()); | |
DownloadMin = 1 + ((59 * MathRand()) / 32768); | |
#ifdef MODE_AREA51 | |
TimesStopConfig(); | |
//+------------------------------------------------------------------+ | |
//|Update Files | | |
//+------------------------------------------------------------------+ | |
iPending = Pending; | |
#endif | |
UpdateFiles(MODE_INIT); | |
//--- | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
GlobalVariableSet("DNSW",0); | |
DeleteObjects(); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
//--- | |
#ifdef MODE_BUY | |
if (NonStop==false) EndersGameModule(Symbol(),OP_BUY,id_1,EGFlag); | |
#endif | |
#ifdef MODE_SELL | |
if (NonStop==false) EndersGameModule(Symbol(),OP_SELL,id_1,EGFlag); | |
#endif | |
UpdateFiles(MODE_TICK); | |
if (!EAControl_Enders()) return; | |
#ifdef MODE_AREA51 | |
double TimesProfit = 0.0; | |
double TodayProfit = 0.0; | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return; | |
} | |
if (!CloseAllTrades()) return; | |
TimesStop(TimesProfit,TodayProfit); | |
Displayprofit(TimesProfit,TodayProfit); | |
CheckTimeIn(TimesProfit); | |
//+------------------------------------------------------------------+ | |
//|Shutdown EA | | |
//+------------------------------------------------------------------+ | |
if (!EAControl()) return; | |
BrakeSystemFun(); | |
#endif | |
#ifdef MODE_BUY | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_BUY,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_BUY); | |
Martin(OP_BUY); | |
} | |
#endif | |
#ifdef MODE_SELL | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_SELL,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_SELL); | |
Martin(OP_SELL); | |
} | |
#endif | |
} | |
//+------------------------------------------------------------------+ | |
//|Determine Martingale TakeProfi | | |
//+------------------------------------------------------------------+ | |
double Cal_TP(int Type,double iPrice,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double AllOrderLots = 0.0; | |
double TP_Goal; | |
double FirstOrderLots = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == Type && OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
FirstOrderLots = OrderLots(); | |
TotalLots += OrderLots(); | |
if (Type == OP_SELL) | |
TotalDisProduct += (iPrice - OrderOpenPrice()) / point * OrderLots(); | |
if (Type == OP_BUY) | |
TotalDisProduct += (OrderOpenPrice() - iPrice) / point * OrderLots(); | |
} | |
} | |
if (EqualZero(TotalLots)) return(0.0); | |
TP_Goal = ((TPPoint * FirstOrderLots) + TotalDisProduct) / TotalLots; | |
if (Type == OP_SELL) | |
return(iPrice - (TP_Goal * point)); | |
if (Type == OP_BUY) | |
return(iPrice + (TP_Goal * point)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|OrderSend Function | | |
//+------------------------------------------------------------------+ | |
int SendOrders(const int Type ,const string iComment) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double InPrice = 0.0; | |
switch (Type) | |
{ | |
case OP_BUY: | |
InPrice = Ask; | |
break; | |
case OP_SELL: | |
InPrice = Bid; | |
break; | |
} | |
double SendOrders_Lots = MartinLots(Type); | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
int Ticket = OrderSend(Symbol(),Type,SendOrders_Lots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,Type == OP_BUY ? clrLime : clrRed); | |
if (Ticket < 0) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(ErrorCode)); | |
} | |
return(Ticket); | |
} | |
void Set_SL_TP(const int Mode,const int Type,double iSL,double iTP) | |
{ | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++)//執行N次篩選(N=訂單總數) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1)//挑選出同貨幣對 && BUY單 && MagicNumber=本程式 的訂單 | |
{ | |
if (Mode == MODE_SLTP_BYPOINT) | |
{ | |
if (Type == OP_BUY || Type == OP_BUYSTOP || Type == OP_BUYLIMIT) | |
{ | |
TP = OrderOpenPrice() + iTP * point; | |
SL = OrderOpenPrice() - iSL * point; | |
} | |
if (Type == OP_SELL || Type == OP_SELLSTOP || Type == OP_SELLLIMIT) | |
{ | |
TP = OrderOpenPrice() - iTP * point; | |
SL = OrderOpenPrice() + iSL * point; | |
} | |
if (EqualZero(iTP)) TP = 0; | |
if (EqualZero(iSL)) SL = 0; | |
}else | |
{ | |
SL = iSL; | |
TP = iTP; | |
} | |
if (iTP < 0) | |
{ | |
TP = OrderTakeProfit(); | |
} | |
if (iSL < 0) | |
{ | |
SL = OrderStopLoss(); | |
} | |
if ((CompareDouble(OrderStopLoss(),NormalizeDouble(SL,Digits))) && (CompareDouble(OrderTakeProfit(),NormalizeDouble(TP,Digits)))) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_SL_TP " + IntegerToString(OrderTicket()) + " Error: " + ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= ZERO) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= ZERO && A >= -ZERO) return(true); | |
return(false); | |
} | |
void Martin(const int Type) | |
{ | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (MartinSW) | |
{ | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (BarTime == Bars && MyOrderTotal(Type) < MaxTrades) | |
{ | |
if (SendOrders(Type,"XBOMB" + IntegerToString(MyOrderTotal(Type) + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_MARTIN); | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_BUY && MyOrderTotal(OP_BUY) > 0) | |
{ | |
if (Ask <= MinPrice - Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_SELL && MyOrderTotal(OP_SELL) > 0) | |
{ | |
if (Bid >= MaxPrice + Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (MyOrderTotal(Type) > 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_MARTIN); | |
} | |
} | |
void FirstTrade(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
if (Type == OP_BUY && (MyOrderTotal(OP_BUYSTOP) > 0 || MyOrderTotal(OP_BUYLIMIT) > 0)) return; | |
if (Type == OP_SELL && (MyOrderTotal(OP_SELLSTOP) > 0 || MyOrderTotal(OP_SELLLIMIT) > 0)) return; | |
if (Cnt == 0 && FirstTradeBar != Time[0]) | |
{ | |
if (SendOrders(Type,"XBOMB(" + IntegerToString(Type) + ")." + IntegerToString(Cnt + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_FIRSTTRADE); | |
FirstTradeBar = Time[0]; | |
} | |
} | |
if (Cnt == 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_FIRSTTRADE); | |
} | |
double GetLastTciketINFO(const int Type,double &MaxPrice,double &MinPrice) | |
{ | |
double iMaxPrice = 0.0; | |
double iMinPrice = 5000.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol() && OrderType() == Type) | |
{ | |
if (OrderOpenPrice() >= iMaxPrice) | |
iMaxPrice = OrderOpenPrice(); | |
if (OrderOpenPrice() <= iMinPrice && !EqualZero(iMinPrice)) | |
iMinPrice = OrderOpenPrice(); | |
} | |
} | |
MaxPrice = iMaxPrice; | |
MinPrice = iMinPrice; | |
return(0.0); | |
} | |
int MyOrderTotal(int Type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL) | |
{ | |
x++; | |
continue; | |
} | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
void iCloseOrders(int type) | |
{ | |
int C0_cnt; | |
if (type == OP_BUYSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUYSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELLSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELLSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrLime)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
}else { | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selecttickets() | |
{ | |
int cnt_tickets = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)) | |
#endif | |
{ | |
ticket_time=OrderOpenTime(); | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selectticketsfrhistory() | |
{ | |
int cnt_tickets = 0; | |
for (int i=0;i<OrdersHistoryTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && ticket_time<=OrderOpenTime()) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && ticket_time<=OrderOpenTime()) | |
#endif | |
{ | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double MartinLots(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
#ifdef MODE_AREA51 | |
int StartM = MartinMStart; | |
if (StartM <= 0) StartM = 2; | |
#else | |
int StartM = 2; | |
#endif | |
if (Cnt <= 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,0),2)); | |
if (Cnt > 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,(double)(Cnt - 1) - (StartM - 2)),2)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|Optimize Lots | | |
//+------------------------------------------------------------------+ | |
double OpLots() | |
{ | |
double Out = Lots; | |
if (!EqualZero(MM) && MM > 0) | |
{ | |
Out = (AccountBalance() * MM ) / (1000 * TakeProfit); | |
} | |
return(Out); | |
} | |
#ifdef MODE_AREA51 | |
void PendingTrade(const int Type,const string iComment) | |
{ | |
double InPrice = Pending; | |
int Ticket = 0; | |
int iType = -1; | |
if (Type == OP_BUY) | |
{ | |
if (InPrice > Ask) | |
iType = OP_BUYSTOP; | |
if (InPrice < Ask) | |
iType = OP_BUYLIMIT; | |
if (MyOrderTotal(OP_BUYSTOP) >= 1 || MyOrderTotal(OP_BUYLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrLime); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (InPrice < Bid) | |
iType = OP_SELLSTOP; | |
if (InPrice > Bid) | |
iType = OP_SELLLIMIT; | |
if (MyOrderTotal(OP_SELLSTOP) >= 1 || MyOrderTotal(OP_SELLLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrRed); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
return; | |
} | |
bool CloseAllTrades() | |
{ | |
if (GlobalVariableGet(NextSW_str) == 1) | |
{ | |
#ifdef MODE_BUY | |
iCloseOrders(OP_BUY); | |
#endif | |
#ifdef MODE_SELL | |
iCloseOrders(OP_SELL); | |
#endif | |
Sleep(10000); | |
if (MyOrderTotal(OP_ALL) > 0) | |
{ | |
#ifdef CHT | |
Alert("水雷未掃除成功"); | |
#endif | |
#ifdef ENG | |
Alert("Submarine mine sweeper not completed"); | |
#endif | |
return(false); | |
}else | |
{ | |
Sleep((int)(NextTime * 1000 * 60)); | |
} | |
} | |
return(true); | |
} | |
void CheckTimeIn(const double TimesProfit) | |
{ | |
datetime RunTime = (TimeCurrent() - Start_P) / 60; | |
if (SmallLossWinSW && RunTime >= MaxTime && OrdersTotal() >= MaxIn && TimesProfit >= MaxP) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_CHECKTIMEIN,""); | |
} | |
} | |
void WriteLog(const int Mode,const string in) | |
{ | |
double TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
string str = WindowExpertName() + "," + Symbol() + "," + TimeToString(TimeCurrent(),TIME_DATE|TIME_DATE|TIME_SECONDS) + | |
": " + LogText[Mode] + " , " + in + | |
"," + Start_Balance_str + ": " + DoubleToString(Start_Balance,2) + | |
"," + Start_P_str + ": " + IntegerToString(Start_P) + | |
",TimeProfit: " + DoubleToString(TimesProfit,2) + | |
"| Set: 餘:" + DoubleToString(iBalance,2) + | |
",次:" + DoubleToString(TakeProfitPercent,2) + | |
",日:" + DoubleToString(TakeProfitPercentDay,2) + | |
",損:" + DoubleToStr(StopLossPercent ,2) + | |
",時:" + DoubleToString(MaxTime,2) + | |
",單:" + DoubleToString(MaxIn,2) + | |
",虧:" + DoubleToString(MaxP,2) + | |
"\n" | |
; | |
int h1 = FileOpen("Logs.txt",FILE_READ|FILE_WRITE|FILE_TXT); | |
if (h1 < 0) Print("Open Log Failed"); | |
FileSeek(h1,0,SEEK_END); | |
int byte = FileWriteString(h1,str,StringLen(str)); | |
if (byte <= 0) Print("Write Log Failed"); | |
FileClose(h1); | |
} | |
double GetProfitPeriod(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType() == OP_BUY || OrderType() == OP_SELL) | |
{ | |
if (today < OrderCloseTime()) | |
profit += OrderProfit() + OrderCommission() + OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
void Displayprofit(const double TimesProfit, const double TodayProfit) | |
{ | |
int Pos = 50; | |
string Label_Text = ""; | |
string Label_Text1 = ""; | |
if (ObjectFind(Run) < 0) ObjectCreate(0,Run,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Run, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Run, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Run, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Run, OBJPROP_YDISTANCE, Pos); | |
ObjectSetInteger(0,Run, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Run, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetString(0,Run, OBJPROP_FONT, "微軟正黑體"); | |
for (int i=0;i<6;i++) | |
{ | |
if(ObjectFind(Label_arr[i]) < 0) ObjectCreate(0,Label_arr[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_XDISTANCE,120); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr[i],OBJPROP_FONT,"微軟正黑體"); | |
if(ObjectFind(Label_arr1[i]) < 0) ObjectCreate(0,Label_arr1[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_FONT,"微軟正黑體"); | |
switch(i) | |
{ | |
#ifdef CHT | |
case 0: | |
Label_Text = "單次擄獲狂派戰俘: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "進場起始兵力: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "進場經過時間: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " 分鐘"; | |
break; | |
case 3: | |
Label_Text = "戰場兵力: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "今日擄獲狂派戰俘累積至: " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "最大暫時被擄穫兵力: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
#ifdef ENG | |
case 0: | |
Label_Text = "Profit/Loss: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "Start Balance: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "Time Elapsed: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " Minutes"; | |
break; | |
case 3: | |
Label_Text = "Orders: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "Profit/Loss (Today): " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "Maximum Floating Loss: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
} | |
ObjectSetString(0,Label_arr[i],OBJPROP_TEXT,Label_Text); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_TEXT,Label_Text1); | |
} | |
for (int i=0;i<ArraySize(NewsInfo);i++) | |
{ | |
if (ObjectFind(NewsInfo[i]) < 0) ObjectCreate(0,NewsInfo[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_YDISTANCE, Pos + 150 + i * 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,NewsInfo[i], OBJPROP_FONT, "微軟正黑體"); | |
} | |
ObjectCreate(0,Margin,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Margin, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Margin, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Margin, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Margin, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Margin, OBJPROP_YDISTANCE, Pos+195); | |
ObjectSetInteger(0,Margin, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,Margin, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
ObjectCreate(0,FloatLoss,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_YDISTANCE, Pos+215); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,FloatLoss, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
if (ObjectFind(Light) < 0) | |
ObjectCreate(0,Light,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Light,OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Light, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Light,OBJPROP_COLOR,clrGold); | |
ObjectSetInteger(0,Light, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Light,OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Light,OBJPROP_YDISTANCE,Pos+235); | |
ObjectSetString(0,Light, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Light,OBJPROP_TEXT,""); | |
} | |
bool EAControl() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!Margin()) | |
{ | |
str = "Margin"; | |
Out = false; | |
} | |
if (!Float()) | |
{ | |
str = "Float"; | |
Out = false; | |
} | |
if (TimeCurrent() <= EARunTime) | |
{ | |
str = "SLSLeep"; | |
Out = false; | |
} | |
if (!WeekStartOver()) | |
{ | |
str = "Time"; | |
Out = false; | |
} | |
if (!AvoidNews()) | |
{ | |
str = "News"; | |
Out = false; | |
} | |
/*if (!Margin() || !Float() || TimeCurrent() <= EARunTime || !WeekStartOver() || !AvoidNews()) | |
{ | |
Comment("EAControl 1"); | |
return(false); | |
}*/ | |
Comment(str); | |
return(Out); | |
} | |
bool iTimeControl() | |
{ | |
bool EA_Valid = false; | |
if (DayStopHour == DayStartHour) EA_Valid = true; | |
if (DayStopHour < DayStartHour) | |
{ | |
if (TimeHour(TimeLocal()) >= DayStartHour || TimeHour(TimeLocal()) < DayStopHour) | |
EA_Valid = true; | |
} | |
if (DayStopHour > DayStartHour) | |
{ | |
if ((TimeHour(TimeLocal()) >= DayStartHour && TimeHour(TimeLocal()) < DayStopHour)) | |
EA_Valid = true; | |
} | |
return(EA_Valid); | |
} | |
bool WeekStartOver() | |
{ | |
bool EA_Valid = true; | |
int Dayweek = TimeDayOfWeek(TimeLocal()); | |
if (WeekStartOverSW) | |
{ | |
if ( | |
Dayweek < StrToInteger(StringSubstr(WeekDay,0,1)) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,0,1)) && TimeHour(TimeLocal()) < WeekStartHour) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) && TimeHour(TimeLocal()) >= WeekStopHour ) || | |
Dayweek > StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) | |
) | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
if (iTimeControl()) | |
{ | |
if (StringFind(WeekDay,DoubleToStr(Dayweek,0),0) >= 0) | |
{ | |
EA_Valid = true; | |
}else | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
EA_Valid = false; | |
} | |
} | |
#ifdef CHT | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"未在設定時區內,現有戰役達成後將離開戰場"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"已進入作戰時區內,全面啟動執行..."); | |
return(true); | |
} | |
#endif | |
#ifdef ENG | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Not in trading time zone, stop trading after current set"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Entered trading time zone, automated system initiated"); | |
return(true); | |
} | |
#endif | |
} | |
int NewsConvert() | |
{ | |
string maindata[][7]; | |
string sBuffer = ""; | |
if (!FileIsExist(Newsfile)) | |
{ | |
return(0); | |
} | |
int h1=FileOpen(Newsfile,FILE_SHARE_READ|FILE_CSV); | |
int i = 0 , j = 0; | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(sBuffer,"<title>") >= 0) | |
{ | |
ArrayResize(maindata,j+1); | |
maindata[j][0] = sBuffer; | |
for (i=1;i<7;i++) | |
{ | |
maindata[j][i] = FileReadString(h1); | |
} | |
j++; | |
} | |
} | |
FileClose(h1); | |
int count = ArrayRange(maindata,0); | |
string sTags[7] = { "<title>", "<country>", "<date><![CDATA[", "<time><![CDATA[", "<impact><![CDATA[", "<forecast><![CDATA[", "<previous><![CDATA[" }; | |
string eTags[7] = { "</title>", "</country>", "]]></date>", "]]></time>", "]]></impact>", "]]></forecast>", "]]></previous>" }; | |
for (i=0;i<count;i++) | |
{ | |
for (j=0;j<7;j++) | |
{ | |
maindata[i][j]=StringSubstr(maindata[i][j],StringFind(maindata[i][j],sTags[j],0)+StringLen(sTags[j]),StringFind(maindata[i][j],eTags[j],0)-StringFind(maindata[i][j],sTags[j],0)-StringLen(sTags[j])); | |
} | |
} | |
int h2 = FileOpen("NewsEvent.csv",FILE_SHARE_READ|FILE_WRITE|FILE_CSV,';'); | |
int GMT = 0; | |
string time_str = ""; | |
string prev_time_str = ""; | |
for (i=0;i<count;i++) | |
{ | |
if (maindata[i][4] == "High") | |
{ | |
GMT = -1 * (TimeGMTOffset()/3600); | |
time_str = TimeToString( | |
StringToTime(MakeDateTime(maindata[i][2],maindata[i][3])) + GMT * 3600, | |
TIME_DATE|TIME_MINUTES|TIME_SECONDS | |
); | |
if (time_str == prev_time_str) continue; | |
FileWrite(h2,maindata[i][1],time_str); | |
prev_time_str = time_str; | |
} | |
} | |
FileClose(h2); | |
return(count); | |
} | |
string MakeDateTime(string strDate, string strTime) | |
{ | |
// Print("Converting Forex Factory Time into Metatrader time..."); //added by MN | |
// Converts forexfactory time & date into yyyy.mm.dd hh:mm | |
int n1stDash = StringFind(strDate, "-"); | |
int n2ndDash = StringFind(strDate, "-", n1stDash+1); | |
string strMonth = StringSubstr(strDate, 0, 2); | |
string strDay = StringSubstr(strDate, 3, 2); | |
string strYear = StringSubstr(strDate, 6, 4); | |
// strYear = "20" + strYear; | |
int nTimeColonPos = StringFind(strTime, ":"); | |
string strHour = StringSubstr(strTime, 0, nTimeColonPos); | |
string strMinute = StringSubstr(strTime, nTimeColonPos+1, 2); | |
string strAM_PM = StringSubstr(strTime, StringLen(strTime)-2); | |
int nHour24 = StrToInteger(strHour); | |
if ((strAM_PM == "pm" || strAM_PM == "PM") && nHour24 != 12) | |
{ | |
nHour24 += 12; | |
} | |
if ((strAM_PM == "am" || strAM_PM == "AM") && nHour24 == 12) | |
{ | |
nHour24 = 0; | |
} | |
string strHourPad = ""; | |
if (nHour24 < 10) | |
strHourPad = "0"; | |
return(StringConcatenate(strYear, ".", strMonth, ".", strDay, " ", strHourPad, nHour24, ":", strMinute)); | |
} | |
void RebuildNewsArray() | |
{ | |
NewsConvert(); | |
string sBuffer = ""; | |
int i = 0; | |
int h1 = FileOpen(EventFile,FILE_SHARE_READ|FILE_CSV); | |
if (h1 < 0) | |
{ | |
ArrayResize(AvoidTime,1); | |
ArrayResize(AvoidSymbol,1); | |
return; | |
} | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(Symbol(),sBuffer) >= 0) | |
{ | |
ArrayResize(AvoidSymbol,i+2); | |
AvoidSymbol[i+1] = sBuffer; | |
ArrayResize(AvoidTime,i+2); | |
sBuffer = FileReadString(h1); | |
AvoidTime[i+1] = StrToTime(sBuffer); | |
i++; | |
} | |
} | |
FileClose(h1); | |
} | |
bool AvoidNews() | |
{ | |
#ifdef CHT | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "預警雷達已偵測到 " + AvoidSymbol[i] + " 戰場於 " + StringSubstr(TimeToStr(AvoidTime[i]),11,5) + " 可能將有大量狂派埋伏"; | |
str[1] = "超級達摩戰艦於 " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5) + " 啟動防禦系統進入一級警戒"; | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "正在接收狂派情資..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "最後狂派活動時間: " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "已無其他狂派活動或情資已過期,即將休兵......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
#ifdef ENG | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "News Alert on " + AvoidSymbol[i] + " at time " + StringSubstr(TimeToStr(AvoidTime[i]),11,5); | |
str[1] = "Red alert! Braking System Standing By at " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5); | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "News retrieving..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "Latest News at : " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "No trading info available......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
} | |
bool Margin() | |
{ | |
if (MarginSleep) | |
{ | |
double Margin_R = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); | |
if (EqualZero(Margin_R)) return(true); | |
if (MarginLimit >= Margin_R) | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,"Margin level lower than " + DoubleToString(MarginLimit,2) + " automated system suspended"); | |
return(false); | |
}else | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool Float() | |
{ | |
if (FloatEq) | |
{ | |
double Profit = AccountProfit(); | |
if (Profit <= FloatAm) | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,"Floating loss greater than "+DoubleToString(FloatAm,2)+" automated system suspended"); | |
return(0); | |
}else | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool TimesStopConfig() | |
{ | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(NextSW_str,0); | |
#ifdef GLOBAL_FILE | |
if (!GlobalFileRead()) | |
{ | |
GlobalVars.G_EARunTime = 0; | |
GlobalVars.G_MaxEquity = (AccountProfit() / AccountBalance()) * 100; | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalFileWrite(); | |
GlobalFileRead(); | |
} | |
Print("EARuntime: " + TimeToString(GlobalVars.G_EARunTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("MaxLossEquity: " + DoubleToString(GlobalVars.G_MaxEquity,2)); | |
Print("Start_P: " + TimeToString(GlobalVars.G_Start_P,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("Start_Balance: " + DoubleToString(GlobalVars.G_Start_Balance,2)); | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
#endif | |
return(true); | |
} | |
void TimesStop(double &TimesProfit, double &TodayProfit) | |
{ | |
#ifdef GLOBAL_FILE | |
if (OrdersTotal()==0) | |
{ | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Status | | |
//+------------------------------------------------------------------+ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVars.G_Start_Balance; | |
else | |
Start_Balance = iBalance; | |
Start_P = GlobalVars.G_Start_P; | |
EARunTime = GlobalVars.G_EARunTime; | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVars.G_MaxEquity) | |
{ | |
GlobalVars.G_MaxEquity = (AccountProfit() / Start_Balance) * 100; | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Status | | |
//+------------------------------------------------------------------+ | |
GlobalVars.G_EARunTime = EARunTime; | |
GlobalVars.G_Start_Balance = Start_Balance; | |
GlobalVars.G_Start_P = Start_P; | |
GlobalFileWrite(); | |
#else | |
if (OrdersTotal()==0) | |
{ | |
GlobalVariableSet(Start_Balance_str,AccountBalance()); | |
GlobalVariableSet(Start_P_str,TimeCurrent()); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Balance | | |
//+------------------------------------------------------------------+ | |
if (GlobalVariableCheck(Start_Balance_str)) | |
{ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVariableGet(Start_Balance_str); | |
else | |
Start_Balance = iBalance; | |
}else | |
{ | |
Start_Balance = AccountBalance(); | |
} | |
if (GlobalVariableCheck(Start_P_str)) | |
{ | |
Start_P = (datetime)GlobalVariableGet(Start_P_str); | |
}else | |
{ | |
Start_P = TimeCurrent(); | |
} | |
if (GlobalVariableCheck(EARUNTIME_str)) | |
{ | |
EARunTime = (datetime)GlobalVariableGet(EARUNTIME_str); | |
} | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVariableGet(MaxEquity_str)) | |
{ | |
GlobalVariableSet(MaxEquity_str,(AccountProfit() / Start_Balance)*100); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Balnace | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(EARUNTIME_str,EARunTime); | |
GlobalVariableSet(Start_P_str,(double)Start_P); | |
GlobalVariableSet(Start_Balance_str,Start_Balance); | |
#endif | |
} | |
#endif | |
void DeleteObjects() | |
{ | |
for (int i=5;i>=0;i--) | |
{ | |
if(ObjectFind(Label_arr[i]) >= 0) ObjectDelete(0,Label_arr[i]); | |
if(ObjectFind(Label_arr1[i]) >= 0) ObjectDelete(0,Label_arr1[i]); | |
if (i < 2) | |
{ | |
if (ObjectFind(NewsInfo[i]) >= 0) ObjectDelete(0,NewsInfo[i]); | |
if (ObjectFind(Light_str[i]) >= 0) ObjectDelete(0,Light_str[i]); | |
} | |
} | |
if(ObjectFind(Run) >= 0) ObjectDelete(0,Run); | |
if(ObjectFind(FloatLoss) >= 0) ObjectDelete(FloatLoss); | |
if(ObjectFind(Margin) >= 0) ObjectDelete(Margin); | |
if(ObjectFind(Light) >= 0) ObjectDelete(Light); | |
//if (FileIsExist(EventFile)) FileDelete(EventFile); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
if (UninitializeReason() != 3) | |
{ | |
#ifdef MODE_AREA51 | |
if (ArraySize(AvoidTime) <= 1) NewGetSW = true; | |
else | |
{ | |
if (TimeLocal() >= AvoidTime[ArraySize(AvoidTime)-1] + MinAfter) | |
{ | |
NewGetSW = true; | |
} | |
} | |
#endif | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if ( | |
(TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) || | |
NewGetSW | |
) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
if (!NewGetSW) | |
{ | |
Sleep(1000); | |
} | |
Sleep(1000); | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
GlobalVariableSet("DNSW0",0); | |
NewGetSW = false; | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
void AutoRepairSLTP(const bool flag,const int Type,const int Mode) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (!flag) return; | |
int Cnt = MyOrderTotal(Type); | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (Mode == MODE_FIRSTTRADE) | |
{ | |
TP = TakeProfit; | |
SL = StopLoss; | |
if (MartinSW) | |
SL = 0.0; | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,SL,TP); | |
} | |
if (Mode == MODE_MARTIN) | |
{ | |
switch (Type) | |
{ | |
case OP_BUY: | |
TP = Cal_TP(OP_BUY,MinPrice,TakeProfit); | |
SL = 0; | |
break; | |
case OP_SELL: | |
TP = Cal_TP(OP_SELL,MaxPrice,TakeProfit); | |
SL = 0; | |
break; | |
} | |
if (Cnt > 1) | |
{ | |
#ifdef MODE_AREA51 | |
if (SingleOut) | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,0,TakeProfit); | |
else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#endif | |
} | |
} | |
} | |
string FileCharArrayToString(const string str_temp) | |
{ | |
uchar End_arr[]; | |
int j = 0; | |
for(int i=0;i<StringLen(str_temp);i+=2) | |
{ | |
ArrayResize(End_arr,j+1); | |
End_arr[j] = (char)StringToInteger(StringSubstr(str_temp,i,2)); | |
j++; | |
} | |
return(CharArrayToString(End_arr,0,WHOLE_ARRAY,CP_ACP)); | |
} | |
void CheckOSVersion() | |
{ | |
ulong dwVersion = GetVersion(); | |
uint dwMajorVersion = (uchar)(((ushort)(dwVersion & 0xffff)) & 0xff); | |
printf("Major Version Of OS: %d",dwMajorVersion); | |
if (dwMajorVersion < 6) | |
{ | |
#ifdef CHT | |
Alert("請注意!使用Windows XP(Server 2003)之系統將無法保證交易能正常執行!"); | |
#endif | |
#ifdef ENG | |
Alert("Attention! Not guarantee to run well in Windows XP(Server 2003)"); | |
#endif | |
} | |
} | |
void SetKbar(double& Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
#ifdef MODE_AREA51 | |
void BrakeSystemFun() | |
{ | |
int Light_Arrow[2] = {108,161}; | |
if (BrakeSystem) | |
{ | |
int Pos = 50; | |
double Position=20; | |
#ifdef CHT | |
string Active = "煞車系統啟動中"; | |
string Deactive = "煞車系統待命中"; | |
#endif | |
#ifdef ENG | |
string Active = "Braking System Initiating"; | |
string Deactive = "Braking System Stand By"; | |
#endif | |
double Position_L = Bid + Position * point; | |
for (int i=0;i<ArraySize(Light_str);i++) | |
{ | |
if (ObjectFind(Light_str[i]) < 0) | |
ObjectCreate(0,Light_str[i],OBJ_ARROW,0,0,0); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_ARROWCODE,Light_Arrow[i]); | |
if (i == 0) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLime); | |
if (i == 1) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_WIDTH,5); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSetDouble(0,Light_str[i],OBJPROP_PRICE1,Position_L); | |
} | |
int EnergyBall=Ball(Status,SleepStop,Last_Time,Last_Price,SleepTime,(int)TimeCurrent(),point,Energy,Close[0]); | |
if (EnergyBall<0) | |
{ | |
Alert("Energy Brake Error"); | |
TradeSW = false; | |
return; | |
} | |
if (TimeCurrent()<=SleepStop[0]) | |
{ | |
TradeSW = false; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrRed); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrCrimson); | |
#ifdef CHT | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" 分鐘後啟動"); | |
#endif | |
#ifdef ENG | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", initiate after"+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" minutes"); | |
#endif | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrLime); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetString(0,Light,OBJPROP_TEXT,Deactive); | |
} | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectDelete(Light_str[0]); | |
ObjectDelete(Light_str[1]); | |
} | |
} | |
#endif | |
bool Lots_Valid(const double iLot) | |
{ | |
double ValuePerLot = SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE); | |
if ((iLot * ValuePerLot) > AccountInfoDouble(ACCOUNT_FREEMARGIN)) return(false); | |
return(true); | |
} | |
int GetTicket(int &Ticket_arr[]) | |
{ | |
int Cnt = 0; | |
for (int i=0;i<ArraySize(Ticket_arr);i++) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (Ticket_arr[i] == OrderTicket()) | |
{ | |
Cnt++; | |
break; | |
} | |
} | |
} | |
return(Cnt); | |
} | |
int MyOrderTotal_Enders(const string symbol,const int Type,const int ID) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL && OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
continue; | |
} | |
if ((Type == OP_BUY && ((OrderType() & 1) == 1 ? false : true)) || (Type == OP_SELL && ((OrderType() & 1) == 1 ? true : false))) | |
{ | |
if (OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
} | |
} | |
} | |
return(x); | |
} | |
bool EAControl_Enders() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
Comment(str); | |
return(Out); | |
} | |
int EndersGamgeCnt = 0; | |
void EndersGameModule(const string symbol, const int Type, int id, bool &flag) | |
{ | |
if (MyOrderTotal_Enders(symbol,Type,id) == 0) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (OrderCloseTime() > EGStartTime) | |
{ | |
EndersGamgeCnt++; | |
Alert(Symbol() + " 戰場已完成第" + IntegerToString(EndersGamgeCnt) + " 次戰役"); | |
EGStartTime = TimeCurrent(); | |
break; | |
} | |
} | |
} | |
if (EndersGamgeCnt >= OutCnt) | |
{ | |
Alert("已達成設定局數,即將離開 " + Symbol() + " 戰場"); | |
flag = false; | |
ExpertRemove(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| AllNewXBOMB.mq4 | | |
//| APP Wargame-Hydra | | |
//| http://www.facebook.com/groups/moneywargame/ | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import "kernel32.dll" | |
ulong GetVersion(void); | |
#import | |
#define CHT 1 | |
//#define ENG 1 | |
#define ZERO 0.000001 | |
//#define MODE_BUY 1 | |
#define MODE_SELL 1 | |
#define MODE_AREA51 1 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_CHECKTIMEIN 0 | |
#define MODE_TIMESTP 1 | |
#define MODE_TIMESSL 2 | |
#define MODE_DAILYTP 3 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_FIRSTTRADE 99 | |
#define MODE_MARTIN 100 | |
#define OP_ALL 101 | |
#define MODE_SLTP_BYPOINT 102 | |
#define MODE_SLTP_BYPRICE 103 | |
//+------------------------------------------------------------------+ | |
//| Expert initialization function | | |
//+------------------------------------------------------------------+ | |
int id_1 = 3333333; | |
int ErrorCode = 0; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
int BarTime = 0; | |
double NextTime=0.5; | |
double point = 0.0; | |
double iLots = 0.0; | |
double Start_Balance = 0; | |
double iPending; | |
datetime H1BarTime = 0; | |
datetime FirstTradeBar = 0; | |
datetime Start_P = 0; | |
datetime EARunTime = 0; | |
datetime AvoidTime[1] = {0}; | |
datetime EGStartTime = TimeCurrent(); | |
bool EGFlag = true; | |
bool NewGetSW = false; | |
bool AuthorGetSW = false; | |
bool TradeSW = true; | |
int Last_Time[1]={0}; | |
int Status[1]={0}; | |
double Last_Price[1]={-1.0}; | |
double MinPriceShot = 0.0; | |
string AvoidSymbol[1] = {""}; | |
string LogText[4] = {"小虧為贏" , "次止盈" , "次止損" , "日止盈"}; | |
string Label_arr[6] = | |
{ | |
"TimesProfitPer", | |
"Start_Balance", | |
"TimePast", | |
"Orders", | |
"TodayProfitPer", | |
"MaxLoss" | |
}; | |
string Label_arr1[6] = | |
{ | |
"TimesProfitPer0", | |
"Start_Balance0", | |
"TimePast0", | |
"Orders0", | |
"TodayProfitPer0", | |
"MaxLoss0" | |
}; | |
string Start_P_str = "Start_P"; | |
string Start_Balance_str = "Start_Balance"; | |
string MaxEquity_str = "MaxEquity"; | |
string NextSW_str = "NextSW"; | |
string EARUNTIME_str = "EARUNTIME"; | |
string Light = "LightStatus"; | |
string Run = "RunStatus"; | |
string Margin = "Margin"; | |
string FloatLoss = "Float"; | |
string NewsInfo[2] = {"NewsInfo0", "NewsInfo1"}; | |
string Light_str[2] = {"Light","Light2"}; | |
#ifdef CHT | |
string NewsEngage = "狂派情資已接收完全.....全面開戰!"; | |
#endif | |
#ifdef ENG | |
string NewsEngage = "News retrieval completed .....automated system initiated"; | |
#endif | |
//+------------------------------------------------------------------+ | |
//|Ender's Game Par | | |
//+------------------------------------------------------------------+ | |
datetime ticket_time = 0; | |
int OutCntTemp = 0; | |
#ifdef CHT | |
input bool AutoRepairSW = false; //自動回復停損停利 | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //ESP戰艦穩定煞車系統 | |
int pSeconds=14; | |
input double Energy=4; //能量偵測靈敏度 | |
input int SleepTime=15; //能量引擎啟動時間(分鐘) | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //預警雷達控制模組 | |
input int MinBefore = 30; //前幾分鐘閃避 | |
input int MinAfter = 30; //後幾分鐘閃避 | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //保證金比例控制模組 | |
input double MarginLimit = 2000; //保證金低於%數暫停 | |
input bool FloatEq = false; //浮虧控制模組 | |
input double FloatAm = -4000; //虧損(-金額)暫停 | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //開始時間,格式為HH | |
input int DayStopHour = 00; //結束時間,格式為HH | |
input string WeekDay="12345"; //週一執行,鍵入1;週一週二鍵入12 | |
input bool WeekStartOverSW = true; //一個禮拜頭尾不間斷 | |
input int WeekStartHour = 8; //第一天幾點開始 | |
input int WeekStopHour = 17; //最後一天幾點結束 | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //止盈計算本金基準 | |
input bool TakeProfitPercentSW = true; //次止盈開關 | |
input double TakeProfitPercent = 1; //次止盈,每次最多獲利%,立即平倉歸零重啟新局 | |
input bool TakeProfitPercentDaySW = true; //日止盈開關 | |
input double TakeProfitPercentDay = 1000; //日止盈,每日最多獲利%,立即停止交易,隔日重新啟動 | |
input bool StopLossPercentSW = true; //浮虧止損開關 | |
input double StopLossPercent = 1000; //現時帳戶浮虧止損,止損基準值:帳戶餘額 | |
input bool SleepLoss = true; //止損後當日不再出兵 | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //小虧為贏開關 | |
input int MaxTime = 150; //每局最長時間(分鐘) | |
input int MaxIn = 10; //每局最多兵力 | |
input double MaxP = 0.5; //達成以上條件小虧為贏 | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //掛單價格 | |
input bool SingleOut = false; //單進單出 | |
#endif | |
input bool MartinSW = true; //馬丁開關 | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //第幾張馬丁開始加倍 | |
#endif | |
input double LotMultiplikator = 1.382; //馬丁倍率 | |
input double Step = 30; //馬丁間距 | |
input int MaxTrades = 5; //最多單數 | |
input double MM = 0.0; //資金管理%數 | |
input double Lots = 0.01; //手數 | |
input double TakeProfit = 10.0; //獲利點數 | |
input double StopLoss = 10.0; //停損點數 | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
#ifdef ENG | |
input bool AutoRepairSW = false; //Auto Repair SL&TP | |
#ifdef MODE_AREA51 | |
input bool BrakeSystem=true; //BrakeSystem | |
int pSeconds=14; | |
input double Energy=4; //Braking System Sensitivity Control | |
input int SleepTime=15; //Braking System Sleeping Time | |
int SleepStop[1]={0}; | |
input string a2 = "-------------------"; // | |
input bool NewsSW = true; //Braking System Sleeping Time | |
input int MinBefore = 30; //Stop trading few minutes before news alert | |
input int MinAfter = 30; //Stop trading few minutes after news alert | |
input string a3 = "-------------------"; // | |
input bool MarginSleep=false; //Margin Level Control Module | |
input double MarginLimit = 2000; //Margin level % limit to stop trading | |
input bool FloatEq = false; //Floating Loss Control Module | |
input double FloatAm = -4000; //Floating loss (-$amount) limit to stop trading | |
input string a4 = "-------------------"; // | |
input int DayStartHour = 00; //Starting Time, in HH format | |
input int DayStopHour = 00; //Ending Time, in HH format | |
input string WeekDay="12345"; //Star on Monday=1, Monday and Tuesday=12 | |
input bool WeekStartOverSW = true; //Trade everyday without stop | |
input int WeekStartHour = 8; //First Day Staring Time | |
input int WeekStopHour = 17; //Last Day Ending Time | |
input string a5 = "-------------------"; // | |
input double iBalance = 0; //Capital setting for Profit/Loss termination | |
input bool TakeProfitPercentSW = true; //Switch of Secondary Take Profit | |
input double TakeProfitPercent = 1; //Secondary Take Profit, max. % of profit to close trade, and reset | |
input bool TakeProfitPercentDaySW = true; //Switch of Daily Take Profit | |
input double TakeProfitPercentDay = 1000; //Daily Take Profit, max. % of profit to close trade, reset on the next day | |
input bool StopLossPercentSW = true; //Switch of stopping Floating Loss | |
input double StopLossPercent = 1000; //Stop Loss of Current Account Max. Floating Loss (Based on Balance) | |
input bool SleepLoss = true; //Stop trading when reaching stop loss the same day | |
input string b = "-------------------"; // | |
input bool SmallLossWinSW = true; //Switch of Small Loss To Win | |
input int MaxTime = 150; //Max. Time Per Set (Minutes) | |
input int MaxIn = 10; //Max. Trades Per Set | |
input double MaxP = 0.5; //Criteria of Small Loss for Win | |
input string c = "-------------------"; // | |
input double Pending = 0.0; //Pending Order at Price | |
input bool SingleOut = false; //Take Profit Single Out | |
#endif | |
input bool MartinSW = true; //Start Martingale | |
#ifdef MODE_AREA51 | |
input int MartinMStart = 2; //Number to start martingale multiplication | |
#endif | |
input double LotMultiplikator = 1.382; //Martingale Multiplier | |
input double Step = 30; //Martingale Distance | |
input int MaxTrades = 5; //Max. Number of Orders | |
input double MM = 0.0; //Fund Management % | |
input double Lots = 0.01; //Order Size_lot | |
input double TakeProfit = 10.0; //Take Profit | |
input double StopLoss = 10.0; //Stop Loss | |
input bool NonStop = false; //Ender's Game | |
input int OutCnt=1; | |
#endif | |
string Newsfile = "ffcal_week_this.xml"; | |
string EventFile = "NewsEvent.csv"; | |
int OnInit() | |
{ | |
//--- | |
CheckOSVersion(); | |
//+------------------------------------------------------------------+ | |
//|Get Point By Symbol | | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
iLots = OpLots(); | |
ticket_time = TimeCurrent(); | |
//+------------------------------------------------------------------+ | |
//|Random Minute | | |
//+------------------------------------------------------------------+ | |
MathSrand(GetTickCount()); | |
DownloadMin = 1 + ((59 * MathRand()) / 32768); | |
#ifdef MODE_AREA51 | |
TimesStopConfig(); | |
//+------------------------------------------------------------------+ | |
//|Update Files | | |
//+------------------------------------------------------------------+ | |
iPending = Pending; | |
#endif | |
UpdateFiles(MODE_INIT); | |
//--- | |
return(INIT_SUCCEEDED); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert deinitialization function | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
GlobalVariableSet("DNSW",0); | |
DeleteObjects(); | |
} | |
//+------------------------------------------------------------------+ | |
//| Expert tick function | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
//--- | |
UpdateFiles(MODE_TICK); | |
#ifdef MODE_AREA51 | |
double TimesProfit = 0.0; | |
double TodayProfit = 0.0; | |
if (!CloseAllTrades()) return; | |
if (!TimesStop(TimesProfit,TodayProfit)) return; | |
Displayprofit(TimesProfit,TodayProfit); | |
CheckTimeIn(TimesProfit); | |
#endif | |
#ifdef MODE_BUY | |
if (NonStop==false) EndersGameModule(Symbol(),OP_BUY,id_1,EGFlag); | |
#endif | |
#ifdef MODE_SELL | |
if (NonStop==false) EndersGameModule(Symbol(),OP_SELL,id_1,EGFlag); | |
#endif | |
#ifdef MODE_AREA51 | |
//+------------------------------------------------------------------+ | |
//|Shutdown EA | | |
//+------------------------------------------------------------------+ | |
if (!EAControl()) return; | |
BrakeSystemFun(); | |
#endif | |
#ifdef MODE_BUY | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_BUY,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_BUY); | |
Martin(OP_BUY); | |
} | |
#endif | |
#ifdef MODE_SELL | |
#ifdef MODE_AREA51 | |
int PendingTicket = 0; | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_SELL,Symbol() + "Pending"); | |
//do pend once | |
iPending = 0; | |
} | |
#endif | |
if (TradeSW) | |
{ | |
FirstTrade(OP_SELL); | |
Martin(OP_SELL); | |
} | |
#endif | |
} | |
//+------------------------------------------------------------------+ | |
//|Determine Martingale TakeProfi | | |
//+------------------------------------------------------------------+ | |
double Cal_TP(int Type,double iPrice,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double AllOrderLots = 0.0; | |
double TP_Goal; | |
double FirstOrderLots = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == Type && OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
FirstOrderLots = OrderLots(); | |
TotalLots += OrderLots(); | |
if (Type == OP_SELL) | |
TotalDisProduct += (iPrice - OrderOpenPrice()) / point * OrderLots(); | |
if (Type == OP_BUY) | |
TotalDisProduct += (OrderOpenPrice() - iPrice) / point * OrderLots(); | |
} | |
} | |
if (EqualZero(TotalLots)) return(0.0); | |
TP_Goal = ((TPPoint * FirstOrderLots) + TotalDisProduct) / TotalLots; | |
if (Type == OP_SELL) | |
return(iPrice - (TP_Goal * point)); | |
if (Type == OP_BUY) | |
return(iPrice + (TP_Goal * point)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|OrderSend Function | | |
//+------------------------------------------------------------------+ | |
int SendOrders(const int Type ,const string iComment) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double InPrice = 0.0; | |
switch (Type) | |
{ | |
case OP_BUY: | |
InPrice = Ask; | |
break; | |
case OP_SELL: | |
InPrice = Bid; | |
break; | |
} | |
double SendOrders_Lots = MartinLots(Type); | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
int Ticket = OrderSend(Symbol(),Type,SendOrders_Lots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,Type == OP_BUY ? clrLime : clrRed); | |
if (Ticket < 0) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(ErrorCode)); | |
} | |
return(Ticket); | |
} | |
void Set_SL_TP(const int Mode,const int Type,double iSL,double iTP) | |
{ | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++)//執行N次篩選(N=訂單總數) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1)//挑選出同貨幣對 && BUY單 && MagicNumber=本程式 的訂單 | |
{ | |
if (Mode == MODE_SLTP_BYPOINT) | |
{ | |
if (Type == OP_BUY || Type == OP_BUYSTOP || Type == OP_BUYLIMIT) | |
{ | |
TP = OrderOpenPrice() + iTP * point; | |
SL = OrderOpenPrice() - iSL * point; | |
} | |
if (Type == OP_SELL || Type == OP_SELLSTOP || Type == OP_SELLLIMIT) | |
{ | |
TP = OrderOpenPrice() - iTP * point; | |
SL = OrderOpenPrice() + iSL * point; | |
} | |
if (EqualZero(iTP)) TP = 0; | |
if (EqualZero(iSL)) SL = 0; | |
}else | |
{ | |
SL = iSL; | |
TP = iTP; | |
} | |
if (iTP < 0) | |
{ | |
TP = OrderTakeProfit(); | |
} | |
if (iSL < 0) | |
{ | |
SL = OrderStopLoss(); | |
} | |
if ((CompareDouble(OrderStopLoss(),NormalizeDouble(SL,Digits))) && (CompareDouble(OrderTakeProfit(),NormalizeDouble(TP,Digits)))) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_SL_TP " + IntegerToString(OrderTicket()) + " Error: " + ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= ZERO) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= ZERO && A >= -ZERO) return(true); | |
return(false); | |
} | |
void Martin(const int Type) | |
{ | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (MartinSW) | |
{ | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (BarTime == Bars && MyOrderTotal(Type) < MaxTrades) | |
{ | |
if (SendOrders(Type,"XBOMB" + IntegerToString(MyOrderTotal(Type) + 1))) | |
{ | |
AutoRepairSLTP(true,Type,MODE_MARTIN); | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_BUY && MyOrderTotal(OP_BUY) > 0) | |
{ | |
if (Ask <= MinPrice - Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (Type == OP_SELL && MyOrderTotal(OP_SELL) > 0) | |
{ | |
if (Bid >= MaxPrice + Step * point) | |
{ | |
BarTime = Bars + 1; | |
}else | |
{ | |
BarTime = 0; | |
} | |
} | |
if (MyOrderTotal(Type) > 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_MARTIN); | |
} | |
} | |
void FirstTrade(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
if (Type == OP_BUY && (MyOrderTotal(OP_BUYSTOP) > 0 || MyOrderTotal(OP_BUYLIMIT) > 0)) return; | |
if (Type == OP_SELL && (MyOrderTotal(OP_SELLSTOP) > 0 || MyOrderTotal(OP_SELLLIMIT) > 0)) return; | |
if (Cnt == 0 && FirstTradeBar != Time[0]) | |
{ | |
if (SendOrders(Type,"XBOMB(" + IntegerToString(Type) + ")." + IntegerToString(Cnt + 1)) > 0) | |
{ | |
AutoRepairSLTP(true,Type,MODE_FIRSTTRADE); | |
FirstTradeBar = Time[0]; | |
} | |
} | |
if (Cnt == 1) AutoRepairSLTP(AutoRepairSW,Type,MODE_FIRSTTRADE); | |
} | |
double GetLastTciketINFO(const int Type,double &MaxPrice,double &MinPrice) | |
{ | |
double iMaxPrice = 0.0; | |
double iMinPrice = 5000.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol() && OrderType() == Type) | |
{ | |
if (OrderOpenPrice() >= iMaxPrice) | |
iMaxPrice = OrderOpenPrice(); | |
if (OrderOpenPrice() <= iMinPrice && !EqualZero(iMinPrice)) | |
iMinPrice = OrderOpenPrice(); | |
} | |
} | |
MaxPrice = iMaxPrice; | |
MinPrice = iMinPrice; | |
return(0.0); | |
} | |
int MyOrderTotal(int Type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL) | |
{ | |
x++; | |
continue; | |
} | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
void iCloseOrders(int type) | |
{ | |
int C0_cnt; | |
if (type == OP_BUYSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUYSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELLSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELLSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrLime)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
}else { | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selecttickets() | |
{ | |
int cnt_tickets = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)) | |
#endif | |
{ | |
ticket_time=OrderOpenTime(); | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selectticketsfrhistory() | |
{ | |
int cnt_tickets = 0; | |
for (int i=0;i<OrdersHistoryTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && ticket_time<=OrderOpenTime()) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && ticket_time<=OrderOpenTime()) | |
#endif | |
{ | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double MartinLots(const int Type) | |
{ | |
int Cnt = MyOrderTotal(Type); | |
#ifdef MODE_AREA51 | |
int StartM = MartinMStart; | |
if (StartM <= 0) StartM = 2; | |
#else | |
int StartM = 2; | |
#endif | |
if (Cnt <= 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,0),2)); | |
if (Cnt > 0) | |
return(NormalizeDouble(iLots * MathPow(LotMultiplikator,(double)(Cnt - 1) - (StartM - 2)),2)); | |
return(0.0); | |
} | |
//+------------------------------------------------------------------+ | |
//|Optimize Lots | | |
//+------------------------------------------------------------------+ | |
double OpLots() | |
{ | |
double Out = Lots; | |
if (!EqualZero(MM) && MM > 0) | |
{ | |
Out = (AccountBalance() * MM ) / (1000 * TakeProfit); | |
} | |
return(Out); | |
} | |
#ifdef MODE_AREA51 | |
void PendingTrade(const int Type,const string iComment) | |
{ | |
double InPrice = Pending; | |
int Ticket = 0; | |
int iType = -1; | |
if (Type == OP_BUY) | |
{ | |
if (InPrice > Ask) | |
iType = OP_BUYSTOP; | |
if (InPrice < Ask) | |
iType = OP_BUYLIMIT; | |
if (MyOrderTotal(OP_BUYSTOP) >= 1 || MyOrderTotal(OP_BUYLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrLime); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (InPrice < Bid) | |
iType = OP_SELLSTOP; | |
if (InPrice > Bid) | |
iType = OP_SELLLIMIT; | |
if (MyOrderTotal(OP_SELLSTOP) >= 1 || MyOrderTotal(OP_SELLLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,MartinLots(Type),InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrRed); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
} | |
if (MartinSW) | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,0,TakeProfit); | |
} | |
else | |
{ | |
if (MyOrderTotal(iType) > 0) Set_SL_TP(MODE_SLTP_BYPOINT,iType,StopLoss,TakeProfit); | |
} | |
} | |
return; | |
} | |
bool CloseAllTrades() | |
{ | |
if (GlobalVariableGet(NextSW_str) == 1) | |
{ | |
#ifdef MODE_BUY | |
iCloseOrders(OP_BUY); | |
#endif | |
#ifdef MODE_SELL | |
iCloseOrders(OP_SELL); | |
#endif | |
Sleep(10000); | |
if (MyOrderTotal(OP_ALL) > 0) | |
{ | |
#ifdef CHT | |
Alert("水雷未掃除成功"); | |
#endif | |
#ifdef ENG | |
Alert("Submarine mine sweeper not completed"); | |
#endif | |
return(false); | |
}else | |
{ | |
Sleep((int)(NextTime * 1000 * 60)); | |
} | |
} | |
return(true); | |
} | |
void CheckTimeIn(const double TimesProfit) | |
{ | |
datetime RunTime = (TimeCurrent() - Start_P) / 60; | |
if (SmallLossWinSW && RunTime >= MaxTime && OrdersTotal() >= MaxIn && TimesProfit >= MaxP) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_CHECKTIMEIN,""); | |
} | |
} | |
void WriteLog(const int Mode,const string in) | |
{ | |
double TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
string str = WindowExpertName() + "," + Symbol() + "," + TimeToString(TimeCurrent(),TIME_DATE|TIME_DATE|TIME_SECONDS) + | |
": " + LogText[Mode] + " , " + in + | |
"," + Start_Balance_str + ": " + DoubleToString(Start_Balance,2) + | |
"," + Start_P_str + ": " + IntegerToString(Start_P) + | |
",TimeProfit: " + DoubleToString(TimesProfit,2) + | |
"| Set: 餘:" + DoubleToString(iBalance,2) + | |
",次:" + DoubleToString(TakeProfitPercent,2) + | |
",日:" + DoubleToString(TakeProfitPercentDay,2) + | |
",損:" + DoubleToStr(StopLossPercent ,2) + | |
",時:" + DoubleToString(MaxTime,2) + | |
",單:" + DoubleToString(MaxIn,2) + | |
",虧:" + DoubleToString(MaxP,2) + | |
"\n" | |
; | |
int h1 = FileOpen("Logs.txt",FILE_READ|FILE_WRITE|FILE_TXT); | |
if (h1 < 0) Print("Open Log Failed"); | |
FileSeek(h1,0,SEEK_END); | |
int byte = FileWriteString(h1,str,StringLen(str)); | |
if (byte <= 0) Print("Write Log Failed"); | |
FileClose(h1); | |
} | |
double GetProfitPeriod(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType() == OP_BUY || OrderType() == OP_SELL) | |
{ | |
if (today < OrderCloseTime()) | |
profit += OrderProfit() + OrderCommission() + OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
void Displayprofit(const double TimesProfit, const double TodayProfit) | |
{ | |
int Pos = 50; | |
string Label_Text = ""; | |
string Label_Text1 = ""; | |
if (ObjectFind(Run) < 0) ObjectCreate(0,Run,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Run, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Run, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Run, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Run, OBJPROP_YDISTANCE, Pos); | |
ObjectSetInteger(0,Run, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Run, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetString(0,Run, OBJPROP_FONT, "微軟正黑體"); | |
for (int i=0;i<6;i++) | |
{ | |
if(ObjectFind(Label_arr[i]) < 0) ObjectCreate(0,Label_arr[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_XDISTANCE,120); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr[i],OBJPROP_FONT,"微軟正黑體"); | |
if(ObjectFind(Label_arr1[i]) < 0) ObjectCreate(0,Label_arr1[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_FONT,"微軟正黑體"); | |
switch(i) | |
{ | |
#ifdef CHT | |
case 0: | |
Label_Text = "單次擄獲狂派戰俘: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "進場起始兵力: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "進場經過時間: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " 分鐘"; | |
break; | |
case 3: | |
Label_Text = "戰場兵力: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "今日擄獲狂派戰俘累積至: " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "最大暫時被擄穫兵力: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
#ifdef ENG | |
case 0: | |
Label_Text = "Profit/Loss: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "Start Balance: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "Time Elapsed: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " Minutes"; | |
break; | |
case 3: | |
Label_Text = "Orders: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "Profit/Loss (Today): " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "Maximum Floating Loss: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
} | |
ObjectSetString(0,Label_arr[i],OBJPROP_TEXT,Label_Text); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_TEXT,Label_Text1); | |
} | |
for (int i=0;i<ArraySize(NewsInfo);i++) | |
{ | |
if (ObjectFind(NewsInfo[i]) < 0) ObjectCreate(0,NewsInfo[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_YDISTANCE, Pos + 150 + i * 20); | |
ObjectSetInteger(0,NewsInfo[i], OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,NewsInfo[i], OBJPROP_FONT, "微軟正黑體"); | |
} | |
ObjectCreate(0,Margin,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Margin, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Margin, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Margin, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Margin, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Margin, OBJPROP_YDISTANCE, Pos+195); | |
ObjectSetInteger(0,Margin, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,Margin, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
ObjectCreate(0,FloatLoss,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_YDISTANCE, Pos+215); | |
ObjectSetInteger(0,FloatLoss, OBJPROP_FONTSIZE, 10); | |
ObjectSetString(0,FloatLoss, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
if (ObjectFind(Light) < 0) | |
ObjectCreate(0,Light,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Light,OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Light, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetInteger(0,Light,OBJPROP_COLOR,clrGold); | |
ObjectSetInteger(0,Light, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Light,OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Light,OBJPROP_YDISTANCE,Pos+235); | |
ObjectSetString(0,Light, OBJPROP_FONT, "微軟正黑體"); | |
ObjectSetString(0,Light,OBJPROP_TEXT,""); | |
} | |
bool EAControl() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!Margin()) | |
{ | |
str = "Margin"; | |
Out = false; | |
} | |
if (!Float()) | |
{ | |
str = "Float"; | |
Out = false; | |
} | |
if (TimeCurrent() <= EARunTime) | |
{ | |
str = "SLSLeep"; | |
Out = false; | |
} | |
if (!WeekStartOver()) | |
{ | |
str = "Time"; | |
Out = false; | |
} | |
if (!AvoidNews()) | |
{ | |
str = "News"; | |
Out = false; | |
} | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
/*if (!Margin() || !Float() || TimeCurrent() <= EARunTime || !WeekStartOver() || !AvoidNews()) | |
{ | |
Comment("EAControl 1"); | |
return(false); | |
}*/ | |
Comment(str); | |
return(Out); | |
} | |
bool iTimeControl() | |
{ | |
bool EA_Valid = false; | |
if (DayStopHour == DayStartHour) EA_Valid = true; | |
if (DayStopHour < DayStartHour) | |
{ | |
if (TimeHour(TimeLocal()) >= DayStartHour || TimeHour(TimeLocal()) < DayStopHour) | |
EA_Valid = true; | |
} | |
if (DayStopHour > DayStartHour) | |
{ | |
if ((TimeHour(TimeLocal()) >= DayStartHour && TimeHour(TimeLocal()) < DayStopHour)) | |
EA_Valid = true; | |
} | |
return(EA_Valid); | |
} | |
bool WeekStartOver() | |
{ | |
bool EA_Valid = true; | |
int Dayweek = TimeDayOfWeek(TimeLocal()); | |
if (WeekStartOverSW) | |
{ | |
if ( | |
Dayweek < StrToInteger(StringSubstr(WeekDay,0,1)) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,0,1)) && TimeHour(TimeLocal()) < WeekStartHour) || | |
(Dayweek == StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) && TimeHour(TimeLocal()) >= WeekStopHour ) || | |
Dayweek > StrToInteger(StringSubstr(WeekDay,StringLen(WeekDay)-1,1)) | |
) | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
if (iTimeControl()) | |
{ | |
if (StringFind(WeekDay,DoubleToStr(Dayweek,0),0) >= 0) | |
{ | |
EA_Valid = true; | |
}else | |
{ | |
EA_Valid = false; | |
} | |
}else | |
{ | |
EA_Valid = false; | |
} | |
} | |
#ifdef CHT | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"未在設定時區內,現有戰役達成後將離開戰場"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"已進入作戰時區內,全面啟動執行..."); | |
return(true); | |
} | |
#endif | |
#ifdef ENG | |
if (!EA_Valid && OrdersTotal() == 0) | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Not in trading time zone, stop trading after current set"); | |
return(false); | |
} | |
else | |
{ | |
ObjectSetString(0,Run,OBJPROP_TEXT,"Entered trading time zone, automated system initiated"); | |
return(true); | |
} | |
#endif | |
} | |
int NewsConvert() | |
{ | |
string maindata[][7]; | |
string sBuffer = ""; | |
if (!FileIsExist(Newsfile)) | |
{ | |
return(0); | |
} | |
int h1=FileOpen(Newsfile,FILE_SHARE_READ|FILE_CSV); | |
int i = 0 , j = 0; | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(sBuffer,"<title>") >= 0) | |
{ | |
ArrayResize(maindata,j+1); | |
maindata[j][0] = sBuffer; | |
for (i=1;i<7;i++) | |
{ | |
maindata[j][i] = FileReadString(h1); | |
} | |
j++; | |
} | |
} | |
FileClose(h1); | |
int count = ArrayRange(maindata,0); | |
string sTags[7] = { "<title>", "<country>", "<date><![CDATA[", "<time><![CDATA[", "<impact><![CDATA[", "<forecast><![CDATA[", "<previous><![CDATA[" }; | |
string eTags[7] = { "</title>", "</country>", "]]></date>", "]]></time>", "]]></impact>", "]]></forecast>", "]]></previous>" }; | |
for (i=0;i<count;i++) | |
{ | |
for (j=0;j<7;j++) | |
{ | |
maindata[i][j]=StringSubstr(maindata[i][j],StringFind(maindata[i][j],sTags[j],0)+StringLen(sTags[j]),StringFind(maindata[i][j],eTags[j],0)-StringFind(maindata[i][j],sTags[j],0)-StringLen(sTags[j])); | |
} | |
} | |
int h2 = FileOpen("NewsEvent.csv",FILE_SHARE_READ|FILE_WRITE|FILE_CSV,';'); | |
int GMT = 0; | |
string time_str = ""; | |
string prev_time_str = ""; | |
for (i=0;i<count;i++) | |
{ | |
if (maindata[i][4] == "High") | |
{ | |
GMT = -1 * (TimeGMTOffset()/3600); | |
time_str = TimeToString( | |
StringToTime(MakeDateTime(maindata[i][2],maindata[i][3])) + GMT * 3600, | |
TIME_DATE|TIME_MINUTES|TIME_SECONDS | |
); | |
if (time_str == prev_time_str) continue; | |
FileWrite(h2,maindata[i][1],time_str); | |
prev_time_str = time_str; | |
} | |
} | |
FileClose(h2); | |
return(count); | |
} | |
string MakeDateTime(string strDate, string strTime) | |
{ | |
// Print("Converting Forex Factory Time into Metatrader time..."); //added by MN | |
// Converts forexfactory time & date into yyyy.mm.dd hh:mm | |
int n1stDash = StringFind(strDate, "-"); | |
int n2ndDash = StringFind(strDate, "-", n1stDash+1); | |
string strMonth = StringSubstr(strDate, 0, 2); | |
string strDay = StringSubstr(strDate, 3, 2); | |
string strYear = StringSubstr(strDate, 6, 4); | |
// strYear = "20" + strYear; | |
int nTimeColonPos = StringFind(strTime, ":"); | |
string strHour = StringSubstr(strTime, 0, nTimeColonPos); | |
string strMinute = StringSubstr(strTime, nTimeColonPos+1, 2); | |
string strAM_PM = StringSubstr(strTime, StringLen(strTime)-2); | |
int nHour24 = StrToInteger(strHour); | |
if ((strAM_PM == "pm" || strAM_PM == "PM") && nHour24 != 12) | |
{ | |
nHour24 += 12; | |
} | |
if ((strAM_PM == "am" || strAM_PM == "AM") && nHour24 == 12) | |
{ | |
nHour24 = 0; | |
} | |
string strHourPad = ""; | |
if (nHour24 < 10) | |
strHourPad = "0"; | |
return(StringConcatenate(strYear, ".", strMonth, ".", strDay, " ", strHourPad, nHour24, ":", strMinute)); | |
} | |
void RebuildNewsArray() | |
{ | |
NewsConvert(); | |
string sBuffer = ""; | |
int i = 0; | |
int h1 = FileOpen(EventFile,FILE_SHARE_READ|FILE_CSV); | |
if (h1 < 0) | |
{ | |
ArrayResize(AvoidTime,1); | |
ArrayResize(AvoidSymbol,1); | |
return; | |
} | |
while(!FileIsEnding(h1)) | |
{ | |
sBuffer = FileReadString(h1); | |
if (StringFind(Symbol(),sBuffer) >= 0) | |
{ | |
ArrayResize(AvoidSymbol,i+2); | |
AvoidSymbol[i+1] = sBuffer; | |
ArrayResize(AvoidTime,i+2); | |
sBuffer = FileReadString(h1); | |
AvoidTime[i+1] = StrToTime(sBuffer); | |
i++; | |
} | |
} | |
FileClose(h1); | |
} | |
bool AvoidNews() | |
{ | |
#ifdef CHT | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "預警雷達已偵測到 " + AvoidSymbol[i] + " 戰場於 " + StringSubstr(TimeToStr(AvoidTime[i]),11,5) + " 可能將有大量狂派埋伏"; | |
str[1] = "超級達摩戰艦於 " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5) + " 啟動防禦系統進入一級警戒"; | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "正在接收狂派情資..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "最後狂派活動時間: " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "已無其他狂派活動或情資已過期,即將休兵......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
#ifdef ENG | |
bool Out = true; | |
string str[2] = {""}; | |
int i = 0; | |
if (NewsSW) | |
{ | |
for (i=1;i<ArraySize(AvoidTime);i++) | |
{ | |
if (TimeLocal() >= AvoidTime[i] - MinBefore * 60 && TimeLocal() <= AvoidTime[i] + MinAfter * 60) | |
{ | |
str[0] = "News Alert on " + AvoidSymbol[i] + " at time " + StringSubstr(TimeToStr(AvoidTime[i]),11,5); | |
str[1] = "Red alert! Braking System Standing By at " + StringSubstr(TimeToStr(AvoidTime[i] - MinBefore * 60),11,5); | |
Out = false; | |
break; | |
}else | |
{ str[0] = NewsEngage; | |
str[1] = ""; | |
} | |
} | |
if (ArraySize(AvoidTime) == 1) | |
{ | |
str[0] = "News retrieving..."; | |
str[1] = ""; | |
Out = false; | |
} | |
if (i == ArraySize(AvoidTime) - 1) | |
{ | |
if (TimeLocal() >= AvoidTime[i] + MinAfter) | |
{ | |
str[0] = "Latest News at : " + TimeToString(AvoidTime[i],TIME_DATE|TIME_MINUTES); | |
str[1] = "No trading info available......"; | |
Out = false; | |
} | |
} | |
}else | |
{ | |
str[0] = ""; | |
str[1] = ""; | |
} | |
for (i=0;i<ArraySize(str);i++) | |
{ | |
ObjectSetString(0,NewsInfo[i],OBJPROP_TEXT,str[i]); | |
} | |
return(Out); | |
#endif | |
} | |
bool Margin() | |
{ | |
if (MarginSleep) | |
{ | |
double Margin_R = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL); | |
if (EqualZero(Margin_R)) return(true); | |
if (MarginLimit >= Margin_R) | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,"Margin level lower than " + DoubleToString(MarginLimit,2) + " automated system suspended"); | |
return(false); | |
}else | |
{ | |
ObjectSetString(0,Margin,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool Float() | |
{ | |
if (FloatEq) | |
{ | |
double Profit = AccountProfit(); | |
if (Profit <= FloatAm) | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,"Floating loss greater than "+DoubleToString(FloatAm,2)+" automated system suspended"); | |
return(0); | |
}else | |
{ | |
ObjectSetString(0,FloatLoss,OBJPROP_TEXT,""); | |
} | |
} | |
return(true); | |
} | |
bool TimesStopConfig() | |
{ | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(NextSW_str,0); | |
#ifdef GLOBAL_FILE | |
if (!GlobalFileRead()) | |
{ | |
GlobalVars.G_EARunTime = 0; | |
GlobalVars.G_MaxEquity = (AccountProfit() / AccountBalance()) * 100; | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalFileWrite(); | |
GlobalFileRead(); | |
} | |
Print("EARuntime: " + TimeToString(GlobalVars.G_EARunTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("MaxLossEquity: " + DoubleToString(GlobalVars.G_MaxEquity,2)); | |
Print("Start_P: " + TimeToString(GlobalVars.G_Start_P,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("Start_Balance: " + DoubleToString(GlobalVars.G_Start_Balance,2)); | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
#endif | |
return(true); | |
} | |
bool TimesStop(double &TimesProfit, double &TodayProfit) | |
{ | |
#ifdef GLOBAL_FILE | |
if (OrdersTotal()==0) | |
{ | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Status | | |
//+------------------------------------------------------------------+ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVars.G_Start_Balance; | |
else | |
Start_Balance = iBalance; | |
Start_P = GlobalVars.G_Start_P; | |
EARunTime = GlobalVars.G_EARunTime; | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVars.G_MaxEquity) | |
{ | |
GlobalVars.G_MaxEquity = (AccountProfit() / Start_Balance) * 100; | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Status | | |
//+------------------------------------------------------------------+ | |
GlobalVars.G_EARunTime = EARunTime; | |
GlobalVars.G_Start_Balance = Start_Balance; | |
GlobalVars.G_Start_P = Start_P; | |
GlobalFileWrite(); | |
#else | |
if (OrdersTotal()==0) | |
{ | |
GlobalVariableSet(Start_Balance_str,AccountBalance()); | |
GlobalVariableSet(Start_P_str,TimeCurrent()); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Balance | | |
//+------------------------------------------------------------------+ | |
if (GlobalVariableCheck(Start_Balance_str)) | |
{ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVariableGet(Start_Balance_str); | |
else | |
Start_Balance = iBalance; | |
}else | |
{ | |
Start_Balance = AccountBalance(); | |
} | |
if (GlobalVariableCheck(Start_P_str)) | |
{ | |
Start_P = (datetime)GlobalVariableGet(Start_P_str); | |
}else | |
{ | |
Start_P = TimeCurrent(); | |
} | |
if (GlobalVariableCheck(EARUNTIME_str)) | |
{ | |
EARunTime = (datetime)GlobalVariableGet(EARUNTIME_str); | |
} | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVariableGet(MaxEquity_str)) | |
{ | |
GlobalVariableSet(MaxEquity_str,(AccountProfit() / Start_Balance)*100); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Balnace | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(EARUNTIME_str,EARunTime); | |
GlobalVariableSet(Start_P_str,(double)Start_P); | |
GlobalVariableSet(Start_Balance_str,Start_Balance); | |
#endif | |
return(true); | |
} | |
#endif | |
void DeleteObjects() | |
{ | |
for (int i=5;i>=0;i--) | |
{ | |
if(ObjectFind(Label_arr[i]) >= 0) ObjectDelete(0,Label_arr[i]); | |
if(ObjectFind(Label_arr1[i]) >= 0) ObjectDelete(0,Label_arr1[i]); | |
if (i < 2) | |
{ | |
if (ObjectFind(NewsInfo[i]) >= 0) ObjectDelete(0,NewsInfo[i]); | |
if (ObjectFind(Light_str[i]) >= 0) ObjectDelete(0,Light_str[i]); | |
} | |
} | |
if(ObjectFind(Run) >= 0) ObjectDelete(0,Run); | |
if(ObjectFind(FloatLoss) >= 0) ObjectDelete(FloatLoss); | |
if(ObjectFind(Margin) >= 0) ObjectDelete(Margin); | |
if(ObjectFind(Light) >= 0) ObjectDelete(Light); | |
//if (FileIsExist(EventFile)) FileDelete(EventFile); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
if (UninitializeReason() != 3) | |
{ | |
#ifdef MODE_AREA51 | |
if (ArraySize(AvoidTime) <= 1) NewGetSW = true; | |
else | |
{ | |
if (TimeLocal() >= AvoidTime[ArraySize(AvoidTime)-1] + MinAfter) | |
{ | |
NewGetSW = true; | |
} | |
} | |
#endif | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if ( | |
(TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) || | |
NewGetSW | |
) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
if (!NewGetSW) | |
{ | |
Sleep(1000); | |
} | |
Sleep(1000); | |
#ifdef MODE_AREA51 | |
RebuildNewsArray(); | |
#endif | |
GlobalVariableSet("DNSW0",0); | |
NewGetSW = false; | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
void AutoRepairSLTP(const bool flag,const int Type,const int Mode) | |
{ | |
double TP = 0.0; | |
double SL = 0.0; | |
double MaxPrice = 0.0; | |
double MinPrice = 0.0; | |
if (!flag) return; | |
int Cnt = MyOrderTotal(Type); | |
GetLastTciketINFO(Type,MaxPrice,MinPrice); | |
if (Mode == MODE_FIRSTTRADE) | |
{ | |
TP = TakeProfit; | |
SL = StopLoss; | |
if (MartinSW) | |
SL = 0.0; | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,SL,TP); | |
} | |
if (Mode == MODE_MARTIN) | |
{ | |
switch (Type) | |
{ | |
case OP_BUY: | |
TP = Cal_TP(OP_BUY,MinPrice,TakeProfit); | |
SL = 0; | |
break; | |
case OP_SELL: | |
TP = Cal_TP(OP_SELL,MaxPrice,TakeProfit); | |
SL = 0; | |
break; | |
} | |
if (Cnt > 1) | |
{ | |
#ifdef MODE_AREA51 | |
if (SingleOut) | |
Set_SL_TP(MODE_SLTP_BYPOINT,Type,0,TakeProfit); | |
else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#else | |
Set_SL_TP(MODE_SLTP_BYPRICE,Type,SL,TP); | |
#endif | |
} | |
} | |
} | |
string FileCharArrayToString(const string str_temp) | |
{ | |
uchar End_arr[]; | |
int j = 0; | |
for(int i=0;i<StringLen(str_temp);i+=2) | |
{ | |
ArrayResize(End_arr,j+1); | |
End_arr[j] = (char)StringToInteger(StringSubstr(str_temp,i,2)); | |
j++; | |
} | |
return(CharArrayToString(End_arr,0,WHOLE_ARRAY,CP_ACP)); | |
} | |
void CheckOSVersion() | |
{ | |
ulong dwVersion = GetVersion(); | |
uint dwMajorVersion = (uchar)(((ushort)(dwVersion & 0xffff)) & 0xff); | |
printf("Major Version Of OS: %d",dwMajorVersion); | |
if (dwMajorVersion < 6) | |
{ | |
#ifdef CHT | |
Alert("請注意!使用Windows XP(Server 2003)之系統將無法保證交易能正常執行!"); | |
#endif | |
#ifdef ENG | |
Alert("Attention! Not guarantee to run well in Windows XP(Server 2003)"); | |
#endif | |
} | |
} | |
void SetKbar(double& Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
#ifdef MODE_AREA51 | |
void BrakeSystemFun() | |
{ | |
int Light_Arrow[2] = {108,161}; | |
if (BrakeSystem) | |
{ | |
int Pos = 50; | |
double Position=20; | |
#ifdef CHT | |
string Active = "煞車系統啟動中"; | |
string Deactive = "煞車系統待命中"; | |
#endif | |
#ifdef ENG | |
string Active = "Braking System Initiating"; | |
string Deactive = "Braking System Stand By"; | |
#endif | |
double Position_L = Bid + Position * point; | |
for (int i=0;i<ArraySize(Light_str);i++) | |
{ | |
if (ObjectFind(Light_str[i]) < 0) | |
ObjectCreate(0,Light_str[i],OBJ_ARROW,0,0,0); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_ARROWCODE,Light_Arrow[i]); | |
if (i == 0) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLime); | |
if (i == 1) ObjectSetInteger(0,Light_str[i],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_WIDTH,5); | |
ObjectSetInteger(0,Light_str[i],OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSetDouble(0,Light_str[i],OBJPROP_PRICE1,Position_L); | |
} | |
int EnergyBall=Ball(Status,SleepStop,Last_Time,Last_Price,SleepTime,(int)TimeCurrent(),point,Energy,Close[0]); | |
if (EnergyBall<0) | |
{ | |
Alert("Energy Brake Error"); | |
TradeSW = false; | |
return; | |
} | |
if (TimeCurrent()<=SleepStop[0]) | |
{ | |
TradeSW = false; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrRed); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrCrimson); | |
#ifdef CHT | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" 分鐘後啟動"); | |
#endif | |
#ifdef ENG | |
ObjectSetString(0,Light,OBJPROP_TEXT,Active+", initiate after"+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" minutes"); | |
#endif | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectSetInteger(0,Light_str[0],OBJPROP_COLOR,clrLime); | |
ObjectSetInteger(0,Light_str[1],OBJPROP_COLOR,clrLawnGreen); | |
ObjectSetString(0,Light,OBJPROP_TEXT,Deactive); | |
} | |
} | |
else | |
{ | |
TradeSW = true; | |
ObjectDelete(Light_str[0]); | |
ObjectDelete(Light_str[1]); | |
} | |
} | |
#endif | |
bool Lots_Valid(const double iLot) | |
{ | |
double ValuePerLot = SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE); | |
if ((iLot * ValuePerLot) > AccountInfoDouble(ACCOUNT_FREEMARGIN)) | |
{ | |
Alert("No Money"); | |
return(false); | |
} | |
return(true); | |
} | |
int GetTicket(int &Ticket_arr[]) | |
{ | |
int Cnt = 0; | |
for (int i=0;i<ArraySize(Ticket_arr);i++) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (Ticket_arr[i] == OrderTicket()) | |
{ | |
Cnt++; | |
break; | |
} | |
} | |
} | |
return(Cnt); | |
} | |
int MyOrderTotal_Enders(const string symbol,const int Type,const int ID) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL && OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
continue; | |
} | |
if ((Type == OP_BUY && ((OrderType() & 1) == 1 ? false : true)) || (Type == OP_SELL && ((OrderType() & 1) == 1 ? true : false))) | |
{ | |
if (OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
} | |
} | |
} | |
return(x); | |
} | |
bool EAControl_Enders() | |
{ | |
bool Out = true; | |
string str =""; | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
Comment(str); | |
return(Out); | |
} | |
int EndersGamgeCnt = 0; | |
void EndersGameModule(const string symbol, const int Type, int id, bool &flag) | |
{ | |
if (MyOrderTotal_Enders(symbol,Type,id) == 0) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (OrderCloseTime() > EGStartTime) | |
{ | |
EndersGamgeCnt++; | |
Alert(Symbol() + " 戰場已完成第" + IntegerToString(EndersGamgeCnt) + " 次戰役"); | |
EGStartTime = TimeCurrent(); | |
break; | |
} | |
} | |
} | |
if (EndersGamgeCnt >= OutCnt) | |
{ | |
Alert("已達成設定局數,即將離開 " + Symbol() + " 戰場"); | |
flag = false; | |
ExpertRemove(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| ProjectName | | |
//| Copyright 2012, CompanyName | | |
//| http://www.companyname.net | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import | |
//#define ENG 1 | |
#define CHT 1 | |
//#define MODE_BUY 1 | |
#define MODE_SELL 1 | |
#define TOLERANCE 0.000001 | |
#define OP_ALL 101 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_CHECKTIMEIN 0 | |
#define MODE_TIMESTP 1 | |
#define MODE_TIMESSL 2 | |
#define MODE_DAILYTP 3 | |
#ifdef CHT | |
string t1=""; | |
input bool AutoRepairSLTP = false; //自動回復停損停利 | |
input double Pending = 0.0; //掛單價格 | |
string T21="-------------------------------------------"; | |
input double MM = 1; //資金管理%數 | |
input double Lots = 0.01; //手數 | |
input double TakeProfit = 10.0; //獲利點數 | |
input double StopLoss = 10.0; //停損點數 | |
input string T0="-------------ESP戰艦穩定煞車系統------------"; | |
input bool BrakeSystem=true; | |
int pSeconds=14; | |
input double Energy=4; //能量偵測靈敏度 | |
input int SleepTime=15; //能量引擎啟動時間(分鐘) | |
int SleepStop[1]={0}; | |
input string T00="-------------------------------------------"; | |
input double iBalance = 0; //止盈計算本金基準 | |
input bool TakeProfitPercentSW = true; //次止盈開關 | |
input double TakeProfitPercent = 1; //次止盈,每次最多獲利%,立即平倉歸零重啟新局 | |
input bool TakeProfitPercentDaySW = true; //日止盈開關 | |
input double TakeProfitPercentDay = 1000; //日止盈,每日最多獲利%,立即停止交易,隔日重新啟動 | |
input bool StopLossPercentSW = true; //浮虧止損開關 | |
input double StopLossPercent = 1000; //現時帳戶浮虧止損,止損基準值:帳戶餘額 | |
input bool SleepLoss = true; //止損後當日不再出兵 | |
double NextTime=0.5; | |
input string T9_="-------------------小虧為贏模組-------------"; | |
input bool SmallLossWinSW = true; //小虧為贏開關 | |
input int MaxTime=150; //每局最長時間(分鐘) | |
input int MaxIn=20; //每局最多兵力 | |
input double MaxP=0.5; //達成以上條件小虧為贏 | |
input string T20="-------------------------------------------"; | |
input bool SingleOut=false; //單獨出場 | |
input double i_Multiple=1.382; //馬丁倍率 | |
input int i_MaxMartin=5; //馬丁張數 | |
input string K="馬丁距離"; | |
input double i_MartinStep_0=30; | |
input double i_MartinStep_1=40; | |
input double i_MartinStep_2=50; | |
input double i_MartinStep_3=60; | |
input double i_MartinStep_4=60; | |
input double i_MartinStep_5=60; | |
input double i_MartinStep_6=60; | |
input double i_MartinStep_7=60; | |
input double i_MartinStep_8=60; | |
input double i_MartinStep_9=60; | |
input bool MartinCalLots=true; //馬丁計算獲利手數開關,T:第一張手數 | F:總手數 | |
input double i_TPPoint=10; //馬丁計算獲利點數 | |
input int MOrders=2; //馬丁第幾張開始調整手數 | |
input bool MartinSW = true; //逆勢攤平馬丁 | |
input int M_Period = 60; //攤平馬丁時區 | |
input string Q0="-------------------------------------------"; | |
input int AutoSWAntiMartin=0; //不出場自動控制 | |
input int AntiMartinTrades=2; //不出場單數 | |
input double AntiMartinMultiple=0.5; //反馬丁倍率 | |
input double AntiMartinStep=20; //反馬丁間距 | |
input double Ai_TPPoint=0; //反馬丁獲利點設定為第一張的獲利點數 | |
input string Q5="-------------------------------------------"; | |
input bool MartinAntiMSW=true; //Counter Strike模式 | |
input double CSMultiple=0.5; //Counter Strike倍率 | |
input double CSStep=20; //Counter Strike間距 | |
input double CSTPPoint=0; //Counter Strike獲利點數 | |
input bool CSProtect = true; //Counter Strike保護裝置 | |
input string Q11="-------------------------------------------"; | |
bool NoMartininAttck=false; //執行Attack模式後,反向不下單" | |
input bool Non_Stop=false; //Enders Game | |
double Position=20; | |
input int OutCnt=1; | |
#endif | |
#ifdef ENG | |
string t1=""; | |
input bool AutoRepairSLTP = false; //Auto Repair SL&TP | |
input double Pending = 0.0; //Pending Order at Price | |
string T21="-------------------------------------------"; | |
input double MM = 1; //Fund Management % | |
input double Lots = 0.01; //Order Size_lot | |
input double TakeProfit = 10.0; //Take Profit | |
input double StopLoss = 10.0; //Stop Loss | |
input string T0="-------------BrakeSystem------------"; | |
input bool BrakeSystem=true; | |
int pSeconds=14; | |
input double Energy=4; //Braking System Sensitivity Control | |
input int SleepTime=15; //Braking System Sleeping Time | |
int SleepStop[1]={0}; | |
input string T00="-------------------------------------------"; | |
input double iBalance = 0; //Capital setting for Profit/Loss termination | |
input bool TakeProfitPercentSW = true; //Switch of Secondary Take Profit | |
input double TakeProfitPercent = 1; //Secondary Take Profit, max. % of profit to close trade, and reset | |
input bool TakeProfitPercentDaySW = true; //Switch of Daily Take Profit | |
input double TakeProfitPercentDay = 1000; //Daily Take Profit, max. % of profit to close trade, reset on the next day | |
input bool StopLossPercentSW = true; //Switch of stopping Floating Loss | |
input double StopLossPercent = 1000; // | |
input bool SleepLoss = true; //Stop trading when reaching stop loss the same day | |
double NextTime=0.5; | |
input string T9_="-------------------Small Loss for Win-------------"; | |
input bool SmallLossWinSW = true; //Switch of Small Loss To Win | |
input int MaxTime=150; //Max. Time Per Set (Minutes) | |
input int MaxIn=20; //Max. Trades Per Set | |
input double MaxP=0.5; //Criteria of Small Loss for Win | |
input string T20="-------------------------------------------"; | |
input bool SingleOut=false; //Take Profit Single Out | |
input double i_Multiple=1.382; //Martingale Multiplier | |
input int i_MaxMartin=5; //Martingale Order No. | |
input string K="Martingale Distance"; | |
input double i_MartinStep_0=30; | |
input double i_MartinStep_1=40; | |
input double i_MartinStep_2=50; | |
input double i_MartinStep_3=60; | |
input double i_MartinStep_4=60; | |
input double i_MartinStep_5=60; | |
input double i_MartinStep_6=60; | |
input double i_MartinStep_7=60; | |
input double i_MartinStep_8=60; | |
input double i_MartinStep_9=60; | |
input bool MartinCalLots=true; //Martingale Lots Profit Calculation. T: First Order Lot | F: Total Order Lot | |
input double i_TPPoint=10; //Martin Profit Calculation in Pips | |
input int MOrders=2; //Number to start martingale multiplication | |
input bool MartinSW = true; //Against Trend Martin | |
input int M_Period = 60; //Martin Order Time Frame | |
input string Q0="-------------------------------------------"; | |
input int AutoSWAntiMartin=0; //Mode Selector | |
input int AntiMartinTrades=2; //Retaining Order No. | |
input double AntiMartinMultiple=0.5; //Anti-Martingale Multiplier | |
input double AntiMartinStep=20; //Anti-Martingale Distance | |
input double Ai_TPPoint=0; //First Anti-Martingale order take profit | |
input string Q5="-------------------------------------------"; | |
input bool MartinAntiMSW=true; //Counter Strike Mode | |
input double CSMultiple=0.5; //Counter Strike Multiplier | |
input double CSStep=20; //Counter Strike Distance | |
input double CSTPPoint=0; //Counter Strike Take Profit | |
input bool CSProtect = true; //Counter Strike Protection System | |
input string Q11="-------------------------------------------"; | |
bool NoMartininAttck=false; //Stop placing opposite trend order after Attack Mode | |
input bool Non_Stop=false; //Enders Game | |
double Position=20; | |
input int OutCnt=1; | |
#endif | |
datetime ticket_time=0; | |
int Last_Time[1]={0}; | |
int OutCntTemp=0; | |
int MartinBuyBar,MartinSellBar; | |
int id_1=3333333; | |
int Status[1]={0}; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
int ErrorCode = 0; | |
bool NewGetSW = false; | |
datetime H1BarTime = 0; | |
datetime Start_P = 0; | |
datetime EARunTime = 0; | |
datetime EGStartTime = TimeCurrent(); | |
bool EGFlag = true; | |
double point; | |
double Last_Price[1]={-1.0}; | |
double FontDPI=96; | |
double MinPriceShot; | |
double iPending = 0.0; | |
double Start_Balance = 0; | |
double iLots = 0.0; | |
string Start_P_str = "Start_P"; | |
string Start_Balance_str = "Start_Balance"; | |
string MaxEquity_str = "MaxEquity"; | |
string NextSW_str = "NextSW"; | |
string EARUNTIME_str = "EARUNTIME"; | |
string B_Status,S_Status; | |
string Run = "RunStatus"; | |
string LogText[4] = {"小虧為贏" , "次止盈" , "次止損" , "日止盈"}; | |
string Label_arr[6] = | |
{ | |
"TimesProfitPer", | |
"Start_Balance", | |
"TimePast", | |
"Orders", | |
"TodayProfitPer", | |
"MaxLoss" | |
}; | |
string Label_arr1[6] = | |
{ | |
"TimesProfitPer0", | |
"Start_Balance0", | |
"TimePast0", | |
"Orders0", | |
"TodayProfitPer0", | |
"MaxLoss0" | |
}; | |
bool NewsgetSW=false; | |
bool GetAuthorSW=true; | |
bool FirstNotOut=false; | |
bool FirstIn = true; | |
bool MartinIn = true; | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void OnInit() | |
{ | |
int dc=GetDC(0); | |
FontDPI=GetDeviceCaps(dc,88); | |
if (FontDPI==0) | |
FontDPI=96; | |
ReleaseDC(0,dc); | |
//+------------------------------------------------------------------+ | |
iLots = OpLots(); | |
ticket_time=TimeCurrent(); | |
MinPriceShot=Energy; | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
TimesStopConfig(); | |
UpdateFiles(MODE_INIT); | |
iPending = Pending; | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
ObjectDelete("Lable"); | |
ObjectDelete("Lable1"); | |
ObjectDelete("percent"); | |
ObjectDelete("start_p"); | |
ObjectDelete("takepercent_day"); | |
ObjectDelete("percent0"); | |
ObjectDelete("start_p0"); | |
ObjectDelete("takepercent_day0"); | |
ObjectDelete("percent.1"); | |
ObjectDelete("start_p.1"); | |
ObjectDelete("takepercent_day.1"); | |
ObjectDelete("percent0.1"); | |
ObjectDelete("start_p0.1"); | |
ObjectDelete("takepercent_day0.1"); | |
ObjectDelete("run"); | |
ObjectDelete("Newsinfo"); | |
ObjectDelete("Newsinfo2"); | |
ObjectDelete("ERR"); | |
ObjectDelete("ERR2"); | |
ObjectDelete("Expert"); | |
ObjectDelete("Light"); | |
ObjectDelete("Light2"); | |
ObjectDelete("lightstatus"); | |
ObjectDelete("Margin"); | |
ObjectDelete("Float"); | |
ObjectDelete("LOGO"); | |
ObjectDelete("LOGO0"); | |
ObjectDelete("NAME"); | |
ObjectDelete("MSG0"); | |
ObjectDelete("MSG0.0"); | |
ObjectDelete("MSG1"); | |
ObjectDelete("MSG1.0"); | |
ObjectDelete("MSG2"); | |
ObjectDelete("MSG2.0"); | |
ObjectDelete("MSG3"); | |
GlobalVariableSet("DNSW",0); | |
DeleteObjects(); | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void OnTick() | |
{ | |
string tComment="ERTYUUI"; | |
string tMComment="Martin"; | |
UpdateFiles(MODE_TICK); | |
double TimesProfit = 0.0; | |
double TodayProfit = 0.0; | |
if (!CloseAllTrades()) return; | |
if (!TimesStop(TimesProfit,TodayProfit)) return; | |
Displayprofit(TimesProfit,TodayProfit); | |
CheckTimeIn(TimesProfit); | |
//--------------------------------------------------------------海卓拉加入的片段 | |
#ifdef MODE_BUY | |
if (Non_Stop==false) EndersGameModule(Symbol(),OP_BUY,id_1,EGFlag); | |
#endif | |
#ifdef MODE_SELL | |
if (Non_Stop==false) EndersGameModule(Symbol(),OP_SELL,id_1,EGFlag); | |
#endif | |
//--------------------------------------------------------------海卓拉加入的片段 | |
//+------------------------------------------------------------------+ | |
//|Shutdown EA | | |
//+------------------------------------------------------------------+ | |
if (!EAControl()) return; | |
//+------------------------------------------------Brake_System---------------------------------------------------------------------+ | |
if (BrakeSystem) | |
{ | |
int Pos = 50; | |
#ifdef CHT | |
string Active="煞車系統啟動中"; | |
string Deactive="煞車系統待命中"; | |
#endif | |
#ifdef ENG | |
string Active="Braking System Initiating"; | |
string Deactive="Braking System Stand By"; | |
#endif | |
color Light_status=Gold; | |
double Position_L=Bid+Position*point; | |
if (ObjectFind("Light")==-1) | |
ObjectCreate("Light",OBJ_ARROW,0,0,0); | |
ObjectSet("Light",OBJPROP_ARROWCODE,108); | |
ObjectSet("Light",OBJPROP_CORNER,1); | |
ObjectSet("Light",OBJPROP_COLOR,Lime); | |
ObjectSet("Light",OBJPROP_WIDTH,5); | |
if (ObjectFind("Light2")==-1) | |
ObjectCreate("Light2",OBJ_ARROW,0,0,0); | |
ObjectSet("Light2",OBJPROP_ARROWCODE,161); | |
ObjectSet("Light2",OBJPROP_CORNER,1); | |
ObjectSet("Light2",OBJPROP_COLOR,LawnGreen); | |
ObjectSet("Light2",OBJPROP_WIDTH,5); | |
if (ObjectFind("lightstatus")==-1) | |
ObjectCreate("lightstatus",OBJ_LABEL,0,0,0); | |
ObjectSet("lightstatus",OBJPROP_CORNER,1); | |
ObjectSet("lightstatus",OBJPROP_COLOR,Gold); | |
ObjectSet("lightstatus",OBJPROP_XDISTANCE,20); | |
ObjectSet("lightstatus",OBJPROP_YDISTANCE,Pos+155); | |
ObjectSet("Light",OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSet("Light",OBJPROP_PRICE1,Position_L); | |
ObjectSet("Light2",OBJPROP_TIME1,TimeCurrent()+300); | |
ObjectSet("Light2",OBJPROP_PRICE1,Position_L); | |
int EnergyBall=Ball(Status,SleepStop,Last_Time,Last_Price,SleepTime,(int)TimeCurrent(),point,MinPriceShot,Close[0]); | |
if (EnergyBall<0) | |
{ | |
Alert("Energy Brake Error"); | |
return; | |
} | |
if (TimeCurrent()<=SleepStop[0]) | |
{ | |
FirstNotOut=true; | |
MartinIn=false; | |
ObjectSet("Light",OBJPROP_COLOR,Red); | |
ObjectSet("Light2",OBJPROP_COLOR,Crimson); | |
#ifdef CHT | |
ObjectSetText("lightstatus",Active+", "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" 分鐘後啟動",10,"微軟正黑體",Light_status); | |
#endif | |
#ifdef ENG | |
ObjectSetText("lightstatus",Active+", initiate after "+DoubleToStr((SleepStop[0]-TimeCurrent())/60,0)+" minutes",10,"微軟正黑體",Light_status); | |
#endif} | |
else | |
{ | |
FirstNotOut=false; | |
MartinIn=true; | |
ObjectSet("Light",OBJPROP_COLOR,Lime); | |
ObjectSet("Light2",OBJPROP_COLOR,LawnGreen); | |
ObjectSetText("lightstatus",Deactive,10,"微軟正黑體",Light_status); | |
} | |
} | |
else | |
{ | |
FirstNotOut=false; | |
MartinIn=true; | |
ObjectDelete("Light"); | |
ObjectDelete("Light2"); | |
ObjectDelete("lightstatus"); | |
} | |
//+------------------------------------------------Brake_System---------------------------------------------------------------------+ | |
if (iLots<0.01 && iLots != 0.0) | |
{ | |
#ifdef CHT | |
Alert(Symbol()+" 已達最小手數(<0.01),無法下單"); | |
#endif | |
#ifdef ENG | |
Alert(Symbol()+" Minimum lot size (<0.01), trading not available"); | |
#endif | |
return; | |
} | |
#ifdef MODE_BUY | |
BuyModule(); | |
#endif | |
#ifdef MODE_SELL | |
SellModule(); | |
#endif | |
ShowMSG(); | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool profit_control(datetime i_0,double takepercent_) | |
{ | |
double today_profit; | |
today_profit=((GetProfitFromDateInCurrency(i_0)+AccountProfit())/Start_Balance)*100; | |
if (today_profit>=takepercent_) | |
{ | |
GlobalVariableSet("LastProfit",today_profit); | |
return(false); | |
} | |
return(true); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double GetProfitFromDateInCurrency(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType()==OP_BUY || OrderType()==OP_SELL) | |
{ | |
if (today<OrderCloseTime()) | |
profit+=OrderProfit()+OrderCommission()+OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void iCloseOrders(int type) | |
{ | |
int C0_cnt; | |
if (type == OP_BUYSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUYSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELLSTOP) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELLSTOP && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_BUY && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrLime)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==OP_SELL && OrderMagicNumber()==id_1){ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
}else { | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),clrRed)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selecttickets() | |
{ | |
int cnt_tickets = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)) | |
#endif | |
{ | |
ticket_time=OrderOpenTime(); | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int Selectticketsfrhistory() | |
{ | |
int cnt_tickets = 0; | |
for (int i=0;i<OrdersHistoryTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
continue; | |
else | |
#ifdef MODE_BUY | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) && ticket_time<=OrderOpenTime()) | |
#else | |
if (OrderSymbol()==Symbol() && OrderMagicNumber()==id_1 && (OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) && ticket_time<=OrderOpenTime()) | |
#endif | |
{ | |
cnt_tickets++; | |
} | |
} | |
return(cnt_tickets); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int OrderSendF(int type,double inLots,double SL,double TP,string Comm) | |
{ | |
int Ertemp=0; | |
double SendOrders_Lots = inLots; | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
switch (type) | |
{ | |
case OP_BUY: | |
Ertemp=OrderSend(Symbol(),OP_BUY,inLots,Ask,20,0,0,Comm,id_1,0,Lime); | |
if (Ertemp<0) | |
{ | |
return(-99); | |
} | |
Set_sl_tp_buy(Symbol(),TP,SL); | |
break; | |
case OP_SELL: | |
Ertemp=OrderSend(Symbol(),OP_SELL,inLots,Bid,20,0,0,Comm,id_1,0,Red); | |
if (Ertemp<0) | |
{ | |
return(-99); | |
} | |
Set_sl_tp_sell(Symbol(),TP,SL); | |
break; | |
} | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double NormPoint(double in) | |
{ | |
return(in * point); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void Set_sl_tp_sell(string symbol,double tp_m,double sl_m) | |
{ | |
double stoploss_m,takeprofit_m; | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==symbol && OrderType()==OP_SELL && OrderMagicNumber()==id_1) | |
{ | |
if (sl_m>0) | |
{ | |
stoploss_m=OrderOpenPrice()+sl_m*point; | |
} | |
else | |
if (sl_m==0) | |
{ | |
stoploss_m=0; | |
} | |
else | |
{ | |
stoploss_m=OrderStopLoss(); | |
} | |
if (tp_m>0) | |
{ | |
takeprofit_m=OrderOpenPrice()-tp_m*point;//宣告一個takeprofit放停利"價位" | |
} | |
else | |
if (tp_m==0) | |
{ | |
takeprofit_m=0; | |
} | |
else | |
{ | |
takeprofit_m=OrderTakeProfit(); | |
} | |
if (CompareDouble(stoploss_m,OrderStopLoss()) && CompareDouble(takeprofit_m,OrderTakeProfit())) | |
{ | |
continue; | |
} | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss_m,takeprofit_m,0,White)) | |
{ | |
Alert("Set_sl_tp_sell" + IntegerToString(GetLastError())); | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void GetAllOrders(int type,int &b_Ticket[],int &s_Ticket[]) | |
{ | |
int x=0,y=0; | |
if (type==OP_BUY) | |
{ | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
b_Ticket[x]=OrderTicket(); | |
x++; | |
} | |
} | |
} | |
if (type==OP_SELL) | |
{ | |
for (int j=0;j<OrdersTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
s_Ticket[y]=OrderTicket(); | |
y++; | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int MyOrderTotal(int Type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL) | |
{ | |
x++; | |
continue; | |
} | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void Set_sl_tp(int type,double tp_m,double sl_m) | |
{ | |
for (int z=0;z<OrdersTotal();z++)//執行N次篩選(N=訂單總數) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue;//用"流水號"找訂單 | |
if (OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1)//挑選出同貨幣對 && BUY單 && MagicNumber=本程式 的訂單 | |
{ | |
if (DoubleCutDigis(OrderStopLoss())==DoubleCutDigis(sl_m) && DoubleCutDigis(OrderTakeProfit())==DoubleCutDigis(tp_m)) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (tp_m == -1) | |
{ | |
tp_m = OrderTakeProfit(); | |
} | |
if (sl_m == -1) | |
{ | |
sl_m = OrderStopLoss(); | |
} | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(sl_m),DoubleCutDigis(tp_m),0,White)) | |
{ | |
Alert("Set_sl_tp "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void Set_sl_tp_Quota(int type,double tp_m0,double tp_m1,int WithOut) | |
{ | |
int QQ=0; | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1) | |
{ | |
QQ++; | |
if (QQ <= WithOut) continue; | |
if (QQ <= AntiMartinTrades) | |
{ | |
if (DoubleCutDigis(OrderStopLoss())==DoubleCutDigis(tp_m0) && DoubleCutDigis(OrderTakeProfit())==0) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(tp_m0),0,0,White)) | |
{ | |
Alert("Set_sl_tp Q0 "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
else | |
{ | |
if (DoubleCutDigis(OrderStopLoss()) == DoubleCutDigis(tp_m0) && DoubleCutDigis(OrderTakeProfit())==DoubleCutDigis(tp_m1)) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(tp_m0),DoubleCutDigis(tp_m1),0,White)) | |
{ | |
Alert("Set_sl_tp Q1 "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void Set_sl_tp_CS(int type,double tp_m,double sl_m,int MaxTicket_s) | |
{ | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if ((OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1 && StringFind(OrderComment(),"CS")>=0) || OrderTicket()==MaxTicket_s) | |
{ | |
if (DoubleCutDigis(OrderStopLoss())==DoubleCutDigis(sl_m) && DoubleCutDigis(OrderTakeProfit())==DoubleCutDigis(tp_m)) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (tp_m == -1) | |
{ | |
tp_m = OrderTakeProfit(); | |
} | |
if (sl_m == -1) | |
{ | |
sl_m = OrderStopLoss(); | |
} | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(sl_m),DoubleCutDigis(tp_m),0,White)) | |
{ | |
Alert("Set_sl_tp CS.0 "+IntegerToString(OrderTicket())+"|"+DoubleToStr(sl_m,Digits)+"|"+DoubleToStr(Ask,Digits)+"|"+DoubleToStr(Bid,Digits)+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
if (OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1 && StringFind(OrderComment(),"CS")<0 && OrderTicket()!=MaxTicket_s) | |
{ | |
if (/*DoubleCutDigis(OrderStopLoss()) == DoubleCutDigis(sl_m)*/OrderStopLoss()==0 && DoubleCutDigis(OrderTakeProfit())==DoubleCutDigis(tp_m)) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),DoubleCutDigis(OrderStopLoss()),DoubleCutDigis(tp_m),0,White)) | |
{ | |
Alert("Set_sl_tp CS.0 "+IntegerToString(OrderTicket())+"|"+DoubleToStr(sl_m,Digits)+"|"+DoubleToStr(Ask,Digits)+"|"+DoubleToStr(Bid,Digits)+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double NormPrice(double in,int dd) | |
{ | |
in=NormalizeDouble(in,dd); | |
return(in); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double Cal_TP_Sell(bool Mode,int type,double MaxPrice_s,double TPPoint,int WithOut) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
int x = 0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
x++; | |
if (x <= WithOut) continue; | |
TotalLots+=OrderLots(); | |
TotalDisProduct+= (MaxPrice_s - OrderOpenPrice())/point * OrderLots(); | |
} | |
} | |
if (Mode) | |
{ | |
TP_Goal= (TPPoint * fGetFirstLots(OP_SELL) + TotalDisProduct) / TotalLots; | |
} | |
else | |
{ | |
TP_Goal= (TPPoint * GetAllLots(OP_SELL) + TotalDisProduct) / TotalLots; | |
} | |
return(TP_Goal); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double Cal_TP_CS_SELL(int type,double MaxPrice_s,int MaxTicket_s,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol() && StringFind(OrderComment(),"CS")>=0) | |
{ | |
TotalLots+=OrderLots(); | |
TotalDisProduct+=(MaxPrice_s-OrderOpenPrice())/point*OrderLots(); | |
} | |
if (OrderTicket()==MaxTicket_s) | |
{ | |
if (OrderSelect(MaxTicket_s,SELECT_BY_TICKET,MODE_TRADES)) | |
{ | |
TotalLots+=OrderLots(); | |
TotalDisProduct+=(MaxPrice_s-OrderOpenPrice())/point*OrderLots(); | |
} | |
} | |
} | |
TP_Goal=(TPPoint*fGetFirstLots(type)+TotalDisProduct)/TotalLots; | |
return(TP_Goal); | |
} | |
double Cal_TP_CS_BUY(int type,double MinPrice_b,int MinTicket_b,double TPPoint) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if ((OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol() && StringFind(OrderComment(),"CS")>=0) || OrderTicket()==MinTicket_b) | |
{ | |
TotalLots+=OrderLots(); | |
TotalDisProduct+=(OrderOpenPrice()-MinPrice_b)/point*OrderLots(); | |
} | |
} | |
TP_Goal=(TPPoint*fGetFirstLots(1)+TotalDisProduct)/TotalLots; | |
return(TP_Goal); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int OrderSendA(int type,double price,color tColor,string tSymbol,double inLots,double SL,double TP,string tComment) | |
{ | |
RefreshRates(); | |
double SendOrders_Lots = inLots; | |
if (!Lots_Valid(SendOrders_Lots)) | |
{ | |
Alert("No Money"); | |
return(-1); | |
} | |
int ticket_c=OrderSend(tSymbol,type,inLots,price,(int)(2*(point/Point)),0,0,tComment,id_1,0,tColor); //if yes send order | |
if (ticket_c < 0) | |
{ | |
Alert("OrderSend (OrderSendA) Error: "+ErrorDescription(GetLastError())); | |
return(-1); | |
} | |
if (TP == 0 && SL ==0) return(1); | |
if (!OrderModify(ticket_c,price,SL,TP,0,tColor)) | |
{ | |
Alert("OrderModify SL TP (OrderSendA) Error: "+ErrorDescription(GetLastError())); | |
return(1); | |
} | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double fGetFirstLots(int type) | |
{ | |
double fLot = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderMagicNumber()==id_1 && OrderType()==type && OrderSymbol()==Symbol()) | |
{ | |
fLot=OrderLots(); | |
break; | |
} | |
} | |
fLot=NormPrice(fLot,2); | |
return(fLot); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double GetProfit() | |
{ | |
double Profit=0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderMagicNumber()==id_1 && OrderType()==OP_SELL && OrderSymbol()==Symbol()) | |
{ | |
Profit+=OrderProfit()+OrderSwap()+OrderCommission(); | |
} | |
} | |
return(Profit); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double DoubleCutDigis(double in) | |
{ | |
return(StrToDouble(DoubleToStr(in,Digits))); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void TicketInfo_SELL(int &b_Ticket[],double &b_Price[],int &s_Ticket[],double &s_Price[],double &MaxPrice_s,int &MaxTicket_s,double &MinPrice_s,int &MinTicket_s) | |
{ | |
if (MyOrderTotal(OP_SELL)!=0) | |
{ | |
ArrayResize(s_Ticket,MyOrderTotal(OP_SELL)); | |
ArrayResize(s_Price,MyOrderTotal(OP_SELL)); | |
GetAllOrders(OP_SELL,b_Ticket,s_Ticket); | |
for (int j=0;j<ArraySize(s_Ticket);j++) | |
{ | |
if (!OrderSelect(s_Ticket[j],SELECT_BY_TICKET,MODE_TRADES)) continue; | |
s_Price[j]=OrderOpenPrice(); | |
} | |
MaxPrice_s=s_Price[ArrayMaximum(s_Price)]; | |
MaxTicket_s= s_Ticket[ArrayMaximum(s_Price)]; | |
MinPrice_s = s_Price[ArrayMinimum(s_Price)]; | |
MinTicket_s= s_Ticket[ArrayMinimum(s_Price)]; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool CheckQuota_SELL() | |
{ | |
bool Sig=false; | |
double Price[2] = {0}; | |
int x=0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (x==2) | |
break; | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==OP_SELL && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
Price[x]=OrderOpenPrice(); | |
x++; | |
} | |
} | |
if (x < 2) | |
return(false); | |
if (Price[0] < Price[1]) | |
Sig = false; | |
if (Price[0] > Price[1]) | |
Sig = true; | |
return(Sig); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int GetLastOrder(int type) | |
{ | |
int Ticket=0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
Ticket=OrderTicket(); | |
break; | |
} | |
} | |
return(Ticket); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void ShowMSG() | |
{ | |
double Scale=StrToDouble(DoubleToStr(FontDPI/96,2)); | |
double Start=55; | |
Start=(Start * Scale); | |
double Y=20; | |
Y=(Y*Scale); | |
if (ObjectFind("LOGO")==-1) | |
{ | |
//ObjectCreate("LOGO",OBJ_LABEL,0,0,0); | |
ObjectCreate(0,"LOGO",OBJ_BITMAP_LABEL,0,0,0); | |
} | |
ObjectSetString(0,"LOGO",OBJPROP_BMPFILE,"\\Images\\Axe.bmp"); | |
ObjectSetInteger(0,"LOGO",OBJPROP_XDISTANCE,8); | |
ObjectSetInteger(0,"LOGO",OBJPROP_YDISTANCE,15); | |
if (ObjectFind("LOGO0")==-1) | |
{ | |
ObjectCreate("LOGO0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("LOGO0",OBJPROP_CORNER,0); | |
ObjectSet("LOGO0",OBJPROP_XDISTANCE,60*Scale); | |
ObjectSet("LOGO0",OBJPROP_YDISTANCE,10*Scale); | |
ObjectSetText("LOGO0","登峰戰士",30,"標楷體",White); | |
if (ObjectFind("NAME")==-1) | |
{ | |
ObjectCreate("NAME",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("NAME",OBJPROP_CORNER,0); | |
ObjectSet("NAME",OBJPROP_XDISTANCE,65*Scale); | |
ObjectSet("NAME",OBJPROP_YDISTANCE,50*Scale+Y*0); | |
#ifdef MODE_BUY | |
ObjectSetText("NAME","EVEREST WARRIOR BUY",14,"Impact",GreenYellow); | |
#endif | |
#ifdef MODE_SELL | |
ObjectSetText("NAME","EVEREST WARRIOR SELL",14,"Impact",Red); | |
#endif | |
if (ObjectFind("MSG0")==-1) | |
{ | |
ObjectCreate("MSG0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG0",OBJPROP_CORNER,0); | |
ObjectSet("MSG0",OBJPROP_XDISTANCE,10*Scale); | |
ObjectSet("MSG0",OBJPROP_YDISTANCE,Start+Y*1); | |
ObjectSetText("MSG0","Martingale Multiple: ",14,"Impact",Gold); | |
if (ObjectFind("MSG0.0")==-1) | |
{ | |
ObjectCreate("MSG0.0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG0.0",OBJPROP_CORNER,0); | |
ObjectSet("MSG0.0",OBJPROP_XDISTANCE,185*Scale); | |
ObjectSet("MSG0.0",OBJPROP_YDISTANCE,Start+Y*1); | |
ObjectSetText("MSG0.0",DoubleToStr(i_Multiple,3),14,"Impact",Gold); | |
if (ObjectFind("MSG1")==-1) | |
{ | |
ObjectCreate("MSG1",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG1",OBJPROP_CORNER,0); | |
ObjectSet("MSG1",OBJPROP_XDISTANCE,10*Scale); | |
ObjectSet("MSG1",OBJPROP_YDISTANCE,Start+Y*2); | |
ObjectSetText("MSG1","Max Martingale Trades: ",14,"Impact",White); | |
if (ObjectFind("MSG1.0")==-1) | |
{ | |
ObjectCreate("MSG1.0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG1.0",OBJPROP_CORNER,0); | |
ObjectSet("MSG1.0",OBJPROP_XDISTANCE,210*Scale); | |
ObjectSet("MSG1.0",OBJPROP_YDISTANCE,Start+Y*2); | |
ObjectSetText("MSG1.0",DoubleToStr(i_MaxMartin,0),14,"Impact",White); | |
if (ObjectFind("MSG2")==-1) | |
{ | |
ObjectCreate("MSG2",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG2",OBJPROP_CORNER,0); | |
ObjectSet("MSG2",OBJPROP_XDISTANCE,10*Scale); | |
ObjectSet("MSG2",OBJPROP_YDISTANCE,Start+Y*3); | |
ObjectSetText("MSG2","Martingale Range: ",14,"Impact",CornflowerBlue); | |
if (ObjectFind("MSG2.0")==-1) | |
{ | |
ObjectCreate("MSG2.0",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG2.0",OBJPROP_CORNER,0); | |
ObjectSet("MSG2.0",OBJPROP_XDISTANCE,170*Scale); | |
ObjectSet("MSG2.0",OBJPROP_YDISTANCE,Start+Y*3); | |
ObjectSetText("MSG2.0",DoubleToStr(i_MartinStep_0,0)+" | "+DoubleToStr(i_MartinStep_1,0)+" | "+DoubleToStr(i_MartinStep_2,0)+" | "+DoubleToStr(i_MartinStep_3,0)+".....",14,"Impact",CornflowerBlue); | |
if (ObjectFind("MSG3")==-1) | |
{ | |
ObjectCreate("MSG3",OBJ_LABEL,0,0,0); | |
} | |
ObjectSet("MSG3",OBJPROP_CORNER,0); | |
ObjectSet("MSG3",OBJPROP_XDISTANCE,10*Scale); | |
ObjectSet("MSG3",OBJPROP_YDISTANCE,Start+Y*4); | |
if (FirstNotOut) | |
{ | |
ObjectSetText("MSG3","Mode: "+"ATTACK",14,"Impact",CornflowerBlue); | |
} | |
else | |
{ | |
ObjectSetText("MSG3","Mode: "+"FLAT",14,"Impact",CornflowerBlue); | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void SetKbar(double &Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool AnyCS(int type) | |
{ | |
bool Sig = false; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol() && StringFind(OrderComment(),"CS")>=0) | |
{ | |
Sig=true; | |
break; | |
} | |
} | |
return(Sig); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double GetAllLots(int type) | |
{ | |
double inLots=0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderMagicNumber()==id_1 && OrderType()==type && OrderSymbol()==Symbol()) | |
{ | |
inLots+=OrderLots(); | |
} | |
} | |
return(inLots); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool F_CSProtect(int type,int MaxTicket_s,double in_SL) | |
{ | |
bool Out = false; | |
string OutPrint = ""; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if ((OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol() && StringFind(OrderComment(),"CS")>=0) || OrderTicket() == MaxTicket_s) | |
{ | |
if (OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),(int)(2*(point/Point)),White)) | |
{ | |
Out = true; | |
OutPrint = OutPrint + DoubleToStr(OrderTicket(),0) + ","; | |
} | |
} | |
} | |
if (Out) Alert("CS Protect Active: " + OutPrint + "Ask:" +DoubleToStr(Ask,Digits) + ",SL:" + DoubleToStr(in_SL,Digits)); | |
return(Out); | |
} | |
//+------------------------------------------------------------------+ | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= TOLERANCE) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= TOLERANCE && A >= -TOLERANCE) return(true); | |
return(false); | |
} | |
void PendingTrade(const int Type,const string iComment,const double iSL,const double iTP) | |
{ | |
double InPrice = Pending; | |
int Ticket = 0; | |
int iType = -1; | |
double SL = 0.0; | |
double TP = 0.0; | |
if (Type == OP_BUY) | |
{ | |
if (InPrice > Ask) | |
iType = OP_BUYSTOP; | |
if (InPrice < Ask) | |
iType = OP_BUYLIMIT; | |
if (MyOrderTotal(OP_BUYSTOP) >= 1 || MyOrderTotal(OP_BUYLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,iLots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrLime); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
}else | |
{ | |
if (!OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) return; | |
SL = OrderOpenPrice() - iSL * point; | |
TP = OrderOpenPrice() + iTP * point; | |
if (iSL == 0 || MartinSW) SL = 0; | |
if (iTP == 0) TP = 0; | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_sl_tp "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (InPrice < Bid) | |
iType = OP_SELLSTOP; | |
if (InPrice > Bid) | |
iType = OP_SELLLIMIT; | |
if (MyOrderTotal(OP_SELLSTOP) >= 1 || MyOrderTotal(OP_SELLLIMIT) >= 1 || MyOrderTotal(Type) >= 1) return; | |
Ticket = OrderSend(Symbol(),iType,iLots,InPrice,(int)(2 * (point / Point)),0,0,iComment,id_1,0,clrRed); | |
if (Ticket < 0) | |
{ | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(GetLastError())); | |
}else | |
{ | |
if (!OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)) return; | |
SL = OrderOpenPrice() + iSL * point; | |
TP = OrderOpenPrice() - iTP * point; | |
if (iSL == 0 || MartinSW) SL = 0; | |
if (iTP == 0) TP = 0; | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_sl_tp "+IntegerToString(OrderTicket())+" error : "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
bool CloseAllTrades() | |
{ | |
if (GlobalVariableGet(NextSW_str) == 1) | |
{ | |
#ifdef MODE_BUY | |
iCloseOrders(OP_BUY); | |
#endif | |
#ifdef MODE_SELL | |
iCloseOrders(OP_SELL); | |
#endif | |
if (MyOrderTotal(OP_ALL) > 0) | |
{ | |
#ifdef CHT | |
Alert("水雷未掃除成功"); | |
#endif | |
#ifdef ENG | |
Alert("Submarine mine sweeper not completed"); | |
#endif | |
return(false); | |
}else | |
{ | |
Sleep((int)(NextTime * 1000 * 60)); | |
} | |
} | |
return(true); | |
} | |
void Displayprofit(const double TimesProfit, const double TodayProfit) | |
{ | |
int Pos = 50; | |
string Label_Text = ""; | |
string Label_Text1 = ""; | |
if (ObjectFind(Run) < 0) ObjectCreate(0,Run,OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Run, OBJPROP_CORNER, 1); | |
ObjectSetInteger(0,Run, OBJPROP_COLOR, clrGold); | |
ObjectSetInteger(0,Run, OBJPROP_XDISTANCE, 20); | |
ObjectSetInteger(0,Run, OBJPROP_YDISTANCE, Pos); | |
ObjectSetInteger(0,Run, OBJPROP_FONTSIZE, 10); | |
ObjectSetInteger(0,Run, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER); | |
ObjectSetString(0,Run, OBJPROP_FONT, "微軟正黑體"); | |
for (int i=0;i<6;i++) | |
{ | |
if(ObjectFind(Label_arr[i]) < 0) ObjectCreate(0,Label_arr[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_XDISTANCE,120); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr[i],OBJPROP_FONT,"微軟正黑體"); | |
if(ObjectFind(Label_arr1[i]) < 0) ObjectCreate(0,Label_arr1[i],OBJ_LABEL,0,0,0); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_CORNER,1); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_COLOR,White); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_XDISTANCE,20); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_YDISTANCE,Pos + 30 + 20 * i); | |
ObjectSetInteger(0,Label_arr1[i],OBJPROP_ANCHOR,ANCHOR_RIGHT); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_FONT,"微軟正黑體"); | |
switch(i) | |
{ | |
#ifdef CHT | |
case 0: | |
Label_Text = "單次擄獲狂派戰俘: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "進場起始兵力: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "進場經過時間: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " 分鐘"; | |
break; | |
case 3: | |
Label_Text = "戰場兵力: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "今日擄獲狂派戰俘累積至: " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "最大暫時被擄穫兵力: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
#ifdef ENG | |
case 0: | |
Label_Text = "Profit/Loss: "; | |
Label_Text1 = DoubleToString(TimesProfit,2) + " %"; | |
break; | |
case 1: | |
Label_Text = "Start Balance: "; | |
Label_Text1 = DoubleToString(Start_Balance,2); | |
break; | |
case 2: | |
Label_Text = "Time Elapsed: " ; | |
Label_Text1 = DoubleToString((TimeCurrent() - Start_P) / 60,1) + " Minutes"; | |
break; | |
case 3: | |
Label_Text = "Orders: "; | |
Label_Text1 = IntegerToString(OrdersTotal()); | |
break; | |
case 4: | |
Label_Text = "Profit/Loss (Today): " ; | |
Label_Text1 = DoubleToString(TodayProfit,2); | |
break; | |
case 5: | |
Label_Text = "Maximum Floating Loss: "; | |
Label_Text1 = DoubleToString(GlobalVariableGet(MaxEquity_str),2)+" %"; | |
break; | |
#endif | |
} | |
ObjectSetString(0,Label_arr[i],OBJPROP_TEXT,Label_Text); | |
ObjectSetString(0,Label_arr1[i],OBJPROP_TEXT,Label_Text1); | |
} | |
} | |
void CheckTimeIn(const double TimesProfit) | |
{ | |
datetime RunTime = (TimeCurrent() - Start_P) / 60; | |
if (SmallLossWinSW && RunTime >= MaxTime && OrdersTotal() >= MaxIn && TimesProfit >= MaxP) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_CHECKTIMEIN,""); | |
} | |
} | |
bool EAControl() | |
{ | |
bool Out = true; | |
string str =""; | |
if (TimeCurrent() <= EARunTime) | |
{ | |
str = "SLSLeep"; | |
Out = false; | |
} | |
if (!EGFlag) | |
{ | |
str = "Ender"; | |
Out = false; | |
} | |
Comment(str); | |
return(Out); | |
} | |
double OpLots() | |
{ | |
double Out = Lots; | |
if (!EqualZero(MM) && MM > 0) | |
{ | |
Out = (AccountBalance() * MM ) / (1000 * TakeProfit); | |
} | |
return(Out); | |
} | |
double GetProfitPeriod(datetime today) | |
{ | |
double profit=0; | |
int total =OrdersHistoryTotal(); | |
for (int i=0; i<total; i++) | |
{ | |
if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) | |
{ | |
if (OrderType() == OP_BUY || OrderType() == OP_SELL) | |
{ | |
if (today < OrderCloseTime()) | |
profit += OrderProfit() + OrderCommission() + OrderSwap(); | |
} | |
} | |
} | |
return (profit); | |
} | |
void WriteLog(const int Mode,const string in) | |
{ | |
double TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
string str = WindowExpertName() + "," + Symbol() + "," + TimeToString(TimeCurrent(),TIME_DATE|TIME_DATE|TIME_SECONDS) + | |
": " + LogText[Mode] + " , " + in + | |
"," + Start_Balance_str + ": " + DoubleToString(Start_Balance,2) + | |
"," + Start_P_str + ": " + IntegerToString(Start_P) + | |
",TimeProfit: " + DoubleToString(TimesProfit,2) + | |
"| Set: 餘:" + DoubleToString(iBalance,2) + | |
",次:" + DoubleToString(TakeProfitPercent,2) + | |
",日:" + DoubleToString(TakeProfitPercentDay,2) + | |
",損:" + DoubleToStr(StopLossPercent ,2) + | |
",時:" + DoubleToString(MaxTime,2) + | |
",單:" + DoubleToString(MaxIn,2) + | |
",虧:" + DoubleToString(MaxP,2) + | |
"\n" | |
; | |
int h1 = FileOpen("Logs.txt",FILE_READ|FILE_WRITE|FILE_TXT); | |
if (h1 < 0) Print("Open Log Failed"); | |
FileSeek(h1,0,SEEK_END); | |
int byte = FileWriteString(h1,str,StringLen(str)); | |
if (byte <= 0) Print("Write Log Failed"); | |
FileClose(h1); | |
} | |
void SellModule() | |
{ | |
string tMComment="Martin"; | |
// EveryTrade Modify | |
bool SellModifySLTP = false; | |
// Mode Selection & Determine SL TP Lots | |
double SellLots = 0.0; | |
double FirstSellTP = 0.0; | |
double FirstSellSL = 0.0; | |
double FirstSellLots = 0.0; | |
SellLots = iLots; | |
FirstSellLots = iLots; | |
FirstSellTP = TakeProfit; | |
FirstSellSL = StopLoss; | |
switch (AutoSWAntiMartin) | |
{ | |
case -1: | |
FirstNotOut=false; | |
break; | |
case 0: | |
if (MyOrderTotal(OP_SELL) > 1) | |
{ | |
if (CheckQuota_SELL()) | |
{ | |
FirstNotOut = true; | |
} | |
} | |
break; | |
case 1: | |
FirstNotOut=true; | |
break; | |
} | |
if (FirstNotOut) | |
{ | |
FirstSellTP = 0; | |
} | |
// Mode Selection & Determine SL TP Lots End | |
if (AutoRepairSLTP) | |
{ | |
if (MyOrderTotal(OP_SELL) == 1) | |
{ | |
if (MartinSW) | |
Set_sl_tp_sell(Symbol(),FirstSellTP,0); | |
else | |
Set_sl_tp_sell(Symbol(),FirstSellTP,FirstSellSL); | |
} | |
} | |
if (MyOrderTotal(OP_SELL)==0) | |
{ | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_SELL,"Pending" + IntegerToString(MyOrderTotal(OP_SELL)),FirstSellSL,FirstSellTP); | |
iPending = 0; | |
} | |
if (MyOrderTotal(OP_SELLSTOP) == 0 && MyOrderTotal(OP_SELLLIMIT) == 0) | |
{ | |
if (FirstIn) | |
{ | |
if (MartinSW) | |
OrderSendF(OP_SELL,iLots,0,FirstSellTP,Symbol()); | |
else | |
OrderSendF(OP_SELL,iLots,FirstSellSL,FirstSellTP,Symbol()); | |
} | |
} | |
} | |
double MartinStep[]; | |
ArrayResize(MartinStep,10); | |
MartinStep[0]=i_MartinStep_0; | |
MartinStep[1]=i_MartinStep_1; | |
MartinStep[2]=i_MartinStep_2; | |
MartinStep[3]=i_MartinStep_3; | |
MartinStep[4]=i_MartinStep_4; | |
MartinStep[5]=i_MartinStep_5; | |
MartinStep[6]=i_MartinStep_6; | |
MartinStep[7]=i_MartinStep_7; | |
MartinStep[8]=i_MartinStep_8; | |
MartinStep[9]=i_MartinStep_9; | |
int b_Ticket[],s_Ticket[]; | |
double b_Price[]; | |
double s_Price[]; | |
double MaxPrice_s,MinPrice_s; | |
int MaxTicket_s,MinTicket_s; | |
// FirstNotOut | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
double AntiMartinLots = 0.0; | |
int ti = 0; | |
double in_TP = 0.0; | |
double in_SL = 0.0; | |
string CommentADD=""; | |
if (FirstNotOut) | |
{ | |
if (MyOrderTotal(OP_SELL) >= 1 && MyOrderTotal(OP_SELL) < AntiMartinTrades + 1) | |
{ | |
if ( (MinPrice_s - Bid) / point > AntiMartinStep) | |
{ | |
AntiMartinLots = NormalizeDouble(SellLots * MathPow(AntiMartinMultiple,MyOrderTotal(OP_SELL)),2); | |
if (AntiMartinLots < 0.01) AntiMartinLots = 0.01; | |
if (!Lots_Valid(AntiMartinLots)) return; | |
ti = OrderSend(Symbol(),OP_SELL,AntiMartinLots,Bid,20,0,0,"FNO"+tMComment+DoubleToStr(MyOrderTotal(OP_SELL),0),id_1,0,Red) ; | |
if (ti < 0) | |
{ | |
Alert("OrderSnend Buy FNO Error: " + ErrorDescription(GetLastError())); | |
} | |
else | |
{ | |
SellModifySLTP = true; | |
} | |
} | |
} | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
if (AutoRepairSLTP || SellModifySLTP) | |
{ | |
if (CheckQuota_SELL()) | |
{ | |
in_TP = MinPrice_s - (TakeProfit * point); | |
if (in_TP > Ask && in_TP != 0) in_TP = 0; | |
in_SL = MaxPrice_s - Cal_TP_Sell(true,OP_SELL,MaxPrice_s,Ai_TPPoint,0) * point; | |
if (in_SL < Ask && in_SL != 0) in_SL = 0; | |
Set_sl_tp_Quota(OP_SELL,in_SL,in_TP,0); | |
SellModifySLTP = false; | |
} | |
} | |
} | |
//+------------------------------------------------Martin Start---------------------------------------------------------------------+ | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
double SellMartinLots_Temp = 0.0; | |
double SellMartinLots = 0.0; | |
double Step_S = 0.0; | |
if (MartinSW) | |
{ | |
if (MartinIn) | |
{ | |
if (MyOrderTotal(OP_SELL) > 0) | |
{ | |
SellMartinLots_Temp = MyOrderTotal(OP_SELL)-(MOrders-1); | |
if (SellMartinLots_Temp < 0) SellMartinLots_Temp = 0; | |
SellMartinLots=NormalizeDouble(SellLots * MathPow(i_Multiple,SellMartinLots_Temp),2); | |
if (MyOrderTotal(OP_SELL)-1 > ArraySize(MartinStep)-1) | |
{ | |
Step_S=MartinStep[ArrayMaximum(MartinStep)]; | |
}else | |
{ | |
Step_S=MartinStep[MyOrderTotal(OP_SELL)-1]; | |
} | |
if (Step_S == 0) Step_S=MartinStep[ArrayMaximum(MartinStep)]; | |
if ( (Bid - MaxPrice_s) / point > Step_S && MyOrderTotal(OP_SELL) < i_MaxMartin) | |
{ | |
if (MartinSellBar != iBars(Symbol(),M_Period)) | |
{ | |
MartinSellBar = iBars(Symbol(),M_Period) + 1; | |
} | |
else if (MartinSellBar == iBars(Symbol(),M_Period)) | |
{ | |
if (OrderSendA(OP_SELL,Bid,Red,Symbol(),SellMartinLots,0,0,Symbol()+"_"+tMComment+DoubleToStr(MyOrderTotal(OP_SELL),0)) > 0) | |
{ | |
SellModifySLTP = true; | |
MartinSellBar=0; | |
} | |
} | |
} | |
else | |
{ | |
MartinSellBar=0; | |
} | |
} | |
else | |
{ | |
MartinSellBar=0; | |
} | |
} | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
if (AutoRepairSLTP || SellModifySLTP) | |
{ | |
if (!AnyCS(OP_SELL)) | |
{ | |
if (!CheckQuota_SELL() && MyOrderTotal(OP_SELL) > 1) | |
{ | |
if (SingleOut) | |
{ | |
Set_sl_tp_sell(Symbol(),i_TPPoint,0); | |
} | |
else | |
{ | |
in_TP = MaxPrice_s - Cal_TP_Sell(MartinCalLots,OP_SELL,MaxPrice_s,i_TPPoint,0) * point; | |
if (in_TP > Ask && in_TP != 0) in_TP = 0; | |
in_SL = 0; | |
if (in_SL < Ask && in_SL != 0) in_SL = 0; | |
Set_sl_tp(OP_SELL,in_TP,in_SL); | |
} | |
SellModifySLTP = false; | |
} | |
} | |
} | |
} | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
double CSLots=0.0; | |
double LastPrice=0.0; | |
if (MartinAntiMSW && !SingleOut) | |
{ | |
if (MyOrderTotal(OP_SELL)>1) | |
{ | |
if (!CheckQuota_SELL()) | |
{ | |
if (OrderSelect(GetLastOrder(OP_SELL),SELECT_BY_TICKET,MODE_TRADES)) | |
{ | |
CSLots=NormalizeDouble(OrderLots()*CSMultiple,2); | |
if (CSLots<0.01) | |
{ | |
CSLots=0.01; | |
} | |
LastPrice=OrderOpenPrice(); | |
} | |
if ((LastPrice-Bid)/point>=CSStep /*&& (MarketInfo(Symbol(),MODE_SPREAD)) * (Point/point) <= CSStep / 2*/) | |
{ | |
if (Lots_Valid(CSLots) && OrderSend(Symbol(),OP_SELL,CSLots,Bid,20,0,0,Symbol()+"_CS_"+DoubleToStr(MyOrderTotal(OP_SELL),0),id_1,0,Red)>0) | |
{ | |
SellModifySLTP = true; | |
} | |
else | |
{ | |
Alert("CS Error: "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
if (AutoRepairSLTP || SellModifySLTP) { | |
if (AnyCS(OP_SELL)) | |
{ | |
in_TP = MaxPrice_s - Cal_TP_Sell(MartinCalLots,OP_SELL,MaxPrice_s,i_TPPoint,0) * point; | |
if (in_TP > Ask && in_TP != 0) in_TP = 0; | |
in_SL = MaxPrice_s - Cal_TP_CS_SELL(OP_SELL,MaxPrice_s,MaxTicket_s,CSTPPoint) * point; | |
if (in_SL < Ask && in_SL != 0) | |
{ | |
if (CSProtect) | |
{ | |
if (AnyCS(OP_SELL)) | |
{ | |
if (F_CSProtect(OP_SELL,MaxTicket_s,in_SL)) | |
{ | |
return; | |
TicketInfo_SELL(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_s,MaxTicket_s,MinPrice_s,MinTicket_s); | |
} | |
} | |
} | |
in_SL = 0; | |
} | |
Set_sl_tp_CS(OP_SELL,in_TP,in_SL,MaxTicket_s); | |
SellModifySLTP = false; | |
} | |
} | |
} | |
} | |
void DeleteObjects() | |
{ | |
for (int i=5;i>=0;i--) | |
{ | |
if(ObjectFind(Label_arr[i]) >= 0) ObjectDelete(0,Label_arr[i]); | |
if(ObjectFind(Label_arr1[i]) >= 0) ObjectDelete(0,Label_arr1[i]); | |
} | |
if(ObjectFind(Run) >= 0) ObjectDelete(0,Run); | |
} | |
void BuyModule() | |
{ | |
string tMComment="Martin"; | |
// EveryTrade Modify | |
bool BuyModifySLTP = false; | |
// Mode Selection & Determine SL TP Lots | |
double BuyLots = 0.0; | |
double FirstBuyTP = 0.0; | |
double FirstBuySL = 0.0; | |
double FirstBuyLots = 0.0; | |
BuyLots = iLots; | |
FirstBuyLots = iLots; | |
FirstBuyTP = TakeProfit; | |
FirstBuySL = StopLoss; | |
switch (AutoSWAntiMartin) | |
{ | |
case -1: | |
FirstNotOut=false; | |
break; | |
case 0: | |
if (MyOrderTotal(OP_BUY) > 1) | |
{ | |
if (CheckQuota_BUY()) | |
{ | |
FirstNotOut = true; | |
} | |
} | |
break; | |
case 1: | |
FirstNotOut=true; | |
break; | |
} | |
if (FirstNotOut) | |
{ | |
FirstBuyTP = 0; | |
} | |
// Mode Selection & Determine SL TP Lots End | |
if (AutoRepairSLTP) | |
{ | |
if (MyOrderTotal(OP_BUY) == 1) | |
{ | |
if (MartinSW) | |
Set_sl_tp_buy(Symbol(),FirstBuyTP,0); | |
else | |
Set_sl_tp_buy(Symbol(),FirstBuyTP,FirstBuySL); | |
} | |
} | |
if (MyOrderTotal(OP_BUY)==0) | |
{ | |
if (!EqualZero(iPending)) | |
{ | |
PendingTrade(OP_BUY,"Pending" + IntegerToString(MyOrderTotal(OP_BUY)),FirstBuySL,FirstBuyTP); | |
iPending = 0; | |
} | |
if (MyOrderTotal(OP_BUYSTOP) == 0 && MyOrderTotal(OP_BUYLIMIT) == 0) | |
{ | |
if (FirstIn) | |
{ | |
if (MartinSW) | |
OrderSendF(OP_BUY,iLots,0,FirstBuyTP,Symbol()); | |
else | |
OrderSendF(OP_BUY,iLots,FirstBuySL,FirstBuyTP,Symbol()); | |
} | |
} | |
} | |
double MartinStep[]; | |
ArrayResize(MartinStep,10); | |
MartinStep[0]=i_MartinStep_0; | |
MartinStep[1]=i_MartinStep_1; | |
MartinStep[2]=i_MartinStep_2; | |
MartinStep[3]=i_MartinStep_3; | |
MartinStep[4]=i_MartinStep_4; | |
MartinStep[5]=i_MartinStep_5; | |
MartinStep[6]=i_MartinStep_6; | |
MartinStep[7]=i_MartinStep_7; | |
MartinStep[8]=i_MartinStep_8; | |
MartinStep[9]=i_MartinStep_9; | |
int b_Ticket[],s_Ticket[]; | |
double b_Price[]; | |
double s_Price[]; | |
double MaxPrice_b,MinPrice_b; | |
int MaxTicket_b,MinTicket_b; | |
// FirstNotOut | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
double AntiMartinLots = 0.0; | |
int ti = 0.0; | |
double in_TP = 0.0; | |
double in_SL = 0.0; | |
string CommentADD=""; | |
if (FirstNotOut) | |
{ | |
if (MyOrderTotal(OP_BUY) >= 1 && MyOrderTotal(OP_BUY) < AntiMartinTrades + 1 ) | |
{ | |
if ( (Ask - MaxPrice_b) / point > AntiMartinStep) | |
{ | |
AntiMartinLots = NormalizeDouble(BuyLots * MathPow(AntiMartinMultiple,MyOrderTotal(OP_BUY)),2); | |
if (AntiMartinLots < 0.01) AntiMartinLots = 0.01; | |
if (!Lots_Valid(AntiMartinLots)) return; | |
ti = OrderSend(Symbol(),OP_BUY,AntiMartinLots,Ask,20,0,0,"FNO"+tMComment+DoubleToStr(MyOrderTotal(OP_BUY),0),id_1,0,Lime) ; | |
if (ti < 0) | |
{ | |
Alert("OrderSnend Buy FNO Error: " + ErrorDescription(GetLastError())); | |
} | |
else | |
{ | |
BuyModifySLTP = true; | |
} | |
} | |
} | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
if (AutoRepairSLTP || BuyModifySLTP) | |
{ | |
if (CheckQuota_BUY()) | |
{ | |
in_TP = MaxPrice_b + (TakeProfit * point); | |
if (in_TP < Bid && in_TP != 0) in_TP = 0; | |
in_SL = MinPrice_b + Cal_TP_Buy(true,OP_BUY,MinPrice_b,Ai_TPPoint,0) * point; | |
if (in_SL > Bid && in_SL != 0) in_SL = 0; | |
Set_sl_tp_Quota(OP_BUY,in_SL,in_TP,0); | |
BuyModifySLTP = false; | |
} | |
} | |
} | |
//+------------------------------------------------Martin Start---------------------------------------------------------------------+ | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
double BuyMartinLots_Temp = 0.0; | |
double BuyMartinLots = 0.0; | |
double Step_B = 0.0; | |
if (MartinSW) | |
{ | |
if (MartinIn) | |
{ | |
if (MyOrderTotal(OP_BUY) > 0) | |
{ | |
BuyMartinLots_Temp = MyOrderTotal(OP_BUY)-(MOrders-1); | |
if (BuyMartinLots_Temp < 0) BuyMartinLots_Temp = 0; | |
BuyMartinLots = NormalizeDouble(BuyLots * MathPow(i_Multiple,BuyMartinLots_Temp),2); | |
if (MyOrderTotal(OP_BUY)-1 > ArraySize(MartinStep)-1) | |
{ | |
Step_B=MartinStep[ArrayMaximum(MartinStep)]; | |
}else | |
{ | |
Step_B=MartinStep[MyOrderTotal(OP_BUY)-1]; | |
} | |
if ( (MinPrice_b - Ask) / point > Step_B && MyOrderTotal(OP_BUY) < i_MaxMartin) | |
{ | |
if (MartinBuyBar != iBars(Symbol(),M_Period)) | |
{ | |
MartinBuyBar = iBars(Symbol(),M_Period) + 1; | |
} | |
else if (MartinBuyBar == iBars(Symbol(),M_Period)) | |
{ | |
if (OrderSendA(OP_BUY,Ask,Lime,Symbol(),BuyMartinLots,0,0,Symbol()+"_"+tMComment+DoubleToStr(MyOrderTotal(OP_BUY),0)) > 0) | |
{ | |
BuyModifySLTP = true; | |
MartinBuyBar=0; | |
} | |
} | |
} | |
else | |
{ | |
MartinBuyBar=0; | |
} | |
} | |
else | |
{ | |
MartinBuyBar=0; | |
} | |
} | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
if (AutoRepairSLTP || BuyModifySLTP) | |
{ | |
if (!AnyCS(OP_BUY)) | |
{ | |
if (!CheckQuota_BUY() && MyOrderTotal(OP_BUY) > 1) | |
{ | |
if (SingleOut) | |
{ | |
Set_sl_tp_buy(Symbol(),i_TPPoint,0); | |
} | |
else | |
{ | |
in_TP = MinPrice_b + Cal_TP_Buy(MartinCalLots,OP_BUY,MinPrice_b,i_TPPoint,0) * point; | |
if (in_TP < Bid && in_TP != 0) in_TP = 0; | |
in_SL = 0; | |
if (in_SL > Bid && in_SL != 0) in_SL = 0; | |
Set_sl_tp(OP_BUY,in_TP,in_SL); | |
} | |
BuyModifySLTP = false; | |
} | |
} | |
} | |
} | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
double CSLots=0.0; | |
double LastPrice=0.0; | |
if (MartinAntiMSW && !SingleOut) | |
{ | |
if (MyOrderTotal(OP_BUY)>1) | |
{ | |
if (!CheckQuota_BUY()) | |
{ | |
if (OrderSelect(GetLastOrder(OP_BUY),SELECT_BY_TICKET,MODE_TRADES)) | |
{ | |
CSLots=NormalizeDouble(OrderLots()*CSMultiple,2); | |
if (CSLots<0.01) | |
{ | |
CSLots=0.01; | |
} | |
LastPrice=OrderOpenPrice(); | |
} | |
if ((Ask-LastPrice)/point>=CSStep /*&& (MarketInfo(Symbol(),MODE_SPREAD)) * (Point/point) <= CSStep / 2*/) | |
{ | |
if (Lots_Valid(CSLots) && OrderSend(Symbol(),OP_BUY,CSLots,Ask,20,0,0,Symbol()+"_CS_"+DoubleToStr(MyOrderTotal(OP_BUY),0),id_1,0,Lime)>0) | |
{ | |
BuyModifySLTP = true; | |
} | |
else | |
{ | |
Alert("CS Error: "+ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
if (AutoRepairSLTP || BuyModifySLTP) | |
{ | |
if (AnyCS(OP_BUY)) | |
{ | |
in_TP = MinPrice_b + Cal_TP_Buy(MartinCalLots,OP_BUY,MinPrice_b,i_TPPoint,0) * point; | |
if (in_TP < Bid && in_TP != 0) in_TP = 0; | |
in_SL = MinPrice_b + Cal_TP_CS_BUY(OP_BUY,MinPrice_b,MinTicket_b,CSTPPoint) * point; | |
if (in_SL > Bid && in_SL != 0) | |
{ | |
if (CSProtect) | |
{ | |
if (AnyCS(OP_BUY)) | |
{ | |
if (F_CSProtect(OP_BUY,MinTicket_b,in_SL)) | |
{ | |
return; | |
TicketInfo_BUY(b_Ticket,b_Price,s_Ticket,s_Price,MaxPrice_b,MaxTicket_b,MinPrice_b,MinTicket_b); | |
} | |
} | |
} | |
in_SL = 0; | |
} | |
Set_sl_tp_CS(OP_BUY,in_TP,in_SL,MinTicket_b); | |
BuyModifySLTP = false; | |
} | |
} | |
} | |
} | |
bool CheckQuota_BUY() | |
{ | |
bool Sig=false; | |
double Price[2] = {0}; | |
int x=0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (x==2) | |
break; | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderType()==OP_BUY && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
Price[x]=OrderOpenPrice(); | |
x++; | |
} | |
} | |
if (x < 2) | |
return(false); | |
if (Price[0] > Price[1]) | |
Sig = false; | |
if (Price[0] < Price[1]) | |
Sig = true; | |
return(Sig); | |
} | |
void Set_sl_tp_buy(string symbol,double tp_m,double sl_m) | |
{ | |
double stoploss_m,takeprofit_m; | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==symbol && OrderType()==OP_BUY && OrderMagicNumber()==id_1) | |
{ | |
if (sl_m>0) | |
{ | |
stoploss_m=OrderOpenPrice()-sl_m*point; | |
} | |
else | |
if (sl_m==0) | |
{ | |
stoploss_m=0; | |
} | |
else | |
{ | |
stoploss_m=OrderStopLoss(); | |
} | |
if (tp_m>0) | |
{ | |
takeprofit_m=OrderOpenPrice()+tp_m*point;//宣告一個takeprofit放停利"價位" | |
} | |
else | |
if (tp_m==0) | |
{ | |
takeprofit_m= 0; | |
} | |
else | |
{ | |
takeprofit_m=OrderTakeProfit(); | |
} | |
if (CompareDouble(stoploss_m,OrderStopLoss()) && CompareDouble(takeprofit_m,OrderTakeProfit())) | |
{ | |
continue; | |
} | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss_m,takeprofit_m,0,White)) | |
{ | |
Alert("Set_sl_tp_buy" + IntegerToString(GetLastError())); | |
} | |
} | |
} | |
} | |
void TicketInfo_BUY(int &b_Ticket[],double &b_Price[],int &s_Ticket[],double &s_Price[],double &MaxPrice_b,int &MaxTicket_b,double &MinPrice_b,int &MinTicket_b) | |
{ | |
if (MyOrderTotal(OP_BUY)!=0) | |
{ | |
ArrayResize(b_Ticket,MyOrderTotal(OP_BUY)); | |
ArrayResize(b_Price,MyOrderTotal(OP_BUY)); | |
GetAllOrders(OP_BUY,b_Ticket,s_Ticket); | |
for (int i=0;i<ArraySize(b_Ticket);i++) | |
{ | |
if (!OrderSelect(b_Ticket[i],SELECT_BY_TICKET,MODE_TRADES)) continue; | |
b_Price[i]=OrderOpenPrice(); | |
} | |
MaxPrice_b=b_Price[ArrayMaximum(b_Price)]; | |
MaxTicket_b= b_Ticket[ArrayMaximum(b_Price)]; | |
MinPrice_b = b_Price[ArrayMinimum(b_Price)]; | |
MinTicket_b= b_Ticket[ArrayMinimum(b_Price)]; | |
} | |
} | |
double Cal_TP_Buy(bool Mode,int type,double MinPrice_b,double TPPoint,int WithOut) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
int x = 0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType()==type && OrderMagicNumber()==id_1 && OrderSymbol()==Symbol()) | |
{ | |
x++; | |
if (x <= WithOut) continue; | |
TotalLots+=OrderLots(); | |
TotalDisProduct+= (OrderOpenPrice() - MinPrice_b)/point * OrderLots(); | |
} | |
} | |
if (Mode) { | |
TP_Goal= (TPPoint * fGetFirstLots(OP_BUY) + TotalDisProduct) / TotalLots; | |
}else { | |
TP_Goal= (TPPoint * GetAllLots(OP_BUY) + TotalDisProduct) / TotalLots; | |
} | |
return(TP_Goal); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
if (UninitializeReason() != 3) | |
{ | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if ( | |
(TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) | |
) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
Sleep(1000); | |
GlobalVariableSet("DNSW0",0); | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
bool TimesStopConfig() | |
{ | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(NextSW_str,0); | |
#ifdef GLOBAL_FILE | |
if (!GlobalFileRead()) | |
{ | |
GlobalVars.G_EARunTime = 0; | |
GlobalVars.G_MaxEquity = (AccountProfit() / AccountBalance()) * 100; | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalFileWrite(); | |
GlobalFileRead(); | |
} | |
Print("EARuntime: " + TimeToString(GlobalVars.G_EARunTime,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("MaxLossEquity: " + DoubleToString(GlobalVars.G_MaxEquity,2)); | |
Print("Start_P: " + TimeToString(GlobalVars.G_Start_P,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); | |
Print("Start_Balance: " + DoubleToString(GlobalVars.G_Start_Balance,2)); | |
//+------------------------------------------------------------------+ | |
//|Close All Trades Function Init | | |
//+------------------------------------------------------------------+ | |
#else | |
return(true); | |
} | |
bool TimesStop(double &TimesProfit, double &TodayProfit) | |
{ | |
#ifdef GLOBAL_FILE | |
if (OrdersTotal()==0) | |
{ | |
GlobalVars.G_Start_Balance = AccountBalance(); | |
GlobalVars.G_Start_P = TimeCurrent(); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Status | | |
//+------------------------------------------------------------------+ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVars.G_Start_Balance; | |
else | |
Start_Balance = iBalance; | |
Start_P = GlobalVars.G_Start_P; | |
EARunTime = GlobalVars.G_EARunTime; | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVars.G_MaxEquity) | |
{ | |
GlobalVars.G_MaxEquity = (AccountProfit() / Start_Balance) * 100; | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = iTime(Symbol(),PERIOD_D1,0) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Status | | |
//+------------------------------------------------------------------+ | |
GlobalVars.G_EARunTime = EARunTime; | |
GlobalVars.G_Start_Balance = Start_Balance; | |
GlobalVars.G_Start_P = Start_P; | |
GlobalFileWrite(); | |
#else | |
if (OrdersTotal()==0) | |
{ | |
GlobalVariableSet(Start_Balance_str,AccountBalance()); | |
GlobalVariableSet(Start_P_str,TimeCurrent()); | |
GlobalVariableSet(NextSW_str,0); | |
Sleep(1000); | |
} | |
//+------------------------------------------------------------------+ | |
//|Read Balance | | |
//+------------------------------------------------------------------+ | |
if (GlobalVariableCheck(Start_Balance_str)) | |
{ | |
if(EqualZero(iBalance)) | |
Start_Balance = GlobalVariableGet(Start_Balance_str); | |
else | |
Start_Balance = iBalance; | |
}else | |
{ | |
Start_Balance = AccountBalance(); | |
} | |
if (GlobalVariableCheck(Start_P_str)) | |
{ | |
Start_P = (datetime)GlobalVariableGet(Start_P_str); | |
}else | |
{ | |
Start_P = TimeCurrent(); | |
} | |
if (GlobalVariableCheck(EARUNTIME_str)) | |
{ | |
EARunTime = (datetime)GlobalVariableGet(EARUNTIME_str); | |
} | |
//+------------------------------------------------------------------+ | |
//|Renew Max Equity | | |
//+------------------------------------------------------------------+ | |
if ((AccountProfit() / Start_Balance) * 100 < GlobalVariableGet(MaxEquity_str)) | |
{ | |
GlobalVariableSet(MaxEquity_str,(AccountProfit() / Start_Balance)*100); | |
} | |
//+------------------------------------------------------------------+ | |
//|Check Balance | | |
//+------------------------------------------------------------------+ | |
if (Start_Balance < AccountBalance() / 2 || Start_P == 0) | |
{ | |
Alert("Check your balance setting"); | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
//|Times Take Profit | | |
//+------------------------------------------------------------------+ | |
TimesProfit = ((GetProfitPeriod(Start_P) + AccountProfit()) / Start_Balance) * 100; | |
if (TakeProfitPercentSW && TimesProfit >= TakeProfitPercent) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Daily Take Profit | | |
//+------------------------------------------------------------------+ | |
TodayProfit = ((GetProfitPeriod(StrToTime(TimeToStr(TimeCurrent(),TIME_DATE))) + AccountProfit()) / Start_Balance)* 100; | |
if (TakeProfitPercentDaySW && TimesProfit >= TakeProfitPercentDay) | |
{ | |
GlobalVariableSet(NextSW_str,1); | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
WriteLog(MODE_DAILYTP,DoubleToString(TimesProfit,2) + "%"); | |
} | |
//+------------------------------------------------------------------+ | |
//|Account Stop Loss | | |
//+------------------------------------------------------------------+ | |
if (StopLossPercentSW && (AccountProfit() / AccountBalance()) * 100 <= -1 * StopLossPercent) | |
{ | |
//Close all Trades | |
GlobalVariableSet(NextSW_str,1); | |
WriteLog(MODE_TIMESSL,DoubleToString((AccountProfit() / AccountBalance()) * 100,2) + "%"); | |
//Shutdown the EA | |
if (SleepLoss) | |
{ | |
EARunTime = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE)) + 86400; | |
} | |
} | |
//+------------------------------------------------------------------+ | |
//|Write Balnace | | |
//+------------------------------------------------------------------+ | |
GlobalVariableSet(EARUNTIME_str,EARunTime); | |
GlobalVariableSet(Start_P_str,(double)Start_P); | |
GlobalVariableSet(Start_Balance_str,Start_Balance); | |
#endif | |
return(true); | |
} | |
bool Lots_Valid(const double iLot) | |
{ | |
double ValuePerLot = SymbolInfoDouble(NULL,SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE); | |
if ((iLot * ValuePerLot) > AccountInfoDouble(ACCOUNT_FREEMARGIN)) | |
{ | |
Alert("No Money"); | |
return(false); | |
} | |
return(true); | |
} | |
int EndersGamgeCnt = 0; | |
void EndersGameModule(const string symbol, const int Type, int id, bool &flag) | |
{ | |
if (MyOrderTotal_Enders(symbol,Type,id) == 0) | |
{ | |
for (int j=0;j<OrdersHistoryTotal();j++) | |
{ | |
if (!OrderSelect(j,SELECT_BY_POS,MODE_HISTORY)) continue; | |
if (OrderCloseTime() > EGStartTime) | |
{ | |
EndersGamgeCnt++; | |
Alert(Symbol() + " 戰場已完成第" + IntegerToString(EndersGamgeCnt) + " 次戰役"); | |
EGStartTime = TimeCurrent(); | |
break; | |
} | |
} | |
} | |
if (EndersGamgeCnt >= OutCnt) | |
{ | |
Alert("已達成設定局數,即將離開 " + Symbol() + " 戰場"); | |
flag = false; | |
ExpertRemove(); | |
} | |
} | |
int MyOrderTotal_Enders(const string symbol,const int Type,const int ID) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (Type == OP_ALL && OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
continue; | |
} | |
if ((Type == OP_BUY && ((OrderType() & 1) == 1 ? false : true)) || (Type == OP_SELL && ((OrderType() & 1) == 1 ? true : false))) | |
{ | |
if (OrderSymbol() == symbol && OrderMagicNumber() == ID) | |
{ | |
x++; | |
} | |
} | |
} | |
return(x); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//+------------------------------------------------------------------+ | |
//| ProjectName | | |
//| Copyright 2012, CompanyName | | |
//| http://www.companyname.net | | |
//+------------------------------------------------------------------+ | |
#property strict | |
#include <WarGameDLL.mqh> | |
#include <WinUser32.mqh> | |
#import "stdlib.ex4" | |
string ErrorDescription(int error_code); | |
#import "User32.dll" | |
int GetDC(int); | |
int ReleaseDC(int,int); | |
int GetSystemMetrics(int nIndex); | |
#import "Gdi32.dll" | |
int GetDeviceCaps(int,int); | |
#import "shell32.dll" | |
int ShellExecuteW(int hWnd, string lpVerb, string lpFile, string lpParameters, string lpDirectory, int nCmdShow); | |
#import "kernel32.dll" | |
ulong GetVersion(void); | |
#import "wininet.dll" | |
int InternetOpenUrlW(int hInternetSession,string sUrl,string sHeaders="",int lHeadersLength=0,uint lFlags=0,int lContext=0); | |
int InternetReadFile(int hFile,uchar &sBuffer[],int lNumBytesToRead,int& lNumberOfBytesRead[]); | |
int InternetCloseHandle(int hInet); | |
int InternetOpenW(string sAgent,int lAccessType,string sProxyName="",string sProxyBypass="",int lFlags=0); | |
#import | |
#define CHT | |
//#define ENG | |
#define MAX 128 | |
#define OP_ALL 101 | |
#define MODE_AGET 0 | |
#define MODE_FGET 1 | |
#define MODE_INIT 3 | |
#define MODE_TICK 4 | |
#define MODE_SLTP_BYPOINT 102 | |
#define MODE_SLTP_BYPRICE 103 | |
#define TOLERANCE 0.000001 | |
#define EA_VERSION "5.0" | |
#define BUFFER_LEN 1024 | |
#define INTERNET_FLAG_PRAGMA_NOCACHE 0x00000100 // Forces the request to be resolved by the origin server, even if a cached copy exists on the proxy. | |
#define INTERNET_FLAG_NO_CACHE_WRITE 0x04000000 // Does not add the returned entity to the cache. | |
#define INTERNET_FLAG_RELOAD 0x80000000 // Forces a download of the requested file, object, or directory listing from the origin server, not from the cache. | |
double Price = 0.0; | |
double Lots = 0.0; | |
double TPPrice = 0.0; | |
double SLPrice = 0.0; | |
double TPPips = 0.0; | |
double SLPips = 0.0; | |
double RangeBuy = 0.0; | |
double RangeSell = 0.0; | |
string ProfitOutSW = ""; | |
string ProfitOutLossSW = ""; | |
double TPsPrice = 0.0; | |
double SLsPrice = 0.0; | |
double TPsPips = 0.0; | |
double SLsPips = 0.0; | |
int id_1 = 0; | |
int InitXDis = 10; | |
int InitXDisSlave = 10; | |
int InitYDis = 100; | |
int InitYDisSlave = 10; | |
int LabelFontSize = 10; | |
int Space = 30; | |
int ErrorCode = 0; | |
int width; | |
int DownloadHour = 6; | |
int DownloadMin = 0; | |
datetime H1BarTime = 0; | |
bool ProfitOut = false; | |
bool ProfitOutLoss = false; | |
double point = 0.0; | |
double FontDPI=96; | |
double DPIratio = 0.8; | |
double BuyCloseData[MAX][2] = {0}; | |
double SellCloseData[MAX][2] = {0}; | |
double TrailStop = 0.0; | |
#ifdef CHT | |
string AllFont = "標楷體"; | |
#endif | |
#ifdef ENG | |
string AllFont = "Impact"; | |
#endif | |
string AllFontENG = "Impact"; | |
string VisiSW[3] = {"SW0","SW1","SW2"}; | |
string check_on = "\Images\check_on.bmp"; | |
string check_off = "\Images\check_off.bmp"; | |
string CheckBox[8] = {"CheckBox0", "CheckBox1", "CheckBox2", "CheckBox3", "CheckBox4", "CheckBox5", "CheckBox6" ,"CheckBox7"}; | |
string CheckBoxs[4] = {"CheckBoxs0", "CheckBoxs1", "ChecksBox2", "ChecksBox3"}; | |
string BGID[2] = {"BG","BG1"}; | |
string logoID[2] = {"logo",""}; | |
string BGIDslave[1] = {"BGs0"}; | |
string buttonID[11] = {"Button0", "Button1", "Button2", "Button3", "Button4" ,"Button5", "Button6", "Button7", "Button8" , "Button9","Button10"}; | |
string buttonsID[2] = {"Buttons0", "Buttons1"}; | |
string labelID[37] = {"label0", "label1", "label2", "label3", "label4" , "label5" ,"label6" ,"label7" ,"label8" ,"label9" ,"label10","label11","label12","label13","label14","label15","label16","label17","label18","label19","label20","label21","label22","label23","label24","label25","label26","label27","label28","label29","label30","label31","label32","label33","label34","label35","label36"}; | |
string labelsID[10] = {"labels0","labels1","labels2","labels3","labels4","labels5","labels6","labels7","labels8","labels9"}; | |
#ifdef CHT | |
string buttonsText[2] = {"修改買單","修改賣單"}; | |
string buttonText[11] = {"進場買", "進場賣", "神鬼掛單","平倉買單", "水雷", "平倉賣單" ,"刪除買單", "刪除掛單","刪除賣單", "重設","修改選單"}; | |
string labelText[37] = {"市場價", "手數", "獲利(點)", "停損(點)", "獲利(價)", "停損(價)" ,"買","賣", "只平獲利", "追蹤止損", "保護","開始點","MagicNumber", "預付款設定%", "達比率點數: " ,"" ,"達比率價格: " ,"","買: ","0","手: ","0","賣: ","0","手: ","0","只平虧損","盈虧:","0","盈虧: ","0","全: " ,"0","手: ","0","盈虧: ","0"}; | |
string labelsText[10] = {"買單盈虧","賣單盈虧","價位","價位","獲利(點)","停損(點)","獲利(價)","停損(價)","全部盈虧","價位"}; | |
#endif | |
#ifdef ENG | |
string buttonsText[2] = {"Modify Buy","Modify Sell"}; | |
string buttonText[11] = {"Buy Order", "Sell Order", "Two Way Order","Close Buy", "Close All", "Close Sell" ,"Delete Buy", "Delete All","Delete Sell", "Reset","Advanced"}; | |
string labelText[37] = {"Price", "Lots", "TP(Pips)", "SL(Pips)", "TP(Price)", "SL(Price)" ,"Buy","Sell", "Close Profit Only", "Trail Stop", "B. E.","Start LV","MagicNumber", "Margin Level", "Margin Call Pips: " ,"" ,"Margin Call Price: " ,"","Buy: ","0","Lot: ","0","Sell: ","0","Lot: ","0","Close Loss Only","P/L: ","0","P/L: ","0","ALL: ","0","Lot: ","0","P/L: ","0"}; | |
string labelsText[10] = {"Buy P/L","Sell P/L","Price","Price","TP(Pips)","SL(Pips)","TP(Price)","SL(Price)","ALL P/L","Price"}; | |
#endif | |
string editID[13] = {"edit0", "edit1", "edit2", "edit3", "edit4","edit5", "edit6" ,"edit7", "edit8", "edit9", "edit10", "edit11","edit12"}; | |
string editsID[10] = {"edits0", "edits1", "edits2", "edits3", "edits4","edits5","edits6","edits7","edits8","edits9"}; | |
string editText[13] = {"0","0.01","10","10","","","10","10","5","5","5","","30"}; | |
string editsText[10] = {"0","0","10","10","0","0","0","0","0","0"}; | |
struct systemtime | |
{ | |
ushort wYear; | |
ushort wMonth; | |
ushort wDayOfWeek; | |
ushort wDay; | |
ushort wHour; | |
ushort wMinute; | |
ushort wSecond; | |
ushort wMilliseconds; | |
}; | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
int OnInit() | |
{ | |
if (UninitializeReason() != 3) | |
{ | |
if (!CheckValid()) return(-1); | |
} | |
ulong dwVersion = GetVersion(); | |
uint dwMajorVersion = (uchar)(((ushort)(dwVersion & 0xffff)) & 0xff); | |
//printf("Major Version Of OS: %d",dwMajorVersion); | |
if (dwMajorVersion < 6) | |
{ | |
Alert("請注意!使用Windows XP(Server 2003)之系統將無法保證交易能正常執行!"); | |
} | |
int dc=GetDC(0); | |
FontDPI=GetDeviceCaps(dc,88); | |
if (FontDPI==0) FontDPI=96; | |
ReleaseDC(0,dc); | |
DPIratio = FontDPI / 120; | |
int YSCREEN = GetSystemMetrics(1); | |
if (YSCREEN <= 1024) Alert("建議解析度在 1280 * 1024 以上"); | |
if (IsTesting()==true) | |
{ | |
return(0); | |
} | |
MathSrand(GetTickCount()); | |
DownloadMin = 1 + ((59 * MathRand()) / 32768); | |
//+------------------------------------------------------------------+ | |
if (Digits==5 || Digits==3) | |
{ | |
point=Point*10; | |
} | |
else | |
{ | |
point=Point; | |
} | |
bgCreate(); | |
bgsCreate(1); | |
CheckBoxCreate(1); | |
CheckBoxsCreate(1); | |
editCreate(); | |
editsCreate(1); | |
editClear(); | |
LoadPrevious(); | |
CreateSW(1); | |
ButtonsCreate(1); | |
labelsCreate(1); | |
InitXDisSlave = (int)(ObjectGetInteger(0,BGID[0],OBJPROP_XSIZE) / DPIratio) + 20; | |
EventSetMillisecondTimer(100); | |
//+------------------------------------------------------------------+ | |
return(0); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void OnDeinit(const int reason) | |
{ | |
//---- | |
EventKillTimer(); | |
SaveParameter(); | |
DeleteObj(); | |
//---- | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
void OnTimer() | |
{ | |
if (GlobalVariableGet("RUNEA")==1) //EA控制器 | |
{ | |
Alert("EA is not working"); | |
return; | |
} | |
//+------------------------------------------------Author End----------------------------------------------------------------------------+ | |
bgCreate(); | |
bgsCreate(0); | |
ButtonCreate(); | |
logoCreate(); | |
CheckBoxCreate(0); | |
CheckBoxsCreate(0); | |
labelCreate(); | |
labelsCreate(0); | |
editCreate(); | |
editsCreate(0); | |
GetValue(); | |
ButtonsCreate(0); | |
bgsCreate(0); | |
string str_temp = ObjectGetString(0,CheckBox[5],OBJPROP_BMPFILE); | |
if (str_temp == check_on) | |
{ | |
double MoveSL = StringToDouble(ObjectGetString(0,editID[8],OBJPROP_TEXT,0)); | |
iMoveStopLoss(MoveSL); | |
} | |
str_temp = ObjectGetString(0,CheckBox[6],OBJPROP_BMPFILE); | |
if (str_temp == check_on) | |
{ | |
double BE = StringToDouble(ObjectGetString(0,editID[9],OBJPROP_TEXT,0)); | |
double LV = StringToDouble(ObjectGetString(0,editID[10],OBJPROP_TEXT,0)); | |
iBE(BE,LV); | |
} | |
//width = (int)ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0); | |
//InitXDis = width - (int)ObjectGetInteger(0,BGID[0],OBJPROP_XSIZE); | |
//InitXDisSlave = InitXDis /*- (int)ObjectGetInteger(0,BGIDslave[0],OBJPROP_XSIZE)*/ - (int)ObjectGetInteger(0,BGID[0],OBJPROP_XSIZE) - 10; | |
MarginCall(); | |
CreateSW(0); | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
double NormPoint(double inF) | |
{ | |
return(inF * point); | |
} | |
//+------------------------------------------------------------------+ | |
//| | | |
//+------------------------------------------------------------------+ | |
bool FindCommentOrder(int type,string in) | |
{ | |
for (int i = 0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
if (StringFind(OrderComment(),in) >= 0) | |
return(true); | |
} | |
} | |
return(false); | |
} | |
//+------------------------------------------------------------------+ | |
int MyOrderTotal(int type) | |
{ | |
int x=0; | |
for(int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) continue; | |
if(OrderSymbol()==Symbol() && OrderType()==type && OrderMagicNumber()==id_1) | |
{ | |
x++; | |
} | |
} | |
return(x); | |
} | |
int SearchArray(double& in[][],int match) | |
{ | |
for (int i=0;i<MAX;i++) | |
{ | |
if ((double)match == in[i][0]) return(i); | |
} | |
return(-1); | |
} | |
void SetKbar(double& Kbar[]) | |
{ | |
Kbar[0] = (double)iTime(Symbol(),1,0); | |
Kbar[1] = iOpen(Symbol(),1,0); | |
Kbar[2] = iHigh(Symbol(),1,0); | |
Kbar[3] = iLow(Symbol(),1,0); | |
Kbar[4] = iClose(Symbol(),1,0); | |
Kbar[5] = (double)iVolume(Symbol(),1,0); | |
} | |
void CheckBoxCreate(const int Mode) | |
{ | |
int i = 0; | |
for (;i<ArraySize(CheckBox);i++) | |
{ | |
if (ObjectFind(0,CheckBox[i]) < 0) ObjectCreate(0,CheckBox[i],OBJ_BITMAP_LABEL,0,100,100); | |
if (Mode == 1) | |
{ | |
ObjectSetString(0,CheckBox[i],OBJPROP_BMPFILE,(i <= 1 ? check_on :check_off)); | |
}else | |
{ | |
ObjectSetInteger(0,CheckBox[i],OBJPROP_XDISTANCE,(int)(CheckBoxX(i) * DPIratio)); | |
ObjectSetInteger(0,CheckBox[i],OBJPROP_YDISTANCE,(int)(CheckBoxY(i) * DPIratio)); | |
//ObjectSetInteger(0,CheckBox[i],OBJPROP_ZORDER,1) | |
} | |
} | |
} | |
void CheckBoxsCreate(const int Mode) | |
{ | |
int i = 0; | |
for (;i<ArraySize(CheckBoxs);i++) | |
{ | |
if (ObjectFind(0,CheckBoxs[i]) < 0) ObjectCreate(0,CheckBoxs[i],OBJ_BITMAP_LABEL,0,100,100); | |
if (Mode == 1) | |
{ | |
ObjectSetString(0,CheckBoxs[i],OBJPROP_BMPFILE,(i <= 1 ? check_on :check_off)); | |
ObjectSetInteger(0,CheckBoxs[i],OBJPROP_TIMEFRAMES,EMPTY); | |
}else | |
{ | |
ObjectSetInteger(0,CheckBoxs[i],OBJPROP_XDISTANCE,(int)(CheckBoxsX(i) * DPIratio)); | |
ObjectSetInteger(0,CheckBoxs[i],OBJPROP_YDISTANCE,(int)(CheckBoxsY(i) * DPIratio)); | |
} | |
} | |
} | |
int CheckBoxsX(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 2: | |
Out = (InitXDisSlave + 75); | |
break; | |
case 1: | |
case 3: | |
Out = (InitXDisSlave + 255); | |
break; | |
} | |
return(Out); | |
} | |
int CheckBoxX(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 2: | |
Out = (InitXDis + 75); | |
break; | |
case 1: | |
case 3: | |
Out = (InitXDis + 255); | |
break; | |
case 4: | |
case 5: | |
Out = (InitXDis); | |
break; | |
case 7: | |
case 6: | |
Out = (InitXDis + 140); | |
break; | |
} | |
return(Out); | |
} | |
int CheckBoxsY(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
Out = (InitYDisSlave + (LabelFontSize / 2) + Space * 3); | |
break; | |
case 2: | |
case 3: | |
Out = (InitYDisSlave + (LabelFontSize / 2) + Space * 4); | |
break; | |
} | |
return(Out); | |
} | |
int CheckBoxY(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
Out = (InitYDis + (LabelFontSize / 2) + Space); | |
break; | |
case 2: | |
case 3: | |
Out = (InitYDis + (LabelFontSize / 2) + Space * 2); | |
break; | |
case 4: | |
case 7: | |
Out = (InitYDis + (Space * 5) + (LabelFontSize)); | |
break; | |
case 5: | |
case 6: | |
Out = (InitYDis + (Space * 8) + (LabelFontSize)); | |
break; | |
} | |
return(Out); | |
} | |
void logoCreate() | |
{ | |
int i = 0; | |
for (;i<ArraySize(logoID);i++) | |
{ | |
if (ObjectFind(0,logoID[i]) < 0) ObjectCreate(0,logoID[i],OBJ_LABEL,0,100,100); | |
ObjectSetInteger(0,logoID[i],OBJPROP_COLOR,clrGold); | |
ObjectSetInteger(0,logoID[i],OBJPROP_XDISTANCE,(int)((InitXDis + 20) * DPIratio)); | |
ObjectSetInteger(0,logoID[i],OBJPROP_YDISTANCE,(int)(16 * DPIratio)); | |
ObjectSetInteger(0,logoID[i],OBJPROP_CORNER,0); | |
ObjectSetString(0,logoID[i],OBJPROP_TEXT,"XBomb Tool Kit"); | |
ObjectSetString(0,logoID[i],OBJPROP_FONT,"Impact"); | |
ObjectSetInteger(0,logoID[i],OBJPROP_FONTSIZE,30); | |
ObjectSetInteger(0,logoID[i],OBJPROP_SELECTABLE,0); | |
} | |
} | |
void bgCreate() | |
{ | |
int i = 0; | |
for (;i<ArraySize(BGID);i++) | |
{ | |
if (ObjectFind(0,BGID[i]) < 0) ObjectCreate(0,BGID[i],OBJ_RECTANGLE_LABEL,0,100,100); | |
ObjectSetInteger(0,BGID[i],OBJPROP_BORDER_COLOR,i == 0 ? C'25,35,45' : clrGold); | |
ObjectSetInteger(0,BGID[i],OBJPROP_BORDER_TYPE,i == 0 ? BORDER_FLAT : BORDER_RAISED); | |
ObjectSetInteger(0,BGID[i],OBJPROP_BGCOLOR,(i == 0 ? C'25,35,45' : C'139,102,16')); | |
ObjectSetInteger(0,BGID[i],OBJPROP_COLOR,(i == 0 ? C'25,35,45' : C'139,102,16')); | |
ObjectSetInteger(0,BGID[i],OBJPROP_XDISTANCE,(int)(( i == 0 ? InitXDis - 10 : InitXDis + 5) * DPIratio)); | |
ObjectSetInteger(0,BGID[i],OBJPROP_YDISTANCE,(int)(( i == 0 ? 0 : 15) * DPIratio)); | |
ObjectSetInteger(0,BGID[i],OBJPROP_XSIZE,(int)(( i == 0 ? 362 : 330) * DPIratio)); | |
ObjectSetInteger(0,BGID[i],OBJPROP_YSIZE,(int)(( i == 0 ? 620 : 60) * DPIratio)); | |
ObjectSetInteger(0,BGID[i],OBJPROP_CORNER,0); | |
} | |
} | |
void bgsCreate(const int Mode) | |
{ | |
int i = 0; | |
for (;i<ArraySize(BGIDslave);i++) | |
{ | |
if (ObjectFind(0,BGIDslave[i]) < 0) ObjectCreate(0,BGIDslave[i],OBJ_RECTANGLE_LABEL,0,100,100); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_BORDER_COLOR,C'25,35,45'); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_BORDER_TYPE,BORDER_FLAT); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_BGCOLOR,C'25,35,45'); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_COLOR,C'25,35,45'); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_XDISTANCE,(int)((InitXDisSlave - 10) * DPIratio)); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_YDISTANCE,0); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_XSIZE,(int)(362 * DPIratio)); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_YSIZE,(int)(310 * DPIratio)); | |
ObjectSetInteger(0,BGIDslave[i],OBJPROP_CORNER,0); | |
if (Mode == 1) ObjectSetInteger(0,BGIDslave[i],OBJPROP_TIMEFRAMES,EMPTY); | |
} | |
} | |
void editCreate() | |
{ | |
int i = 0; | |
for (;i<ArraySize(editID);i++) | |
{ | |
if (ObjectFind(0,editID[i]) < 0) ObjectCreate(0,editID[i],OBJ_EDIT,0,100,100); | |
ObjectSetInteger(0,editID[i],OBJPROP_COLOR,clrBlack); | |
ObjectSetInteger(0,editID[i],OBJPROP_BGCOLOR,clrSilver); | |
ObjectSetInteger(0,editID[i],OBJPROP_BORDER_COLOR,C'153,163,174'); | |
ObjectSetInteger(0,editID[i],OBJPROP_XDISTANCE,(int)(editX(i) * DPIratio)); | |
ObjectSetInteger(0,editID[i],OBJPROP_YDISTANCE,(int)(editY(i) * DPIratio)); | |
ObjectSetInteger(0,editID[i],OBJPROP_XSIZE,(int)(sizeXEdit(i) * DPIratio)); | |
ObjectSetInteger(0,editID[i],OBJPROP_YSIZE,(int)(25 * DPIratio)); | |
ObjectSetInteger(0,editID[i],OBJPROP_CORNER,0); | |
ObjectSetString(0,editID[i],OBJPROP_FONT,AllFontENG); | |
ObjectSetInteger(0,editID[i],OBJPROP_FONTSIZE,(int)(10 * DPIratio)); | |
ObjectSetInteger(0,editID[i],OBJPROP_SELECTABLE,0); | |
ObjectSetInteger(0,editID[i],OBJPROP_ALIGN,ALIGN_CENTER); | |
} | |
} | |
void editsCreate(const int Mode) | |
{ | |
int i = 0; | |
for (;i<ArraySize(editsID);i++) | |
{ | |
if (ObjectFind(0,editsID[i]) < 0) ObjectCreate(0,editsID[i],OBJ_EDIT,0,100,100); | |
ObjectSetInteger(0,editsID[i],OBJPROP_COLOR,clrBlack); | |
ObjectSetInteger(0,editsID[i],OBJPROP_BGCOLOR,clrSilver); | |
ObjectSetInteger(0,editsID[i],OBJPROP_BORDER_COLOR,C'153,163,174'); | |
ObjectSetInteger(0,editsID[i],OBJPROP_XDISTANCE,(int)(editsX(i) * DPIratio)); | |
ObjectSetInteger(0,editsID[i],OBJPROP_YDISTANCE,(int)(editsY(i) * DPIratio)); | |
ObjectSetInteger(0,editsID[i],OBJPROP_XSIZE,(int)(sizeXEdits(i) * DPIratio)); | |
ObjectSetInteger(0,editsID[i],OBJPROP_YSIZE,(int)(25 * DPIratio)); | |
ObjectSetInteger(0,editsID[i],OBJPROP_CORNER,0); | |
ObjectSetString(0,editsID[i],OBJPROP_FONT,AllFontENG); | |
ObjectSetInteger(0,editsID[i],OBJPROP_FONTSIZE,(int)(10 * DPIratio)); | |
ObjectSetInteger(0,editsID[i],OBJPROP_SELECTABLE,0); | |
ObjectSetInteger(0,editsID[i],OBJPROP_ALIGN,ALIGN_CENTER); | |
if (Mode == 1) ObjectSetInteger(0,editsID[i],OBJPROP_TIMEFRAMES,EMPTY); | |
} | |
} | |
int sizeXEdits(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 2: | |
case 3: | |
case 4: | |
case 5: | |
Out = (65); | |
break; | |
default: | |
Out = (85); | |
break; | |
} | |
return(Out); | |
} | |
int sizeXEdit(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 2: | |
case 3: | |
case 4: | |
case 5: | |
Out = (65); | |
break; | |
case 6: | |
case 7: | |
Out = (25); | |
break; | |
case 8: | |
case 9: | |
case 10: | |
Out = (40); | |
break; | |
default: | |
Out = (85); | |
break; | |
} | |
return(Out); | |
} | |
void editClear() | |
{ | |
int i = 0; | |
for (;i<ArraySize(editID);i++) | |
{ | |
if (ObjectFind(0,editID[i]) < 0) ObjectCreate(0,editID[i],OBJ_EDIT,0,100,100); | |
ObjectSetString(0,editID[i],OBJPROP_TEXT,editText[i]); | |
} | |
for (i=0;i<ArraySize(editsID);i++) | |
{ | |
if (ObjectFind(0,editsID[i]) < 0) ObjectCreate(0,editsID[i],OBJ_EDIT,0,100,100); | |
ObjectSetString(0,editsID[i],OBJPROP_TEXT,editsText[i]); | |
} | |
} | |
int editsX(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
case 8: | |
Out = (InitXDisSlave + 75); | |
break; | |
case 6: | |
case 7: | |
case 9: | |
Out = (InitXDisSlave + 255); | |
break; | |
case 2: | |
case 4: | |
Out = (InitXDisSlave + 95); | |
break; | |
case 3: | |
case 5: | |
Out = (InitXDisSlave + 275); | |
break; | |
} | |
return(Out); | |
} | |
int editX(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
Out = (InitXDis + 75); | |
break; | |
case 1: | |
Out = (InitXDis + 255); | |
break; | |
case 2: | |
case 4: | |
Out = (InitXDis + 95); | |
break; | |
case 3: | |
case 5: | |
Out = (InitXDis + 275); | |
break; | |
case 6: | |
Out = (InitXDis + 205); | |
break; | |
case 7: | |
Out = (InitXDis + 270); | |
break; | |
case 8: | |
Out = (InitXDis + 90); | |
break; | |
case 9: | |
Out = (InitXDis + 200); | |
break; | |
case 10: | |
Out = (InitXDis + 300); | |
break; | |
case 11: | |
case 12: | |
Out = (InitXDis + 110); | |
break; | |
} | |
return(Out); | |
} | |
int editsY(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 6: | |
Out = (InitYDisSlave); | |
break; | |
case 1: | |
case 7: | |
Out = (InitYDisSlave + Space); | |
break; | |
case 8: | |
case 9: | |
Out = (InitYDisSlave + Space * 2); | |
break; | |
case 2: | |
case 3: | |
Out = (InitYDisSlave + Space * 3); | |
break; | |
case 4: | |
case 5: | |
Out = (InitYDisSlave + Space * 4); | |
break; | |
} | |
return(Out); | |
} | |
int editY(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
Out = (InitYDis); | |
break; | |
case 2: | |
case 3: | |
Out = (InitYDis + Space); | |
break; | |
case 4: | |
case 5: | |
Out = (InitYDis + Space * 2); | |
break; | |
case 6: | |
case 7: | |
Out = (InitYDis + Space * 4); | |
break; | |
case 8: | |
case 9: | |
case 10: | |
Out = (InitYDis + (Space * 8) + (LabelFontSize)); | |
break; | |
case 11: | |
Out = (InitYDis + (Space * 9) + (LabelFontSize)); | |
break; | |
case 12: | |
Out = (InitYDis + (Space * 10) + (LabelFontSize)); | |
break; | |
} | |
return(Out); | |
} | |
void labelCreate() | |
{ | |
int i = 0; | |
for (;i<ArraySize(labelID);i++) | |
{ | |
if (ObjectFind(0,labelID[i]) < 0) ObjectCreate(0,labelID[i],OBJ_LABEL,0,100,100); | |
ObjectSetInteger(0,labelID[i],OBJPROP_COLOR,colorLabel(i)); | |
//ObjectSetInteger(0,labelID[i],OBJPROP_BGCOLOR,clrDimGray); | |
ObjectSetInteger(0,labelID[i],OBJPROP_XDISTANCE,(int)(LabelX(i) * DPIratio)); | |
ObjectSetInteger(0,labelID[i],OBJPROP_YDISTANCE,(int)(LabelY(i) * DPIratio)); | |
ObjectSetInteger(0,labelID[i],OBJPROP_CORNER,0); | |
ObjectSetString(0,labelID[i],OBJPROP_FONT,fontLabel(i)); | |
ObjectSetString(0,labelID[i],OBJPROP_TEXT,labelText[i]); | |
ObjectSetInteger(0,labelID[i],OBJPROP_FONTSIZE,(int)(LabelFontSize)); | |
ObjectSetInteger(0,labelID[i],OBJPROP_SELECTABLE,0); | |
ObjectSetInteger(0,labelID[i],OBJPROP_ANCHOR,(i == 30 || i== 28 || i== 36 ? ANCHOR_RIGHT_UPPER : ANCHOR_LEFT_UPPER)); | |
} | |
} | |
void labelsCreate(const int Mode) | |
{ | |
int i = 0; | |
for (;i<ArraySize(labelsID);i++) | |
{ | |
if (ObjectFind(0,labelsID[i]) < 0) ObjectCreate(0,labelsID[i],OBJ_LABEL,0,100,100); | |
ObjectSetInteger(0,labelsID[i],OBJPROP_COLOR,colorLabels(i)); | |
ObjectSetInteger(0,labelsID[i],OBJPROP_XDISTANCE,(int)(LabelsX(i) * DPIratio)); | |
ObjectSetInteger(0,labelsID[i],OBJPROP_YDISTANCE,(int)(LabelsY(i) * DPIratio)); | |
ObjectSetInteger(0,labelsID[i],OBJPROP_CORNER,0); | |
ObjectSetString(0,labelsID[i],OBJPROP_FONT,fontLabels(i)); | |
ObjectSetString(0,labelsID[i],OBJPROP_TEXT,labelsText[i]); | |
ObjectSetInteger(0,labelsID[i],OBJPROP_FONTSIZE,(int)(LabelFontSize)); | |
ObjectSetInteger(0,labelsID[i],OBJPROP_SELECTABLE,0); | |
if (Mode == 1) ObjectSetInteger(0,labelsID[i],OBJPROP_TIMEFRAMES,EMPTY); | |
} | |
} | |
string fontLabels(const int in) | |
{ | |
return(AllFont); | |
} | |
string fontLabel(const int in) | |
{ | |
if (in == 12) return(AllFontENG); | |
if (in == 17 || in == 15) return(AllFontENG); | |
if (in == 19 || in == 21 || in == 23 || in == 25 || in == 28 || in == 30 || in == 32 || in == 34 || in == 36) return(AllFontENG); | |
return(AllFont); | |
} | |
int colorLabels(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 5: | |
case 7: | |
Out = C'173,93,71'; | |
break; | |
case 4: | |
case 6: | |
Out = C'105,125,83'; | |
break; | |
case 14: | |
case 16: | |
Out = C'162,144,83'; | |
break; | |
default: | |
Out = (clrSilver); | |
break; | |
} | |
return(Out); | |
} | |
int colorLabel(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 3: | |
case 5: | |
Out = C'173,93,71'; | |
break; | |
case 2: | |
case 4: | |
Out = C'105,125,83'; | |
break; | |
case 14: | |
case 16: | |
Out = C'162,144,83'; | |
break; | |
case 19: | |
case 21: | |
case 28: | |
Out = clrLime; | |
break; | |
case 30: | |
case 23: | |
case 25: | |
Out = clrRed; | |
break; | |
default: | |
Out = (clrSilver); | |
break; | |
} | |
if (in == 32 || in == 34 || in == 36) Out = clrGold; | |
return(Out); | |
} | |
int LabelsX(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
case 4: | |
case 6: | |
case 8: | |
Out = (InitXDisSlave); | |
break; | |
case 2: | |
case 3: | |
case 5: | |
case 7: | |
case 9: | |
Out = (InitXDisSlave + 175); | |
break; | |
} | |
return(Out); | |
} | |
int LabelX(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 2: | |
case 4: | |
case 12: | |
case 13: | |
case 14: | |
case 16: | |
case 18: | |
case 22: | |
case 31: | |
Out = (InitXDis); | |
break; | |
case 1: | |
case 3: | |
case 5: | |
case 6: | |
Out = (InitXDis + 175); | |
break; | |
case 7: | |
Out = (InitXDis + 235); | |
break; | |
case 8: | |
case 9: | |
Out = (InitXDis + 20); | |
break; | |
case 10: | |
Out = (InitXDis + 160); | |
break; | |
case 11: | |
Out = (InitXDis + 245); | |
break; | |
case 15: | |
case 17: | |
Out = (InitXDis + 130); | |
break; | |
case 19: | |
case 23: | |
case 32: | |
Out = (InitXDis + 40); | |
break; | |
case 20: | |
case 24: | |
case 33: | |
Out = (InitXDis + 60); | |
break; | |
case 21: | |
case 25: | |
case 34: | |
Out = (InitXDis + 90); | |
break; | |
case 26: | |
Out = (InitXDis + 160); | |
break; | |
case 27: | |
case 29: | |
case 35: | |
Out = (InitXDis + 140); | |
break; | |
case 28: | |
case 30: | |
case 36: | |
Out = (InitXDis + 280); | |
break; | |
} | |
return(Out); | |
} | |
int LabelsY(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 2: | |
Out = (InitYDisSlave); | |
break; | |
case 1: | |
case 3: | |
Out = (InitYDisSlave + Space); | |
break; | |
case 8: | |
case 9: | |
Out = (InitYDisSlave + Space * 2); | |
break; | |
case 4: | |
case 5: | |
Out = (InitYDisSlave + Space * 3); | |
break; | |
case 6: | |
case 7: | |
Out = (InitYDisSlave + Space * 4); | |
break; | |
} | |
return(Out); | |
} | |
int LabelY(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
Out = (InitYDis); | |
break; | |
case 2: | |
case 3: | |
Out = (InitYDis + Space); | |
break; | |
case 4: | |
case 5: | |
Out = (InitYDis + Space * 2); | |
break; | |
case 6: | |
case 7: | |
Out = (InitYDis + Space * 4); | |
break; | |
case 8: | |
Out = (InitYDis + Space * 5 + LabelFontSize); | |
break; | |
case 9: | |
case 10: | |
case 11: | |
Out = (InitYDis + (Space * 8) + (LabelFontSize)); | |
break; | |
case 12: | |
Out = (InitYDis + (Space * 9) + (LabelFontSize)); | |
break; | |
case 13: | |
Out = (InitYDis + (Space * 10) + (LabelFontSize)); | |
break; | |
case 14: | |
case 15: | |
Out = (InitYDis + (Space * 11) + (LabelFontSize)); | |
break; | |
case 16: | |
case 17: | |
Out = (InitYDis + (Space * 12) + (LabelFontSize)); | |
break; | |
case 18: | |
case 19: | |
case 20: | |
case 21: | |
case 27: | |
case 28: | |
Out = (InitYDis + (Space * 13) + (LabelFontSize)); | |
break; | |
case 22: | |
case 23: | |
case 24: | |
case 25: | |
case 29: | |
case 30: | |
Out = (InitYDis + (Space * 14) + (LabelFontSize)); | |
break; | |
case 26: | |
Out = (InitYDis + (Space * 5) + (LabelFontSize)); | |
break; | |
} | |
if (in >= 31 && in <= 36) Out = (InitYDis + (Space * 15) + (LabelFontSize)); | |
return(Out); | |
} | |
void ButtonCreate() | |
{ | |
for (int i=0;i<ArraySize(buttonID);i++) | |
{ | |
if (ObjectFind(0,buttonID[i]) < 0) ObjectCreate(0,buttonID[i],OBJ_BUTTON,0,100,100); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_COLOR,clrSilver); | |
//ObjectSetInteger(0,buttonID[i],OBJPROP_BGCOLOR,C'220,231,242'); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_BGCOLOR,colorButton(i)); | |
//ObjectSetInteger(0,buttonID[i],OBJPROP_BORDER_COLOR,C'153,163,174'); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_XDISTANCE,(int)(ButtonX(i) * DPIratio)); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_XSIZE,(int)(sizeXButton(i) * DPIratio)); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_YDISTANCE,(int)(ButtonY(i) * DPIratio)); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_YSIZE,(int)(25 * DPIratio)); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_CORNER,0); | |
ObjectSetString(0,buttonID[i],OBJPROP_FONT,AllFont); | |
ObjectSetString(0,buttonID[i],OBJPROP_TEXT,buttonText[i]); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_FONTSIZE,10); | |
ObjectSetInteger(0,buttonID[i],OBJPROP_SELECTABLE,0); | |
} | |
} | |
void ButtonsCreate(const int Mode) | |
{ | |
for (int i=0;i<ArraySize(buttonsID);i++) | |
{ | |
if (ObjectFind(0,buttonsID[i]) < 0) ObjectCreate(0,buttonsID[i],OBJ_BUTTON,0,100,100); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_COLOR,clrSilver); | |
//ObjectSetInteger(0,buttonsID[i],OBJPROP_BGCOLOR,C'220,231,242'); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_BGCOLOR,colorButtons(i)); | |
//ObjectSetInteger(0,buttonsID[i],OBJPROP_BORDER_COLOR,C'153,163,174'); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_XDISTANCE,(int)(ButtonsX(i) * DPIratio)); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_XSIZE,(int)(sizeXButtons(i) * DPIratio)); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_YDISTANCE,(int)(ButtonsY(i) * DPIratio)); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_YSIZE,(int)(25 * DPIratio)); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_CORNER,0); | |
ObjectSetString(0,buttonsID[i],OBJPROP_FONT,AllFont); | |
ObjectSetString(0,buttonsID[i],OBJPROP_TEXT,buttonsText[i]); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_FONTSIZE,10); | |
ObjectSetInteger(0,buttonsID[i],OBJPROP_SELECTABLE,0); | |
if (Mode == 1) ObjectSetInteger(0,buttonsID[i],OBJPROP_TIMEFRAMES,EMPTY); | |
} | |
} | |
int sizeXButton(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
case 2: | |
Out = (170); | |
break; | |
default: | |
Out = (110); | |
break; | |
} | |
return(Out); | |
} | |
int sizeXButtons(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
Out = (170); | |
break; | |
default: | |
Out = (110); | |
break; | |
} | |
return(Out); | |
} | |
int colorButtons(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
Out = (C'25,56,95'); | |
break; | |
case 1: | |
Out = (C'94,25,25'); | |
break; | |
} | |
return(Out); | |
} | |
int colorButton(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
Out = (C'25,56,95'); | |
break; | |
case 1: | |
Out = (C'94,25,25'); | |
break; | |
case 2: | |
case 3: | |
Out = (C'94,26,91'); | |
break; | |
case 4: | |
Out = (C'51,94,25'); | |
break; | |
case 5: | |
Out = (C'128,0,0'); | |
break; | |
case 6: | |
Out = (C'62,17,60'); | |
break; | |
case 7: | |
Out = (C'35,65,17'); | |
break; | |
case 8: | |
Out = (C'64,0,0'); | |
break; | |
case 9: | |
Out = (C'94,93,25'); | |
break; | |
} | |
return(Out); | |
} | |
int ButtonsX(const int in) | |
{ | |
int iX = 20; | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
Out = InitXDisSlave - 2; | |
break; | |
case 1: | |
Out = (InitXDisSlave + 174); | |
break; | |
} | |
return(Out); | |
} | |
int ButtonX(const int in) | |
{ | |
int iX = 20; | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 2: | |
case 3: | |
case 6: | |
Out = InitXDis - 2; | |
break; | |
case 1: | |
Out = (InitXDis + 174); | |
break; | |
case 4: | |
case 7: | |
case 10: | |
Out = (117 + InitXDis); | |
break; | |
case 5: | |
case 8: | |
case 9: | |
Out = (InitXDis + 236); | |
break; | |
} | |
return(Out); | |
} | |
int ButtonsY(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
Out = (InitYDisSlave + Space * 5); | |
break; | |
} | |
return(Out); | |
} | |
int ButtonY(const int in) | |
{ | |
int Out = 0; | |
switch (in) | |
{ | |
case 0: | |
case 1: | |
Out = (InitYDis + Space * 3); | |
break; | |
case 2: | |
Out = (InitYDis + (Space * 4)); | |
break; | |
case 3: | |
case 4: | |
case 5: | |
Out = (InitYDis + (Space * 5) + (LabelFontSize * 3)); | |
break; | |
case 6: | |
case 7: | |
case 8: | |
Out = (InitYDis + (Space * 6) + (LabelFontSize * 3)); | |
break; | |
case 9: | |
case 10: | |
Out = (595); | |
break; | |
} | |
return(Out); | |
} | |
void GetValue() | |
{ | |
Price = StringToDouble(ObjectGetString(0,editID[0],OBJPROP_TEXT,0)); | |
Lots = StringToDouble(ObjectGetString(0,editID[1],OBJPROP_TEXT,0)); | |
TPPrice = StringToDouble(ObjectGetString(0,editID[4],OBJPROP_TEXT,0)); | |
SLPrice = StringToDouble(ObjectGetString(0,editID[5],OBJPROP_TEXT,0)); | |
TPPips = StringToDouble(ObjectGetString(0,editID[2],OBJPROP_TEXT,0)); | |
SLPips = StringToDouble(ObjectGetString(0,editID[3],OBJPROP_TEXT,0)); | |
RangeBuy = StringToDouble(ObjectGetString(0,editID[6],OBJPROP_TEXT,0)); | |
RangeSell = StringToDouble(ObjectGetString(0,editID[7],OBJPROP_TEXT,0)); | |
ProfitOutSW = ObjectGetString(0,CheckBox[4],OBJPROP_BMPFILE); | |
ProfitOutLossSW = ObjectGetString(0,CheckBox[7],OBJPROP_BMPFILE); | |
TPsPrice = StringToDouble(ObjectGetString(0,editsID[4],OBJPROP_TEXT,0)); | |
SLsPrice = StringToDouble(ObjectGetString(0,editsID[5],OBJPROP_TEXT,0)); | |
TPsPips = StringToDouble(ObjectGetString(0,editsID[2],OBJPROP_TEXT,0)); | |
SLsPips = StringToDouble(ObjectGetString(0,editsID[3],OBJPROP_TEXT,0)); | |
} | |
void OnChartEvent(const int id, | |
const long &lparam, | |
const double &dparam, | |
const string &sparam) | |
{ | |
if (ProfitOutSW == check_on) | |
{ | |
ProfitOut = true; | |
}else | |
{ | |
ProfitOut = false; | |
} | |
if (ProfitOutLossSW == check_on) | |
{ | |
ProfitOutLoss = true; | |
}else | |
{ | |
ProfitOutLoss = false; | |
} | |
id_1 = (int)StringToInteger(ObjectGetString(0,editID[11],OBJPROP_TEXT,0)); | |
if (id == CHARTEVENT_OBJECT_ENDEDIT) | |
{ | |
CheckAlledit(); | |
string endEditChartObject=sparam; | |
if(endEditChartObject==editsID[0] || endEditChartObject==editsID[1] || endEditChartObject==editsID[8]) | |
{ | |
double CalProfitBuy = StringToDouble(ObjectGetString(0,editsID[0],OBJPROP_TEXT)); | |
double CalProfitSell = StringToDouble(ObjectGetString(0,editsID[1],OBJPROP_TEXT)); | |
double CalProfitAll = StringToDouble(ObjectGetString(0,editsID[8],OBJPROP_TEXT)); | |
ObjectSetString(0,editsID[6],OBJPROP_TEXT,DoubleToString(Cal_TP(OP_BUY,0,CalProfitBuy),Digits)); | |
ObjectSetString(0,editsID[7],OBJPROP_TEXT,DoubleToString(Cal_TP(OP_SELL,0,CalProfitSell),Digits)); | |
ObjectSetString(0,editsID[9],OBJPROP_TEXT,DoubleToString(Cal_TP(OP_ALL,0,CalProfitAll),Digits)); | |
} | |
if(endEditChartObject==editsID[6] || endEditChartObject==editsID[7] || endEditChartObject==editsID[9]) | |
{ | |
double CalPriceBuy = StringToDouble(ObjectGetString(0,editsID[6],OBJPROP_TEXT)); | |
double CalPriceSell = StringToDouble(ObjectGetString(0,editsID[7],OBJPROP_TEXT)); | |
double CalPriceAll = StringToDouble(ObjectGetString(0,editsID[9],OBJPROP_TEXT)); | |
ObjectSetString(0,editsID[0],OBJPROP_TEXT,DoubleToString(Cal_Profit(OP_BUY,CalPriceBuy),2)); | |
ObjectSetString(0,editsID[1],OBJPROP_TEXT,DoubleToString(Cal_Profit(OP_SELL,CalPriceSell),2)); | |
ObjectSetString(0,editsID[8],OBJPROP_TEXT,DoubleToString(Cal_Profit(OP_ALL,CalPriceAll),Digits)); | |
} | |
} | |
if(id==CHARTEVENT_OBJECT_CLICK) | |
{ | |
string clickedChartObject=sparam; | |
if(clickedChartObject==VisiSW[0]) | |
{ | |
string str_temp = ObjectGetString(0,VisiSW[0],OBJPROP_TEXT); | |
bool selected = ObjectGetInteger(0,buttonID[10],OBJPROP_STATE); | |
if (str_temp == "5") | |
{ | |
ObjectSetString(0,VisiSW[0],OBJPROP_TEXT,"6"); | |
VisiObjs(false); | |
if (selected) | |
{ | |
VisiObjsSlave(false); | |
} | |
} | |
if (str_temp == "6") | |
{ | |
ObjectSetString(0,VisiSW[0],OBJPROP_TEXT,"5"); | |
VisiObjs(true); | |
if (selected) | |
{ | |
VisiObjsSlave(true); | |
} | |
} | |
} | |
if(clickedChartObject==VisiSW[1]) | |
{ | |
bool selected = ObjectGetInteger(0,VisiSW[1],OBJPROP_STATE); | |
if (selected) | |
{ | |
InitXDis = 10; | |
InitXDisSlave = InitXDis + (int)(ObjectGetInteger(0,BGID[0],OBJPROP_XSIZE) / DPIratio) + 10; | |
ObjectSetInteger(0,VisiSW[1],OBJPROP_STATE,0); | |
} | |
} | |
if(clickedChartObject==VisiSW[2]) | |
{ | |
bool selected = ObjectGetInteger(0,VisiSW[2],OBJPROP_STATE); | |
if (selected) | |
{ | |
width = (int)ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0); | |
InitXDis = (int)((width - ObjectGetInteger(0,BGID[0],OBJPROP_XSIZE)) / DPIratio); | |
InitXDisSlave = InitXDis /*- (int)ObjectGetInteger(0,BGIDslave[0],OBJPROP_XSIZE)*/ - (int)(ObjectGetInteger(0,BGID[0],OBJPROP_XSIZE) / DPIratio) - 10; | |
ObjectSetInteger(0,VisiSW[2],OBJPROP_STATE,0); | |
} | |
} | |
if(clickedChartObject==CheckBoxs[0]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBoxs[0],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBoxs[0],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBoxs[2],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBoxs[0],OBJPROP_BMPFILE,check_off); | |
ObjectSetString(0,CheckBoxs[2],OBJPROP_BMPFILE,check_on); | |
} | |
} | |
if(clickedChartObject==CheckBoxs[1]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBoxs[1],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBoxs[1],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBoxs[3],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBoxs[1],OBJPROP_BMPFILE,check_off); | |
ObjectSetString(0,CheckBoxs[3],OBJPROP_BMPFILE,check_on); | |
} | |
} | |
if(clickedChartObject==CheckBox[0]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBox[0],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBox[0],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBox[2],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBox[0],OBJPROP_BMPFILE,check_off); | |
ObjectSetString(0,CheckBox[2],OBJPROP_BMPFILE,check_on); | |
} | |
} | |
if(clickedChartObject==CheckBox[1]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBox[1],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBox[1],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBox[3],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBox[1],OBJPROP_BMPFILE,check_off); | |
ObjectSetString(0,CheckBox[3],OBJPROP_BMPFILE,check_on); | |
} | |
} | |
if(clickedChartObject==CheckBoxs[2]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBoxs[2],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBoxs[2],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBoxs[0],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBoxs[2],OBJPROP_BMPFILE,check_off); | |
ObjectSetString(0,CheckBoxs[0],OBJPROP_BMPFILE,check_on); | |
} | |
} | |
if(clickedChartObject==CheckBoxs[3]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBoxs[3],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBoxs[3],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBoxs[1],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBoxs[3],OBJPROP_BMPFILE,check_off); | |
ObjectSetString(0,CheckBoxs[1],OBJPROP_BMPFILE,check_on); | |
} | |
} | |
if(clickedChartObject==CheckBox[2]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBox[2],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBox[2],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBox[0],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBox[2],OBJPROP_BMPFILE,check_off); | |
ObjectSetString(0,CheckBox[0],OBJPROP_BMPFILE,check_on); | |
} | |
} | |
if(clickedChartObject==CheckBox[3]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBox[3],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBox[3],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBox[1],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBox[3],OBJPROP_BMPFILE,check_off); | |
ObjectSetString(0,CheckBox[1],OBJPROP_BMPFILE,check_on); | |
} | |
} | |
if(clickedChartObject==CheckBox[4]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBox[4],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBox[4],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBox[7],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBox[4],OBJPROP_BMPFILE,check_off); | |
} | |
} | |
if(clickedChartObject==CheckBox[5]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBox[5],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBox[5],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBox[6],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBox[5],OBJPROP_BMPFILE,check_off); | |
} | |
} | |
if(clickedChartObject==CheckBox[6]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBox[6],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBox[6],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBox[5],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBox[6],OBJPROP_BMPFILE,check_off); | |
} | |
} | |
if(clickedChartObject==CheckBox[7]) | |
{ | |
string str_temp = ObjectGetString(0,CheckBox[7],OBJPROP_BMPFILE); | |
if (str_temp == check_off) | |
{ | |
ObjectSetString(0,CheckBox[7],OBJPROP_BMPFILE,check_on); | |
ObjectSetString(0,CheckBox[4],OBJPROP_BMPFILE,check_off); | |
} | |
if (str_temp == check_on) | |
{ | |
ObjectSetString(0,CheckBox[7],OBJPROP_BMPFILE,check_off); | |
} | |
} | |
if(clickedChartObject==buttonsID[0]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonsID[0],OBJPROP_STATE); | |
string TPSW = ObjectGetString(0,CheckBoxs[0],OBJPROP_BMPFILE); | |
string SLSW = ObjectGetString(0,CheckBoxs[1],OBJPROP_BMPFILE); | |
if (selected) | |
{ | |
double SL,TP; | |
if (TPSW == check_on) | |
{ | |
TP = TPsPips; | |
Set_SL_TP(MODE_SLTP_BYPOINT,OP_BUY,-1,TP); | |
}else | |
{ | |
TP = TPsPrice; | |
Set_SL_TP(MODE_SLTP_BYPRICE,OP_BUY,-1,TP); | |
} | |
Print(TP); | |
if (SLSW == check_on) | |
{ | |
SL = SLsPips; | |
Set_SL_TP(MODE_SLTP_BYPOINT,OP_BUY,SL,-1); | |
}else | |
{ | |
SL = SLsPrice; | |
Set_SL_TP(MODE_SLTP_BYPRICE,OP_BUY,SL,-1); | |
} | |
Sleep(200); | |
ObjectSetInteger(0,buttonsID[0],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject==buttonsID[1]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonsID[1],OBJPROP_STATE); | |
string TPSW = ObjectGetString(0,CheckBoxs[0],OBJPROP_BMPFILE); | |
string SLSW = ObjectGetString(0,CheckBoxs[1],OBJPROP_BMPFILE); | |
if (selected) | |
{ | |
double SL,TP; | |
if (TPSW == check_on) | |
{ | |
TP = TPsPips; | |
Set_SL_TP(MODE_SLTP_BYPOINT,OP_SELL,-1,TP); | |
}else | |
{ | |
TP = TPsPrice; | |
Set_SL_TP(MODE_SLTP_BYPRICE,OP_SELL,-1,TP); | |
} | |
if (SLSW == check_on) | |
{ | |
SL = SLsPips; | |
Set_SL_TP(MODE_SLTP_BYPOINT,OP_SELL,SL,-1); | |
}else | |
{ | |
SL = SLsPrice; | |
Set_SL_TP(MODE_SLTP_BYPRICE,OP_SELL,SL,-1); | |
} | |
Sleep(200); | |
ObjectSetInteger(0,buttonsID[1],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject==buttonID[0]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[0],OBJPROP_STATE); | |
string TPSW = ObjectGetString(0,CheckBox[0],OBJPROP_BMPFILE); | |
string SLSW = ObjectGetString(0,CheckBox[1],OBJPROP_BMPFILE); | |
if (selected) | |
{ | |
double iPrice = Price; | |
double SL,TP; | |
int Type = -1; | |
if (EqualZero(iPrice)) | |
{ | |
iPrice = Ask; | |
Type = OP_BUY; | |
if (TPSW == check_on) | |
{ | |
if (EqualZero(TPPips)) | |
TP = 0; | |
else | |
TP = Ask + TPPips * point; | |
}else | |
{ | |
TP = TPPrice; | |
} | |
if (SLSW == check_on) | |
{ | |
if (EqualZero(SLPips)) | |
SL = 0; | |
else | |
SL = Ask - SLPips * point; | |
}else | |
{ | |
SL = SLPrice; | |
} | |
}else | |
{ | |
Type = OutOrderType(iPrice,0); | |
if (TPSW == check_on) | |
{ | |
if (EqualZero(TPPips)) | |
TP = 0; | |
else | |
TP = iPrice + TPPips * point; | |
}else | |
{ | |
TP = TPPrice; | |
} | |
if (SLSW == check_on) | |
{ | |
if (EqualZero(SLPips)) | |
SL = 0; | |
else | |
SL = iPrice - SLPips * point; | |
}else | |
{ | |
SL = SLPrice; | |
} | |
} | |
SendOrders(Type,"Savior_" + IntegerToString(OP_BUY),iPrice,Lots,SL,TP); | |
Sleep(200); | |
ObjectSetInteger(0,buttonID[0],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject==buttonID[1]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[1],OBJPROP_STATE); | |
string TPSW = ObjectGetString(0,CheckBox[0],OBJPROP_BMPFILE); | |
string SLSW = ObjectGetString(0,CheckBox[1],OBJPROP_BMPFILE); | |
if (selected) | |
{ | |
double iPrice = Price; | |
double SL,TP; | |
int Type = -1; | |
if (EqualZero(iPrice)) | |
{ | |
iPrice = Bid; | |
Type = OP_SELL; | |
if (TPSW == check_on) | |
{ | |
if (EqualZero(TPPips)) | |
TP = 0; | |
else | |
TP = Bid - TPPips * point; | |
}else | |
{ | |
TP = TPPrice; | |
} | |
if (SLSW == check_on) | |
{ | |
if (EqualZero(SLPips)) | |
SL = 0; | |
else | |
SL = Bid + SLPips * point; | |
}else | |
{ | |
SL = SLPrice; | |
} | |
}else | |
{ | |
Type = OutOrderType(iPrice,1); | |
if (TPSW == check_on) | |
{ | |
if (EqualZero(TPPips)) | |
TP = 0; | |
else | |
TP = iPrice - TPPips * point; | |
}else | |
{ | |
TP = TPPrice; | |
} | |
if (SLSW == check_on) | |
{ | |
if (EqualZero(SLPips)) | |
SL = 0; | |
else | |
SL = iPrice + SLPips * point; | |
}else | |
{ | |
SL = SLPrice; | |
} | |
} | |
SendOrders(Type,"Savior_" + IntegerToString(OP_SELL),iPrice,Lots,SL,TP); | |
Sleep(200); | |
ObjectSetInteger(0,buttonID[1],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[2]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[2],OBJPROP_STATE); | |
if (selected) | |
{ | |
SendOrders(OutOrderType(Ask + RangeBuy * point,OP_BUY),"Savior_" + IntegerToString(OP_BUY), | |
Ask + RangeBuy * point,Lots, | |
(EqualZero(SLPips) ? 0.0 : (Ask + (RangeBuy - SLPips) * point)), | |
(EqualZero(TPPips) ? 0.0 : (Ask + (RangeBuy + TPPips) * point))); | |
SendOrders(OutOrderType(Bid - RangeSell * point,OP_SELL),"Savior_" + IntegerToString(OP_SELL), | |
Bid - RangeSell * point,Lots, | |
(EqualZero(SLPips) ? 0.0 : (Bid - (RangeSell - SLPips) * point)), | |
(EqualZero(TPPips) ? 0.0 : (Bid - (RangeSell + TPPips) * point))); | |
Sleep(200); | |
ObjectSetInteger(0,buttonID[2],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[3]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[3],OBJPROP_STATE); | |
if (selected) | |
{ | |
Sleep(200); | |
iCloseOrders(OP_BUY); | |
ObjectSetInteger(0,buttonID[3],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[4]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[4],OBJPROP_STATE); | |
if (selected) | |
{ | |
Sleep(200); | |
int ans_bomb = MessageBox("確認是否掃除水雷?","確認命令",MB_YESNO|MB_ICONQUESTION); | |
if (ans_bomb == 6) iCloseOrders(OP_ALL); | |
ObjectSetInteger(0,buttonID[4],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[5]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[5],OBJPROP_STATE); | |
if (selected) | |
{ | |
Sleep(200); | |
iCloseOrders(OP_SELL); | |
ObjectSetInteger(0,buttonID[5],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[6]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[6],OBJPROP_STATE); | |
if (selected) | |
{ | |
Sleep(200); | |
iCloseOrders(OP_BUYSTOP); | |
iCloseOrders(OP_BUYLIMIT); | |
ObjectSetInteger(0,buttonID[6],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[7]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[7],OBJPROP_STATE); | |
if (selected) | |
{ | |
Sleep(200); | |
iCloseOrders(OP_BUYSTOP); | |
iCloseOrders(OP_BUYLIMIT); | |
iCloseOrders(OP_SELLSTOP); | |
iCloseOrders(OP_SELLLIMIT); | |
ObjectSetInteger(0,buttonID[7],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[8]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[8],OBJPROP_STATE); | |
if (selected) | |
{ | |
Sleep(200); | |
iCloseOrders(OP_SELLSTOP); | |
iCloseOrders(OP_SELLLIMIT); | |
ObjectSetInteger(0,buttonID[8],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[9]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[9],OBJPROP_STATE); | |
if (selected) | |
{ | |
editClear(); | |
CheckBoxCreate(1); | |
Sleep(200); | |
ObjectSetInteger(0,buttonID[9],OBJPROP_STATE,false); | |
} | |
} | |
if(clickedChartObject == buttonID[10]) | |
{ | |
bool selected = ObjectGetInteger(0,buttonID[10],OBJPROP_STATE); | |
if (selected) | |
{ | |
VisiObjsSlave(true); | |
}else | |
{ | |
VisiObjsSlave(false); | |
} | |
} | |
} | |
} | |
void CheckNewVersion(string match_Type,string match_Version) | |
{ | |
int hInternet = 0; | |
int iResult = 0; | |
int lReturn[]={1}; | |
uchar sBuffer[1024]; | |
int bytes = 0; | |
string strUrl = "http://wargame.shinehope.com/wp-content/uploads/ew-xml/Update.csv"; | |
string strWebPage = ""; | |
hInternet=InternetOpenUrlW(hSession(false),strUrl,"",0, | |
INTERNET_FLAG_NO_CACHE_WRITE| | |
INTERNET_FLAG_PRAGMA_NOCACHE| | |
INTERNET_FLAG_RELOAD,0); | |
if(hInternet==0) | |
{ | |
Alert("Check New Version Failed"); | |
return; | |
} | |
int h1=FileOpen("Update.csv",FILE_CSV|FILE_SHARE_WRITE|FILE_SHARE_READ,','); | |
while(lReturn[0]!=0) | |
{ | |
iResult=InternetReadFile(hInternet,sBuffer,BUFFER_LEN,lReturn); | |
if(lReturn[0]==0) | |
break; | |
bytes += lReturn[0]; | |
strWebPage += CharArrayToString(sBuffer,0,lReturn[0],CP_ACP); | |
FileWriteString(h1, CharArrayToString(sBuffer,0,lReturn[0],CP_ACP), lReturn[0]); | |
} | |
FileSeek(h1,0,SEEK_SET); | |
string Buffer = ""; | |
while(!FileIsEnding(h1)) | |
{ | |
Buffer = FileReadString(h1); | |
if (StringFind(Buffer,match_Type) >= 0) | |
{ | |
Buffer = FileReadString(h1); | |
break; | |
} | |
} | |
FileClose(h1); | |
iResult=InternetCloseHandle(hInternet); | |
if(iResult==0) | |
{ | |
Alert("Check New Version Failed"); | |
return; | |
} | |
printf("EA Version: %s, New Version: %s",match_Version,Buffer); | |
if (Buffer != match_Version && Buffer != "") | |
{ | |
Alert("已有新版封包,請向上級長官索取"); | |
} | |
return; | |
} | |
//+------------------------------------------------------------------+ | |
int hSession(bool Direct) | |
{ | |
int hSession_IEType = 0; | |
int hSession_Direct = 0; | |
int Internet_Open_Type_Preconfig = 0; | |
int Internet_Open_Type_Direct = 1; | |
int Internet_Open_Type_Proxy = 3; | |
string InternetAgent=""; | |
if(hSession_IEType == 0) | |
{ | |
InternetAgent="Mozilla/4.0 (compatible; MSIE 8.0; Win32)"; | |
hSession_IEType = InternetOpenW(InternetAgent, Internet_Open_Type_Preconfig, "", "", 0); | |
hSession_Direct = InternetOpenW(InternetAgent, Internet_Open_Type_Direct, "", "", 0); | |
} | |
if(Direct) | |
{ | |
return(hSession_Direct); | |
} | |
else | |
{ | |
return(hSession_IEType); | |
} | |
} | |
void CheckServerLocalTime() | |
{ | |
int LocalMinute = TimeMinute(TimeLocal()); | |
int LocalSeconds = TimeSeconds(TimeLocal()); | |
int ServerMinute = TimeMinute(TimeCurrent()); | |
int ServerSeconds = TimeSeconds(TimeCurrent()); | |
if (AccountInfoInteger(ACCOUNT_TRADE_ALLOWED)) | |
{ | |
Print("System Time: " + IntegerToString(LocalMinute) + ":" + IntegerToString(LocalSeconds)); | |
Print("Server Time: " + IntegerToString(ServerMinute) + ":" + IntegerToString(ServerSeconds)); | |
if (MathAbs(LocalMinute - ServerMinute) >= 1 || MathAbs(LocalSeconds - ServerSeconds) >= 5) | |
{ | |
Alert("系統與伺服器時間分鐘或秒數不同步,請檢查設定"); | |
} | |
} | |
return; | |
} | |
void DeleteObj() | |
{ | |
for (int i=0;i<40;i++) | |
{ | |
if (i < ArraySize(BGID) && ObjectFind(0,BGID[i]) >= 0) ObjectDelete(BGID[i]); | |
if (i < ArraySize(logoID) && ObjectFind(0,logoID[i]) >= 0) ObjectDelete(logoID[i]); | |
if (i < ArraySize(buttonID) && ObjectFind(0,buttonID[i]) >= 0) ObjectDelete(buttonID[i]); | |
if (i < ArraySize(editID) && ObjectFind(0,editID[i]) >= 0) ObjectDelete(editID[i]); | |
if (i < ArraySize(labelID) && ObjectFind(0,labelID[i]) >= 0) ObjectDelete(labelID[i]); | |
if (i < ArraySize(CheckBox) && ObjectFind(0,CheckBox[i]) >= 0) ObjectDelete(CheckBox[i]); | |
if (i < ArraySize(VisiSW) && ObjectFind(0,VisiSW[i]) >= 0) ObjectDelete(VisiSW[i]); | |
if (i < ArraySize(buttonsID) && ObjectFind(0,buttonsID[i]) >= 0) ObjectDelete(buttonsID[i]); | |
if (i < ArraySize(labelsID) && ObjectFind(0,labelsID[i]) >= 0) ObjectDelete(labelsID[i]); | |
if (i < ArraySize(BGIDslave) && ObjectFind(0,BGIDslave[i]) >= 0) ObjectDelete(BGIDslave[i]); | |
if (i < ArraySize(editsID) && ObjectFind(0,editsID[i]) >= 0) ObjectDelete(editsID[i]); | |
if (i < ArraySize(CheckBoxs) && ObjectFind(0,CheckBoxs[i]) >= 0) ObjectDelete(CheckBoxs[i]); | |
} | |
} | |
void Set_SL_TP(const int Mode,const int Type,double iSL,double iTP) | |
{ | |
double SL = 0.0; | |
double TP = 0.0; | |
for (int z=0;z<OrdersTotal();z++) | |
{ | |
if (!OrderSelect(z,SELECT_BY_POS,MODE_TRADES)) | |
continue; | |
if (OrderSymbol() == Symbol() && OrderType() == Type && OrderMagicNumber() == id_1) | |
{ | |
if (Mode == MODE_SLTP_BYPOINT) | |
{ | |
if (Type == OP_BUY || Type == OP_BUYSTOP || Type == OP_BUYLIMIT) | |
{ | |
TP = OrderOpenPrice() + iTP * point; | |
SL = OrderOpenPrice() - iSL * point; | |
} | |
if (Type == OP_SELL || Type == OP_SELLSTOP || Type == OP_SELLLIMIT) | |
{ | |
TP = OrderOpenPrice() - iTP * point; | |
SL = OrderOpenPrice() + iSL * point; | |
} | |
if (EqualZero(iTP)) TP = 0; | |
if (EqualZero(iSL)) SL = 0; | |
}else | |
{ | |
SL = iSL; | |
TP = iTP; | |
} | |
if (iTP < 0) | |
{ | |
TP = OrderTakeProfit(); | |
} | |
if (iSL < 0) | |
{ | |
SL = OrderStopLoss(); | |
} | |
if ((CompareDouble(OrderStopLoss(),NormalizeDouble(SL,Digits))) && (CompareDouble(OrderTakeProfit(),NormalizeDouble(TP,Digits)))) | |
{ | |
continue; | |
} | |
else | |
{ | |
RefreshRates(); | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White)) | |
{ | |
Alert("Set_SL_TP " + IntegerToString(OrderTicket()) + " Error: " + ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
int SendOrders(const int Type ,const string iComment,const double iPrice,const double iLots,const double iSL,const double iTP) | |
{ | |
double InPrice = 0.0; | |
switch (Type) | |
{ | |
case OP_BUY: | |
InPrice = Ask; | |
break; | |
case OP_SELL: | |
InPrice = Bid; | |
break; | |
default: | |
InPrice = iPrice; | |
break; | |
} | |
if (Type == OP_BUYSTOP || Type == OP_BUYLIMIT) | |
{ | |
if (MathAbs(iPrice - Ask) / Point <= SymbolInfoInteger(NULL,SYMBOL_TRADE_STOPS_LEVEL)) | |
{ | |
return(-2); | |
} | |
} | |
if (Type == OP_SELLSTOP || Type == OP_SELLLIMIT) | |
{ | |
if (MathAbs(iPrice - Bid) / Point <= SymbolInfoInteger(NULL,SYMBOL_TRADE_STOPS_LEVEL)) | |
{ | |
return(-2); | |
} | |
} | |
int Ticket = OrderSend(Symbol(),Type,iLots,InPrice,(int)(2 * (point / Point)),iSL,iTP,iComment,id_1,0,((Type & 1) == 1) ? clrRed : clrLime); | |
if (Ticket < 0) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert("Send " + iComment + " Order Error: " + ErrorDescription(ErrorCode)); | |
} | |
return(Ticket); | |
} | |
bool CompareDouble(const double A,const double B) | |
{ | |
if (MathAbs(A - B) <= TOLERANCE) return(true); | |
return(false); | |
} | |
bool EqualZero(const double A) | |
{ | |
if (A <= TOLERANCE && A >= -TOLERANCE) return(true); | |
return(false); | |
} | |
void SaveParameter() | |
{ | |
int h1 = FileOpen("Var.bin",FILE_CSV|FILE_WRITE); | |
if (h1 > 0) | |
{ | |
for(int i=0;i<ArraySize(editID);i++) | |
{ | |
FileWrite(h1,ObjectGetString(0,editID[i],OBJPROP_TEXT)); | |
} | |
FileClose(h1); | |
} | |
} | |
void LoadPrevious() | |
{ | |
int h1 = FileOpen("Var.bin",FILE_CSV|FILE_READ); | |
if (h1 > 0) | |
{ | |
for(int i=0;i<ArraySize(editID);i++) | |
{ | |
ObjectSetString(0,editID[i],OBJPROP_TEXT,FileReadString(h1)); | |
} | |
FileClose(h1); | |
} | |
} | |
void iCloseOrders(const int type) | |
{ | |
int C0_cnt; | |
if (type != OP_BUY && type != OP_SELL && type != OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==type && OrderMagicNumber()==id_1){ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
if (type == OP_BUY || type == OP_SELL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) //從最近一張單開始 | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol()==Symbol()&&OrderType()==type && OrderMagicNumber()==id_1 && (ProfitOut ? (OrderProfit() > 0) : (ProfitOutLoss ? (OrderProfit() < 0) : true))) | |
{ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),20,clrWhite)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
{ | |
Print("BS"); | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
if (type == OP_ALL) | |
{ | |
for(C0_cnt=OrdersTotal()-1;C0_cnt>=0;C0_cnt--) | |
{ | |
if (!OrderSelect(C0_cnt,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT) | |
{ | |
if (!OrderDelete(OrderTicket())) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
{ | |
Print("ALL Delete"); | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
}else { | |
if ((ProfitOut ? (OrderProfit() > 0) : (ProfitOutLoss ? (OrderProfit() < 0) : true))) | |
{ | |
if (!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),20,clrWhite)) | |
{ | |
ErrorCode = GetLastError(); | |
if (ErrorCode != 4109) | |
{ | |
Print("ALL Close"); | |
Alert(ErrorDescription(ErrorCode)); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
int OutOrderType(const double inPrice,const int Type) | |
{ | |
if (Type == OP_BUY) | |
{ | |
if (inPrice > Ask) | |
{ | |
return(OP_BUYSTOP); | |
}else if (inPrice < Ask) | |
{ | |
return(OP_BUYLIMIT); | |
} | |
} | |
if (Type == OP_SELL) | |
{ | |
if (inPrice > Bid) | |
{ | |
return(OP_SELLLIMIT); | |
}else if (inPrice < Bid) | |
{ | |
return(OP_SELLSTOP); | |
} | |
} | |
return(-1); | |
} | |
void iMoveStopLoss(const double SL) | |
{ | |
int Cnt = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
if (OrderType() == OP_BUY && OrderProfit() > 0 && MathAbs(OrderClosePrice() - OrderOpenPrice()) / point >= SL) | |
{ | |
if ((OrderClosePrice() - SL * point) > OrderStopLoss()) | |
{ | |
if (!CompareDouble(OrderStopLoss(),NormalizeDouble(OrderClosePrice() - SL * point,Digits))) | |
{ | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice() - SL * point,OrderTakeProfit(),0,clrWhite)) | |
{ | |
Print("Trail Stop " + IntegerToString(OrderTicket()) + "Error: "+ ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
if (OrderType() == OP_SELL && OrderProfit() > 0 && MathAbs(OrderClosePrice() - OrderOpenPrice()) / point >= SL) | |
{ | |
if ((OrderClosePrice() + SL * point) < OrderStopLoss() || EqualZero(OrderStopLoss())) | |
{ | |
if (!CompareDouble(OrderStopLoss(),NormalizeDouble(OrderClosePrice() + SL * point,Digits))) | |
{ | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice() + SL * point,OrderTakeProfit(),0,clrWhite)) | |
{ | |
Print("Trail Stop " + IntegerToString(OrderTicket()) + "Error: "+ ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
void iBE(const double BE,const double LV) | |
{ | |
int Cnt = 0; | |
for (int i=OrdersTotal()-1;i>=0;i--) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
if (OrderType() == OP_BUY && OrderProfit() > 0 && MathAbs(OrderClosePrice() - OrderOpenPrice()) / point >= LV) | |
{ | |
if (!(CompareDouble(OrderStopLoss(),NormalizeDouble(OrderOpenPrice() + BE * point,Digits)))) | |
{ | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() + BE * point,OrderTakeProfit(),0,clrWhite)) | |
{ | |
Print("BE " + IntegerToString(OrderTicket()) + "Error: "+ ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
if (OrderType() == OP_SELL && OrderProfit() > 0 && MathAbs(OrderClosePrice() - OrderOpenPrice()) / point >= LV) | |
{ | |
if (!(CompareDouble(OrderStopLoss(),NormalizeDouble(OrderOpenPrice() - BE * point,Digits)))) | |
{ | |
if (!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - BE * point,OrderTakeProfit(),0,clrWhite)) | |
{ | |
Print("BE " + IntegerToString(OrderTicket()) + "Error: "+ ErrorDescription(GetLastError())); | |
} | |
} | |
} | |
} | |
} | |
} | |
void CreateSW(const int Mode) | |
{ | |
for (int i=0;i<ArraySize(VisiSW);i++) | |
{ | |
if (ObjectFind(0,VisiSW[i]) < 0) ObjectCreate(0,VisiSW[i],OBJ_BUTTON,0,100,100); | |
ObjectSetInteger(0,VisiSW[i],OBJPROP_BGCOLOR,C'229,228,183'); | |
ObjectSetInteger(0,VisiSW[i],OBJPROP_XSIZE,(int)(14 * DPIratio)); | |
ObjectSetInteger(0,VisiSW[i],OBJPROP_YSIZE,(int)(14 * DPIratio)); | |
ObjectSetInteger(0,VisiSW[i],OBJPROP_XDISTANCE,(int)(SWX(i) * DPIratio)); | |
ObjectSetInteger(0,VisiSW[i],OBJPROP_YDISTANCE,(int)(0 * DPIratio)); | |
ObjectSetInteger(0,VisiSW[i],OBJPROP_CORNER,0); | |
ObjectSetString(0,VisiSW[i],OBJPROP_FONT,"Webdings"); | |
if (Mode == 1) ObjectSetString(0,VisiSW[i],OBJPROP_TEXT,SWstring(i)); | |
//ObjectSetInteger(0,VisiSW[i],OBJPROP_FONTSIZE,15); | |
ObjectSetInteger(0,VisiSW[i],OBJPROP_SELECTABLE,0); | |
ObjectSetInteger(0,VisiSW[i],OBJPROP_ZORDER,10); | |
} | |
} | |
int SWX(const int in) | |
{ | |
int Out = 0; | |
switch(in) | |
{ | |
case 0: | |
Out = InitXDis + 340; | |
break; | |
case 1: | |
Out = InitXDis; | |
break; | |
case 2: | |
Out = InitXDis + 15; | |
break; | |
} | |
return(Out); | |
} | |
string SWstring(const int in) | |
{ | |
string Out = ""; | |
switch(in) | |
{ | |
case 1: | |
Out = "3"; | |
break; | |
case 2: | |
Out = "4"; | |
break; | |
case 0: | |
Out = "5"; | |
} | |
return(Out); | |
} | |
void VisiObjs(const bool SW) | |
{ | |
for (int i=0;i<40;i++) | |
{ | |
if (i < ArraySize(BGID) && ObjectFind(0,BGID[i]) >= 0) ObjectSetInteger(0,BGID[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(logoID) && ObjectFind(0,logoID[i]) >= 0) ObjectSetInteger(0,logoID[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(buttonID) && ObjectFind(0,buttonID[i]) >= 0) ObjectSetInteger(0,buttonID[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(editID) && ObjectFind(0,editID[i]) >= 0) ObjectSetInteger(0,editID[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(labelID) && ObjectFind(0,labelID[i]) >= 0) ObjectSetInteger(0,labelID[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(CheckBox) && ObjectFind(0,CheckBox[i]) >= 0) ObjectSetInteger(0,CheckBox[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
} | |
} | |
void VisiObjsSlave(const bool SW) | |
{ | |
for (int i=0;i<40;i++) | |
{ | |
if (i < ArraySize(BGIDslave) && ObjectFind(0,BGIDslave[i]) >= 0) ObjectSetInteger(0,BGIDslave[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(buttonsID) && ObjectFind(0,buttonsID[i]) >= 0) ObjectSetInteger(0,buttonsID[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(labelsID) && ObjectFind(0,labelsID[i]) >= 0) ObjectSetInteger(0,labelsID[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(editsID) && ObjectFind(0,editsID[i]) >= 0) ObjectSetInteger(0,editsID[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
if (i < ArraySize(CheckBoxs) && ObjectFind(0,CheckBoxs[i]) >= 0) ObjectSetInteger(0,CheckBoxs[i],OBJPROP_TIMEFRAMES,(SW ? OBJ_ALL_PERIODS : EMPTY)); | |
} | |
} | |
void MarginCall() | |
{ | |
double LotsB = 0.0; | |
double LotsS = 0.0; | |
double ProfitB = 0.0; | |
double ProfitS = 0.0; | |
int Buy = 0; | |
int Sell = 0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderSymbol() == Symbol()) | |
{ | |
if (OrderType() == OP_BUY) | |
{ | |
LotsB += OrderLots(); | |
ProfitB += OrderProfit() + OrderSwap() + OrderCommission(); | |
Buy++; | |
} | |
if (OrderType() == OP_SELL) | |
{ | |
LotsS += OrderLots(); | |
ProfitS += OrderProfit() + OrderSwap() + OrderCommission(); | |
Sell++; | |
} | |
} | |
} | |
double MarginLv = StringToDouble(ObjectGetString(0,editID[12],OBJPROP_TEXT)) * 0.01; | |
double MarginPips = 0.0; | |
double MarginPrice = 0.0; | |
if (EqualZero(LotsB - LotsS)) | |
{ | |
MarginPips = 0; | |
MarginPrice = 0; | |
}else | |
{ | |
MarginPips = (((MarginLv * AccountMargin()) - AccountEquity()) / (LotsB - LotsS)) / 10; | |
MarginPrice = Bid + MarginPips * point; | |
} | |
ObjectSetString(0,labelID[15],OBJPROP_TEXT,DoubleToString(MarginPips,2)); | |
ObjectSetString(0,labelID[17],OBJPROP_TEXT,DoubleToString(MarginPrice,Digits)); | |
ObjectSetString(0,labelID[19],OBJPROP_TEXT,IntegerToString(Buy)); | |
ObjectSetString(0,labelID[21],OBJPROP_TEXT,DoubleToString(LotsB,2)); | |
ObjectSetString(0,labelID[23],OBJPROP_TEXT,IntegerToString(Sell)); | |
ObjectSetString(0,labelID[25],OBJPROP_TEXT,DoubleToString(LotsS,2)); | |
ObjectSetString(0,labelID[28],OBJPROP_TEXT,DoubleToString(ProfitB,2) + " USD"); | |
ObjectSetString(0,labelID[30],OBJPROP_TEXT,DoubleToString(ProfitS,2) + " USD"); | |
ObjectSetString(0,labelID[32],OBJPROP_TEXT,IntegerToString(OrdersTotal())); | |
ObjectSetString(0,labelID[34],OBJPROP_TEXT,DoubleToString(LotsB + LotsS,2)); | |
ObjectSetString(0,labelID[36],OBJPROP_TEXT,DoubleToString(AccountProfit(),2) + " USD"); | |
} | |
bool CheckAlledit() | |
{ | |
int i = 0; | |
string str_temp = ""; | |
for (;i<ArraySize(editID);i++) | |
{ | |
str_temp = ObjectGetString(0,editID[i],OBJPROP_TEXT); | |
if ((i == 10 ? !CheckString(str_temp,0) : !CheckString(str_temp,1))) | |
{ | |
Alert("不合法的參數,欄位 " + (i >= 7 ? labelText[i+1] : labelText[i])); | |
return(false); | |
} | |
} | |
for (i=0;i<ArraySize(editsID);i++) | |
{ | |
str_temp = ObjectGetString(0,editsID[i],OBJPROP_TEXT); | |
if ((i == 10 ? !CheckString(str_temp,0) : !CheckString(str_temp,1))) | |
{ | |
Alert("不合法的參數,欄位 " + (i >= 2 ? labelsText[i+2] : labelsText[i])); | |
return(false); | |
} | |
} | |
return(true); | |
} | |
bool CheckString(const string in,const int Mode) | |
{ | |
for(int i=0;i<StringLen(in);i++) | |
{ | |
if (!CheckChar(StringGetCharacter(in,i),Mode)) | |
{ | |
return(false); | |
} | |
} | |
return(true); | |
} | |
bool CheckChar(const ushort in,const int Mode) | |
{ | |
if (Mode == 0) | |
{ | |
if (in != '-' && in != '0' && in != '1' && in != '2' && in != '3' && in != '4' && in != '5' && in != '6' && in != '7' && in != '8' && in != '9' && in != '\0') | |
{ | |
return(false); | |
} | |
} | |
if (Mode == 1) | |
{ | |
if (in != '-' && in != '0' && in != '1' && in != '2' && in != '3' && in != '4' && in != '5' && in != '6' && in != '7' && in != '8' && in != '9' && in != '.' && in != '\0') | |
{ | |
return(false); | |
} | |
} | |
return(true); | |
} | |
double Cal_TP(int Type,double iPrice,double Profit) | |
{ | |
double TotalLots=0.0; | |
double TotalDisProduct=0.0; | |
double TP_Goal; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
if (OrderType() == Type) | |
{ | |
TotalLots += OrderLots(); | |
if (Type == OP_SELL) | |
{ | |
TotalDisProduct += (iPrice - OrderOpenPrice()) / point * OrderLots(); | |
} | |
if (Type == OP_BUY) | |
{ | |
TotalDisProduct += (OrderOpenPrice() - iPrice) / point * OrderLots(); | |
} | |
} | |
if (Type == OP_ALL) | |
{ | |
if (OrderType() == OP_SELL) | |
{ | |
//Profit += ((OrderOpenPrice() - iPrice) / point * OrderLots()) * 10; | |
TotalLots += OrderLots(); | |
TotalDisProduct += (iPrice - OrderOpenPrice()) / point * OrderLots(); | |
} | |
if (OrderType() == OP_BUY) | |
{ | |
//Profit += ((iPrice - OrderOpenPrice()) / point * OrderLots()) * 10; | |
TotalLots -= OrderLots(); | |
TotalDisProduct += (OrderOpenPrice() - iPrice) / point * OrderLots(); | |
} | |
} | |
} | |
} | |
if (EqualZero(TotalLots)) return(0.0); | |
TP_Goal = (((Profit) / 10) + TotalDisProduct) / TotalLots; | |
if (Type == OP_SELL) | |
return(iPrice - (TP_Goal * point)); | |
if (Type == OP_BUY) | |
return(iPrice + (TP_Goal * point)); | |
if (Type == OP_ALL) | |
{ | |
return(TP_Goal > 0 ? (iPrice + (TP_Goal * point)) : (iPrice - (TP_Goal * point))); | |
} | |
return(0.0); | |
} | |
double Cal_Profit(int Type,double iPrice) | |
{ | |
double Profit = 0.0; | |
for (int i=0;i<OrdersTotal();i++) | |
{ | |
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; | |
if (OrderMagicNumber() == id_1 && OrderSymbol() == Symbol()) | |
{ | |
if (OrderType() == Type) | |
{ | |
if (Type == OP_SELL) | |
{ | |
Profit += ((OrderOpenPrice() - iPrice) / point * OrderLots()) * 10; | |
} | |
if (Type == OP_BUY) | |
{ | |
Profit += ((iPrice - OrderOpenPrice()) / point * OrderLots()) * 10; | |
} | |
} | |
if (Type == OP_ALL) | |
{ | |
if (OrderType() == OP_SELL) | |
{ | |
Profit += ((OrderOpenPrice() - iPrice) / point * OrderLots()) * 10; | |
} | |
if (OrderType() == OP_BUY) | |
{ | |
Profit += ((iPrice - OrderOpenPrice()) / point * OrderLots()) * 10; | |
} | |
} | |
} | |
} | |
return(Profit); | |
} | |
bool CheckValid() | |
{ | |
uchar temp[256] = {0}; | |
int SW = 0; | |
if (!IsDemo()) | |
{ | |
SW = CheckList(AccountNumber(),2,temp); | |
string str_temp = CharArrayToString(temp,0,WHOLE_ARRAY,CP_UTF8); | |
StringReplace(str_temp,"\r\n",""); | |
if (SW < 0 || str_temp == "1") | |
{ | |
return(false); | |
} | |
} | |
return(true); | |
} | |
void UpdateFiles(const int Mode) | |
{ | |
if (Mode == MODE_INIT) | |
{ | |
if (!GlobalVariableCheck("DNSW")) GlobalVariableSet("DNSW",0); | |
if (GlobalVariableSetOnCondition("DNSW",1,0)) | |
{ | |
if (UninitializeReason() != 3) | |
{ | |
Sleep(1000); | |
} | |
} | |
} | |
if (Mode == MODE_TICK) | |
{ | |
if (TimeHour(TimeLocal()) == DownloadHour && TimeMinute(TimeLocal()) == DownloadMin && H1BarTime != iTime(Symbol(),PERIOD_H1,0)) | |
{ | |
if (!GlobalVariableCheck("DNSW0")) GlobalVariableSet("DNSW0",0); | |
if (GlobalVariableSetOnCondition("DNSW0",1,0)) | |
{ | |
Sleep(1000); | |
GlobalVariableSet("DNSW0",0); | |
} | |
H1BarTime = iTime(Symbol(),PERIOD_H1,0); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#property copyright "" | |
#property link "" | |
#include <WinUser32.mqh> | |
#include <stdlib.mqh> | |
bool Get_Full_Take_Profit = TRUE; | |
bool Alert_With_Sound = TRUE; | |
bool Get_Protected_Stop_Loss = false; | |
color Lines_Color = Gold; | |
double gd_unused_92 = 1.0; | |
string gs_100 = ""; | |
string gsa_108[9]; | |
int init() { | |
if (Digits == 5 || Digits == 3) gd_unused_92 = 10; | |
HideTestIndicators(TRUE); | |
CheckForOrder(); | |
Comments(); | |
return (0); | |
} | |
int deinit() { | |
deleteObject("line_FirstTakeProfit"); | |
deleteObject("arrow_FirstTakeProfit"); | |
deleteObject("label_FirstTakeProfit"); | |
deleteObject("Sirius_down"); | |
deleteObject("Sirius_up"); | |
return (0); | |
} | |
int start() { | |
Comments(); | |
CheckForOrder(); | |
return (0); | |
} | |
void Comments() { | |
string ls_0 = "\n"; | |
string ls_8 = "" | |
+ "\n" | |
+ "Sirius Management Robot" | |
+ "\n" | |
+ "------------------------------------------------" | |
+ "\n" | |
+ "Account Leverage: " + DoubleToStr(AccountLeverage(), 0) | |
+ "\n" | |
+ "Account Currency: " + AccountCurrency() | |
+ "\n" | |
+ "Account Balance: " + DoubleToStr(AccountBalance(), 2) | |
+ "\n" | |
+ "Account Equity: " + DoubleToStr(AccountEquity(), 2) | |
+ "\n" | |
+ "------------------------------------------------" | |
+ "\n" | |
+ "Trade Information:" | |
+ "\n" | |
+ "Trade Open Price: " + gsa_108[4] | |
+ "\n" | |
+ "Trade Stop Loss: " + gsa_108[2] | |
+ "\n" | |
+ "First Take Profit: " + gsa_108[8] | |
+ "\n" | |
+ "------------------------------------------------" + ls_0; | |
for (int l_count_16 = 0; !IsStopped() && !IsConnected(); l_count_16++) { | |
Comment("Waiting for connection"); | |
Sleep(150); | |
} | |
if (gsa_108[0] == "") { | |
deleteObject("line_80%獲利目標"); | |
deleteObject("arrow_80%獲利目標"); | |
deleteObject("label_80%獲利目標"); | |
Comment(Symbol() + ": Did not find any open order! Exiting..."); | |
return; | |
} | |
Comment(ls_8); | |
} | |
void createObject(string as_0, string as_8, double a_price_16, color a_color_24) { | |
if (ObjectFind("arrow_" + as_0) == -1) { | |
ObjectCreate("arrow_" + as_0, OBJ_HLINE, 0, Time[0], a_price_16); | |
ObjectSet("arrow_" + as_0, OBJPROP_STYLE, STYLE_DASH); | |
ObjectSet("arrow_" + as_0, OBJPROP_COLOR, a_color_24); | |
ObjectSetText("arrow_" + as_0, as_8 + DoubleToStr(a_price_16, Digits), 12, "Tahoma", a_color_24); | |
} | |
if (ObjectFind("line_" + as_0) == -1) { | |
ObjectCreate("line_" + as_0, OBJ_HLINE, 0, TimeCurrent(), a_price_16); | |
ObjectSet("line_" + as_0, OBJPROP_COLOR, a_color_24); | |
ObjectSet("line_" + as_0, OBJPROP_STYLE, STYLE_DASH); | |
} | |
if (ObjectFind("label_" + as_0) != 0) { | |
ObjectCreate("label_" + as_0, OBJ_TEXT, 0, Time[20], a_price_16); | |
ObjectSetText("label_" + as_0, as_8 + DoubleToStr(a_price_16, Digits), 12, "Tahoma", a_color_24); | |
} | |
} | |
void deleteObject(string a_name_0) { | |
if (ObjectFind(a_name_0) != -1) ObjectDelete(a_name_0); | |
} | |
void SetOrderProperties() { | |
int l_file_0 = FileOpen("Sirius" + gsa_108[1] + gsa_108[4] + gsa_108[5] + ".dat", FILE_BIN|FILE_WRITE); | |
if (l_file_0 > 0) { | |
FileWriteArray(l_file_0, gsa_108, 0, 9); | |
FileClose(l_file_0); | |
return; | |
} | |
int l_error_4 = GetLastError(); | |
if (l_error_4 > 0/* NO_ERROR */) Print("Sirius" + gsa_108[1] + gsa_108[4] + gsa_108[5] + ".dat" + "Error File error=", l_error_4, ": ", ErrorDescription(l_error_4)); | |
} | |
void GetOrderProperties(string as_0) { | |
int l_file_8 = FileOpen("Sirius" + as_0 + ".dat", FILE_BIN|FILE_READ); | |
if (l_file_8 > 0) { | |
FileReadArray(l_file_8, gsa_108, 0, 9); | |
FileClose(l_file_8); | |
return; | |
} | |
int l_error_12 = GetLastError(); | |
if (l_error_12 > 0/* NO_ERROR */) Print("Sirius" + as_0 + ".dat" + "Error File error=", l_error_12, ": ", ErrorDescription(l_error_12)); | |
} | |
bool FileExists(string a_name_0) { | |
int l_file_8 = FileOpen(a_name_0, FILE_READ); | |
if (l_file_8 < 1) return (FALSE); | |
FileClose(l_file_8); | |
return (TRUE); | |
} | |
void CheckForOrder() { | |
int l_error_4; | |
bool l_ord_close_8; | |
int l_count_0 = 0; | |
for (int l_pos_12 = OrdersTotal() - 1; l_pos_12 >= 0; l_pos_12--) { | |
if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES) == FALSE) break; | |
if (OrderSymbol() == Symbol()) { | |
RefreshRates(); | |
l_count_0++; | |
if (OrderStopLoss() <= 0.0) { | |
DoAlert(Symbol() + ": 請設定止損價格! "); | |
break; | |
} | |
if (FileExists("Sirius" + Symbol() + DoubleToStr(OrderOpenPrice(), Digits) + OrderOpenTime() + ".dat")) { | |
GetOrderProperties(Symbol() + DoubleToStr(OrderOpenPrice(), Digits) + OrderOpenTime()); | |
if (gsa_108[3] == "1" && gsa_108[2] != DoubleToStr(OrderStopLoss(), Digits)) { | |
gsa_108[2] = DoubleToStr(OrderStopLoss(), Digits); | |
switch (OrderType()) { | |
case OP_SELL: | |
gsa_108[8] = DoubleToStr(StrToDouble(gsa_108[4]) - (StrToDouble(gsa_108[2]) - StrToDouble(gsa_108[4])) / 2.0, Digits); | |
break; | |
case OP_BUY: | |
gsa_108[8] = DoubleToStr(StrToDouble(gsa_108[4]) + (StrToDouble(gsa_108[4]) - StrToDouble(gsa_108[2])) / 2.0, Digits); | |
} | |
SetOrderProperties(); | |
} | |
} else { | |
gsa_108[0] = OrderTicket(); | |
gsa_108[1] = OrderSymbol(); | |
gsa_108[2] = DoubleToStr(OrderStopLoss(), Digits); | |
gsa_108[3] = "1"; | |
gsa_108[4] = DoubleToStr(OrderOpenPrice(), Digits); | |
gsa_108[5] = OrderOpenTime(); | |
gsa_108[6] = DoubleToStr(OrderLots(), 2); | |
gsa_108[7] = OrderType(); | |
switch (OrderType()) { | |
case OP_SELL: | |
gsa_108[8] = DoubleToStr(StrToDouble(gsa_108[4]) - (StrToDouble(gsa_108[2]) - StrToDouble(gsa_108[4])) / 2.0, Digits); | |
break; | |
case OP_BUY: | |
gsa_108[8] = DoubleToStr(StrToDouble(gsa_108[4]) + (StrToDouble(gsa_108[4]) - StrToDouble(gsa_108[2])) / 2.0, Digits); | |
} | |
SetOrderProperties(); | |
} | |
switch (OrderType()) { | |
case OP_SELL: | |
if (gsa_108[3] == "1") { | |
deleteObject("line_80%獲利目標"); | |
deleteObject("arrow_80%獲利目標"); | |
deleteObject("label_80%獲利目標"); | |
createObject("80%獲利目標t", "80%獲利目標: ", StrToDouble(gsa_108[8]), Lines_Color); | |
if (StrToDouble(gsa_108[2]) > 0.0 && Ask <= StrToDouble(gsa_108[8])) { | |
l_ord_close_8 = OrderClose(OrderTicket(), 0.8 * OrderLots(), Ask, 5, Lines_Color); | |
if (l_ord_close_8) { | |
if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " TP1 "); | |
if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " TP1 ", "Sirius Information", MB_OKCANCEL); | |
if (Get_Full_Take_Profit) { | |
gsa_108[3] = "2"; | |
SetOrderProperties(); | |
} else CleanParams(); | |
} else { | |
l_error_4 = GetLastError(); | |
if (l_error_4 > 0/* NO_ERROR */) { | |
Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4)); | |
if (l_error_4 == 138/* REQUOTE */) Alert("Sirius tried to close a trade @" + DoubleToStr(Ask, Digits) + " but broker rejected order."); | |
} | |
} | |
} | |
} | |
if (gsa_108[3] == "2" && Get_Full_Take_Profit) { | |
if (iMA(OrderSymbol(), 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(OrderSymbol(), 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) && iMA(OrderSymbol(), 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(OrderSymbol(), 0, 14, 0, MODE_EMA, PRICE_CLOSE, 1)) { | |
l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), Ask, 5, Lines_Color); | |
if (l_ord_close_8) { | |
if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " TP2 "); | |
if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " TP2 ", "Sirius Information", MB_OKCANCEL); | |
CleanParams(); | |
} else { | |
l_error_4 = GetLastError(); | |
if (l_error_4 > 0/* NO_ERROR */) { | |
Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4)); | |
if (l_error_4 == 138/* REQUOTE */) Alert("Sirius tried to close a trade @" + DoubleToStr(Ask, Digits) + " but broker rejected order."); | |
} | |
} | |
} | |
} | |
if (gsa_108[3] == "1" && Get_Protected_Stop_Loss && TimeCurrent() - OrderOpenTime() > 60 * Period()) { | |
if (iMA(NULL, 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) && iMA(NULL, 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, 0, 14, 0, MODE_EMA, PRICE_CLOSE, 1)) { | |
l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), Ask, 5, Lines_Color); | |
if (l_ord_close_8) { | |
if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " PSL "); | |
if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " PSL ", "Sirius Information", MB_OKCANCEL); | |
CleanParams(); | |
} else { | |
l_error_4 = GetLastError(); | |
if (l_error_4 > 0/* NO_ERROR */) { | |
Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4)); | |
if (l_error_4 == 138/* REQUOTE */) Alert("Sirius tried to close a trade @" + DoubleToStr(Ask, Digits) + " but broker rejected order."); | |
} | |
} | |
} | |
} | |
break; | |
case OP_BUY: | |
if (gsa_108[3] == "1") { | |
deleteObject("line_80%獲利目標"); | |
deleteObject("arrow_80%獲利目標"); | |
deleteObject("label_80%獲利目標"); | |
createObject("80%獲利目標", "80%獲利目標: ", StrToDouble(gsa_108[8]), Lines_Color); | |
if (StrToDouble(gsa_108[2]) > 0.0 && Bid >= StrToDouble(gsa_108[8])) { | |
l_ord_close_8 = OrderClose(OrderTicket(), 0.8 * OrderLots(), Bid, 5, Lines_Color); | |
if (l_ord_close_8) { | |
if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " TP1 "); | |
if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " TP1 ", "Sirius Information", MB_OKCANCEL); | |
if (Get_Full_Take_Profit) { | |
gsa_108[3] = "2"; | |
SetOrderProperties(); | |
} else CleanParams(); | |
} else { | |
l_error_4 = GetLastError(); | |
if (l_error_4 > 0/* NO_ERROR */) { | |
if (l_error_4 == 138/* REQUOTE */) Alert("Sirius tried to close a trade @" + DoubleToStr(Bid, Digits) + " but broker rejected order."); | |
Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4)); | |
} | |
} | |
} | |
} | |
if (gsa_108[3] == "2" && Get_Full_Take_Profit) { | |
if (iMA(OrderSymbol(), 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(OrderSymbol(), 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) && iMA(OrderSymbol(), 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(OrderSymbol(), 0, 14, 0, MODE_EMA, PRICE_CLOSE, 1)) { | |
l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), Bid, 5, Lines_Color); | |
if (l_ord_close_8) { | |
if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " TP2 "); | |
if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " TP2 ", "Sirius Information", MB_OKCANCEL); | |
CleanParams(); | |
} else { | |
l_error_4 = GetLastError(); | |
if (l_error_4 > 0/* NO_ERROR */) { | |
if (l_error_4 == 138/* REQUOTE */) Alert("Sirius tried to close a trade @" + DoubleToStr(Bid, Digits) + " but broker rejected order."); | |
Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4)); | |
} | |
} | |
} | |
} | |
if (gsa_108[3] == "1" && Get_Protected_Stop_Loss && TimeCurrent() - OrderOpenTime() > 60 * Period()) { | |
if (iMA(NULL, 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) && iMA(NULL, 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, 0, 14, 0, MODE_EMA, PRICE_CLOSE, 1)) { | |
l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), Bid, 5, Lines_Color); | |
if (l_ord_close_8) { | |
if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " PSL "); | |
if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " PSL ", "Sirius Information", MB_OKCANCEL); | |
CleanParams(); | |
} else { | |
l_error_4 = GetLastError(); | |
if (l_error_4 > 0/* NO_ERROR */) { | |
if (l_error_4 == 138/* REQUOTE */) Alert("Sirius tried to close a trade @" + DoubleToStr(Bid, Digits) + " but broker rejected order."); | |
Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4)); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
if (l_count_0 == 0) { | |
deleteObject("line_80%獲利目標"); | |
deleteObject("arrow_80%獲利目標"); | |
deleteObject("label_80%獲利目標"); | |
Comment(Symbol() + ": Did not find any open order! Exiting..."); | |
} | |
} | |
void CleanParams() { | |
if (gsa_108[0] != "") { | |
if (FileExists("Sirius" + gsa_108[1] + gsa_108[4] + gsa_108[5] + ".dat")) FileDelete("Sirius" + gsa_108[1] + gsa_108[4] + gsa_108[5] + ".dat"); | |
deleteObject("line_MyTakeProfit"); | |
deleteObject("arrow_MyTakeProfit"); | |
deleteObject("label_MyTakeProfit"); | |
deleteObject("Sirius_down"); | |
deleteObject("Sirius_up"); | |
gsa_108[0] = ""; | |
gsa_108[1] = ""; | |
gsa_108[2] = ""; | |
gsa_108[3] = ""; | |
gsa_108[4] = ""; | |
gsa_108[5] = ""; | |
gsa_108[6] = ""; | |
gsa_108[7] = ""; | |
gsa_108[8] = ""; | |
} | |
} | |
void DoAlert(string as_0) { | |
if (as_0 != gs_100) { | |
if (Alert_With_Sound) PlaySound("alert.wav"); | |
MessageBox(as_0, "Sirius Trade Management Robot", 0); | |
gs_100 = as_0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment