《资金管理EA.doc》由会员分享,可在线阅读,更多相关《资金管理EA.doc(32页珍藏版)》请在三一办公上搜索。
1、#include #include #include extern bool Debug = false;extern string UserComment = ;extern int MagicNum = 0;extern bool EADisabled = false;extern bool EmergencyCloseAll = false;extern int Portion = 1;extern bool UseAccountProtect = false;extern double StopTradePercent = 50; / percent of account balanc
2、e lost before trading stopsextern double MaxDDPercent = 60; / Percent of portion for max drawdown level.extern bool MoveToBEP = true; extern int MoveStopWhenPoint = 48;extern int LockProfit = 3;extern string mm8 = OrderProfitProtect Settings;extern bool UseOrderProfitProtect = true;extern int BeginP
3、rotectPositions = 2;extern double ProfitProtectLevel_0_Pips = 50;extern double ProfitProtectLevel_0_Percen = 10;extern double ProfitProtectLevel_1_Pips = 120;extern double ProfitProtectLevel_1_Percen = 20;extern double ProfitProtectLevel_2_Pips = 220;extern double ProfitProtectLevel_2_Percen = 40;ex
4、tern double ProfitProtectLevel_3_Pips = 280;extern double ProfitProtectLevel_3_Percen = 50;extern double ProfitProtectLevel_4_Pips = 380;extern double ProfitProtectLevel_4_Percen = 60;extern double ProfitProtectLevel_5_Pips = 450;extern double ProfitProtectLevel_5_Percen = 70;/-extern string s08 = -
5、TRADING TIME MANAGEMENT-;extern string TTM1 = Set time frames when new trades can open.;extern string TTM2 = If Starthour = Stophour, then trade 24/5.;extern string TTM3 = If TTMGoFlat=true, close all open trades;extern string TTM4 = when outside trading hours or days.;extern bool EnableTradeTimeMan
6、age = false;extern int ManualGMToffset = 3; / Set your brokers GMT Offsetextern int GMTStartHour = 0; / Start trading at 0:00/GMTextern int GMTStopHour = 0; / Stop trading at 23:59/GMTextern bool TradeOnFriday = true;extern int FridayGMTStopHour = -1; / If set to 0 or higher, will prevent new trades
7、 from opening on Friday starting at that hour GMT.extern bool TTMGoFlat = false;extern string s12 = - Use this section to exclude trading days;extern bool EnableBlackout = false;extern int StartBlackoutDay = 20;extern int StartBlackoutMonth = 12;extern int StopBlackoutDay = 15;extern int StopBlackou
8、tMonth = 01;int StartBlackout = 0;int StopBlackout = 0;/-extern bool UsingTakeProfit = true;extern int TakeProfit = 300;extern bool UsingStopLoss = true;extern double StopLoss = 39; / piontextern bool UsingTrailingStop = false;extern double TrailingStop = 55;/piontextern bool UsingHiLoTrailingStop =
9、 true;extern bool UsingTimeStopLoss = true;extern int TimeStopLossMin = 125; /Minutestring myType = ALL;int Magic;string Type;double myPoint;datetime BarTime;double Lot;double StopTradeBalance;double InitialAB;double MaxDDPer;double MaxDD;double OrdersBuyLots;double OrdersSellLots;double MaxTradeLot
10、s;int OrderCount;int BuyOrderCount;int SellOrderCount;double OrderProfitPips = 0;double OrderProfits = 0;double MaxOrderProfitPips = 0;double LastOrderOpenPrice;/-signal variableint BaseEntrySignal;int AddPositionSignal;int CloseSignal;int SignalFilter;double uplevel;double dnlevel;/ Comment variabl
11、estring txt;/+-+/| expert initialization function |/+-+int init() /- myPoint = SetmyPoint(); Magic = 0; Type = ALL; while (!IsConnected() Comment(Waiting for connection.); Sleep(10000); StartBlackout = DayNumber(StartBlackoutMonth, StartBlackoutDay); StopBlackout = DayNumber(StopBlackoutMonth, StopB
12、lackoutDay); InitialAB=AccountBalance(); StopTradeBalance=InitialAB - InitialAB*(StopTradePercent/100);/- return(0); /+-+/| expert deinitialization function |/+-+int deinit() /- /- return(0); /+-+/| expert start function |/+-+int start() if (EADisabled = true) if (IsTesting() Comment(EA Disabled! Ch
13、eck Journal Log for details.n); else Comment(EA Disabled! Check Experts Log for details.n); return(0); Comment(txt); TraceOrderDetail(Type); if(EmergencyCloseAll = true) CloseOrders(OP_SELL,ALL); CloseOrders(OP_BUY,ALL); EADisabled = true; Comment(Close All Position and Halt); Print(Close All Positi
14、on and Halt); return(0); if(UseAccountProtect = true) if(AccountProtection()= true) Comment(Account Balance dropped below stop trade percent); Comment( Reset EA, account balance dropped below stop trade percent); Print(Account Balance dropped below stop trade percent); return(0); if(UseOrderProfitPr
15、otect = true) if(PositionsProtection(Type)= true) BarTime = Time0; /dont open order in the current Bar return(0); if (!IsTradingTime() & TTMGoFlat & OrderProfitTotalPips(ALL) 0) Comment(Closing orders because outside of trading window.); Print(Closing orders because outside of trading window.); Clos
16、eOrders(OP_SELL,ALL); CloseOrders(OP_BUY,ALL); if(!IsTradingTime() Comment(Closing orders because outside of trading window.); Print(Closing orders because outside of trading window.); return(0); ModifyDefaultStopLoss(); if(MoveToBEP = true) BEP(Type); if(UsingTrailingStop = true) TrailStop(Type,Tra
17、ilingStop); if(UsingTimeStopLoss = true) TimeStopLoss (Type); /- /- return(0); /+-+/1 long -1 short 0 no signal/-double SetmyPoint() double res; if (Digits 4) res = 0.01; else res = 0.0001; return (res);double StopLong(double Price, int Pips) if (Price = 0) return (0); else return (NormalizeDouble(P
18、rice - Pips * myPoint,Digits); double StopShort(double Price, int Pips) if (Price = 0) return (0); else return (NormalizeDouble(Price + Pips * myPoint,Digits);double TakeLong(double Price, int Pips) if (Price = 0) return (0); else return (NormalizeDouble(Price + Pips * myPoint,Digits);double TakeSho
19、rt(double Price, int Pips) if (Price = 0) return (0); else return (NormalizeDouble(Price - Pips * myPoint,Digits);double ValidStopLoss(int myOrderType, double Price, double StopPrice) double Spread = MarketInfo(Symbol(), MODE_STOPLEVEL); if (Digits = 3 | Digits = 5) Spread /= 10.0; if (myOrderType =
20、 OP_BUY) if (Price - StopPrice Spread * myPoint) StopPrice = NormalizeDouble(Price + Spread * myPoint,Digits); if (myOrderType = OP_SELL) if (StopPrice - Price Spread * myPoint) StopPrice = NormalizeDouble(Price - Spread * myPoint,Digits); return (StopPrice);double ValidTakeProfit(int myOrderType, d
21、ouble Price, double myTakeProfit) double Spread = MarketInfo(Symbol(), MODE_STOPLEVEL); if (Digits = 3 | Digits = 5) Spread /= 10.0; if (myOrderType = OP_SELL) if (Price - myTakeProfit Spread * myPoint) myTakeProfit = Price - Spread * myPoint; if (myOrderType = OP_BUY) if (myTakeProfit - Price Sprea
22、d * myPoint) myTakeProfit = Price + Spread * myPoint; return (myTakeProfit);int CalculateBuyOrders(string myType) int cnt = 0; int total = OrdersTotal(); for(int i = 0;itotal;i+) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) =false) continue; if( isOrder(OrderSymbol(), OrderMagicNumber(), OrderComment
23、(), myType) & (OrderType()=OP_BUY) ) cnt+; return(cnt);int CalculateSellOrders(string myType) int cnt = 0; int total = OrdersTotal(); for(int i = 0;itotal;i+) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) =false) continue; if( isOrder(OrderSymbol(), OrderMagicNumber(), OrderComment(), myType) & (Order
24、Type()=OP_SELL) ) cnt+; return(cnt);/-double CalculateBuyLots(string myType) double sum = 0; int total = OrdersTotal(); for(int i = 0;itotal;i+) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) =false) continue; if( isOrder(OrderSymbol(), OrderMagicNumber(), OrderComment(), myType) & (OrderType()=OP_BUY)
25、 ) sum+=OrderLots(); return(sum);double CalculateSellLots(string myType) double sum = 0; int total = OrdersTotal(); for(int i = 0;i=0 & mySymbol = Symbol() & myMagicNumber = Magic ) return(true); else return(false); /END FUNvoid TraceOrderDetail(string myType) static int LastOrderCount = -1; BuyOrde
26、rCount = CalculateBuyOrders (ALL); SellOrderCount = CalculateSellOrders(ALL); OrderCount = BuyOrderCount + SellOrderCount; if(OrderCount 1 | LastOrderCount != OrderCount) MaxOrderProfitPips = 0; LastOrderOpenPrice = 99999999999; LastOrderCount = OrderCount; else OrderProfitPips = OrderProfitTotalPip
27、s(myType); /order profit pips OrderProfits = OrderProfitPips * MarketInfo(Symbol(),MODE_TICKVALUE) * (myPoint /Point); OrdersBuyLots = CalculateBuyLots(myType); OrdersSellLots = CalculateSellLots(myType); if(MaxOrderProfitPips OrderProfitPips) MaxOrderProfitPips = OrderProfitPips; /+-+/-double Order
28、ProfitTotalPips(string myType) int cnt, total = 0; double tickvalue = MarketInfo(Symbol(),MODE_TICKVALUE); double TotalProfitPips = 0; for(cnt=0;cntOrdersTotal();cnt+) if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) =false) continue; if(OrderType()=OP_SELL|OrderType()=OP_BUY) & isOrder(OrderSymbol(),
29、OrderMagicNumber(), OrderComment(), myType) double PipProfitPips = (OrderProfit()/OrderLots()/ (tickvalue * (myPoint / Point); TotalProfitPips = TotalProfitPips+PipProfitPips; return(TotalProfitPips); /-void StopMoveToCost (string myType,int OffsetPips) bool res = false; int total = OrdersTotal(); f
30、or(int i = 0;i OrderStopLoss() ModiFyOrder(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - OffsetPips * myPoint, OrderTakeProfit(); if( OrderType()=OP_SELL & OrderOpenPrice() + OffsetPips * myPoint = (PortionBalance * MaxDDPercent / 100) ) CloseOrders(OP_SELL,ALL); CloseOrders(OP_BUY,ALL); Print(
31、AccountProtection Close); if ( -OrderProfits MaxDD) MaxDD = -OrderProfits;MaxDDPer = MathMax(MaxDDPer, MaxDD / PortionBalance * 100);double StepAB = InitialAB + InitialAB * (StopTradePercent / 100);double StepSTB = AccountBalance() - AccountBalance()*(StopTradePercent / 100);double NextISTB = StepAB
32、 - StepAB*(StopTradePercent / 100);if (StepSTB NextISTB)InitialAB = StepAB;StopTradeBalance = StepSTB;double InitialAccountMultiPortion = StopTradeBalance/Portion;if (PortionBalance InitialAccountMultiPortion)return (true); return(false);bool PositionsProtection(string myType) if(OrderCount = ProfitProtectLevel_5_Pips) if(OrderProfitPips NormalizeDouble(MaxOrderProfitPips * (ProfitProtectLevel_5_Percen /100),0) CloseOrders(OP_SELL,myType); CloseOrders(OP_BUY,myType); Print(Order protected