Back

Converting MQL4 code to C# for QuantConnect

Are there volunteers or freelance that are available to convert MQL4 codes to C#?
Update Backtest








using System;
using CodeBase;
using Interfaces;
using System.Drawing;
using System.ComponentModel;

namespace UserCode
{
[Serializable]
public class Moving Average : EABase
{

#region Properties

#endregion

//+------------------------------------------------------------------+
//| expert default constructor. DO NOT REMOVE |
//+------------------------------------------------------------------+
public Moving Average()
{

copyright = "2005-2014, MetaQuotes Software Corp.";
link = "http://www.mql4.com";
description = "Moving Average sample expert advisor";

}

private const double MAGICMA = 20131111
;
input double Lots =0.1;
input double MaximumRisk =0.02;
input double DecreaseFactor=3;
input int MovingPeriod =12;
input int MovingShift =6;

int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;

int i;
for(i=0;i {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() & OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}

if(buys>0) return(buys);
else return(-sells);
}

double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal();
int losses=0;

lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);

if(DecreaseFactor>0)
{
int i;
for(i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error in history!");
break;
}
if(OrderSymbol()!=Symbol() | OrderType()>OP_SELL)
continue;

if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1)
lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}

if(lot<0.1) lot=0.1;
return(lot);
}

void CheckForOpen()
{
double ma;
int res;

if(Volume[0]>1) return;

ma=iMA(null,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);

if(Open[1]>ma & Close[1] {
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
return;
}

if(Open[1]ma)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
return;
}

}

void CheckForClose()
{
double ma;

if(Volume[0]>1) return;

ma=iMA(null,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);

int i;
for(i=0;i {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA | OrderSymbol()!=Symbol()) continue;

if(OrderType()==OP_BUY)
{
if(Open[1]>ma & Close[1] {
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Open[1]ma)
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}

}

void OnTick()
{

if(Bars<100 | IsTradeAllowed()==false)
return;

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();

}

}
}
1

Here in QC there is moving average cross example, just use it as code above is simple actually.
0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed