How to open trade after 3 candles since last trade was closed? - if-statement

I am having trouble with my strategy. It opens and closes trades in the same exact time sometimes. I need the strategy to wait 3 candles before it can close trade, then wait another 3 candles before it opens the next trade.
Appreciate any solutions!
Thanks
I tried using ta.barssince or strategy.opentrades.entry_bar_index but I couldn't get it to work.

Tray with strategy.position_size
Something like that:
if longEntry and strategy.position_size <= 0 and strategy.position_size[1] <= 0 and strategy.position_size[2] <= 0
strategy.entry('Long', strategy.long, comment = 'Long')

Related

Problem with programming a basic hardware

I have an animation shown on LEDs. When the button is pressed, the animation has to stop and then continue after the button is pressed again.
There is a method that processes working with the button:
void checkButton(){
GPIO_PinState state;
state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15);
if (state == GPIO_PIN_RESET) {
while(1){
state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15);
if (state == GPIO_PIN_SET){
break;
}
}
//while (state == GPIO_PIN_RESET) {
//state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15);
//}
}
}
GPIO_PIN_SET is the default button position. GPIO_PIN_RESET is the condition when the button is pressed. The commented section is what I tried instead of the while(1){...} loop. The checkButton() method is called in the main loop from time to time to be run. The program runs on STM32 with an extension module (here the type of an extension module does not matter).
The fact is that this method stops animation just for a moment and does not work as I would like it to. Could you correct anything about this program to make it work properly?
Could you correct anything about this program to make it work
properly?
My guess is that you are trying to add a 'human interaction' aspect to your design. Your current approach relies on a single (button position) sample randomly timed by a) your application and b) a human finger. This timing is simply not reliable, but the correction is possibly not too difficult.
Note 1: A 'simple' mechanical button will 'bounce' during it's activation or release (yes, either way). This means that the value which the software 'sees' (in a few microseconds) is unpredictable for several (tbd) milliseconds(?) near the button push or release.
Note 2: Another way to look at this issue, is that your state value exists two places: in the physical button AND in the variable "GPIO_PinState state;". IMHO, a state value can only reside in one location. Two locations is always a mistake.
The solution, then (if you believe) is to decide to keep one state 'record', and eliminate the other. IMHO, I think you want to keep the button, which seems to be your human input. To be clear, you want to eliminate the variable "GPIO_PinState state;"
This line:
state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15);
samples the switch state one time.
HOWEVER, you already know that this design can not rely on the one read being correct. After all, your user might have just pressed or released the button, and it is simply bouncing at the time of the sample.
Before we get to accumulating samples, you should be aware that the bouncing can last much more than a few microseconds. I've seen some switches bounce up to 10 milliseconds or more. If test equipment is available, I would hook it up and take a look at the characteristics of your button. If not, well, you can try the adjusting the controls of the following sample accumulator.
So, how do we 'accumulate' enough samples to feel confident we can know the state of the switch?
Consider multiple samples, spaced-in-time by short delays (2 controls?). I think you can simply accumulate them. The first count to reach tbr - 5 (or 10 or 100?) samples wins. So spin sample, delay, and increment one of two counters:
stateCount [2] = {0,0}; // state is either set or reset, init both to 0
// vvv-------max samples
for (int i=0; i<100; ++i) // worst case how long does your switch bounce
{
int sample = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15); // capture 1 sample
stateCount[sample] += 1; // increment based on sample
// if 'enough' samples are the same, kick out early
// v ---- how long does your switch bounce
if (stateCount[sample] > 5) break; // 5 or 10 or 100 ms
// to-be-determined --------vvv --- how long does switch bounce
std::this_thread::sleep_for(1ms); // 1, 3, 5 or 11 ms between samples
// C++ provides, but use what is available for your system
// and balanced with the needs of your app
}
FYI - The above scheme has 3 adjustments to handle different switch-bounce durations ... You have some experimenting to do. I would start with max samples at 20. I have no recommendation for sleep_for ... you provided no other info about your system.
Good luck.
It has been a long time, but I think I remember the push-buttons on a telecom infrastructure equipment bounced 5 to 15 ms.

Determinate progress indicator with AppleScript-Objc

I would like to know how I can create a due hereunder progress indicator that shows me real information of the file that I am downloading or is download speed and how much time is remaining to complete, I tried an example that I saw here and it works but Does not seem to provide me with actual data. I wanted something like the one in the picture attached, could you help me?
Thanks in advance!!
set c to 0
repeat 100 times
set c to c + 1
delay 0.04
tell ProgressIndicator to setDoubleValue_(c)
if c > 99 then
exit repeat
end if
end repeat

Python time.sleep only for one part of script

