How to exit after certain days from `strategy.entry` - stock

I can't seem to figure out the syntax for exit a trade from a certain time interval from the trade entry. Any help would be greatly appreciated.
if (crossover(delta, 0))
strategy.entry("Long", strategy.long, comment="Long")
strategy.exit("Exit", "Long", when = 15)
The code above I want to exit the long position after 15 days. But it doesn't seem to work.

Try barssince
// Example - Buy when the price closes below 22
myEntry = close < 22
strategy.entry(id= "sample", long = strategy.long, when= myEntry)
// Close 10 bar periods after the condition that triggered the entry
strategy.close(id = "sample", when = barssince(myEntry) >= 10)

I figured it out one solution. I created another if statement but offset by 15 days to trigger. I also set the crossover to a variable. See code:
buy = (crossover(delta, 0))
if (buy)
strategy.entry("Long", strategy.long, comment="Long")
if (buy[15])
strategy.close("Long")

Related

Water rising and descending

im in a process of making a roblox game. I want to make the water rise and descend again down. I think i did something wrong with coding,(its my second day of coding and i cant find any video on it) im not sure what. So everytime i run roblox after 1st descending and starting of 2nd game, it freezes,water dissapears and it throws a error: Script timeout: exhausted allowed execution time. Here is my code:
wait(21)
t = 17
repeat
t = t-1
script.Parent.Position = script.Parent.Position + Vector3.new(0, 1, 0)
wait(2)
until
t == 0
wait(1)
t = 17
repeat
t = t-1
script.Parent.Position = script.Parent.Position + Vector3.new(0, -1, 0)
wait(0.5)
until
t == 0
wait(9)
t = 17
while true do
t = t-1
script.Parent.Position = script.Parent.Position + Vector3.new(0, 1, 0)
end
wait(2)
while false do
t = t-1
script.Parent.Position = script.Parent.Position + Vector3.new(0, -1, 0)
end
Script timeout: exhausted allowed execution time.
Because the code never stops looping here:
while true do
//...
end
This will continue to loop forever. (Well, for as long as true is true. And I can't think of a condition in which it wouldn't be.)
Contrast that with your loops above:
t = 17
repeat
t = t-1
//...
until
t == 0
In this case the loop explicitly repeats "until t equals 0", and each iteration of the loop brings t one step closer to 0.
Modify your later loops to have similar structures and conditions. For example:
t = 17
while t > 0 do
t = t-1
//...
end
Note also that this loop will never execute:
while false do
//...
end
Because false is never true. Now is a good time to take a step back and consider the logic of what you want these loops to do. Just like with your first two loops, modify your second two loops to indicate how many times they should repeat.

Pine script setting timestamp for last quarter not working

