一、输入变量与全局变量
//--- 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);
}