Last active
August 29, 2015 14:03
-
-
Save jonathascosta/76676c0fa4851ff08a1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern int BarCount = 100; | |
extern int MagicNumber = 123456789; | |
extern double InitialVolume = 0.01; | |
extern int SlipPage = 3; | |
extern int MaxOrders = 40; | |
extern double MinimumDistanceForNewOrder = 70; | |
extern bool AutoConfig = false; | |
double lowest, highest; | |
double buyVolume, sellVolume; | |
int buyTicketsIds[], sellTicketsIds[]; | |
int week = 1; | |
datetime startDay, currentDay; | |
int init() | |
{ | |
week = 1; | |
startDay = TimeCurrent(); | |
} | |
int start() | |
{ | |
if (!CheckData()) | |
return (0); | |
AccountOrders(); | |
DefineTradingCriteria(); | |
CloseOrder(); | |
DefineOrderSize(); | |
OpenOrder(); | |
} | |
/// <summary> | |
/// Checks if we have minimum data to run the bot | |
/// </summary> | |
bool CheckData() | |
{ | |
week = 1 + (TimeCurrent() - startDay) / (7 * 24 * 60 * 60); | |
double maxBalance = 50 * week; | |
if (AccountEquity() < maxBalance) | |
maxBalance = AccountEquity(); | |
if (AutoConfig) | |
MaxOrders = MathFloor(maxBalance / MinimumDistanceForNewOrder); | |
if (MaxOrders < 1) | |
MaxOrders = 1; | |
return (Bars > BarCount); | |
} | |
void AccountOrders() | |
{ | |
ArrayResize(buyTicketsIds, 0); | |
ArrayResize(sellTicketsIds, 0); | |
for(int i = 0; i < OrdersTotal(); i++) | |
{ | |
OrderSelect(i, SELECT_BY_POS); | |
if ((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol())) | |
{ | |
if (OrderType() == OP_BUY) | |
{ | |
ArrayResize(buyTicketsIds, ArraySize(buyTicketsIds) + 1); | |
buyTicketsIds[ArraySize(buyTicketsIds) - 1] = OrderTicket(); | |
continue; | |
} | |
if (OrderType() == OP_SELL) | |
{ | |
ArrayResize(sellTicketsIds, ArraySize(sellTicketsIds) + 1); | |
sellTicketsIds[ArraySize(sellTicketsIds) - 1] = OrderTicket(); | |
} | |
} | |
} | |
} | |
void DefineTradingCriteria() | |
{ | |
lowest = Low[iLowest(NULL, 0, MODE_LOW, BarCount, 1)]; | |
highest = High[iHighest(NULL, 0, MODE_HIGH, BarCount, 1)]; | |
} | |
void CloseOrder() | |
{ | |
// double stopLoss; | |
if ((Ask < lowest) && (ArraySize(sellTicketsIds) > 0)) | |
{ | |
for(int i = 0; i < ArraySize(sellTicketsIds); i++) | |
{ | |
OrderSelect(sellTicketsIds[i], SELECT_BY_TICKET); | |
// if ((OrderProfit() + OrderSwap()) / OrderLots() > 52) | |
// { | |
// stopLoss = NormalizeDouble(Ask + 0.052, 3); | |
// if (OrderStopLoss() > stopLoss) | |
// OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask + 0.052, 3), 0, 0, Red); | |
// } | |
if (OrderProfit() + OrderSwap() > 0) | |
OrderClose(OrderTicket(), OrderLots(), Ask, SlipPage, Red); | |
} | |
} | |
if ((Bid > highest) && (ArraySize(buyTicketsIds) > 0)) | |
{ | |
for(int j = 0; j < ArraySize(buyTicketsIds); j++) | |
{ | |
OrderSelect(buyTicketsIds[j], SELECT_BY_TICKET); | |
// if ((OrderProfit() + OrderSwap()) / OrderLots() > 52) | |
// { | |
// stopLoss = NormalizeDouble(Bid - 0.052, 3); | |
// if (OrderStopLoss() < stopLoss) | |
// OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid - 0.052, 3), 0, 0, Blue); | |
// } | |
if (OrderProfit() + OrderSwap() > 0) | |
OrderClose(OrderTicket(), OrderLots(), Bid, SlipPage, Blue); | |
} | |
} | |
} | |
void DefineOrderSize() | |
{ | |
buyVolume = (ArraySize(buyTicketsIds) + 1) * InitialVolume; | |
sellVolume = (ArraySize(sellTicketsIds) + 1) * InitialVolume; | |
} | |
void OpenOrder() | |
{ | |
OpenBuyOrder(); | |
OpenSellOrder(); | |
} | |
void OpenBuyOrder() | |
{ | |
double lowestOpened = highest; | |
if ((Ask < lowest) && (ArraySize(buyTicketsIds) < MaxOrders)) | |
{ | |
for(int i = 0; i < ArraySize(buyTicketsIds); i++) | |
{ | |
OrderSelect(buyTicketsIds[i], SELECT_BY_TICKET); | |
if (OrderOpenPrice() < lowest) | |
return; | |
if (OrderOpenPrice() < lowestOpened) | |
lowestOpened = OrderOpenPrice(); | |
} | |
if (Ask < lowestOpened - NormalizeDouble(Sum(ArraySize(buyTicketsIds)) * MinimumDistanceForNewOrder * 0.001, 3)) | |
OrderSend(Symbol(), OP_BUY, buyVolume, Ask, SlipPage, 0, 0, "", MagicNumber, 0, Blue); | |
} | |
} | |
void OpenSellOrder() | |
{ | |
double highestOpened = lowest; | |
if ((Bid > highest) && (ArraySize(sellTicketsIds) < MaxOrders)) | |
{ | |
for(int i = 0; i < ArraySize(sellTicketsIds); i++) | |
{ | |
OrderSelect(sellTicketsIds[i], SELECT_BY_TICKET); | |
if (OrderOpenPrice() > highest) | |
return; | |
if (OrderOpenPrice() > highestOpened) | |
highestOpened = OrderOpenPrice(); | |
} | |
if (Bid > highestOpened + NormalizeDouble(Sum(ArraySize(sellTicketsIds)) * MinimumDistanceForNewOrder * 0.001, 3)) | |
{ | |
OrderSend(Symbol(), OP_SELL, sellVolume, Bid, SlipPage, 0, 0, "", MagicNumber, 0, Red); | |
} | |
} | |
} | |
int Sum(int number) | |
{ | |
int sum = 0; | |
for(int i = 0; i < number; i++) | |
sum += i; | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment