Results 1 to 4 of 4

Thread: EA代碼需要很少的幫助

  1. #1
    下面是一個EA,我將其用作模板,為我使用的系統構建EA。我不是一個經驗豐富的程序員,我希望這個項目能讓我建立我對mql4的了解。下面的代碼運行良好但是我只停留在一小部分。此EA目前一次只開放一筆交易。我希望同步交易成為可能。有人可以快速查看代碼,讓我知道哪個部分限制了這一點。我相信它可能是檢查位置部分。非常感謝。


    Quote Originally Posted by ;
    #define SIGNAL_NONE 0
    #define SIGNAL_BUY 1
    #define SIGNAL_SELL 2
    #define SIGNAL_CLOSEBUY 3
    #define SIGNAL_CLOSESELL 4
    extern int MagicNumber = 0;
    extern bool SignalMail = False;
    extern bool EachTickMode = False;
    extern double Lots = 1.0;
    extern int Slippage = 3;
    extern bool UseStopLoss = False;
    extern int StopLoss = 30;
    extern bool UseTakeProfit = False;
    extern int TakeProfit = 60;
    extern bool UseTrailingStop = False;
    extern int TrailingStop = 30;
    int BarCount;
    當前;
    bool TickCheck =假;
    // ----------------------------------------------- -------------------
    //|專家初始化函數|
    // ----------------------------------------------- -------------------
    int init(){
    BarCount =酒吧;
    if(EachTickMode)Current = 0; else Current = 1;
    返回(0);
    }
    // ----------------------------------------------- -------------------
    //|專家取消初始化功能|
    // ----------------------------------------------- -------------------
    int deinit(){
    返回(0);
    }
    // ----------------------------------------------- -------------------
    //|專家啟動功能|
    // ----------------------------------------------- -------------------
    int start(){
    int Order = SIGNAL_NONE;
    int Total,Ticket;
    double StopLossLevel,TakeProfitLevel;

    if(EachTickMode Bars!= BarCount)TickCheck = False;
    Total = OrdersTotal();
    Order = SIGNAL_NONE;
    // ----------------------------------------------- -------------------
    //|變量開始|
    // ----------------------------------------------- -------------------

    double Buy1_1 = iOpen(NULL,PERIOD_H4,Current 0);
    double Buy1_2 = iCustom(NULL,PERIOD_H4,AndrewFX HiLo,0,Current 0);
    double Sell1_1 = iOpen(NULL,PERIOD_H4,Current 0);
    double Sell1_2 = iCustom(NULL,PERIOD_H4,AndrewFX HiLo,0,Current 0);
    double CloseBuy1_1 = iOpen(NULL,PERIOD_H4,Current 0);
    double CloseBuy1_2 = iCustom(NULL,PERIOD_H4,AndrewFX HiLo,0,Current 0);
    double CloseSell1_1 = iOpen(NULL,PERIOD_H4,Current 0);
    double CloseSell1_2 = iCustom(NULL,PERIOD_H4,AndrewFX HiLo,0,Current 0);

    // ----------------------------------------------- -------------------
    //|變量結束|
    // ----------------------------------------------- -------------------
    //檢查位置
    bool IsTrade = False;
    for(int i = 0; i lt; Total; i ){
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(OrderType()lt; = OP_SELL OrderSymbol()== Symbol()){
    IsTrade = True;
    if(OrderType()== OP_BUY){
    //關
    // ----------------------------------------------- -------------------
    //|信號開始(退出購買)|
    // ----------------------------------------------- -------------------
    if(CloseBuy1_1 lt; CloseBuy1_2)Order = SIGNAL_CLOSEBUY;

    // ----------------------------------------------- -------------------
    //|信號結束(退出購買)|
    // ----------------------------------------------- -------------------
    if(Order == SIGNAL_CLOSEBUY((EachTickMode!TickCheck)||(!EachTickMode(Bars!= BarCount)))){
    OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,MediumSeaGreen);
    if(SignalMail)SendMail([Signal Alert],[ Symbol() ] DoubleToStr(Bid,Digits) Close Buy);
    if(!EachTickMode)BarCount = Bars;
    IsTrade = False;
    繼續;
    }
    //追踪止損
    if(UseTrailingStop TrailingStop gt; 0){
    if(Bid - OrderOpenPrice()gt; Point * TrailingStop){
    if(OrderStopLoss()lt; Bid - Point * TrailingStop){
    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point * TrailingStop,OrderTakeProfit(),0,MediumSeaGreen);
    if(!EachTickMode)BarCount = Bars;
    繼續;
    }
    }
    }
    } else {
    //關
    // ----------------------------------------------- -------------------
    //|信號開始(退出賣出)|
    // ----------------------------------------------- -------------------
    if(CloseSell1_1 gt; CloseSell1_2)Order = SIGNAL_CLOSESELL;

    // ----------------------------------------------- -------------------
    //|信號結束(退出賣出)|
    // ----------------------------------------------- -------------------
    if(Order == SIGNAL_CLOSESELL((EachTickMode!TickCheck)||(!EachTickMode(Bars!= BarCount)))){
    OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,DarkOrange);
    if(SignalMail)SendMail([Signal Alert],[ Symbol() ] DoubleToStr(Ask,Digits) Close Sell);
    if(!EachTickMode)BarCount = Bars;
    IsTrade = False;
    繼續;
    }
    //追踪止損
    if(UseTrailingStop TrailingStop gt; 0){
    if((OrderOpenPrice() - Ask)gt;(Point * TrailingStop)){
    if((OrderStopLoss()gt;(Ask Point * TrailingStop))||(OrderStopLoss()== 0)){
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask Point * TrailingStop,OrderTakeProfit(),0,DarkOrange);
    if(!EachTickMode)BarCount = Bars;
    繼續;
    }
    }
    }
    }
    }
    }
    // ----------------------------------------------- -------------------
    //|信號開始(輸入)|
    // ----------------------------------------------- -------------------
    if(Buy1_1 gt; Buy1_2)訂單= SIGNAL_BUY;
    if(Sell1_1 lt; Sell1_2)Order = SIGNAL_SELL;

    // ----------------------------------------------- -------------------
    //|信號結束|
    // ----------------------------------------------- -------------------
    //購買
    if(Order == SIGNAL_BUY((EachTickMode!TickCheck)||(!EachTickMode(Bars!= BarCount)))){
    if(!IsTrade){
    //檢查可用保證金
    if(AccountFreeMargin()lt;(1000 * Lots)){
    打印(我們沒有錢。免費保證金=,AccountFreeMargin());
    返回(0);
    }
    if(UseStopLoss)StopLossLevel = Ask - StopLoss * Point;否則StopLossLevel = 0.0;
    if(UseTakeProfit)TakeProfitLevel = Ask TakeProfit * Point;否則TakeProfitLevel = 0.0;
    Ticket = OrderSend(符號(),OP_BUY,Lots,Ask,Slippage,StopLossLevel,TakeProfitLevel,Buy(# MagicNumber ),MagicNumber,0,DodgerBlue);
    if(Ticket gt; 0){
    if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)){
    打印(買入訂單已打開:,OrderOpenPrice());
    if(SignalMail)SendMail([Signal Alert],[ Symbol() ] DoubleToStr(Ask,Digits) Open Buy);
    } else {
    打印(錯誤打開購買訂單:,GetLastError());
    }
    }
    if(EachTickMode)TickCheck = True;
    if(!EachTickMode)BarCount = Bars;
    返回(0);
    }
    }
    //賣
    if(Order == SIGNAL_SELL((EachTickMode!TickCheck)||(!EachTickMode(Bars!= BarCount)))){
    if(!IsTrade){
    //檢查可用保證金
    if(AccountFreeMargin()lt;(1000 * Lots)){
    打印(我們沒有錢。免費保證金=,AccountFreeMargin());
    返回(0);
    }
    if(UseStopLoss)StopLossLevel = Bid StopLoss * Point;否則StopLossLevel = 0.0;
    if(UseTakeProfit)TakeProfitLevel = Bid - TakeProfit * Point;否則TakeProfitLevel = 0.0;
    Ticket = OrderSend(符號(),OP_SELL,Lots,Bid,Slippage,StopLossLevel,TakeProfitLevel,Sell(# MagicNumber ),MagicNumber,0,DeepPink);
    if(Ticket gt; 0){
    if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)){
    打印(賣出訂單:,OrderOpenPrice());
    if(SignalMail)SendMail([Signal Alert],[ Symbol() ] DoubleToStr(Bid,Digits) Open Sell);
    } else {
    打印(錯誤打開賣單:,GetLastError());
    }
    }
    if(EachTickMode)TickCheck = True;
    if(!EachTickMode)BarCount = Bars;
    返回(0);
    }
    }
    if(!EachTickMode)BarCount = Bars;
    返回(0);
    }
    // ----------------------------------------------- -------------------
    Quote Originally Posted by ;
    #define SIGNAL_NONE 0
    #define SIGNAL_BUY 1
    #define SIGNAL_SELL 2
    #define SIGNAL_CLOSEBUY 3
    #define SIGNAL_CLOSESELL 4
    extern int MagicNumber = 0;
    extern bool SignalMail = False;
    extern bool EachTickMode = False;
    extern double Lots = 1.0;
    extern int Slippage = 3;
    extern bool UseStopLoss = False;
    extern int StopLoss = 30;
    extern bool UseTakeProfit = False;
    extern int TakeProfit = 60;
    extern bool UseTrailingStop = False;
    extern int TrailingStop = 30;
    int BarCount;
    當前;
    bool TickCheck =假;
    // ----------------------------------------------- -------------------
    //|專家初始化函數|
    // ----------------------------------------------- -------------------
    int init(){
    BarCount =酒吧;
    if(EachTickMode)Current = 0; else Current = 1;
    返回(0);
    }
    // ----------------------------------------------- -------------------
    //|專家取消初始化功能|
    // ----------------------------------------------- -------------------
    int deinit(){
    返回(0);
    }
    // ----------------------------------------------- -------------------
    //|專家啟動功能|
    // ----------------------------------------------- -------------------
    int start(){
    int Order = SIGNAL_NONE;
    int Total,Ticket;
    double StopLossLevel,TakeProfitLevel;

    if(EachTickMode Bars!= BarCount)TickCheck = False;
    Total = OrdersTotal();
    Order = SIGNAL_NONE;
    // ----------------------------------------------- -------------------
    //|變量開始|
    // ----------------------------------------------- -------------------

    double Buy1_1 = iOpen(NULL,PERIOD_H4,Current 0);
    double Buy1_2 = iCustom(NULL,PERIOD_H4,AndrewFX HiLo,0,Current 0);
    double Sell1_1 = iOpen(NULL,PERIOD_H4,Current 0);
    double Sell1_2 = iCustom(NULL,PERIOD_H4,AndrewFX HiLo,0,Current 0);
    double CloseBuy1_1 = iOpen(NULL,PERIOD_H4,Current 0);
    double CloseBuy1_2 = iCustom(NULL,PERIOD_H4,AndrewFX HiLo,0,Current 0);
    double CloseSell1_1 = iOpen(NULL,PERIOD_H4,Current 0);
    double CloseSell1_2 = iCustom(NULL,PERIOD_H4,AndrewFX HiLo,0,Current 0);

    // ----------------------------------------------- -------------------
    //|變量結束|
    // ----------------------------------------------- -------------------
    //檢查位置
    bool IsTrade = False;
    for(int i = 0; i lt; Total; i ){
    OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(OrderType()lt; = OP_SELL OrderSymbol()== Symbol()){
    IsTrade = True;
    if(OrderType()== OP_BUY){
    //關
    // ----------------------------------------------- -------------------
    //|信號開始(退出購買)|
    // ----------------------------------------------- -------------------
    if(CloseBuy1_1 lt; CloseBuy1_2)Order = SIGNAL_CLOSEBUY;

    // ----------------------------------------------- -------------------
    //|信號結束(退出購買)|
    // ----------------------------------------------- -------------------
    if(Order == SIGNAL_CLOSEBUY((EachTickMode!TickCheck)||(!EachTickMode(Bars!= BarCount)))){
    OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,MediumSeaGreen);
    if(SignalMail)SendMail([Signal Alert],[ Symbol() ] DoubleToStr(Bid,Digits) Close Buy);
    if(!EachTickMode)BarCount = Bars;
    IsTrade = False;
    繼續;
    }
    //追踪止損
    if(UseTrailingStop TrailingStop gt; 0){
    if(Bid - OrderOpenPrice()gt; Point * TrailingStop){
    if(OrderStopLoss()lt; Bid - Point * TrailingStop){
    OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point * TrailingStop,OrderTakeProfit(),0,MediumSeaGreen);
    if(!EachTickMode)BarCount = Bars;
    繼續;
    }
    }
    }
    } else {
    //關
    // ----------------------------------------------- -------------------
    //|信號開始(退出賣出)|
    // ----------------------------------------------- -------------------
    if(CloseSell1_1 gt; CloseSell1_2)Order = SIGNAL_CLOSESELL;

    // ----------------------------------------------- -------------------
    //|信號結束(退出賣出)|
    // ----------------------------------------------- -------------------
    if(Order == SIGNAL_CLOSESELL((EachTickMode!TickCheck)||(!EachTickMode(Bars!= BarCount)))){
    OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,DarkOrange);
    if(SignalMail)SendMail([Signal Alert],[ Symbol() ] DoubleToStr(Ask,Digits) Close Sell);
    if(!EachTickMode)BarCount = Bars;
    IsTrade = False;
    繼續;
    }
    //追踪止損
    if(UseTrailingStop TrailingStop gt; 0){
    if((OrderOpenPrice() - Ask)gt;(Point * TrailingStop)){
    if((OrderStopLoss()gt;(Ask Point * TrailingStop))||(OrderStopLoss()== 0)){
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask Point * TrailingStop,OrderTakeProfit(),0,DarkOrange);
    if(!EachTickMode)BarCount = Bars;
    繼續;
    }
    }
    }
    }
    }
    }
    // ----------------------------------------------- -------------------
    //|信號開始(輸入)|
    // ----------------------------------------------- -------------------
    if(Buy1_1 gt; Buy1_2)訂單= SIGNAL_BUY;
    if(Sell1_1 lt; Sell1_2)Order = SIGNAL_SELL;

    // ----------------------------------------------- -------------------
    //|信號結束|
    // ----------------------------------------------- -------------------
    //購買
    if(Order == SIGNAL_BUY((EachTickMode!TickCheck)||(!EachTickMode(Bars!= BarCount)))){
    if(!IsTrade){
    //檢查可用保證金
    if(AccountFreeMargin()lt;(1000 * Lots)){
    打印(我們沒有錢。免費保證金=,AccountFreeMargin());
    返回(0);
    }
    if(UseStopLoss)StopLossLevel = Ask - StopLoss * Point;否則StopLossLevel = 0.0;
    if(UseTakeProfit)TakeProfitLevel = Ask TakeProfit * Point;否則TakeProfitLevel = 0.0;
    Ticket = OrderSend(符號(),OP_BUY,Lots,Ask,Slippage,StopLossLevel,TakeProfitLevel,Buy(# MagicNumber ),MagicNumber,0,DodgerBlue);
    if(Ticket gt; 0){
    if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)){
    打印(買入訂單已打開:,OrderOpenPrice());
    if(SignalMail)SendMail([Signal Alert],[ Symbol() ] DoubleToStr(Ask,Digits) Open Buy);
    } else {
    打印(錯誤打開購買訂單:,GetLastError());
    }
    }
    if(EachTickMode)TickCheck = True;
    if(!EachTickMode)BarCount = Bars;
    返回(0);
    }
    }
    //賣
    if(Order == SIGNAL_SELL((EachTickMode!TickCheck)||(!EachTickMode(Bars!= BarCount)))){
    if(!IsTrade){
    //檢查可用保證金
    if(AccountFreeMargin()lt;(1000 * Lots)){
    打印(我們沒有錢。免費保證金=,AccountFreeMargin());
    返回(0);
    }
    if(UseStopLoss)StopLossLevel = Bid StopLoss * Point;否則StopLossLevel = 0.0;
    if(UseTakeProfit)TakeProfitLevel = Bid - TakeProfit * Point;否則TakeProfitLevel = 0.0;
    Ticket = OrderSend(符號(),OP_SELL,Lots,Bid,Slippage,StopLossLevel,TakeProfitLevel,Sell(# MagicNumber ),MagicNumber,0,DeepPink);
    if(Ticket gt; 0){
    if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)){
    打印(賣出訂單:,OrderOpenPrice());
    if(SignalMail)SendMail([Signal Alert],[ Symbol() ] DoubleToStr(Bid,Digits) Open Sell);
    } else {
    打印(錯誤打開賣單:,GetLastError());
    }
    }
    if(EachTickMode)TickCheck = True;
    if(!EachTickMode)BarCount = Bars;
    返回(0);
    }
    }
    if(!EachTickMode)BarCount = Bars;
    返回(0);
    }
    // ----------------------------------------------- -------------------

  2. #2
    你的代碼太難讀了!嘗試縮進相關部分,並可能將部件放入功能中,這樣您就不必通讀無數行就能弄清楚發生了什麼。從快速瀏覽一下,也許你正在設置IsTrade = True;因此,它不允許另一筆交易'cos已經交易是真實的HTH

  3. #3
    是的,這是EA構建器代碼......我建議你使用另一個模板來構建EA,但是如果你堅持使用它,你必須搞亂IsTrade變量。基本上,將其更改為雙變量,並使每個OrderSend為交易計數添加 1。那麼,在下訂單之前,請確保交易計數小於某個MaxTrades變量。這樣的事情。

  4. #4
    1附件嗨jcalow,我添加了2個新變量。 extern bool UseIsTrade = false; extern int Maxtrades = 5; UseIsTrade:當此設置為false時,它將忽略所有IsTrade變量聲明及其值的任何更改。設置為true時會發生相反的情況。 Maxtrades:這是一個計數器檢查變量。基本上它會計算您的EA開立的未平倉交易數量,無論是買入還是賣出交易。這是通過EA設定的幻數來完成的。它還將檢查交易是否由相應的貨幣對打開。如果未平倉交易的數量大於或等於最大數量。如同交易,它將不允許EA開啟更多新交易。簡而言之,如果我將Maxtrades設置為5,如果我已經有5個未平倉交易,EA將停止開啟新交易。
    https://www.forex-pedia.com/attachme...3665102391.mq4

發布權限

  • 您不可發布新主題
  • 您不可回复
  • 您不可發布附件
  • 您不可編輯您的帖子
  •  
  • BB代碼是打開的
  • 表情符號是打開的
  • [IMG]代碼是打開的
  • [視頻]代碼是打開的
  • HTML代碼是關閉的