I've found an acceptable answer for Objective-C here:
Play interstitial ad every 3rd game
But, I'm trying to do the same thing in Swift. I'm looking how to write the Static variable.
Here's the Objective-C code:
static int count = 0;
-(void) GameOver {
if(count != 0 && count % 3 == 0)
[HZInterstitialAd show];
count++;
}
private var count = 0
func GameOver {
if (count != 0 && count % 3 == 0) {
HZInterstitialAd.show()
}
count++
}
Related
Is this the correct implementation of OrderClose()?
for(int ii = 0; ii < OrdersTotal(); ii++)
{
if(OrderSelect(ii, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType() == OP_BUY && OrderMagicNumber() == MagicStart && OrderSymbol() == symb)
{
int tikt = OrderTicket();
if(!OrderClose(tikt,OrderLots(),bid,4,clrPurple))
{
Print("Close Error", GetLastError());
}
}
if(OrderType() == OP_SELL && OrderMagicNumber() == MagicStart && OrderSymbol() == symb)
{
int tikt = OrderTicket();
if(!OrderClose(tikt,OrderLots(),ask,4,clrPurple))
{
Print("Close Error", GetLastError());
}
}
}
}
I am using this code right before opening a trade. For example, if there is a signal to buy then it closes the sell first and then opens a buy, and if there is a sell signal then it closes a sell first and then opens a buy.
But it only does this for the first time and wont work after that. Let's say there is a sell signal. Then it will close the buy and open the sell, but when there is a second signal for a buy then it won't close the sell neither will it open a buy.
There is no error in the experts tab. The only thing I receive in the experts tab is a message like this: Positions order mismatch. It does not appear like an error or a warning, it just appears as a message.
If you want to close all orders, you need to start iteration from the latest order position to the first position.
Try this:
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderType() == OP_BUY)
{
if(!OrderClose(OrderTicket(), OrderLots(), Bid, 0))
{
Print("Error closing order: ", GetLastError());
}
}
if(OrderType() == OP_SELL)
{
if(!OrderClose(OrderTicket(), OrderLots(), Ask, 0))
{
Print("Error closing order: ", GetLastError());
}
}
}
}
You shouldn't start iterating from 0 when you are closing orders, because OrdersTotal() function decrement its value while you loop through all orders and closing them one by one.
For example (in your loop) you have 5 orders:
i = 0, OrdersTotal() = 5 -> close order on position 0.
i = 1, OrdersTotal() = 4 -> close order on position 1.
i = 2, OrdersTotal() = 3 -> close order on position 2.
i = 3, and now loop is over, because i == OrdersTotal().
Additionally, instead of int tikt= OrderTicket();, just use OrderTicket() in OrderClose() function.
Try the following code which is more robust and does not assume the orders to be closed are of the same symbol of the chart the EA is running on. This will close all orders on the account. If you want to restrict this to orders that are on the chart the EA is running on, or orders only opened by the EA, you should use OrderSymbol() and OrderMagicNumber() to filter the orders before closing them.
if(OrdersTotal()>0)
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
double price;
if(OrderType()==OP_BUY) price=MarketInfo(OrderSymbol(),MODE_BID); else price=MarketInfo(OrderSymbol(),MODE_ASK);
int ticket=OrderTicket();
bool res=OrderClose(ticket, OrderLots(), price, 50, clrNONE);
if(!res) Print("Error Closing Ticket #", IntegerToString(ticket));
}
else
{
int ticket=OrderTicket();
bool res=OrderDelete(ticket);
if(!res) Print("Error Closing Ticket #", IntegerToString(ticket));
}
}
}
}
Description:
Our app is a music player app.
[AVPlayer addPeriodicTimeObserverForInterval:queue:usingBlock:]
We use this method to record the actual playing time of a user's song. Before it starts playing (and so does the single loop), we set the record time variable (playingSeconds) to 0 and keep +1 throughout the callback.
Normally, the method stops after a song is played.
But what we see in the background is that some users, a three-minute song, actually play for over 30,000 seconds (variable playingSeconds).
Question:
I just want to know what's causing the method to keep calling and not stop.
Or is there any other cause for this phenomenon?
Thanks!
#weakify(self);
self.playbackTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0)
queue:NULL
usingBlock:^(CMTime time) {
#strongify(self);
if (!self.flagSeeking) {
double ct = self.playerItem.currentTime.value / self.playerItem.currentTime.timescale;
if (self.totalDuration == 0) {
self.totalDuration = self.playerItem.duration.value / self.playerItem.duration.timescale;
[self updateNowPlayingInfo:YES];
}
if (ct <= self.totalDuration && self.currentTime != ct && !self.isBuffering) {
self.currentTime = ct;
}
if (self.state == BPAudioPlayerStatePlaying && !self.isBuffering) {
if (ct <= 0) {
self.playingSeconds = 0;
} else {
self.playingSeconds += 1;
}
}
}
I have an indicator which gives one signal [Down Buffer]... I wish to only give signal once per day... Like after the first signal it will not paint any signal for the rest of the day! I have tested with below code it's now not painting at all?
//--- indicator buffers
double Buffer1[];
int day = 0;
int OnInit()
{
........................
}
int OnCalculate(......)
{
//--- main loop
for(int i = limit-1; i >= 0; i--)
{
//Indicator Buffer 1
if( here goes my condition )
{
if( day != Day() )
{
Buffer1[i] = High[i] + iATR(NULL, PERIOD_CURRENT, 14, i); //Set indicator value at Candlestick High + Average True Range
day = Day();
}
}
else
{
Buffer1[i] = EMPTY_VALUE;
}
}
return(rates_total);
}
What is wrong with my code? It's now not showing any signal at all...
Note : I have removed some of the code to make it simple...
Use the following function to check whether it is a new day.
It returns true if it is a new day, else returns false.
bool IsNewDay(){
static datetime prevDay = -1;
if( iTime(_Symbol, PERIOD_D1, 0) == prevDay ) return false;
prevDay = iTime(_Symbol, PERIOD_D1, 0);
return true;
}
I now trying to make Break Even Code trigger more than one time,
example EA entry is 1.28000 and stop loss 1.28500
if current price reach 1.175000(50pips), sl move to break even such as to 1.28000(5pips).
EA will not make more modify order after condition are meet.
so how to trigger break even again if price reach 1.17000(100pips), sl move to (1.175000)(50 pips)
and again price reach 1.165000(150pips),sl move to 1.17000(100pips)
I want to make
BE_B_M(sl move to(example:5))
and
BE_B_T(price reach(example:50))
as variable and every time price reach target variable change to next value
so became
BE_B_M(sl move to(example:50)) and BE_B_T(price reach(example:100))
The entire code is as follows
extern double BE_T_1 = 50;
extern double BE_M_1 = 5;
extern double BE_T_2 = 100;
extern double BE_M_2 = 50;
extern double BE_T_3 = 150;
extern double BE_M_3 = 100;
double BE_S_M;
double BE_S_T;
void MOVE_BE_1()
{
for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()!=M_Number)continue;
if(OrderSymbol()==Symbol())
if(OrderType()==OP_BUY)
if(Bid-OrderOpenPrice()>BE_S_T*Pips)
if(OrderOpenPrice()>OrderStopLoss())
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(BE_S_M*Pips),OrderTakeProfit(),0,CLR_NONE))
Print("eror");
}
for(int s=OrdersTotal()-1;s>=0;s--)
{
if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()!=M_Number)continue;
if(OrderSymbol()==Symbol())
if(OrderType()==OP_SELL)
if(OrderOpenPrice()-Ask>BE_S_T*Pips)
if(OrderOpenPrice()<OrderStopLoss())
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(BE_S_M*Pips),OrderTakeProfit(),0,CLR_NONE))
Print("eror");
}
}
i expect sl will move after price reach every 50pips from entry
Here you can put all the 3 break even levels on one function.
Note: you can use 1 for-loop for both OP_BUY and OP_SELL
Here is my OnInit()
// Global variable
double point;
int OnInit()
{
if(Digits == 5 || Digits == 3) point=Point*10;
else point=Point;
return(INIT_SUCCEEDED);
}
Here is the BreakEven() function
//+------------------------------------------------------------------+
//| Break even the trade at 3 levels |
//+------------------------------------------------------------------+
void BreakEven()
{
// get the stop level for that symbol
double stopLevel = SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL)*Point;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderMagicNumber()!=M_Number)continue;
if(OrderSymbol()!=Symbol())continue;
if(OrderType()==OP_BUY)
{
double profitPips=Bid-OrderOpenPrice();
double newSL=OrderStopLoss();
if(profitPips>=BE_T_1*point && OrderStopLoss()<OrderOpenPrice()) // Break Even
{
newSL=OrderOpenPrice()+BE_M_1*point;
}
else if(profitPips>=BE_T_3*point) // 150/100
{
newSL=OrderOpenPrice()+BE_M_3*point;
}
else if(profitPips>=BE_T_2*point) // 100/50
{
newSL=OrderOpenPrice()+BE_M_2*point;
}
if(newSL>=OrderStopLoss()+Point && newSL<Bid-stopLevel)
if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newSL,Digits),OrderTakeProfit(),0))
Print("Error at BE: ",GetLastError());
}
else if(OrderType()==OP_SELL)
{
double profitPips=OrderOpenPrice()-Ask;
double newSL=OrderStopLoss();
if(profitPips>=BE_T_1*point && (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0)) // Break Even
{
newSL=OrderOpenPrice()-BE_M_1*point;
}
else if(profitPips>=BE_T_3*point) // 150/100
{
newSL=OrderOpenPrice()-BE_M_3*point;
}
else if(profitPips>=BE_T_2*point) // 100/50
{
newSL=OrderOpenPrice()-BE_M_2*point;
}
if(newSL<=OrderStopLoss()-Point && newSL>Ask+stopLevel)
if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newSL,Digits),OrderTakeProfit(),0))
Print("Error at BE: ",GetLastError());
}
}
}
I didn't test this myself in a trade, but it should work.
I recently got into Arduino with a Rex Qualis Arduino Uno R3 and I am trying to build a project that would beat the Simon memory game (or Repeat the Beat).
It waits for user response through one of four buttons then adds that to the list, executes the list, then waits for user input on the next move.
Everything works how it's supposed to, but the weirdest things happen on execution:
On the first loop after full execution, Servo 1 will execute its move function without authorization.
On the second loop after full execution, Servo 2 will execute its move function and so on.
After the fourth loop, execution, and servo 4 executing its move function, it doesn't happen again. I don't know why it cycles through all the servos one by one in the first four loops then is fine after but it kinda breaks my project.
Is there a problem in my code that redirects to the move functions or something? All help is appreciated. Here is the code for reference:
//Simon killer
//Da Cube
#include <Servo.h>
//Declare buttons
int button1 = 4;
int button2 = 5;
int button3 = 6;
int button4 = 7;
//Declare servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int moves[100]; //Memory up to 100
int x = 0;
int y = 1;
void setup() {
pinMode(button1, INPUT_PULLUP); //Button setup
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
servo1.attach(8); //Servo setup
servo2.attach(9);
servo3.attach(10);
servo4.attach(11);
moveServo1();//System check
moveServo2();
moveServo3();
moveServo4();
}
//move functions
void moveServo1() {
servo1.write(5);
delay(500);
servo1.write(45);
delay(500);
}
void moveServo2() {
servo2.write(5);
delay(500);
servo2.write(45);
delay(500);
}
void moveServo3() {
servo3.write(175);
delay(500);
servo3.write(135);
delay(500);
}
void moveServo4() {
servo4.write(5);
delay(500);
servo4.write(45);
delay(500);
}
void loop() {
//Read Input by button
while (x < y) {
if (digitalRead(button1) == LOW) {
moves[x] = 1;
x++;
} else if (digitalRead(button2) == LOW) {
moves[x] = 2;
x++;
} else if (digitalRead(button3) == LOW) {
moves[x] = 3;
x++;
} else if (digitalRead(button4) == LOW) {
moves[x] = 4;
x++;
}
}
y++;
//Decode Memory Array
for (int i = 0; i < (sizeof(moves)); i++) {
switch (moves[i]) {
case 1:
moveServo1();
break;
case 2:
moveServo2();
break;
case 3:
moveServo3();
break;
case 4:
moveServo4();
break;
}
}
}
First i would check to see if the code that makes the Servos move 1-4 isn't the one in the setup loop.
moveServo1();//System check
moveServo2();
moveServo3();
moveServo4();
Here you make a servo sistem check, which means every time you power up the arduino, the first servo will move, then the second and so on and only then the void loop starts...comment these lines and see if that helps