I have sript for 3 lcd display on my rassbery pi, and have problem with
time sleep function.
I would like to have a pause only for screen 3 ( after 10s show different data ), and the other LCD should run normaly, without time sleep - pause
example :
# LCD1
mylcd1.lcd_display_string("TEMP1:",1,0)
mylcd1.lcd_display_string(str(temp1),1,5)
mylcd1.lcd_display_string ("%s" %time.strftime("%H:%M:%S"),3, 0)
# LCD2
mylcd2.lcd_display_string("TEMP2:",1,0)
mylcd2.lcd_display_string(str(temp2),1,5)
# LCD3
mylcd3.lcd_clear()
mylcd3.lcd_display_string("TEMP3:",1,0)
mylcd3.lcd_display_string(str(temp3),1,5)
time.sleep(10)
mylcd3.lcd_clear()
mylcd3.lcd_display_string("DIM:",1,0)
mylcd3.lcd_display_string(str(dp1),1,4)
In this example is problem time in LCD1, It does not run smoothly, it has to wait 10s, and temperature data on LCD 1 and LCD 2 it must be refreshed in real time without delay....
Thank you for help!
I would say an easy way to go about this is have your LCD's that need to be updated running in a loop, and have a variable that is keeping track of the time.
Then within your loop have an if statement checking if the time % 10 seconds == 0 then run the refresh of the Screen that needs the delay.
I forgot to write .... the compete code is already in loop...
I want add time.sleep only for part for LCD3 ...
LCD3 change data every 10 sec.

Unexplained crash while polling systemtime type

I have a program that runs every 5 minutes when the stock market is open, which it does by running once, then entering the following function, which returns once 5 minutes has passed if the stock market is open.
What I don't understand, is that after a period of time, usually about 18 or 19 hours, it crashes returning a sigsegv error. I have no idea why, as it isn't writing to any memory - although I don't know much about the systemtime type, so maybe that's it?
Anyway, any help you could give would be very much appreciated! Thanks in advance!!
void KillTimeUntilNextStockDataReleaseOnWeb()
{
SYSTEMTIME tLocalTimeNow;
cout<<"\n*****CHECKING IF RUN HAS JUST COMPLETED OR NOT*****\n";
GetLocalTime(&tLocalTimeNow);//CHECK IF A RUN HAS JUST COMPLETED. IF SO, AWAIT NEXT 5 MINUTE MARK
while((tLocalTimeNow.wMinute % 5)==0)
GetLocalTime(&tLocalTimeNow);
cout<<"\n*****AWAITING 5 MINUTE MARK TO UPDATE STOCK DATA*****\n";
GetLocalTime(&tLocalTimeNow);//LOOP THROUGH THIS SECTION, CHECKING CURRENT TIME, UNTIL 5 MINUTE UPDATE. THEN PROCEED
while((tLocalTimeNow.wMinute % 5)!=0)
GetLocalTime(&tLocalTimeNow);
cout<<"\n*****CHECKING IF MARKET IS OPEN*****\n";
//CHECK IF STOCK MARKET IS EVEN OPEN. IF NOT, REPEAT
GetLocalTime(&tLocalTimeNow);
while((tLocalTimeNow.wHour < 8)||(tLocalTimeNow.wHour) > 17)
GetLocalTime(&tLocalTimeNow);
cout<<"\n*****PROGRAM CONTINUING*****\n";
return;
}
If you want to "wait for X seconds", then the Windows system call Sleep(x) will sleep for x milliseconds. Note however, if you sleep for, say, 300s, after some operation that took 3 seconds, that would mean you drift 3 seconds every 5minutes - it may not matter, but if it's critical that you keep the same timing all the time, you should figure out [based on time or some such function] how long it is to the next boundary, and then sleep that amount [possibly run a bit short and then add another check and sleep if you woke up early]. If "every five minutes" is more of an approximate thing, then 300s is fine.
There are other methods to wait for a given amount of time, but I suspect the above is sufficient.
Instead of using a busy loop, or even Sleep() in a loop, I would suggest using a Waitable Timer instead. That way, the calling thread can sleep effectively while it is waiting, while still providing a mechanism to "wake up" early if needed.

Timed button press

I was playing a racing game and after a while I started thinking how the gear shifting mechanism was implemented. I'm trying to do something similar, but much more simple. In this program I am printing out the number 128. as time goes on the number decreases. If I press a key at 4 secs the number should revert back to 128 and start decreasing again. My main issue is finding a way to calculate the multiplier as you see below. As you can see What I'm currently doing does not work. After 800 miliseconds the resulting value increases again and goes past 1. I want the max value at 1 so that when i press a button at 4 seconds the multiplier is 1 to get exactly 128. Thanks for your help in advance.
start timer
loop after this point
get time to variable
if button press
multiplier=1-(abs(time-400)/400)
clear timer
get time to variable
print (128*multiplier)*(100/(time+100))
Thanks for the help in advance.
start timer
loop after this point
get time to variable
if button press
multiplier=1-(abs(time-400)/400)
if multiplier < 0
multiplier = 0
clear timer
get time to variable
print (128*multiplier)*(100/(time+100))