![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
A "robot" is a creature into which its creator puts whatever he (the creator) wants and is good at. If you think that everyone else on this forum knows what your "robot" is stuffed with, it's not quite true.
#include <Trade/Trade.mqh>
#include <Trade/SymbolInfo.mqh>
#include <Trade/PositionInfo.mqh>
CTrade Trade;
CSymbolInfo Sym;
CPositionInfo Pos;
//--- input parameters
//input double Lots = 0.1; /*Lots*/ //position volume
input int Shift = 10; /*Shift*/ //the magnitude of jump in price to open the position
input int Limit=100; /*Limit*/ //Loss size in points to close position
input int Prof=10;
input int MA_Period=50; // Moving Average period
int maHandle; // indicator handle of Moving Average
double maVal[3]; // static array for storing values of Moving Average indicator
bool first;
double a,b,Lots;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
maHandle=iMA(NULL,0,MA_Period,2,MODE_SMA,PRICE_CLOSE);
if(maHandle<0)
{
Alert("Error in creating indicators - error number: ",GetLastError(),"!");
return(-1);
}
first=false;
if(!Sym.Name(_Symbol))
{
Alert("CSymbolInfo initialization error, try again");
return(-1);
}
Print("Initialization of the Expert Advisor failed");
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(maHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(CopyBuffer(maHandle,0,0,3,maVal)<0)
{
Alert("Indicator buffer copy error - error number:",GetLastError());
return;
}
if(!Sym.RefreshRates())
{
return;
}
if(first)
{
a=Sym.Ask();
b=Sym.Bid();
first=false;
return;
}
Lots=AccountInfoDouble(ACCOUNT_FREEMARGIN)*0.66/1000/SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
Lots=MathMin(999,MathMax(1,Lots));
Lots=NormalizeDouble(Lots,2);
if(Pos.Select(_Symbol))
{
if(Pos.Profit()>2)
{
Trade.PositionClose(_Symbol,Sym.Spread()*2);
}
else
{
if(Pos.PositionType()==POSITION_TYPE_BUY)
{
if((Pos.PriceOpen()-Sym.Ask())/Sym.Point()>Limit)// || Sym.Bid()<maVal[0]-0.0005)
{
Trade.PositionClose(_Symbol,Sym.Spread()*2);
}
}
else if(Pos.PositionType()==POSITION_TYPE_SELL)
{
if((Sym.Bid()-Pos.PriceOpen())/Sym.Point()>Limit)// || Sym.Ask()>maVal[0]+0.0005)
{
Trade.PositionClose(_Symbol,Sym.Spread()*2);
}
}
}
}
else
{
if(b-Sym.Bid()>=Shift*Sym.Point() && maVal[0]>maVal[1] && maVal[1]>maVal[2] && Sym.Bid()<maVal[0])
{
Trade.SetDeviationInPoints(Sym.Spread()*2);
Trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,Lots,Sym.Bid(),0,0,"); //Sym.Bid()+0.0030
// Trade.Sell(Lots,_Symbol,0,0,0,");
Print(__FUNCTION__,"():Sell");
}
if(Sym.Ask()-a>=Shift*Sym.Point() && maVal[0]<maVal[1] && maVal[1]<maVal[2] && Sym.Ask()>maVal[0])
{
Trade.SetDeviationInPoints(Sym.Spread()*2);
Trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,Lots,Sym.Ask(),0,0,"); //Sym.Ask()-0.0030
// Trade.Buy(Lots,_Symbol,0,0,0,");
Print(__FUNCTION__,"():Buy");
}
}
a=Sym.Ask();
b=Sym.Bid();
}
//+------------------------------------------------------------------+
forward666:
If you want to insert a small piece of code, click on SRC as shown in the picture and insert the code. If there is as much code as you insert, attach the file with the code.
forward666:
If you want to insert a small piece of code, click on SRC as in the picture and insert the code. If there is as much code as you insert, attach the file with the code.
Got it!
I can't find anything with the code, I'll try to look it up on my computer in the evening. And what is the "Styler" - look at the link.
Thanks! If you can give me any tips, that would be great!
I'll read more about the styler.