- #property copyright "shiyingpan"
- #property link "https://aijy.github.io"
- #property version "1.0"
- #property strict
- enum mybool
- {
- 是=1,
- 否=0,
- };
- input mybool 是否手动输入净值=否;
- input double 手动输入初始资金=0;
- input double 净值风险率=15; //--- 净值亏损15%时,强平出场
- input double 盈利提示一=5; //--- 净值盈利到5%时,提示关注
- input double 盈利提示二=10; //--- 净值盈利到10%时,强平出场
- input double 亏损提示一=5; //--- 净值亏损到5%时,提示关注
- input double 亏损提示二=10; //--- 净值亏损到10%时,抢平出场
- double 初始资金=0;
- int flag[4]={0,0,0,0}; //--- 净值浮动状态
- int OnInit()
- {
- while(AccountNumber()==0)
- Sleep(10000);
- // 初始资金=AccountEquity();
- if(是否手动输入净值==否)
- 初始资金=AccountBalance();
- else
- 初始资金=手动输入初始资金;
- if(初始资金==0)
- {
- Alert("当前账户无资金!");
- return(INIT_FAILED);
- }
- if(AccountEquity()<初始资金*(100-净值风险率)/100)
- {
- Alert("当前净值亏损超过净值风险率,无法加载!");
- return(INIT_FAILED);
- }
-
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
-
- }
- void OnTick()
- {
- if(AccountEquity()<初始资金*(100-净值风险率)/100)
- {
- Alert("资金亏损超过"+DoubleToString(净值风险率,1)+"%,全部平仓。亏损:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
- SendMail("资金亏损超过"+DoubleToString(净值风险率,1)+"%,全部平仓。亏损:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
- flag[1]=1;flag[2]=1;
- close_chart(); //改Return 为close_chart();
- close_all();
- Sleep(5000);
- }
- if(AccountEquity()<初始资金*(100-亏损提示一)/100 && flag[1]==0)
- {
- Alert("资金亏损超过"+DoubleToString(亏损提示一,1)+"%,亏损:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
- SendMail("资金亏损超过"+DoubleToString(亏损提示一,1)+"%,亏损:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
- flag[1]=1;
- return;
- }
- if(AccountEquity()<初始资金*(100-亏损提示二)/100 && flag[2]==0)
- {
- Alert("资金亏损超过"+DoubleToString(亏损提示二,1)+"%,亏损:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
- SendMail("资金亏损超过"+DoubleToString(亏损提示二,1)+"%,亏损:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
- flag[2]=1;
- close_chart();
- close_all();
- ExpertRemove();
- Sleep(100000);
- return;
- }
- if(AccountEquity()>初始资金*(100+盈利提示一)/100 && flag[3]==0)
- {
- Alert("资金盈利超过"+DoubleToString(盈利提示一,1)+"%,盈利:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
- SendMail("资金盈利超过"+DoubleToString(盈利提示一,1)+"%,盈利:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
- flag[3]=1;
- return;
- }
-
- if(AccountEquity()>初始资金*(100+盈利提示二)/100 && flag[4]==0)
- {
- Alert("资金盈利超过"+DoubleToString(盈利提示二,1)+"%,盈利:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
- SendMail("资金盈利超过"+DoubleToString(盈利提示二,1)+"%,盈利:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
- flag[4]=1;
- close_chart();
- close_all();
- ExpertRemove();
- Sleep(100000);
- return;
- }
- Sleep(500);
-
- }
- void close_all() //--- 平掉所有仓位
- {
- int ticket[200],cnt=0;
- for(int i=0;i<=OrdersTotal()-1;i++)
- {
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
- {
- ticket[cnt]=OrderTicket();
- cnt++;
- }
- }
- for(int i=0;i<=cnt-1;i++)
- {
- if(OrderSelect(ticket[i],SELECT_BY_TICKET,MODE_TRADES))
- {
- if(OrderType()==OP_BUY)
- if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,clrRed))
- OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,clrRed);
- if(OrderType()==OP_SELL)
- if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,clrRed))
- OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,clrRed);
- if(OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)
- if(!OrderDelete(OrderTicket(),clrRed))
- OrderDelete(OrderTicket(),clrRed);
- }
- }
- }
- void close_chart() //--- 关闭所有图表和EA
- {
- long chart_total[100];
- long currChart,prevChart=ChartFirst();
- chart_total[0]=prevChart;
- int i=0,limit=100;
- while(i<limit)// We have certainly not more than 100 open charts
- {
- currChart=ChartNext(prevChart); // Get the new chart ID by using the previous chart ID
- if(currChart<0) break; // Have reached the end of the chart list
- prevChart=currChart;// let's save the current chart ID for the ChartNext()
- i=i+1;// Do not forget to increase the counter
- chart_total[i]=prevChart;
- Print(i,chart_total[i]);
- }
- for(int j=0;j<=i;j++)
- if(chart_total[j]!=ChartID())
- ChartClose(chart_total[j]);
- }
复制代码
|