MQL5语言键盘快捷键交易代码实现

一、输入变量与全局变量

//--- input parameters
input double   InpLots        =  0.1;     /* Lots                       */ // Requested volume
input int      InpMagic       =  1024;    /* Magic number               */ // EA ID
input int      InpDistance    =  300;     /* Orders placement distance  */ // Distance for setting pending orders
input uint     InpDeviation   =  5;       /* Price deviation            */ // Allowed deviation from the price in a request
input string   InpKeyBuy      =  "B";     /* Key to open Buy            */ // Key to open a Buy position (with Shift - Stop, with Ctrl+Shift - Limit)
input string   InpKeySell     =  "S";     /* Key to open Sell           */ // Key to open Sell (with Shift - Stop, with Ctrl+Shift - Limit)
input string   InpKeyClose    =  "X";     /* Key to close/delete        */ // Key for closing a position (without control keys) or deleting an order (with Shift or Shift+Ctrl)
                                                                           //--- Global variables
ushort         key_buy;                   // Key to send a buy order
ushort         key_sell;                  // Key to send a sell order
ushort         key_close;                 // Key to close or delete

二、初始化函数设置

int OnInit()
{
    //--- Convert the text assigned to Buy to uppercase and get the code of the first character
    string tmp=InpKeyBuy;
    tmp.Upper();
    key_buy=StringGetCharacter(tmp,0);
    //--- Convert the text assigned to Sell to uppercase and get the code of the first character
    tmp=InpKeySell;
    tmp.Upper();
    key_sell=StringGetCharacter(tmp,0);
    //--- Convert the text assigned to Close to uppercase and get the code of the first character
    tmp=InpKeyClose;
    tmp.Upper();
    key_close=StringGetCharacter(tmp,0);

    //--- If the keys assigned to Buy and Sell match, report this and exit with an error
    if(key_sell==key_buy)
    {
        PrintFormat("The key assigned to Sell ('%c') is the same as the key assigned to Buy ('%c')",key_sell,key_buy);
        return INIT_PARAMETERS_INCORRECT;
    }
    //--- If the keys assigned to Close and Buy match, report this and exit with an error
    if(key_close==key_buy)
    {
        PrintFormat("The key assigned to Close ('%c') is the same as the key assigned to Buy ('%c')",key_close,key_buy);
        return INIT_PARAMETERS_INCORRECT;
    }
    //--- If the keys assigned to Close and Sell match, report this and exit with an error
    if(key_close==key_sell)
    {
        PrintFormat("The key assigned to Close ('%c') is the same as the key assigned to Sell ('%c')",key_close,key_sell);
        return INIT_PARAMETERS_INCORRECT;
    }
    //--- Successful initialization. Display the assigned keys in the journal and return successful execution
    string kb="Key assigned to Buy: ";
    string ks="Key assigned to Sell: ";
    string kc="Key assigned to Close: ";
    PrintFormat("%-23s%c (key code %lu)\n%-23s%c (key code %lu)\n%-23s%c (key code %lu)",kb,key_buy,key_buy,ks,key_sell,key_sell,kc,key_close,key_close);
    return(INIT_SUCCEEDED);
}

三、事件函数设置

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    //---

}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    //---

}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
        const long &lparam,
        const double &dparam,
        const string &sparam)
{
    //--- If a key is pressed
    if(id==CHARTEVENT_KEYDOWN)
    {
        //--- If neither Ctrl nor Shift are held
        if(!IsCtrlKeyPressed() && !IsShiftKeyPressed())
        {
            //--- If the button is assigned to the Buy position, open Buy
            if(lparam==key_buy)
                OpenBuy(NULL,InpLots,InpMagic,InpDeviation,"TestMqlTradeTransaction");
            //--- If the button is assigned to the Sell position, open Sell
            if(lparam==key_sell)
                OpenSell(NULL,InpLots,InpMagic,InpDeviation,"TestMqlTradeTransaction");
            //--- If the button is assigned to closing positions, close all positions
            if(lparam==key_close)
                ClosePositionsAll(Symbol());
        }
        //--- If only Shift is held
        if(IsShiftKeyPressed() && !IsCtrlKeyPressed())
        {
            //--- If the button is assigned to Buy order, open Buy Stop order
            if(lparam==key_buy)
                SetBuyStop(NULL,InpLots,InpMagic,InpDistance,"TestMqlTradeTransaction");
            //--- If the button is assigned to Sell order, open Sell Stop order
            if(lparam==key_sell)
                SetSellSellStop(NULL,InpLots,InpMagic,InpDistance,"TestMqlTradeTransaction");
            //--- If the button is assigned to delete orders, delete all orders
            if(lparam==key_close)
                DeleteOrdersAll(NULL);
        }
        //--- If Shift is held together with Ctrl
        if(IsShiftKeyPressed() && IsCtrlKeyPressed())
        {
            //--- If the button is assigned to Buy order, open Buy Limit order
            if(lparam==key_buy)
                SetBuyLimit(NULL,InpLots,InpMagic,InpDistance,"TestMqlTradeTransaction");
            //--- If the button is assigned to Sell order, open Sell Limit order
            if(lparam==key_sell)
                SetSellLimit(NULL,InpLots,InpMagic,InpDistance,"TestMqlTradeTransaction");
            //--- If the button is assigned to delete orders, delete all orders
            if(lparam==key_close)
                DeleteOrdersAll(Symbol());
        }
    }
}
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans,
        const MqlTradeRequest& request,
        const MqlTradeResult& result)
{
    //--- Set all incoming trade transactions to the journal
    TradeTransactionInformer(trans,request,result,18,2,true);
}
//+------------------------------------------------------------------+
//| Returns the state of the Ctrl key                                |
//+------------------------------------------------------------------+
bool IsCtrlKeyPressed(void) 
{ 
    return(::TerminalInfoInteger(TERMINAL_KEYSTATE_CONTROL)<0);
}
//+------------------------------------------------------------------+
//| Returns the state of the Shift key                               |
//+------------------------------------------------------------------+
bool IsShiftKeyPressed(void) 
{ 
    return(::TerminalInfoInteger(TERMINAL_KEYSTATE_SHIFT)<0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迈达量化

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值