I am trying to create an expert advisor ( EA ) that would limit the number of open symbol to 3
For example, if I load the EA on 15 different symbols, it would not open a trade on more than 3 at any particular time. Below is the code I inserted in the EA but not working as I want:
int SYMBOL_NUMBER_LIMIT = 3;
string COUNTED_SYMBOLS[];
ArrayResize(COUNTED_SYMBOLS, SYMBOL_NUMBER_LIMIT, 0);
for(int s=0; s<SYMBOL_NUMBER_LIMIT; s++) COUNTED_SYMBOLS[s]="";
int SYMBOLS_IN_TRADE_SO_FAR = 0;
bool NEW_TRADE_PERMISSION = true;
int ALL_POSITIONS = OrdersTotal(); // PositionsTotal();
if(ALL_POSITIONS > 0)
{
for(int index=0; index<ALL_POSITIONS; index++)
{
string THIS_SYMBOL = OrderSymbol(); // PositionGetSymbol(index);
bool Symbol_already_counted = false;
for(int i=0; i<SYMBOL_NUMBER_LIMIT; i++)
{
if(COUNTED_SYMBOLS[i]==THIS_SYMBOL)
{
Symbol_already_counted = true;
break;
}
if(Symbol_already_counted) continue;
else
{
SYMBOLS_IN_TRADE_SO_FAR++;
if(SYMBOLS_IN_TRADE_SO_FAR >= SYMBOL_NUMBER_LIMIT)
{
NEW_TRADE_PERMISSION = false;
break;
}
for(int j=0; j<SYMBOL_NUMBER_LIMIT; j++)
if(COUNTED_SYMBOLS[j]=="")
{
COUNTED_SYMBOLS[j] = THIS_SYMBOL;
break;
}
}
}
}
}
I would appreciate any help to make it work as I described. Thanks in advance
Related
When I run a sample in Vala-ThreadSamples:
Original version:
int question(){
for (var i = 0; i < 3; i++){
print (".");
Thread.usleep (800000);
stdout.flush ();
}
return 42;
}
void main(){
question();
}
Modified version:
int question(){
for (var i = 0; i < 3; i++){
print(".");
Thread.usleep (800000);
}
return 42;
}
void main(){
question();
}
Two version gets the same output:
print a dot and wait, which repeat three times.
Why print will flush every times?
#include <MovingAverages.mqh>
input int fast = 7;
input int slow = 26;
input int signallength =9;
input int closeoropen = 1; //close=0, open=1
input int movingavglength =114;
double ema, macd, signal;
double ind_buffer1[26], ind_buffer2[26];
bool sellsignaldone = false;
bool buysignaldone = false;
void OnTick()
{
ema = iMA(NULL,0,movingavglength,0,MODE_EMA,PRICE_CLOSE,1);
//---- macd counted in the 1-st buffer
for(int i=0; i<slow-1; i++)
ind_buffer1[i]=iMA(NULL,0,fast,0,MODE_EMA,closeoropen,i)-iMA(NULL,0,slow,0,MODE_EMA,closeoropen,i);
//---- signal line counted in the 2-nd buffer
for(int i=0; i<slow-1; i++)
ind_buffer2[i]=iMAOnArray(ind_buffer1,0,signallength,0,MODE_EMA,i);
macd = ind_buffer1[1];
signal = ind_buffer2[1];
if (macd > signal && macd<0 && Close[1]>ema && !buysignaldone && OrdersTotal()==0) {
buysignaldone = true;
OrderSend(NULL, OP_BUY,1/Ask, Ask, 3, Ask-(Ask*0.004), Ask+(Ask*0.008), "Buy Order",0,0,clrGreen);
}
if (macd < signal) {
buysignaldone = false;
}
if (macd < signal && macd>0 && Close[1]<ema && !sellsignaldone && OrdersTotal()==0) {
sellsignaldone = true;
OrderSend(NULL, OP_SELL,1/Bid, Bid, 3, Bid+(Bid*0.004), Bid-(Bid*0.008), "Sell Order",0,0,clrRed);
}
if (macd > signal) {
sellsignaldone = false;
}
}
You can't use iMACD for this. I'm not sure what I've done wrong but the signals are in the wrong place
Crossover is here
I got a much better result using iOsMA, but it's still not identical to the indicator.
#property strict
#include <MovingAverages.mqh>
input int fast = 7;
input int slow = 26;
input int signallength =9;
input int closeoropen = 1; //close=0, open=1
input int movingavglength =114;
double sma1, sma2, ema, macd, fastema, slowema, signal;
double ind_buffer1[26], ind_buffer2[26];
bool sellsignaldone = false;
bool buysignaldone = false;
void OnTick()
{
signal = iOsMA(NULL,0,fast,slow,signallength,closeoropen,1);
fastema = iMA(NULL,0,fast,0,MODE_EMA,closeoropen,1);
slowema = iMA(NULL,0,slow,0,MODE_EMA,closeoropen,1);
macd = fastema-slowema;
ema = iMA(NULL,0,movingavglength,0,MODE_EMA,PRICE_CLOSE,1);
if (signal>0 && macd<0 && Close[1]>ema && !buysignaldone && OrdersTotal()==0) {
buysignaldone = true;
OrderSend(NULL, OP_BUY,1/Ask, Ask, 3, Ask-(Ask*0.004), Ask+(Ask*0.008), "Buy Order",0,0,clrGreen);
}
if (macd < signal) {
buysignaldone = false;
}
if (signal<0 && macd>0 && Close[1]<ema && !sellsignaldone && OrdersTotal()==0) {
sellsignaldone = true;
OrderSend(NULL, OP_SELL,1/Bid, Bid, 3, Bid+(Bid*0.004), Bid-(Bid*0.008), "Sell Order",0,0,clrRed);
}
if (macd > signal) {
sellsignaldone = false;
}
}
You can also modify the MACD indicator in the indicator folder, simply adding on the bottom the "ExponentialMAOnBuffer(...);" and "call" the second indicator in the expert using the iCustom function linked to the number of the index of the indicator. (es. for MACD (0,ExtMacdBuffer);(1,ExtSignalBuffer);)
the ea code now is more clear!
I want to call OnChartEvent() from init() like the code below, so the EA should process first the password and then the rest of the code.
I am just newbie not an expert in coding.
The idea or objective, the password must enter first and correctly, after successful then process the rest of code.
#include <ChartObjects/ChartObjectsTxtControls.mqh>
int init()
{
password_edit.Create(0, "password_edit", 0, 10, 10, 260, 25);
password_edit.BackColor(White);
password_edit.BorderColor(Black);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent( const int id,
const long &lparam,
const double &dparam,
const string &sparam
)
{
//---
if (id == CHARTEVENT_OBJECT_ENDEDIT && sparam == "password_edit" )
{
password_status = -1;
for (int i=0; i<ArraySize(allowed_passwords); i++)
if (password_edit.GetString(OBJPROP_TEXT) == allowed_passwords[i])
{
password_status = i;
break;
}
if (password_status == -1)
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]);
ExpertRemove();
}
else
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[1]);
}
}
}
//+------------------------------------------------------------------+
int OnInit(){
passwordOperation();
return INIT_SUCCEED;
}
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
if (id == CHARTEVENT_OBJECT_ENDEDIT && sparam == "password_edit" )
{
passwordOperation();
}
}
void passwordOperation()
{
password_status = -1;
for (int i=0; i<ArraySize(allowed_passwords); i++)
if (password_edit.GetString(OBJPROP_TEXT) == allowed_passwords[i])
{
password_status = i;
break;
}
if (password_status == -1)
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[0]);
ExpertRemove();
}
else
{
password_edit.SetString(OBJPROP_TEXT, 0, password_message[1]);
}
}
My Martingale EA should be stopped automatically if the previously manually set limit has been reached or exceeded. The last trade should expire normally and not be interrupted.
So far, I've tried the following code, without interruption, etc. the code works fine.
I also heard about RemoveExpert, but I do not know how to do it. Am grateful for any help :)
enum tradeD{Buy=1, Sell=2};
input tradeD TradeType = Buy;
input double Initial_Lot = 0.01;
input double Lot_Factor = 2;
input int Max_Spread = 30;
input double Pct_SL = 0.1; // Percentage Stop Loss
input double Pct_TP = 0.1; // Percentage Take Profit
input int Magic_Number= 73726;
input int interrupt = 1,0000;
input bool StopEA = false;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
int start()
{
if (StopEA == true) { return(0); } //This line will skip the execution of EA.
}
void OnTick()
{
if(OpenOrders()==0 && (SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)<Max_Spread||Max_Spread==0)){
if(TradeType==Buy){
double flot=Initial_Lot;
double etp=Ask+(Ask*(Pct_TP/100));
etp=NormalizeDouble(etp,Digits);
double esl=Ask-(Ask*(Pct_TP/100));
esl=NormalizeDouble(esl,Digits);
if (ask <= interrupt) { StopEA = true; return(0); }
if(lastProfit()<0){
flot=NormalizeDouble(lastLot()*Lot_Factor,2);
}
int snd=OrderSend(Symbol(),OP_BUY,flot,Ask,10,esl,etp,NULL,Magic_Number,0,clrAliceBlue);
}
if(TradeType==Sell){
double flot=Initial_Lot;
double etp=Bid-(Bid*(Pct_TP/100));
etp=NormalizeDouble(etp,Digits);
double esl=Bid+(Bid*(Pct_TP/100));
esl=NormalizeDouble(esl,Digits);
if (bid >= interrupt) { StopEA = true; return(0); }
if(lastProfit()<0){
flot=NormalizeDouble(lastLot()*Lot_Factor,2);
}
int snd=OrderSend(Symbol(),OP_SELL,flot,Bid,10,esl,etp,NULL,Magic_Number,0,clrAliceBlue);
}
}
//checkProfit();
}
int checkProfit(){
int total=0;
for(int i=OrdersTotal()+5; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
string ordsymB = OrderSymbol();
int ordtypeB =OrderType();
int magicnB = OrderMagicNumber();
int magicn=magicnB;
double ordProfit=OrderProfit();
int tick=OrderTicket();
double oLots=OrderLots();
if(ordsymB==Symbol() && magicnB==Magic_Number){
double pctBalT=AccountBalance()*(Pct_TP/100);
double pctBalS=AccountBalance()*(Pct_SL/100);
if(ordProfit>=pctBalT){
int clo=OrderClose(tick,oLots,Close[0],10,clrAntiqueWhite);
}
if(ordProfit<0 && MathAbs(ordProfit)>=pctBalS){
int clo=OrderClose(tick,oLots,Close[0],10,clrAntiqueWhite);
}
}
}
}
return (total);
}
int OpenOrders(){
int total=0;
for(int i=OrdersTotal()+5; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
string ordsymB = OrderSymbol();
int ordtypeB =OrderType();
int magicnB = OrderMagicNumber();
int magicn=magicnB;
if(ordsymB==Symbol() && magicnB==Magic_Number){
total++;
}
}
}
return (total);
}
double lastProfit(){
double total=0;
datetime lastTime=0;
for(int i=OrdersHistoryTotal(); i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
string ordSym=OrderSymbol();
int ordType=OrderType();
datetime ordTime=OrderOpenTime();
double ordLot=OrderLots();
double ordOp=OrderOpenPrice();
int ordMag=OrderMagicNumber();
double ordProfit=OrderProfit();
if(ordSym==Symbol() && ordTime>lastTime && ordMag==Magic_Number){
lastTime=ordTime;
total=ordProfit;
}
}
}
return(total);
}
double lastLot(){
double total=0;
datetime lastTime=0;
for(int i=OrdersHistoryTotal(); i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
string ordSym=OrderSymbol();
int ordType=OrderType();
datetime ordTime=OrderOpenTime();
double ordLot=OrderLots();
double ordOp=OrderOpenPrice();
int ordMag=OrderMagicNumber();
if(ordSym==Symbol() && ordTime>lastTime && ordMag==Magic_Number){
lastTime=ordTime;
total=ordLot;
}
}
}
return(total);
}
if(tick_counter>=ticks_to_close)
{
ExpertRemove();
Print(TimeCurrent(),": ",__FUNCTION__," expert advisor will be unloaded");
}
I have trouble with display image from camera. I using VideoCapture and when I try display image in grayscale it's works perfect, but when I try display image in color that I get something like that:
link
Part of my source code:
public void CaptureVideo()
{
VideoCapture videoCapture = new VideoCapture(0);
Mat frame = new Mat();
while (videoCapture.isOpened() && _canWorking)
{
videoCapture.read(frame);
if (!frame.empty())
{
Image img = MatToImage(frame);
videoView.setImage(img);
}
try { Thread.sleep(33); } catch (InterruptedException e) { e.printStackTrace(); }
}
videoCapture.release();
}
private Image MatToImage(Mat original)
{
BufferedImage image = null;
int width = original.size().width(), height = original.size().height(), channels = original.channels();
byte[] sourcePixels = MatToBytes(original, width, height, channels);
if (original.channels() > 1)
{
image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
}
else
{
image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
}
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);
return SwingFXUtils.toFXImage(image, null);
}
private byte[] MatToBytes(Mat mat, int width, int height, int channels)
{
byte[] output = new byte[width * height * channels];
UByteRawIndexer indexer = mat.createIndexer();
int i = 0;
for (int j = 0; j < mat.rows(); j ++)
{
for (int k = 0; k < mat.cols(); k++)
{
output[i] = (byte)indexer.get(j,k);
i++;
}
}
return output;
}
Anyone can tell me what I doing wrong? I'm new in image processing and I don't get why it's not working.
Ok. I resolve this.
Solution:
byte[] output = new byte[_frame.size().width() * _frame.size().height() * _frame.channels()];
UByteRawIndexer indexer = mat.createIndexer();
int index = 0;
for (int i = 0; i < mat.rows(); i ++)
{
for (int j = 0; j < mat.cols(); j++)
{
for (int k = 0; k < mat.channels(); k++)
{
output[index] = (byte)indexer.get(i, j, k);
index++;
}
}
}
return output;