Kao sto sam i obecao dosao je na red i primer u kojem mozete jasno videti kako iskodirati trejling stop. Deo koda sam preuzeo sa mog projekta "ultimate_hunter",koji vec jedno vreme intezivno razvijam . Sto se tice ovog robota odmah pravi pare, evo pogledajte backtesting graf:
[You must be registered and logged in to see this image.]
Veoma,veoma obecavajuce. U pitanju je sirov kod, koji zahteva jos dosta posla. Ali kao sto sam i napomenuo, kada se EA pokrene na bilo kom valutnom paru i vremenskom opsegu, odmah ste u plusu. Stoga sam odlucio da ga odradim do j..a. Da se vratim na trejling stop.
Necu se bavit objasnjavanjem koda. To cu mozda raditi u drugim temama. Iseckacu kod od pocetka do kraja i dati objasnjenje za svaku stavku sta predstavlja sa cim je povezana ili nije , sta treba da se uveze od biblioteka, kako treba odraditi pocetni koncept , sta ide prvo a sta drugo, trece, i tako redom. Za sada cu vam pokazati kod koji je kompajlovan bez i jedne jedine greske i upozorenja. Slike sa primerima cu postaviti u spojlere jer bi tema bila predugacka. Znaci klik na spojler da bi ste videli snimke ekrana.
- 0 errors i 0 warning :
- [You must be registered and logged in to see this image.]
U primeru ispod videcemo jedan postavljen sell nalog.
- pocetak:
- slika br.2:
Zatim je cena krenula u kontra smeru i udarila u nas stop los. Tako da je ovaj nalog zatvoren sa malim profitom. Da pogledamo
- slika broj 3:
Zelim da napomenem da je EA ful operativan ali mu je logika buzi vuzi. Sto znaci ne pokusavajte da trgujete sa ovim robotom. Takodje nalozi na cekanju su dodati samo radi primera kako se to radi . U stvari ovo je samo jedan od nacina. Mozda i najjednostavniji. Takodje pending orderi nisu povezani sa trejling stopom . Ostao je samo jos jedan korak da se to uradi. Sto se tice buy/sell naloga tu sve sljaka bez greske. Uz malo rada a na osnovu ovo primera za buy/sell naloge mozete vrlo lako povezati i stop/limit ordere sa trejling stopom.
- drugi primeri reagovanja trejling stopa:
Kao sto mozete videti sve sljaka. Na kraju mozete videti ili preuzeti kod koji mozete iskoristiti kao dobru osnovu za neki EA. NPR mozete ubaciti i neki od indikatora kao sto su: Moving average, MACD, RSI. I uradit vasu logiku za otvaranje naloga. U samom kodu ima i nekih nepotrebnih stvari.Mrzelo me da ga cistim.
- t_stop EA mq5:
- Code:
//+------------------------------------------------------------------+
//| t_stop_ea.mq5 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| [You must be registered and logged in to see this link.] |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "drenjanind@mail.ru"
#property version "1.00"
//---
#include<Trade\Trade.mqh>
#include <Expert\ExpertTrailing.mqh>
#include <Expert\ExpertBase.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CSymbolInfo _symbol;
CTrade _trade;
COrderInfo pe_position;
CPositionInfo check_order;
//---
double inp_lots=0.01;
int take_profit= 300;
int stop_los= 300;
int trailing_stop=2;
int trailing_step=1;
ulong my_magic= 32995373;
//---
double ExtTrailingStop=0.0;
double ExtTrailingStep=0.0;
double _point;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
if(! _symbol.Name(Symbol())) // sets symbol name
return(INIT_FAILED);
RefreshRates();
//---
_trade.SetExpertMagicNumber(my_magic);
//--- tuning for 3 or 5 digits
int digits_adjust=1;
if( _symbol.Digits()==3 || _symbol.Digits()==5)
digits_adjust=10;
_point= _symbol.Point()*digits_adjust;
ExtTrailingStop=trailing_stop * _point;
ExtTrailingStep=trailing_step * _point;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
static datetime PrevBars=0;
datetime time_0=iTime(0);
if(time_0==PrevBars)
return;
PrevBars=time_0;
MqlTick last_tick;
//---
if(SymbolInfoTick(Symbol(),last_tick))
{
Print(last_tick.time,": Bid = ",last_tick.bid,
" Ask = ",last_tick.ask," Volume = ",last_tick.volume);
}
else Print("SymbolInfoTick() failed, error = ",GetLastError());
//---
MqlRates rates[];
int copied=CopyRates(NULL,0,0,100,rates);
if(copied<=0)
Print("Error copying price data ",GetLastError());
else Print("Copied ",ArraySize(rates)," bars");
ArraySetAsSeries(rates,true);
//---
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double last= SymbolInfoDouble(_Symbol,SYMBOL_LAST);
//---
bool UpTrend = last_tick.ask && rates[0].high > rates[1].high && rates[1].high < rates[2].high && rates[0].close > rates[1].close && rates[1].close > rates[2].close;
bool DownTrend = last_tick.bid && rates[0].low < rates[1].low && rates[1].low < rates[2].low && rates[0].close < rates[1].close && rates[1].close < rates[2].close;
//---
int count_buys=0;
int count_sells=0;
CalculatePositions(count_buys,count_sells);
int count_buy_stops = 0;
int count_sell_stops = 0;
CalculateAllPendingStopsOrders(count_buy_stops, count_sell_stops);
int count_buy_limit = 0;
int count_sell_limit = 0;
CalculateAllPendingLimitOrders(count_buy_limit,count_sell_limit);
//---
if(count_buys ==0 && count_buy_stops ==0 && count_buy_limit==0 && UpTrend && PositionsTotal()==0)
{
if(!RefreshRates())
{
PrevBars=iTime(1);
return;
}
_trade.Buy(inp_lots,_Symbol,ask,ask-stop_los*_Point,ask+take_profit*_Point,NULL);
_trade.BuyStop(inp_lots,ask + 100 * _Point,_Symbol,ask-stop_los*_Point,ask+take_profit*_Point,ORDER_TIME_DAY,NULL);
_trade.BuyLimit(inp_lots,bid - 200 * _Point,_Symbol,ask-stop_los*_Point,ask+take_profit*_Point,ORDER_TIME_DAY,NULL);
}
else if(count_sells ==0 && count_sell_stops==0 && count_sell_limit==0 && DownTrend && PositionsTotal()==0)
{
if(!RefreshRates())
{
PrevBars=iTime(1);
return;
}
_trade.Sell(inp_lots,_Symbol,bid,bid+stop_los*_Point,bid-take_profit*_Point,NULL);
_trade.SellStop(inp_lots,bid - 100 * _Point,_Symbol,bid + stop_los * _Point, bid - take_profit * _Point,ORDER_TIME_DAY,NULL);
_trade.SellLimit(inp_lots,ask + 200 * _Point,_Symbol,bid + stop_los * _Point, bid - take_profit * _Point,ORDER_TIME_DAY,NULL);
}
else
{
Trail(last_tick.last);
}
}
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data |
//+------------------------------------------------------------------+
bool RefreshRates(void)
{
//--- refresh rates
if(! _symbol.RefreshRates())
{
Print("RefreshRates error");
return(false);
}
//--- protection against the return value of "zero"
if( _symbol.Ask()==0 || _symbol.Bid()==0)
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| Get Time for specified bar index |
//+------------------------------------------------------------------+
datetime iTime(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)
{
if(symbol==NULL)
symbol=Symbol();
if(timeframe==0)
timeframe=Period();
datetime Time[1];
datetime time=0;
int copied=CopyTime(symbol,timeframe,index,1,Time);
if(copied>0) time=Time[0];
return(time);
}
//+------------------------------------------------------------------+
//| Calculate positions Buy and Sell |
//+------------------------------------------------------------------+
void CalculatePositions(int &count_buys,int &count_sells)
{
count_buys=0.0;
count_sells=0.0;
for(int i=PositionsTotal()-1;i>=0;i--)
if(check_order.SelectByIndex(i)) // selects the position by index for further access to its properties
if(check_order.Symbol()== _symbol.Name() && check_order.Magic()==my_magic)
{
if(check_order.PositionType()==POSITION_TYPE_BUY)
count_buys++;
if(check_order.PositionType()==POSITION_TYPE_SELL)
count_sells++;
}
return;
}
//---------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Calculate positions Buy Limit and Sell Limit |
//+------------------------------------------------------------------+
void CalculateAllPendingLimitOrders(int &count_buy_limit,int &count_sell_limit)
{
count_buy_limit=0.0;
count_sell_limit=0.0;
for(int i=OrdersTotal()-1;i>=0;i--)
if(pe_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(pe_position.Symbol()== _symbol.Name() && pe_position.Magic()==my_magic )
{
if(pe_position.OrderType()==ORDER_TYPE_BUY_LIMIT)
count_buy_limit++;
if(pe_position.OrderType()==ORDER_TYPE_SELL_LIMIT)
count_sell_limit++;
}
return;
}
//---------------------------------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Calculate positions Buy Stops and Sell Stops |
//+------------------------------------------------------------------+
void CalculateAllPendingStopsOrders(int &count_buy_stops,int & count_sell_stops)
{
count_buy_stops = 0;
count_sell_stops = 0;
for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
if(pe_position.SelectByIndex(i)) // selects the pending order by index for further access to its properties
if(pe_position.Symbol()== _symbol.Name() && pe_position.Magic()==my_magic)
{
if(pe_position.OrderType()==ORDER_TYPE_BUY_STOP)
count_buy_stops++;
else if(pe_position.OrderType()==ORDER_TYPE_SELL_STOP)
count_sell_stops++;
}
}
//---------------------------------------------------------------------------------------------+
void Trail(double price)
{
if(ExtTrailingStop==0.0)
return;
for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
if(check_order.SelectByIndex(i))
if(check_order.Symbol()== _symbol.Name() && check_order.Magic()==my_magic)
{
if(check_order.PositionType()==POSITION_TYPE_BUY)
{
if(check_order.PriceCurrent()-check_order.PriceOpen()>ExtTrailingStop+ExtTrailingStep)
if(check_order.StopLoss()<check_order.PriceCurrent()-(ExtTrailingStop+ExtTrailingStep))
{
if(!_trade.PositionModify(check_order.Ticket(),
_symbol.NormalizePrice(check_order.PriceCurrent()-ExtTrailingStop),
check_order.TakeProfit()))
Print("Modify ",check_order.Ticket(),
" Position -> false. Result Retcode: ", _trade.ResultRetcode(),
", description of result: ", _trade.ResultRetcodeDescription());
continue;
}
}
else
{
if(check_order.PriceOpen()-check_order.PriceCurrent()>ExtTrailingStop+ExtTrailingStep)
if((check_order.StopLoss()>(check_order.PriceCurrent()+(ExtTrailingStop+ExtTrailingStep))) ||
(check_order.StopLoss()==0))
{
if(!_trade.PositionModify(check_order.Ticket(),
_symbol.NormalizePrice(check_order.PriceCurrent()+ExtTrailingStop),
check_order.TakeProfit()))
Print("Modify ",check_order.Ticket(),
" Position -> false. Result Retcode: ", _trade.ResultRetcode(),
", description of result: ",_trade.ResultRetcodeDescription());
}
}
}
}
//+------------------------------------------------------------------+
U sledecoj temi pokazacu vam primer sa integrisanim brek even i trejling stopom. U principu to mozete uradit i sami jer tema o break even vec postoji. I to bi bilo to za danas. Iskreno se nadam da ce vas ovo zanimati , i da ce vam biti od koristi. Ja kad budem imao sledeci luft , pisem novu temu.
Toliko odoh,