I wrote a simple strategy in pine scrip, which is based on crossover/crossunder for two different SMAs.
It is important for me to test my strategy in some time frames, especially in the last quarter. It doesn't work. I got only a few results. I should get results for whole period (colored on green), when SMAs crossed.
Unwanted result
My script is working very well, when I don't use time range or when I set dateCond on true value.
Below I present source code:
//#version=4
strategy("Moving Average Cross 1", initial_capital=1000, overlay=true)
start = timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
end = timestamp(syminfo.timezone, 2021, 4, 1, 0, 0)
fastSMA = sma(close, 9)
slowSMA = sma(close, 50)
long = crossover(fastSMA, slowSMA)
short = crossunder(fastSMA, slowSMA)
orderSize = floor(strategy.equity / close)
plot(fastSMA, title="20", color=#00ffaa, linewidth=3)
plot(slowSMA, title="50", color=#FFC1CC, linewidth=2)
dateCond = time > start
// dateCond = true
bgcolor(dateCond ? #00ffaa : na)
if dateCond
strategy.entry("long", strategy.long, qty=orderSize, when = long)
strategy.entry("short", strategy.short, qty=orderSize, when = short)
strategy.close("long", when = short)
strategy.close("short", when = long)
I tried with different time ranges (when I set start date on the 1 January 2020 it works very well). I additionally colored a background to check condition, but coloring it also work good. I haven't more ideas why for the last quarter it isn't working properly. I will appreciate any help.
I tested script mainly for pair ETH/USDT (Binance)
Here we use larger capital and close positions before entering a new one. This way the whole position is closed before a new one is opened:
//#version=4
strategy("Moving Average Cross 1", initial_capital=100000, overlay=true)
start = timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
end = timestamp(syminfo.timezone, 2021, 4, 1, 0, 0)
fastSMA = sma(close, 9)
slowSMA = sma(close, 50)
long = crossover(fastSMA, slowSMA)
short = crossunder(fastSMA, slowSMA)
orderSize = floor(strategy.equity / close)
plot(fastSMA, title="20", color=#00ffaa, linewidth=3)
plot(slowSMA, title="50", color=#FFC1CC, linewidth=2)
dateCond = time > start
// dateCond = true
bgcolor(dateCond ? #00ffaa : na)
strategy.close("long", when = short)
strategy.close("short", when = long)
if dateCond
strategy.entry("long", strategy.long, qty=orderSize, when = long)
strategy.entry("short", strategy.short, qty=orderSize, when = short)

Program freezes with specific numbers

This code is supposed to take a number and find it's prime factors.
Why does this code work with numbers like 2345 (which returns 5, 7, 67 like its supposed to) but it doesn't work with numbers like 500 and 800?
EDIT: when I say it doesn't work, the program simply does nothing and/or freezes on button click. I then have to stop the program in vb.
Dim number As Double = txtNum.Text
Dim var As Double = 2
Dim result As Double
If number > 1 Then
lst1.Items.Clear()
lst1.Items.Add("The prime factors of " & number & ":")
Do While number > 1
result = number / var
If result = Int(result) Then
lst1.Items.Add(var)
number = result
End If
var = var + 1
Loop
Else
lst1.Items.Clear()
lst1.Items.Add("Let try that again...")
End If
The problem was caused because 500 divided by 2 is 250, divided by 5 is 50 ... and then the program continued on by dividing by 6 instead of attempting to divide by 5 again. Once var reached 10, number became 5 and no further divisions ever allowed it to reach 1 - thus an infinite loop was created.
By changing
If result = Int(result) Then
lst1.Items.Add(var)
number = result
End If
var = var + 1
to
If result = Int(result) Then
lst1.Items.Add(var)
number = result
Else
var = var + 1
End If
it correctly made multiple divisions by the same prime factor when necessary.

Calculate with time

As I need to log my working hours for project reasons I am trying to configure a macro. As I am rather new to VBA I am still experiencing difficulties with code formulas and time-formats.
Here is how the sheet looks like
When pressing "ARRIVAL" or "DEPARTURE" it's just Range("C7/C9") = Now
(this is in seperate cells because I want to be able to manually override)
When pressing "LOG" this is the code behind it:
'FORMAT
Range("12:12").Insert
Range("C7").Copy
Range("C12:I12").PasteSpecial
Range("C12:I12").ClearContents
'DATE
Range("C7").Copy
Range("C12").PasteSpecial
Range("C12").NumberFormat = "m/d/yyyy"
'ARRIVAL
Range("C7").Copy
Range("D12").PasteSpecial
Range("D12").NumberFormat = "[$-F400]h:mm:ss AM/PM"
'DEPARTURE
Range("C9").Copy
Range("E12").PasteSpecial
Range("E12").NumberFormat = "[$-F400]h:mm:ss AM/PM"
'CALCULATE
Range("E12").Copy
Range("F12").PasteSpecial
Range("F12") = Range("E12").Value - Range("D12").Value
Range("C12:I12").Font.Size = 12
What I want now is in column G the amount of time I have been working on project which is less then 03:30h and in column H the amount of time I have been working on project which is more then 03:30h.
Ideally this would be in #Minutes and even combined in 1 column with a (+/-) so I can easily calculate the totals.
Thanks for looking into this!
I've updated your code a bit and included some notes to explain why. I've added the 2 formulas that convert your total time into minutes and calculate the differences for the < & > requirements. Hope this helps.
Sub LogTime()
Range("12:12").Insert
Range("C7").Copy
'Pastes just the format from C7
Range("C12:I12").PasteSpecial (xlPasteFormats)
'Set as Today's Date
Range("C12").Value = Date
'Arrival
Range("D12").Value = Range("C7").Value
Range("D12").NumberFormat = "[$-F400]h:mm:ss AM/PM"
'Departure
Range("E12").Value = Range("C9").Value
Range("E12").NumberFormat = "[$-F400]h:mm:ss AM/PM"
'Calculate Total with Formula
Range("F12").Value = "=" & Range("E12").Address(0, 0) & "-" & Range("D12").Address(0, 0)
Range("C12:I12").Font.Size = 12
'Convert Time To Minues: (HOUR(F12)*60)+MINUTE(F12)+ROUND((SECOND(F12)/60),0)
'< Formula: In Minutes; if Total Time is => 3:30 return 210 and Total Time if < 3:30 return Total Time
Range("G12").Value = "=IF(((HOUR(F12)*60)+MINUTE(F12)+ROUND((SECOND(F12)/60),0))>=210,210,(HOUR(F12)*60)+MINUTE(F12)+ROUND((SECOND(F12)/60),0))"
Range("G12").NumberFormat = "General"
'< Formula: In Minutes; if Total Time is <= 3:30 return 0 and if Total Time > 3:30 return total time - 3:30
Range("H12").Value = "=IF(((HOUR(F12)*60)+MINUTE(F12)+ROUND((SECOND(F12)/60),0))<=210,0,((HOUR(F12)*60)+MINUTE(F12)+ROUND((SECOND(F12)/60),0)-210))"
Range("H12").NumberFormat = "General"
End Sub

Verify Gamefield VB.NET

So I'm developing a minesweeper game and im assigning the mines, but I've got to check where are the mines now, in order to generate the numbers. The problem is that when I'm verifying the columns and lines I need the program not to get out of the game field.
Here's how my code looks like now:
Public Sub avisinhos(ByVal line, ByVal column)
If mat(line, column) = 0 Then
mat(line, column) = -1
numbandeiras = numbandeiras + 1
End If
For auxlinha = -1 To 1
For auxcolumn = -1 To 1
Next
Next
End Sub
How do I create a IF function to verify that I don't get out of the game field?
Best regards, joao.
pseudo code
int linestart = -1;
int lineend = 1;
int colstart = -1;
int colend = 1;
Assuming a 10 x 10 grid (zero based)
if line < 2 linestart = 0
if line > 8 lineend = 0
if column < 2 colstart = 0
if column > 8 colend = 0
For auxlinha = linestart To lineend
For auxcolumn = colstart To colend
// check
Next
Next
Personally though I wouldn't bother with the loops, they add very little to nothing
HasMineAbove = (line > 1) and (gamefield[line -1,column] = MinePresentValue
would be my approach, do it all in one.
Not to mention the huge potential confusion when auxlinha and auxcolumn are both zero...
I'm not sure exactly what your code is saying. It's a bit cryptic since you're using abbreviations and all lowercase names. You might want to try camelCasing and spelling out the words more completely, intellisense is your friend. =)
But coding style aside, if you are trying to loop through a limited range of values, you can keep your values bounded by using the modulus operator (%). For example, if you need to keep you values between 0-7 and you end up with a value of 12, just take the modulus of 8 to loop back to within range with a value of 4:
12 % 8 = 4
9 % 8 = 1
15 % 8 = 7
24 % 8 = 0
I realize this doesn't answer your specific question, but it's a handy technique might find useful.