Programming logic help

 

Ok this one will be a tough one to handle, if you can help me i will be very grateful.

Here is an illustration what i would like to do.There is a 30 SMA and a 10 SMA and we enter on the trades at the crosses. 10 SMA above 30 then buy, if below then sell.

The tricky part is not with this,but with the method.This is a 15 min chart so here a swing would be around 50-60 pips,but I would like to design a scalping system with 2-4 pip TAKEPROFIT.


My problem is that it should only trade once /swing, that means that only 1 trade / swing, so if 10 SMA is below 30 SMA put a sell order with 2-3 pip TP and stoploss whatever,thats not important;

also if 10 SMA above 30 SMA then put a buy with whatever SL fits.Either we lose the trade or if we win it, we don't put another order at the respective swing even if with a 2-3 pip TP we could have room to put another 20 trades,but we put only 1 per crossover!

So simply to put:

  • If we are at BUY and the BUY trade is over dont put another BUY trade wait until a SELL signal appears and then put a SELL
  • If we are at SELL and the SELL trade is over dont put another SELL trade wait until a BUY signal appears and then put a BUY


So how to program it to trade only once / swing starting from wrong my model which trades as many times it can until another cross ?

So please correct this code to do what i just mentioned above, thanks in advance!

STATUS="NOTRADE";

if(SMA10>SMA30){ 

STATUS="BUY";
}


if(SMA10<SMA30){

STATUS="SELL";
}


if(STATUS=="BUY")   ticket=OrderSend(Symbol(),OP_BUY,start_lot,NormalizeDouble(Ask,BROKERDIGITS),MAX_SLIPPAGE,0,0,"",magic,0,Blue);

if(STATUS=="SELL")  ticket=OrderSend(Symbol(),OP_SELL,start_lot,NormalizeDouble(Bid,BROKERDIGITS),MAX_SLIPPAGE,0,0,"",magic,0,Red);
 
int buy_cnt=0;
int sell_cnt=0;


STATUS="NOTRADE";

if((SMA10>SMA30) && (buy_cnt==0)){ 

STATUS="BUY";
buy_cnt++;
sell_cnt=0;
}


if((SMA10<SMA30) && (sell_cnt==0)){

STATUS="SELL";
sell_cnt++;
buy_cnt=0;

}


if(STATUS=="BUY")   ticket=OrderSend(Symbol(),OP_BUY,start_lot,NormalizeDouble(Ask,BROKERDIGITS),MAX_SLIPPAGE,0,0,"",magic,0,Blue);

if(STATUS=="SELL")  ticket=OrderSend(Symbol(),OP_SELL,start_lot,NormalizeDouble(Bid,BROKERDIGITS),MAX_SLIPPAGE,0,0,"",magic,0,Red);


something like this would allow to open buy only after sell and sell only after buy.
 

szgy74,

wouldn't buy_cnt and sell_cnt have to be global scope and declared outside of {start} ?

Otherwise they will be reset to zero at every new tick.

 
GumRai:

szgy74,

wouldn't buy_cnt and sell_cnt have to be global scope and declared outside of {start} ?

Otherwise they will be reset to zero at every new tick.

Ok i put a buy_cnt and sell_cnt in init, and then what

if((SMA10<SMA30) && (sell_cnt==0)){

STATUS="SELL";
sell_cnt++;
buy_cnt=0;

}

if((SMA10>SMA30) && (buy_cnt==0)){ 

STATUS="BUY";
buy_cnt++;
sell_cnt=0;
}

Once this gets to 0 then both will be 0 after 2 swings and it wont take any trades anymore...

It needs something to change the cnt variables back to a value like 1 to be continuous or else it will get both to 0 and then stop taking trades forever, but if i change it to another value it will reset it every tick, i tried that and doesnt work...

 
szgy74:
something like this would allow to open buy only after sell and sell only after buy.
And what happens when the terminal restarts for any reason ?
 
Proximus:

Ok this one will be a tough one to handle, if you can help me i will be very grateful.

Here is an illustration what i would like to do.There is a 30 SMA and a 10 SMA and we enter on the trades at the crosses. 10 SMA above 30 then buy, if below then sell.

The tricky part is not with this,but with the method.This is a 15 min chart so here a swing would be around 50-60 pips,but I would like to design a scalping system with 2-4 pip TAKEPROFIT.

An OP_BUY can only happen after an OP_SELL and an OP_SELL can only happen after an OP_BUY, find the cross, then check what the last opened trade was, be it still open or closed, if it opposite what your cross says to open then go ahead, if it's the same then don't open the trade. Done.
 
RaptorUK:
An OP_BUY can only happen after an OP_SELL and an OP_SELL can only happen after an OP_BUY, find the cross, then check what the last opened trade was, be it still open or closed, if it opposite what your cross says to open then go ahead, if it's the same then don't open the trade. Done.

That wont work because i have multiple trades running at a time.So i open a trade at a cross and open another at another but what if i put a larger takeprofit so that the one at the first cross doesnt close before opening the other cross then if i look between the open trades it will show the latest trade yes but not necessarly the opposite because i have other filters too not only the SMA, this was just an example to keep it simple

So if i look amongst the open trades for example this was my log


0 BUY

1 BUY

2 SELL

after the #3 sell a #1 buy was opened since a cross but the BUY#1 just gets closed and only SELL#2 remains active and normally a SELL should follow,but since the BUY#1 just got closed another BUY will follow

If i look amongst the closed trades then its even worse...

 
Proximus:

That wont work because i have multiple trades running at a time.

But you only have one most recent trade at a time . . . any others are older, and the most recent trade was opened on the most recent swing . . . that is why I said . . .

then check what the last opened trade was

. . . last being the last one, the most recently opened.
 
Proximus:
Ok i put a buy_cnt and sell_cnt in init, and then what

Once this gets to 0 then both will be 0 after 2 swings and it wont take any trades anymore...

It needs something to change the cnt variables back to a value like 1 to be continuous or else it will get both to 0 and then stop taking trades forever, but if i change it to another value it will reset it every tick, i tried that and doesnt work...



If you declare the variable inside another function, then you are just making them local to that function.

Declare them before init()

Either that or you could use Global variables of the client terminal, which will still be there, if as Raptor has pointed out, the terminal shuts down for some reason.

 
GumRai:


If you declare the variable inside another function, then you are just making them local to that function.

Declare them before init()

Either that or you could use Global variables of the client terminal, which will still be there, if as Raptor has pointed out, the terminal shuts down for some reason.

Please tell me the code, speculation like this is for trading not programming :)

I tried 1000 different times and doesnt work


RaptorUK:

But you only have one most recent trade at a time . . . any others are older, and the most recent trade was opened on the most recent swing . . . that is why I said . . .

. . . last being the last one, the most recently opened.

I dont know why but still doesnt work :(


I tried it will 1 order at a time and still takes many trades at a single buy swing... :(

 
Proximus:

Please tell me the code, speculation like this is for trading not programming :)

I tried 1000 different times and doesnt work




What speculation?

if you declare a variable in a function it will only be local to that function.

if you declare it before init() it can be accessed in all functions and will keep its value in between program runs.

Reason: