I want to back test simple EA only BUY with MT4 Simple moving average 20 period
I see a buy signal and up to 3 orders are running
But when the price is below MA20 it does not automatically close the order
Help me to deal with
void OnTick()
{
double sma20 = iMA(Symbol(),PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,0);
if(OrdersTotal()<3)
{
if(Low[1]>sma20)
{
bool ticket = OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0,NULL,0,0,clrAliceBlue);
}
}
for(int i=0; i<= OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS))
{
if(OrderType()==OP_BUY)
{
if(Close[1] < sma20)
{
bool ticket_close = OrderClose(OrderTicket(),OrderLots(),Ask,3,clrAliceBlue);
}
}
}
}
}
To close order with type OP_BUY, you should use Bid price insted of Ask.
Check why order isn't close by GetLastError(), in your case it should looks like:
bool ticket_close = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrAliceBlue);
if(!ticket_close){
Print("Error closing the order id: ", OrderTicket(), ", Error code: ", GetLastError());
}
I have a problem when I'm trying to find a way to check if a trade was made on the current bar or not to stop the EA for making multiple entries on the same bar.
When I don't do a multi Currency EA I usually just use
static datetime lastTradeBar;
and
if(lastTradeBar!=Time[0])
{
if(PFTP_BuySignal > 0 && PFTP_BuySignal_Prev == 0 && PFTP_Rate > PFTP_Rate_Value)
{
myTP = PFTP_TP1;
mySL = PFTP_BuySL;
return (1);
}
if(PFTP_SellSignal > 0 && PFTP_SellSignal_Prev == 0 && PFTP_Rate > PFTP_Rate_Value)
{
myTP = PFTP_TP1;
mySL = PFTP_SellSL;
return (-1);
}
else
return (0);
lastTradeBar=Time[0];
};
return (0);
}
but this doesn't work when using it as I do now.
I'm thinking I need to make a myArray[sym,period,lastTradeBar] or myArray [sym][period][lastTradeBar]
but I can't wrap my head around how or where to put it.
this is the flow
int OnInit() ->
void OnTimer() ->
void LoopThruSym(stringlistOfSym) ->
void LoopThruPeriod(string sym, string listOfPeriods, int listOfSym) ->
void Trade(string sym, int period) ->
int Signal(string sym, int period)
This is how the flow is now.
int OnInit()
{
EventSetTimer(5);
return(INIT_SUCCEEDED);
}
....
void OnTimer()
{
LoopThruSym(symbols);
}
....
void LoopThruSym(string listOfSym)
{
if(Mode == All)
{
int i;
int numSymbolmarketWatch=SymbolsTotal(false);
numSymbols=numSymbolmarketWatch;
ArrayResize(symbolListFinal,numSymbolmarketWatch);
for(i=0; i<numSymbolmarketWatch; i++)
{
symbolListFinal[i]=SymbolName(i,false);
}
}
else
if(Mode == Selected)
{
string sep=",";
ushort u_sep;
int i;
u_sep=StringGetCharacter(sep,0);
StringSplit(listOfSym,u_sep,symbolList);
numSymbols=ArraySize(symbolList);
ArrayResize(symbolListFinal,numSymbols);
for(i=0; i<numSymbols; i++)
{
symbolListFinal[i]=symbolPrefix+symbolList[i]+symbolSuffix;
LoopThruPeriod(symbolListFinal[i],periods, numSymbols);
}
}
else
if(Mode == Current)
{
LoopThruPeriod(Symbol(),periods,numSymbols);
}
return;
}
....
void LoopThruPeriod(string sym, string listOfPeriods, int listOfSym)
{
if(ModePeriod == All_Period)
{
string periodsALL = "1,5,15,30,60,240,1440,10080,43200";
string sep=",";
ushort u_sep;
int i;
int lastTradeBarArrayCount;
u_sep=StringGetCharacter(sep,0);
StringSplit(periodsALL,u_sep,periodList);
numPeriods=ArraySize(periodList);
ArrayResize(periodListFinal,numPeriods);
lastTradeBarArrayCount = listOfSym+numPeriods;
ArrayResize(lastTradeBarArray,lastTradeBarArrayCount);
for(i=0; i<numPeriods; i++)
{
periodListFinal[i]=symbolPrefix+periodList[i]+symbolSuffix;
Trade(sym,StrToInteger(periodListFinal[i]));
Comment("lastTradeBarArrayCount = "+lastTradeBarArrayCount);
}
}
else
if(ModePeriod == Selected_Period)
{
string sep=",";
ushort u_sep;
int i;
int lastTradeBarArrayCount;
u_sep=StringGetCharacter(sep,0);
StringSplit(listOfPeriods,u_sep,periodList);
numPeriods=ArraySize(periodList);
ArrayResize(periodListFinal,numPeriods);
lastTradeBarArrayCount = listOfSym*numPeriods;
ArrayResize(lastTradeBarArray,lastTradeBarArrayCount);
for(i=0; i<numPeriods; i++)
{
periodListFinal[i]=symbolPrefix+periodList[i]+symbolSuffix;
Trade(sym,StrToInteger(periodListFinal[i]));
Comment("lastTradeBarArrayCount = "+lastTradeBarArrayCount);
}
}
if(ModePeriod == Current_Period)
{
Trade(sym,Period());
}
}
...
void Trade(string sym, int period)
{
//Print("Symbole = " + sym + " : " + period);
if(OrderMethod == BuyandSell)
{
if(Signal(sym,period) == 1 && CheckMoneyForTrade(sym,Lots,OP_BUY) && CheckVolumeValue(sym,Lots))
LimitBuy(sym,period);
else
if(Signal(sym,period) == -1 && CheckMoneyForTrade(sym,Lots,OP_SELL) && CheckVolumeValue(sym,Lots))
LimitSell(sym,period);
}
else
if(OrderMethod == BuyOnly)
{
if(Signal(sym,period) == 1 && CheckMoneyForTrade(sym,Lots,OP_BUY) && CheckVolumeValue(sym,Lots))
LimitBuy(sym,period);
}
else
if(OrderMethod == SellOnly)
{
if(Signal(sym,period) == -1 && CheckMoneyForTrade(sym,Lots,OP_SELL) && CheckVolumeValue(sym,Lots))
LimitSell(sym,period);
}
//Trail(sym);
return;
}
...
int Signal(string sym, int period)
{
if(lastTradeBar!=Time[0])
{
if(PFTP_BuySignal > 0 && PFTP_BuySignal_Prev == 0 && PFTP_Rate > PFTP_Rate_Value)
{
myTP = PFTP_TP1;
mySL = PFTP_BuySL;
return (1);
}
if(PFTP_SellSignal > 0 && PFTP_SellSignal_Prev == 0 && PFTP_Rate > PFTP_Rate_Value)
{
myTP = PFTP_TP1;
mySL = PFTP_SellSL;
return (-1);
}
else
return (0);
lastTradeBar=Time[0];
};
return (0);
}
Code a function on the "void OnTick()" like this:
void OnTick()
{
//---
CheckForSignal();
}
And then code the function "CheckForSignal()"
//+------------------------------------------------------------------+
//| Function "CheckForSignal()" |
//+------------------------------------------------------------------+
void CheckForSignal(){
//check here a bar until a Signal given Signal given then initialize it to Time[]
static datetime candletime=0;
if(candletime!=Time[0]){
double upArrow=iCustom(your Custom indicator or whatever parameters);
if(upArrow != EMPTY_VALUE){
EnterTrade(OP_BUY);
}
double downArrow=iCustom(your Custom indicator or whatever parameters);
if(downArrow != EMPTY_VALUE){
EnterTrade(OP_SELL);
}
// if we have a Signal we will initialize candle time to Time[0] to avoid multiple Orders
candletime=Time[0];
}
}
//+------------------------------------------------------------------+
Then Send Signal to Open or Close or whatever you need in my example we will open Trades
//+------------------------------------------------------------------+
//| Function "EnterTrade()" |
//+------------------------------------------------------------------+
void EnterTrade(int type){
int err=0;
double price=0;
double sl=0;
double tp=0;
if(type == OP_BUY){
price=Ask;
}else{
price=Bid;
}
//steppoin8-step15: replace function "OrderSend" parameter
// ->variablename "name" (magic)
//steppoint8-step16: end ";"
int ticket=OrderSend(Symbol(),type,LotSize,price,slippage,0,0,"EA Trade",magic,0,clrMagenta);
if(ticket>0){
if(OrderSelect(ticket,SELECT_BY_TICKET)){
if(OrderType()==OP_BUY){
sl=OrderOpenPrice()-(stopLoss*pips);
tp=OrderOpenPrice()+(takeProfit*pips);
}else if(OrderType()==OP_SELL){
sl= OrderOpenPrice()+(stopLoss*pips);
tp= OrderOpenPrice()-(takeProfit*pips);
}
if(!OrderModify(ticket,price,sl,tp,0,clrMagenta)){
err=GetLastError();
Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err));
}
}else{
Print("Failed to Select Order",ticket);
err=GetLastError();
Print("Encountered an error while selecting order"+(string)ticket+" error number"+(string)err+" "+ErrorDescription(err));
}
}
else{
err=GetLastError();
Print("Encountered an error during order placement"+(string)err+" "+ErrorDescription(err));
}
}
//+------------------------------------------------------------------+
This is not an answer more of like progress.
So what i'm doing not instead of checking for candletime=Time[0] I check then the last trade close time is for that sym/magic nr and comment. and then runing it thru if(iBarShift(sym,period,OrderCloseTime()) > 1)
this kinda works but I'm getting problems down the road if I'm trying to use symbols with different miniLots. But that will come on another post.
bool getLastOrderClose(string sym, int period)
{
if(OrdersHistoryTotal() == 0)
return true;
string comment = "Multi Currency "+sym+":"+IntegerToString(period);
int count = 0;
int tradesPerSymbole =0;
for(int i=OrdersHistoryTotal()-1; i >= 0; i--)
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if(OrderSymbol() == sym)
{
if(OrderMagicNumber() == Magic)
{
tradesPerSymbole++;
if(StringFind(comment,OrderComment())<0)
{
if(iBarShift(sym,period,OrderCloseTime()) > 1)
{
return true;
}
}
}
}
}
else
{
Print(sym +" : "+"OrderSend() - getLastOrderClose - error - ", ErrorDescription(GetLastError()));
}
if(tradesPerSymbole == 0)
return true;
return false;
};
//OrderSend('EURUSD',blablabla Parameter)
//OrderSend('GBPUSD',blablabla Parameter)
//OrderSend('USDJPY',blablabla Parameter)
//OrderSend('EURCHF',blablabla Parameter)
int ticket=OrderSend('EURUSD',type,LotSize,price,slippage,0,0,"EA Trade",magic,0,clrMagenta);
for(int I=ticket;ticket<Orderstotal();i++){
if(OrderSelect(ticket,SELECT_BY_TICKET)){
if(OrderType()==OP_BUY){
sl=OrderOpenPrice()-(stopLoss*pips);
tp=OrderOpenPrice()+(takeProfit*pips);
}else if(OrderType()==OP_SELL){
sl= OrderOpenPrice()+(stopLoss*pips);
tp= OrderOpenPrice()-(takeProfit*pips);
}
if(!OrderModify(ticket,price,sl,tp,0,clrMagenta)){
err=GetLastError();
Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err));
}
}else{
Print("Failed to Select Order",ticket);
err=GetLastError();
Print("Encountered an error while selecting order"+(string)ticket+" error number"+(string)err+" "+ErrorDescription(err));
}
}
else{
err=GetLastError();
Print("Encountered an error during order placement"+(string)err+" "+ErrorDescription(err));
}
}
//+------------------------------------------------------------------+
I think its not the pro solution but I would declare a Ordersend function for all the pairs where u want to open the order (I need not to say that the order send function should be declared in a conditional so only the the real ordersend be placed)
but the part where I want your attention is you can do it the hardware by declaring the Ordersend(not with Symbol() instead of that with "YourPairname");
hope this help you a little bit to reach your goal gl
When closing a (selected) existing order with the api, and in that moment I try to get the closing price, sometimes (Perhaps one out of ten times) returns a price diferent from the one I can see in the history pool.
The code is something like this:
RefreshRates();
if(type == OP_BUY)
{
currentPrice = NormalizeDouble(MarketInfo(symbol, MODE_ASK), vdigits);
}
else if(type == OP_SELL)
{
currentPrice = NormalizeDouble(MarketInfo(symbol, MODE_BID), vdigits);
}
if (meetsRequirementsToClose(currentPrice))
{
desiredPrice = currentPrice;
// And then....
bool retVal = OrderClose(OrderTicket(), numLots, desiredPrice, currSlippage);
if (retVal)
{
this.reportClosePrice (myOrderId, OrderClosePrice(), desiredPrice, numLots, "closing");
return true;
}
}
The order was previously selected using SELECT_BY_POS in the pool MODE_TRADES.
anyone knows how to fix it?
Edited:
We have a broker that sometimes respects the requested price... sometimes not.
Disregarding the fact that we have to change broker for a more reliable one, we cannot rely on the requested price.
The deviation we see is greater than one hundred points, both for better and for worse prices than the real ones.
As sugested by #TheLastStark, the problem is solved selecting the order by its ticket just before closing.
The final code is something like:
RefreshRates();
if(type == OP_BUY)
{
currentPrice = NormalizeDouble(MarketInfo(symbol, MODE_ASK), vdigits);
}
else if(type == OP_SELL)
{
currentPrice = NormalizeDouble(MarketInfo(symbol, MODE_BID), vdigits);
}
if (meetsRequirementsToClose(currentPrice))
{
desiredPrice = currentPrice;
// And then....
int tickNum = OrderTicket();
bool retVal = OrderClose(OrderTicket(), numLots, desiredPrice, currSlippage);
if (retVal)
{
if (OrderSelect(tickNum,SELECT_BY_TICKET,MODE_HISTORY)== false)
{
//this.logger.error ("Error selecting the order");
this.reportClosePrice (myOrderId, -1, desiredPrice, numLots, "closing");
}
else
{
this.reportClosePrice (myOrderId, OrderClosePrice(), desiredPrice, numLots, "closing");
}
return true;
}
}
I want to create "Expert advisor" which scaling in\pyramiding\snowballing into trend,
(Another winning position is openning after first one already in breakeven)
Im stuck with function which checks if previous LONG\SHORT open position is already profitable
Seems like my current function always return 1,
extern double ProfitForOpenAnother = 30;
double IsLastLongProfitable(string sy="", int op=OP_BUY) {
int LastLongProfitable = 0;
datetime o;
double l=-1;
int i, k=OrdersTotal();
if (sy=="0") sy=Symbol();
for (i=0; i<k; i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==sy || sy=="") {
if (OrderType()==OP_BUY) {
if (op<0 || OrderType()==op) {
if (OrderMagicNumber()==Magic) {
if (o<OrderOpenTime()) {
o=OrderOpenTime();
l=OrderProfit();
if(l>ProfitForOpenAnother)
{
LastLongProfitable=1;
}
}
}
}
}
}
}
}
return(LastLongProfitable);
} ```
double profit_buy=0,profit_sell=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
datetime time_order=OrderOpenTime();
double profit_order=OrderProfit()-OrderCommission()+OrderSwap();
if(OrderType()==OP_BUY && time_buy<time_order)
{
time_buy=time_order;
profit_buy=profit_order;
}
if(OrderType()==OP_SELL && time_sell<time_order)
{
time_sell=time_order;
profit_sell=profit_order;
}
}
}```
I am developing cocos2d-x game which have online game mode.
Online game designed and implemented by Photon Cloud SDK(http://www.exitgames.com).
I implemented only ios version but it doesn't work.
The codes that I have implemented are blow.
void NetworkLogic::opJoinRandomRoom()
{
ExitGames::Common::JVector<ExitGames::LoadBalancing::Room> roomList;
roomList = mLoadBalancingClient.getRoomList();
int count = roomList.getSize();
CCLog("Room Count = %d", count);
if(count == 0)
{
this->opCreateRoom();
}else{
mLoadBalancingClient.opJoinRandomRoom();
}
}
void NetworkLogic::update(float dt)
{
this->run();
}
void NetworkLogic::run(void)
{
if(mLastInput == INPUT_EXIT && mStateAccessor.getState() != STATE_DISCONNECTING && mStateAccessor.getState() != STATE_DISCONNECTED)
{
disconnect();
mStateAccessor.setState(STATE_DISCONNECTING);
}
else
{
State state = mStateAccessor.getState();
switch(state)
{
case STATE_INITIALIZED:
connect();
mStateAccessor.setState(STATE_CONNECTING);
break;
case STATE_CONNECTING:
break; // wait for callback
case STATE_CONNECTED:
{
ExitGames::Common::JVector<ExitGames::LoadBalancing::Room> roomList;
roomList = mLoadBalancingClient.getRoomList();
int count = roomList.getSize();
ExitGames::Common::JString tmp;
tmp = count;
EGLOG(ExitGames::Common::DebugLevel::INFO, tmp);
CCLog("Room count in Room = %d", count);
switch(mLastInput)
{
case INPUT_CREATE_GAME: // create Game
opCreateRoom();
break;
case INPUT_JOIN_RANDOM_GAME: // join Game
opJoinRandomRoom();
mStateAccessor.setState(STATE_JOINING);
break;
default: // no or illegal input -> stay waiting for legal input
break;
}
break;
}
case STATE_JOINING:
break; // wait for callback
case STATE_JOINED:
switch(mLastInput)
{
case INPUT_LEAVE_GAME: // leave Game
mLoadBalancingClient.opLeaveRoom();
mStateAccessor.setState(STATE_LEAVING);
break;
default: // no or illegal input -> stay waiting for legal input
break;
}
break;
case STATE_LEAVING:
break; // wait for callback
case STATE_LEFT:
mStateAccessor.setState(STATE_CONNECTED);
break;
case STATE_DISCONNECTING:
break; // wait for callback
default:
break;
}
}
mLastInput = INPUT_NON;
mLoadBalancingClient.service();
}
First I run one app then getRoomList function returns 0 values.
Also after first room created and run second app but it also returns getRoomList function 0.
Please help me.
I have just taken the code that you have provided in your question and copied it into the according place inside the demo of an otherwise unchanged version 3.2.2.0 build of the Photon C++ Client SDK (and removed the two CCLog() lines to make it compile without cocos2d-x) and it worked just fine for me:
The demo prints 0 for the size of the room list until I let one client create a room. Afterwards the other client prints 1.