资金管理EA.docx
《资金管理EA.docx》由会员分享,可在线阅读,更多相关《资金管理EA.docx(46页珍藏版)》请在冰豆网上搜索。
资金管理EA
#include
#include
#include
externboolDebug=false;
externstringUserComment="";
externintMagicNum=0;
externboolEADisabled=false;
externboolEmergencyCloseAll=false;
externintPortion=1;
externboolUseAccountProtect=false;
externdoubleStopTradePercent=50;//percentofaccountbalancelostbeforetradingstops
externdoubleMaxDDPercent=60;//Percentofportionformaxdrawdownlevel.
externboolMoveToBEP=true;
externintMoveStopWhenPoint=48;
externintLockProfit=3;
externstringmm8="OrderProfitProtectSettings";
externboolUseOrderProfitProtect=true;
externintBeginProtectPositions=2;
externdoubleProfitProtectLevel_0_Pips=50;
externdoubleProfitProtectLevel_0_Percen=10;
externdoubleProfitProtectLevel_1_Pips=120;
externdoubleProfitProtectLevel_1_Percen=20;
externdoubleProfitProtectLevel_2_Pips=220;
externdoubleProfitProtectLevel_2_Percen=40;
externdoubleProfitProtectLevel_3_Pips=280;
externdoubleProfitProtectLevel_3_Percen=50;
externdoubleProfitProtectLevel_4_Pips=380;
externdoubleProfitProtectLevel_4_Percen=60;
externdoubleProfitProtectLevel_5_Pips=450;
externdoubleProfitProtectLevel_5_Percen=70;
//-----------------------------------------------------
externstrings08="--TRADINGTIMEMANAGEMENT--";
externstringTTM1="Settimeframeswhennewtradescanopen.";
externstringTTM2="IfStarthour=Stophour,thentrade24/5.";
externstringTTM3="IfTTMGoFlat=true,closeallopentrades";
externstringTTM4="whenoutsidetradinghoursordays.";
externboolEnableTradeTimeManage=false;
externintManualGMToffset=3;//Setyourbroker'sGMTOffset
externintGMTStartHour=0;//Starttradingat0:
00/GMT
externintGMTStopHour=0;//Stoptradingat23:
59/GMT
externboolTradeOnFriday=true;
externintFridayGMTStopHour=-1;//Ifsetto0orhigher,willpreventnewtradesfromopeningonFridaystartingatthathourGMT.
externboolTTMGoFlat=false;
externstrings12="--Usethissectiontoexcludetradingdays";
externboolEnableBlackout=false;
externintStartBlackoutDay=20;
externintStartBlackoutMonth=12;
externintStopBlackoutDay=15;
externintStopBlackoutMonth=01;
intStartBlackout=0;
intStopBlackout=0;
//-----------------------------------------------------
externboolUsingTakeProfit=true;
externintTakeProfit=300;
externboolUsingStopLoss=true;
externdoubleStopLoss=39;//piont
externboolUsingTrailingStop=false;
externdoubleTrailingStop=55;//piont
externboolUsingHiLoTrailingStop=true;
externboolUsingTimeStopLoss=true;
externintTimeStopLossMin=125;//Minute
stringmyType="ALL";
intMagic;
stringType;
doublemyPoint;
datetimeBarTime;
doubleLot;
doubleStopTradeBalance;
doubleInitialAB;
doubleMaxDDPer;
doubleMaxDD;
doubleOrdersBuyLots;
doubleOrdersSellLots;
doubleMaxTradeLots;
intOrderCount;
intBuyOrderCount;
intSellOrderCount;
doubleOrderProfitPips=0;
doubleOrderProfits=0;
doubleMaxOrderProfitPips=0;
doubleLastOrderOpenPrice;
//---signalvariable
intBaseEntrySignal;
intAddPositionSignal;
intCloseSignal;
intSignalFilter;
doubleuplevel;
doublednlevel;
//Commentvariable
stringtxt;
//+------------------------------------------------------------------+
//|expertinitializationfunction|
//+------------------------------------------------------------------+
intinit()
{
//----
myPoint=SetmyPoint();
Magic=0;
Type="ALL";
while(!
IsConnected())
{
Comment("Waitingforconnection...");
Sleep(10000);
}
StartBlackout=DayNumber(StartBlackoutMonth,StartBlackoutDay);
StopBlackout=DayNumber(StopBlackoutMonth,StopBlackoutDay);
InitialAB=AccountBalance();
StopTradeBalance=InitialAB-InitialAB*(StopTradePercent/100);
//----
return(0);
}
//+------------------------------------------------------------------+
//|expertdeinitializationfunction|
//+------------------------------------------------------------------+
intdeinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//|expertstartfunction|
//+------------------------------------------------------------------+
intstart()
{
if(EADisabled==true)
{
if(IsTesting())Comment("EADisabled!
CheckJournalLogfordetails.\n");
elseComment("EADisabled!
CheckExpertsLogfordetails.\n");
return(0);
}
Comment(txt);
TraceOrderDetail(Type);
if(EmergencyCloseAll==true)
{
CloseOrders(OP_SELL,"ALL");
CloseOrders(OP_BUY,"ALL");
EADisabled=true;
Comment("CloseAllPositionandHalt");
Print("CloseAllPositionandHalt");
return(0);
}
if(UseAccountProtect==true)
{
if(AccountProtection()==true)
{
Comment("AccountBalancedroppedbelowstoptradepercent");
Comment("ResetEA,accountbalancedroppedbelowstoptradepercent");
Print("AccountBalancedroppedbelowstoptradepercent");
return(0);
}
}
if(UseOrderProfitProtect==true)
{
if(PositionsProtection(Type)==true)
{
BarTime=Time[0];//don'topenorderinthecurrentBar
return(0);
}
}
if(!
IsTradingTime()&&TTMGoFlat&&OrderProfitTotalPips("ALL")>0)
{
Comment("Closingordersbecauseoutsideoftradingwindow.");
Print("Closingordersbecauseoutsideoftradingwindow.");
CloseOrders(OP_SELL,"ALL");
CloseOrders(OP_BUY,"ALL");
}
if(!
IsTradingTime())
{
Comment("Closingordersbecauseoutsideoftradingwindow.");
Print("Closingordersbecauseoutsideoftradingwindow.");
return(0);
}
ModifyDefaultStopLoss();
if(MoveToBEP==true)
BEP(Type);
if(UsingTrailingStop==true)
TrailStop(Type,TrailingStop);
if(UsingTimeStopLoss==true)
TimeStopLoss(Type);
//-------------------------------------------------------------
//----
return(0);
}
//+------------------------------------------------------------------+
//1long-1short0nosignal
//--------------------------------------------------------------------------------
doubleSetmyPoint()
{
doubleres;
if(Digits<4)res=0.01;
elseres=0.0001;
return(res);
}
doubleStopLong(doublePrice,intPips)
{
if(Price==0)return(0);
elsereturn(NormalizeDouble(Price-Pips*myPoint,Digits));
}
doubleStopShort(doublePrice,intPips)
{
if(Price==0)return(0);
elsereturn(NormalizeDouble(Price+Pips*myPoint,Digits));
}
doubleTakeLong(doublePrice,intPips)
{
if(Price==0)return(0);
elsereturn(NormalizeDouble(Price+Pips*myPoint,Digits));
}
doubleTakeShort(doublePrice,intPips)
{
if(Price==0)return(0);
elsereturn(NormalizeDouble(Price-Pips*myPoint,Digits));
}
doubleValidStopLoss(intmyOrderType,doublePrice,doubleStopPrice)
{
doubleSpread=MarketInfo(Symbol(),MODE_STOPLEVEL);
if(Digits==3||Digits==5)Spread/=10.0;
if(myOrderType==OP_BUY)
if(Price-StopPriceif(myOrderType==OP_SELL)
if(StopPrice-Pricereturn(StopPrice);
}
doubleValidTakeProfit(intmyOrderType,doublePrice,doublemyTakeProfit)
{
doubleSpread=MarketInfo(Symbol(),MODE_STOPLEVEL);
if(Digits==3||Digits==5)Spread/=10.0;
if(myOrderType==OP_SELL)
if(Price-myTakeProfitmyTakeProfit=Price-Spread*myPoint;
if(myOrderType==OP_BUY)
if(myTakeProfit-PricemyTakeProfit=Price+Spread*myPoint;
return(myTakeProfit);
}
intCalculateBuyOrders(stringmyType)
{
intcnt=0;
inttotal=OrdersTotal();
for(inti=0;i{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue;
if(isOrder(OrderSymbol(),OrderMagicNumber(),OrderComment(),myType)&&
(OrderType()==OP_BUY)
)
cnt++;
}
return(cnt);
}
intCalculateSellOrders(stringmyType)
{
intcnt=0;
inttotal=OrdersTotal();
for(inti=0;i{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue;
if(isOrder(OrderSymbol(),OrderMagicNumber(),OrderComment(),myType)&&
(OrderType()==OP_SELL)
)
cnt++;
}
return(cnt);
}
//------------------------------------------------------------------------------------
doubleCalculateBuyLots(stringmyType)
{
doublesum=0;
inttotal=OrdersTotal();
for(inti=0;i{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue;
if(isOrder(OrderSymbol(),OrderMagicNumber(),OrderComment(),myType)&&
(OrderType()==OP_BUY)
)
sum+=OrderLots();
}
return(sum);
}
doubleCalculateSellLots(stringmyType)
{
doublesum=0;
inttotal=OrdersTotal();
for(inti=0;i{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue;
if(isOrder(OrderSymbol(),OrderMagicNumber(),OrderComment(),myType)&&
(OrderType()==OP_SELL)
)
sum+=OrderLots();
}
return(sum);
}
boolisOrder(stringmySymbol,intmyMagicNumber,stringmyOrderComment,stringmyType)
{
if(Debug)
{
Print(mySymbol,"",myMagicNumber,"",myOrderComment,"",myType);
}
if(myType=="ALL"||myType=="")
{if(StringFind(myOrderComment,UserComment)>=0&&
mySymbol==Symbol()&&myMagicNumber==Magic)
return(true);
else
return(false);
}
}//ENDFUN
voidTraceOrderDetail(stringmyType)
{
staticintLastOrderCount=-1;
BuyOrderCount=CalculateBuyOrders("ALL");
SellOrderCount=CalculateSellOrders("ALL");
OrderCount=BuyOrderCount+SellOrderCount;
if(OrderCount<1||LastOrderCount!
=OrderCount)
{
MaxOrderProfitPips=0;
LastOrderOpenPrice=99999999999;
LastOr