Subject and Questions: Trade Operations by means of Key Events
To illustrate the above I post code that I am familiar with and which should not be hard to follow
MQL5 Trade Operation via Key Events
#include <Trade\Trade.mqh>
//--- object of class CTrade
CTrade trade;
#include <Trade\PositionInfo.mqh>
CPositionInfo m_position;
#include <Trade\SymbolInfo.mqh>
CSymbolInfo m_symbol;
#include <Trade\OrderInfo.mqh>
COrderInfo m_order;
......
......
#define KEY_NL_UP 38 // NL Up Arrow// BUY AT ASK
#define KEY_UP 104 // Up Arrow // BUY AT ASK
......
......
// BUY at Ask (market) /////////////////////
if(lparam == KEY_UP || lparam == KEY_NL_UP)
{
cl=0;
trade.Buy(LotSizex, NULL, SymbolInfoDouble(Symbol(),SYMBOL_ASK), SymbolInfoDouble(Symbol(),
SYMBOL_ASK) - StopLoss*_Point, 0, NULL);
PlaySound(SND);
}
So it should be fairly clear that if the Up Key or NumLock Up Key is pressed a Buy position will be opened at preset LotSizex.
The way hotkeys work in MT5/4 is that the hotkey will only be alive or able to fire if the chart has focus/is the active chart. If the chart is not the active chart/does not have focus the hotkey is non active. Further, the hotkey is local only to the active chart and will not trigger any event in any other MT5 terminal using the exact same hotkey allocations nor will the same hotkey external to the chart trigger a key event on the chart..
cTrader/cAlgo does not incorporate Key Events at present so possible recourse to achieve objective is Manage References >> .NET Framework >> System.Windows.Forms.
using System.Windows.Forms;
However, I am at loss how to create a Key Event for the Event Handler in cAlgo (if that description is correct).
Below is the code so far - can anyone point to a direction of the next step that I can research. Thanks for any assistance.
The test code is deliberately basic - B Key down to open a Buy position, S key down to open a Sell position and Z Key down to close all positions .
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Windows.Forms;
namespace Key_Test
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class KeyTest : Robot
{
[Parameter("SL", Group = "Parameters", DefaultValue = 0)]
public int defaultSL { get; set; }
[Parameter("TP", Group = "Parameters", DefaultValue = 0)]
public int defaultTP { get; set; }
[Parameter("Lots", Group = "Parameters", DefaultValue = 0.01)]
public double defaultLots { get; set; }
private string label = "KeyTest";
//public event System.Windows.Forms.KeyPressEventHandler KeyPress;
public event System.Windows.Forms.KeyEventHandler KeyDown;
//protected virtual void OnKeyDown(System.Windows.Forms.KeyEventArgs e);
protected override void OnStart()
{
// Event for KBE Event Handle - ??? Confusion ....
}
private void KBE(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Z)
{
allClose();
}
else if (e.KeyCode == Keys.B)
{
entry(TradeType.Buy);
}
else if (e.KeyCode == Keys.S)
{
entry(TradeType.Sell);
}
}
private void entry(TradeType type)
{
var order = ExecuteMarketOrder(type, SymbolName, defaultLots * 100000, label, defaultSL, defaultTP);
if (!order.IsSuccessful)
{
Print("Error:{0}", order.Error.ToString());
}
}
private void allClose()
{
var positions = Positions.FindAll(label);
foreach (Position p in positions)
{
ClosePosition(p);
}
}
}
}