📅 财经日历 📊 实时波动 📈 大盘云图 📶 行情走势 🆚 投机情绪 🚀 今日热点

    净值风险控制

    2019-12-09 · 1769 阅读
    1. #property copyright "shiyingpan"
    2. #property link      "https://aijy.github.io"
    3. #property version   "1.0"
    4. #property strict

    5. enum mybool
    6. {
    7.    是=1,
    8.    否=0,
    9. };

    10. input mybool 是否手动输入净值=否;
    11. input double 手动输入初始资金=0;
    12. input double 净值风险率=15;        //--- 净值亏损15%时,强平出场
    13. input double 盈利提示一=5;         //--- 净值盈利到5%时,提示关注
    14. input double 盈利提示二=10;        //--- 净值盈利到10%时,强平出场
    15. input double 亏损提示一=5;         //--- 净值亏损到5%时,提示关注
    16. input double 亏损提示二=10;        //--- 净值亏损到10%时,抢平出场
    17. double 初始资金=0;

    18. int flag[4]={0,0,0,0}; //--- 净值浮动状态

    19. int OnInit()
    20.   {
    21.    while(AccountNumber()==0)
    22.       Sleep(10000);
    23. //   初始资金=AccountEquity();   
    24.    if(是否手动输入净值==否)
    25.        初始资金=AccountBalance();
    26.    else
    27.        初始资金=手动输入初始资金;   
    28.    if(初始资金==0)
    29.    {
    30.       Alert("当前账户无资金!");
    31.       return(INIT_FAILED);
    32.    }
    33.    if(AccountEquity()<初始资金*(100-净值风险率)/100)
    34.    {
    35.       Alert("当前净值亏损超过净值风险率,无法加载!");
    36.       return(INIT_FAILED);  
    37.    }
    38.    
    39.    return(INIT_SUCCEEDED);
    40.   }
    41. //+------------------------------------------------------------------+
    42. //| Expert deinitialization function                                 |
    43. //+------------------------------------------------------------------+
    44. void OnDeinit(const int reason)
    45.   {

    46.   }
    47. void OnTick()
    48.   {
    49.    if(AccountEquity()<初始资金*(100-净值风险率)/100)
    50.    {
    51.       Alert("资金亏损超过"+DoubleToString(净值风险率,1)+"%,全部平仓。亏损:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
    52.       SendMail("资金亏损超过"+DoubleToString(净值风险率,1)+"%,全部平仓。亏损:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
    53.       flag[1]=1;flag[2]=1;
    54.       close_chart(); //改Return 为close_chart();
    55.       close_all();
    56.       Sleep(5000);
    57.    }   
    58.    if(AccountEquity()<初始资金*(100-亏损提示一)/100 && flag[1]==0)
    59.    {
    60.       Alert("资金亏损超过"+DoubleToString(亏损提示一,1)+"%,亏损:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
    61.       SendMail("资金亏损超过"+DoubleToString(亏损提示一,1)+"%,亏损:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
    62.       flag[1]=1;
    63.       return;   
    64.    }  
    65.    if(AccountEquity()<初始资金*(100-亏损提示二)/100 && flag[2]==0)
    66.    {
    67.       Alert("资金亏损超过"+DoubleToString(亏损提示二,1)+"%,亏损:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
    68.       SendMail("资金亏损超过"+DoubleToString(亏损提示二,1)+"%,亏损:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
    69.       flag[2]=1;
    70.       close_chart();
    71.       close_all();
    72.       ExpertRemove();
    73.       Sleep(100000);
    74.       return;
    75.    }      
    76.    if(AccountEquity()>初始资金*(100+盈利提示一)/100 && flag[3]==0)
    77.    {
    78.       Alert("资金盈利超过"+DoubleToString(盈利提示一,1)+"%,盈利:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
    79.       SendMail("资金盈利超过"+DoubleToString(盈利提示一,1)+"%,盈利:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
    80.       flag[3]=1;
    81.       return;
    82.    }
    83.    
    84.    if(AccountEquity()>初始资金*(100+盈利提示二)/100 && flag[4]==0)
    85.    {
    86.       Alert("资金盈利超过"+DoubleToString(盈利提示二,1)+"%,盈利:",DoubleToStr(AccountEquity()-初始资金,2)," 净值:",AccountEquity());
    87.       SendMail("资金盈利超过"+DoubleToString(盈利提示二,1)+"%,盈利:"+DoubleToStr(AccountEquity()-初始资金,2)+" 净值:"+DoubleToStr(AccountEquity()),"");
    88.       flag[4]=1;
    89.       close_chart();
    90.       close_all();
    91.       ExpertRemove();
    92.       Sleep(100000);
    93.       return;
    94.    }
    95.    Sleep(500);   
    96.    
    97.   }

    98. void close_all() //--- 平掉所有仓位
    99. {
    100.    int ticket[200],cnt=0;
    101.    for(int i=0;i<=OrdersTotal()-1;i++)
    102.    {
    103.       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
    104.       {
    105.          ticket[cnt]=OrderTicket();
    106.          cnt++;
    107.       }   
    108.    }
    109.    for(int i=0;i<=cnt-1;i++)
    110.    {
    111.       if(OrderSelect(ticket[i],SELECT_BY_TICKET,MODE_TRADES))
    112.       {
    113.          if(OrderType()==OP_BUY)
    114.             if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,clrRed))
    115.                OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,clrRed);
    116.          if(OrderType()==OP_SELL)
    117.             if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,clrRed))
    118.                OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,clrRed);  
    119.          if(OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)  
    120.             if(!OrderDelete(OrderTicket(),clrRed))
    121.                OrderDelete(OrderTicket(),clrRed);           
    122.       }   
    123.    }

    124. }

    125. void close_chart()  //--- 关闭所有图表和EA
    126. {
    127.    long chart_total[100];
    128.    long currChart,prevChart=ChartFirst();
    129.    chart_total[0]=prevChart;
    130.    int i=0,limit=100;

    131.    while(i<limit)// We have certainly not more than 100 open charts
    132.      {
    133.       currChart=ChartNext(prevChart); // Get the new chart ID by using the previous chart ID
    134.       if(currChart<0) break;          // Have reached the end of the chart list
    135.       prevChart=currChart;// let's save the current chart ID for the ChartNext()
    136.       i=i+1;// Do not forget to increase the counter
    137.       chart_total[i]=prevChart;
    138.       Print(i,chart_total[i]);      
    139.      }

    140.    for(int j=0;j<=i;j++)
    141.       if(chart_total[j]!=ChartID())
    142.          ChartClose(chart_total[j]);
    143. }
    复制代码


    ""
    还没有人打赏,支持一下
    回复

    举报

     
    您需要登录后才可以回帖 登录 | 注册

    提醒: 禁止引战、谩骂、灌水内容

    微信二维码

    有问题联系客服