From mql4 to C# cAlgo cBoot ? How do you create a cBoot by converting mql4?
From mql4 to C# cAlgo cBoot ? How do you create a cBoot by converting mql4?
05 Dec 2018, 18:05
Hi.
How do you create a cBoot by converting mql4?
Brain V2 mql4
#include <stdlib.mqh>
//---- input parameters
extern string Expert_Name = "BrainExpert_v2";
extern int Magic = 52000;
extern int Slippage = 3;
extern string Main_data = " Trade Volume & Trade Method";
extern double Lots = 0.01;
extern double TakeProfit = 0; // Take Profit Value
extern int StopLossMode = 2; // Stop Loss Mode
extern int TrailStopMode = 2; // Trailing Stop Mode
extern double BreakEven = 0; // Break-Even Value
extern int SessionStart = 0; // Start Hour of Trade Session
extern int SessionEnd = 24; // End Hour of Trade Session
extern bool SignalMail = false;
extern string Inputs = " BrainTrend parameters ";
extern int TimeFrame = 60; // Large Time Frame in min
extern int NumBars = 500;
extern int ConfirmBars = 3;
extern double Use_pSAR = 0;
extern double pSAR_Step = 0.02;
extern double pSAR_Maximum = 0.2;
extern double Use_Stoch = 0;
extern int Kperiod = 14;
extern int Dperiod = 5;
extern int slowing = 5;
extern double UpLevel = 75;
extern double DnLevel = 25;
extern double Use_iTrend = 0;
extern int FDSize = 30;
extern int FDPrice = PRICE_MEDIAN;
extern double FDTreshold = 1.5;
extern string MM_inputs = " MoneyManagement by L.Williams ";
extern bool MM = false; // ÌÌ Switch
extern double MMRisk = 0.15; // Risk Factor
extern double MaxLoss = 1000; // Maximum Loss by 1 Lot
int OrderBar=0;
double Lotsi;
int BEvent=0, TriesNum=5;
datetime CurrBar,PrevBar;
double pointvalue=1;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
if (Digits == 4 || Digits == 2) pointvalue = Point;
else if (Digits == 5 || Digits == 3) pointvalue = 10.0 * Point;
CurrBar = iTime(Symbol(),TimeFrame,0);
PrevBar = iTime(Symbol(),TimeFrame,1);
//----
return(0);
}
// ---- Scan Trades
int ScanTrades()
{
int total = OrdersTotal();
int numords = 0;bool tck;
for(int cnt=0; cnt<total; cnt++)
{
tck=OrderSelect(cnt, SELECT_BY_POS);
if(OrderSymbol() == Symbol() && OrderType()<=OP_SELLSTOP && OrderMagicNumber() == Magic)
numords++;
}
return(numords);
}
int ConfirmSignal(int mode,int num)
{
int result = 0;
for (int i=num;i>=1;i--)
{
double hiBT1 = iCustom(Symbol(),0,"BrainTrend1",NumBars,1,i);
double loBT1 = iCustom(Symbol(),0,"BrainTrend1",NumBars,0,i);
double hi = High[i];
double lo = Low[i];
double fde1 = iCustom(NULL,0,"Fractal dimesion Ehlers",FDSize,FDPrice,FDTreshold,2,i);
double fde2 = iCustom(NULL,0,"Fractal dimesion Ehlers",FDSize,FDPrice,FDTreshold,2,i+1);
if (mode==1 && result >= 0 && fde1 < fde2 && (hiBT1 != lo && loBT1 != hi)) {result+=1;}
//else {result=0;break;}
if (mode==2 && result <= 0 && fde1 < fde2 && (hiBT1 != hi && loBT1 != lo)) {result-=1;}
//else {result=0;break;}
}
return(result);
}
int TradeSignal()
{
double buyBT1 = iCustom(Symbol(),TimeFrame,"BrainTrend1Sig",NumBars,0,0,1,1);
double buyBT2 = iCustom(Symbol(),TimeFrame,"BrainTrend2Sig",NumBars,0,1);
double sellBT1 = iCustom(Symbol(),TimeFrame,"BrainTrend1Sig",NumBars,0,0,0,1);
double sellBT2 = iCustom(Symbol(),TimeFrame,"BrainTrend2Sig",NumBars,1,1);
if (Use_pSAR > 0)
{
double SAR = iSAR(NULL,TimeFrame,pSAR_Step,pSAR_Maximum,1);
if (SAR < Close[1]) int pSAR = 1;
else
if (SAR > Close[1]) pSAR = -1;
}
if (Use_Stoch > 0)
{
double StoMain = iStochastic(NULL,TimeFrame,Kperiod,Dperiod,slowing,MODE_SMA,0,MODE_MAIN,1);
double StoSig = iStochastic(NULL,TimeFrame,Kperiod,Dperiod,slowing,MODE_SMA,0,MODE_SIGNAL,1);
if (StoMain > StoSig && StoMain < DnLevel && StoSig < DnLevel ) int Stoch = 1;
else
if (StoMain < StoSig && StoMain > UpLevel && StoSig > UpLevel ) Stoch = -1;
}
if (Use_iTrend > 0)
{
int Bands_Mode_0_2 = 0; // =0-2 MODE_MAIN, MODE_LOW, MODE_HIGH
int Power_Price_0_6= 0; // =0-6 PRICE_CLOSE,PRICE_OPEN,PRICE_HIGH,PRICE_LOW,PRICE_MEDIAN,PRICE_TYPICAL,PRICE_WEIGHTED
int Price_Type_0_3 = 0; // =0-3 PRICE_CLOSE,PRICE_OPEN,PRICE_HIGH,PRICE_LOW
int Bands_Period = 20;
int Bands_Deviation= 2;
int Power_Period = 13;
double iTrend1 = iCustom(Symbol(),TimeFrame,"iTrend",Bands_Mode_0_2,Power_Price_0_6,Price_Type_0_3,Bands_Period,Bands_Deviation,Power_Period,NumBars,0,1);
double iTrend2 = iCustom(Symbol(),TimeFrame,"iTrend",Bands_Mode_0_2,Power_Price_0_6,Price_Type_0_3,Bands_Period,Bands_Deviation,Power_Period,NumBars,1,1);
if(iTrend1 > 0 && iTrend2 < 0) int iTrend = 1;
else
if(iTrend2 > 0 && iTrend1 < 0) iTrend = -1;
}
if (TimeHour(CurTime()) >= SessionStart && TimeHour(CurTime()) <= SessionEnd)
{
if (
buyBT1>0 && sellBT1<=0
&&
buyBT2>0 && sellBT2<=0
&&
ConfirmSignal(1,ConfirmBars)==ConfirmBars
&&
(pSAR == 1 || Use_pSAR == 0)
&&
(Stoch == 1 || Use_Stoch == 0)
&&
(iTrend == 1 || Use_iTrend == 0)
) return( 1);
if (
buyBT1<=0 && sellBT1>0
&&
buyBT2<=0 && sellBT2>0
&&
ConfirmSignal(2,ConfirmBars)==-ConfirmBars
&&
(pSAR == -1 || Use_pSAR == 0)
&&
(Stoch == -1 || Use_Stoch == 0)
&&
(iTrend == -1 || Use_iTrend == 0)
) return(-1);
}
return(0);
}
double MoneyManagement ( bool flag, double aLots, double risk, double maxloss)
{
Lotsi=Lots;
if ( flag ) Lotsi=aLots*NormalizeDouble(aLots*AccountFreeMargin()*risk/maxloss,1);
if (Lotsi<0.01) Lotsi=0.01;
return(Lotsi);
}
void TrailStop()
{
int error;
bool result=false;
double Gain = 0;
bool tck;
for (int cnt=0;cnt<OrdersTotal();cnt++)
{
tck=OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if (mode==OP_BUY)
{
if (TrailStopMode == 1 )
double BuyStop = NormalizeDouble(iCustom(Symbol(),TimeFrame,"BrainTrend1StopLine",NumBars,1,1),Digits);
if (TrailStopMode == 2 )
BuyStop = NormalizeDouble(iCustom(Symbol(),TimeFrame,"BrainTrend2StopLine",NumBars,0,0,1),Digits);
if ( BreakEven>0 && BEvent==0 )
{
Gain = (MarketInfo(Symbol(),MODE_BID) - OrderOpenPrice())/pointvalue;
if( Gain >= BreakEven && OrderStopLoss()<=OrderOpenPrice()+1*pointvalue)
{
BuyStop = NormalizeDouble(OrderOpenPrice()+1*pointvalue,Digits);
BEvent=1;
}
}
if ( BuyStop > NormalizeDouble(OrderStopLoss(),Digits))
{
for(int k = 0 ; k < TriesNum; k++)
{
result = OrderModify(OrderTicket(),OrderOpenPrice(),
BuyStop,
OrderTakeProfit(),0,Lime);
error=GetLastError();
if(error==0) break;
else {Sleep(5000); RefreshRates(); continue;}
}
}
}
// - SELL Orders
if (mode==OP_SELL)
{
if (TrailStopMode == 1 )
double SellStop = NormalizeDouble(iCustom(Symbol(),TimeFrame,"BrainTrend1StopLine",NumBars,0,1),Digits);
if (TrailStopMode == 2 )
SellStop = NormalizeDouble(iCustom(Symbol(),TimeFrame,"BrainTrend2StopLine",NumBars,0,1,1),Digits);
if ( BreakEven > 0 && BEvent==0)
{
Gain = (OrderOpenPrice()-MarketInfo(Symbol(),MODE_ASK))/pointvalue;
if( Gain >= BreakEven && OrderStopLoss()>=OrderOpenPrice()-1*pointvalue)
{
SellStop = NormalizeDouble(OrderOpenPrice()-1*pointvalue,Digits);
BEvent=-1;
}
}
if( SellStop < NormalizeDouble(OrderStopLoss(),Digits) && SellStop > 0)
{
for( k = 0 ; k < TriesNum; k++)
{
result = OrderModify(OrderTicket(),OrderOpenPrice(),
SellStop,
OrderTakeProfit(),0,Orange);
error=GetLastError();
if(error==0) break;
else {Sleep(5000); RefreshRates(); continue;}
}
}
}
}
}
}
// ---- Open Sell Orders
void SellOrdOpen()
{
double SellPrice = Bid;
double StopPrice = Bid;
int Mode = OP_SELL;
if (StopLossMode == 1)
double SellStop = iCustom(Symbol(),TimeFrame,"BrainTrend1Sig",NumBars,0,0,0,1);
else
if (StopLossMode == 2)
SellStop = iCustom(Symbol(),TimeFrame,"BrainTrend2Sig",NumBars,1,1);
else SellStop = 0;
if (TakeProfit > 0) double SellProfit = SellPrice - TakeProfit*pointvalue;
else SellProfit=0;
int ticket = OrderSend( Symbol(),Mode,MoneyManagement ( MM, Lots, MMRisk, MaxLoss),
NormalizeDouble(SellPrice , Digits),
Slippage,
NormalizeDouble(SellStop , Digits),
NormalizeDouble(SellProfit, Digits),
Expert_Name+" SELL",Magic,0,Red);
if(ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
{
OrderBar =iBars(OrderSymbol(),TimeFrame);
Print("SELL order opened : ", OrderOpenPrice());
BEvent=0;
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
}
}
else
if(GetLastError()>0)
Print("SELL: OrderSend failed with error #",GetLastError());
}
// ---- Open Buy Orders
void BuyOrdOpen()
{
double BuyPrice = Ask;
double StopPrice = Ask;
int Mode = OP_BUY;
if (StopLossMode == 1)
double BuyStop = iCustom(Symbol(),TimeFrame,"BrainTrend1Sig",NumBars,0,0,1,1);
else
if (StopLossMode == 2)
BuyStop = iCustom(Symbol(),TimeFrame,"BrainTrend2Sig",NumBars,0,1);
else BuyStop = 0;
if (TakeProfit > 0) double BuyProfit= BuyPrice + TakeProfit*pointvalue;
else BuyProfit=0;
int ticket = OrderSend(Symbol(),Mode, MoneyManagement ( MM, Lots, MMRisk, MaxLoss),
NormalizeDouble(BuyPrice , Digits),
Slippage,
NormalizeDouble(BuyStop , Digits),
NormalizeDouble(BuyProfit, Digits),
Expert_Name+" BUY",Magic,0,Blue);
if(ticket > 0)
{
if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
{
OrderBar =iBars(OrderSymbol(),TimeFrame);
Print("BUY order opened : ", OrderOpenPrice());
BEvent=0;
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
}
}
else
if(GetLastError()>0)
Print("BUY : OrderSend failed with error #",GetLastError());
}
void CloseOrder(int mode)
{
bool result=false;
int total=OrdersTotal();
bool tck;
for (int i=0; i<=OrdersTotal(); i++)
{
tck=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == Magic && OrderSymbol() == Symbol())
{
if ((mode == 0 || mode ==1) && OrderType()==OP_BUY ) result=CloseAtMarket(OrderTicket(),OrderLots(),Aqua);
if ((mode == 0 || mode ==2) && OrderType()==OP_SELL) result=CloseAtMarket(OrderTicket(),OrderLots(),Pink);
}
}
}
bool CloseAtMarket(int ticket,double lot,color clr)
{
bool result = false;
int ntr;
int tries=0;
while (!result && tries < TriesNum)
{
ntr=0;
//while (ntr<5 && !IsTradeAllowed()) { ntr++; Sleep(5000); }
RefreshRates();
result=OrderClose(ticket,lot,OrderClosePrice(),Slippage,clr);
tries++;
}
if (!result) Print("Error closing order : ",ErrorDescription(GetLastError()));
return(result);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(iBars(Symbol(),TimeFrame)< 100 || IsTradeAllowed()==false) return(0);
if(AccountFreeMargin()< 1000*MoneyManagement ( MM, Lots, MMRisk, MaxLoss))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
CurrBar = iTime(Symbol(),TimeFrame,0);
//PrevBar = iTime(Symbol(),TimeFrame,1);
if (CurrBar != PrevBar)
{
if(ScanTrades()>0)
{
if (TradeSignal()>0) CloseOrder(2);
if (TradeSignal()<0) CloseOrder(1);
}
if(ScanTrades()==0)
{
if (TradeSignal()>0) BuyOrdOpen() ;
if (TradeSignal()<0) SellOrdOpen();
}
}
else
if(ScanTrades()>0)
{
if(BreakEven>0 || TrailStopMode>0) TrailStop();
}
PrevBar = CurrBar;
//----
return(0);
} //start()
//+------------------------------------------------------------------+
After converted to C# everything
Error : Unable to load assembly: Algo file 'C:\Users\John\Documents\cAlgo\Sources\Robots\..\Indicators\BrainTrend1_Indicator.algo' does not exist.
Robots \ .. \ Indicators does not exist
Indicators are in a different folder
How to fix it ?
How do I add alerts when the H1 H4 indicator signal appears ?! Important!
Greetings.
Replies
PanagiotisCharalampous
06 Dec 2018, 09:18
Hi tgjobscv,
You can consider contacting a Consultant to convert the cBot for you.
Best Regards,
Panagiotis
@PanagiotisCharalampous

tgjobscv
05 Dec 2018, 18:07
'stdlib.mqh' stdlib.mqh 1 1
0 error(s), 0 warning(s), compile time: 145 msec 1 1
stdlib.mqh
This is necessary?
How to transfer Brain Trader EA to C #
@tgjobscv