How to make a strategy enter in the middle of current bar? - algorithmic-trading

I tried a very simple strategy as below,
VB_signal = (close - open) > high[1] - low[1]
strategy.entry("VB_buy", strategy.long, when = VB_signal)
strategy.close("VB_buy", when = barstate.isnew)
when current price goes over open price + previous bar's range (high - low), immediately enters in the middle of current bar.
But when I backtested this strategy, the strategy always entered after current bar close (beggining of next bar) How can I make this strategy enter in the middle of current bar?
In the image below,
Wanted to enter in the middle of second bar, but it enters on 3rd bar.

Both during historical and real-time calculation, code is calculated on bar closes by default.
Source: Backtesting and Forwardtesting

Related

How to open and close a position in the same bar in pinescript v5

I am trying just that. Open one position at the open of bar and close it at the end of the bar, not in the next bar. Can be done? I can't find a way.
Probably I need to activate the "process_orders_on_close" to force the exit in that bar, okay, but how can I force the position open in that bar and not at the end of the one bar before?
I have been playing with commands like time_close and time_tradingday comparing one or both with a session input. But the problem is the same, the script is executed at the end of the bar by default and any change will be done in the next bar. So if I use the "process_orders_on_close" the entry signal appears in the bar before.
I also tried request.security_lower_tf() but It doesn't work, maybe because the lower timeframes data no longer exits, only does in a higher temp.
So my question is that, can it be done? Can I open an order at the end of a bar and close that order at the end of the same bar? I am talking in historical chart alwais, of course.
I think it doesn't matter the timeframe but I am working on D chart.

CRichEditCtrl: How to have it scroll to end while focus lost?

I have a CRichEditCtrl with auto vertical scrolling set, which works, but not quite as I'd like. The text in the control can be updated via a message from another thread, so it can be busy updating while the user does other things, but the way it updates is different depending on whether it has focus or not (whether or not the caret is blinking). If it has focus, it auto scrolls to the most recent text (at the bottom of the window), without focus you can see the scroll bar shrinking as text is added but there is no actual scroll. Is there any way of getting it to always scroll to the bottom, so that the user always sees what's been most recently added?
I've tried sending EM_SCROLLCARET after ReplaceSel() but it made no difference. Also tried using LineScroll(), but that puts the most recent line at the top of the window, not at the bottom as happens when auto scrolling - this can also result in a blank window if there are trailing new lines ...
Looking on MSDN/forums I can't see much else to try.
Cheers for any help.

ZedGraph scrolling left truncates data

I am using a ZedGraphControl in a WindowsForms project in C#. The ZedGraphControl is V5.1.5.
The data in the control is static and I add it all before the form is shown. The X axis of the data consists of numbers indicating seconds offset from the beginning. in other words from 0 to some number of seconds.
I want to initially show the last 5 seconds, but provide a horizontal scrollbar so the user can scroll back and forth. I set the "graphPane.XAxis.Scale.Max = maxX;" where maxX is the largest X value in my data. I set the "graphPane.XAxis.Scale.Min = maxX - 5;".
The data starts off displaying the way I want it, but when the user scrolls the horizontal bar, bizzar behavior occurs.
As you drag the thumb of the scrollbar to the left, the beginning of the data shown in the grid moves to the lower values as expected, and the thumb of the scrollbar moves to the left, but the right edge of the thumb stays at the right of the scrollbar and you cannot move back to the right. It is as if the data to the right of the viewing range gets truncated as you scroll left.
I cannot find any reason for this nor any way to control it. Does anyone have any ideas about this behavior?
Ok, found it myself.
I found a fine article that describes scrolling:
Add a ScrollBar
In it the author specifically says "the scrolling will be wacky because the scrollable range has not been set".
I used the sample "Manually Setting the Scroll Range" and the part that I was missing is setting the zedGraphControl1.ScrollMinX and zedGraphControl1.ScrollMaxX properties. Once I defined these values everything started working as expected. I also found that in my case, the value of zedGraphControl1.IsAutoScrollRange had no effect, but I left it set to false to be consistent with the example. This would probably have an effect if the dataset is dynamic.

how to keep a running score in a textbox/object in powerpoint

My powerpoint is made is like a quiz, one slide has a question and 4 answers (buttons with text, not numbers) that can be clicked on. When an answer is clicked on, it is linked to another slide that explains if the answer being right/wrong and then adds/subtracts points from the score.
Currently what I have done is a simple macro that each button is linked to and upon clicking any of the 4 answer buttons, it will go to a certain slide and show a pop up box with their current score.
What I want to do is have either a text box or some object that holds the score throughout the entire presentation and ideally if possible, the score resets when the ppt is closed or opened but the running score is the most important aspect I'm trying to hammer down.
Is this possible for what I'm asking? Let me know if there are any details that would help
Instead of resetting the score when the presentation shuts down, do it at startup. For example, you could put a "Begin the quiz" button on the first slide, have it reset the score, then move to the next slide.
As to where to store the score, there are a number of ways of doing it. One would be to put a rectangle or text box on the last slide and store the score as its text. Make sure the rectangle's sent to back so it's the first shape on the slide, or change Shapes(1) below to indicate the actual z-order position of the shape.
Function SetScore(lScore as Long)
Dim lLastSlide as Long
lLastSlide = ActivePresentation.Slides.Count
With ActivePresentation.Slides(lLastSlide).Shapes(1).TextFrame.TextRange
.Text = Cstr(lScore)
End With
End Function
A companion GetScore function should be easy to come up with, similar to the above.

win32: detect if start menu is auto-hidden?

I want to position a window at the bottom of the screen. If the start menu is present, I want it to lie along the top of the start menu. If it is not (or it is auto-hidden), I still want it to be in the same position as it would be if the start menu was there, meaning there will be a gap of a few pixels.
Currently I get the monitor work area, position the window at the bottom, and always offset by 20 pixels or so. If the start menu isn't there, this works well. If it is, however, the work area also shrinks (as it should), and I end up double-offsetting.
How would I fix the issue?
To get the work area of the screen not obscured by the system taskbar or by application desktop toolbars, you can use SystemParametersInfo() with SPI_GETWORKAREA as uiAction parameter. The pvParam parameter must point to a RECT structure that receives the coordinates of the work area, expressed in virtual screen coordinates. For example:
RECT rectWorkArea;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rectWorkArea, 0);
As you said in the comment, to get the bounding rectangle of the taskbar, we can call SHAppBarMessage(ABM_GETTASKBARPOS, ...)
To determine the position of the taskbar (whether it is currently at the bottom, top, left, or right of the screen), you could use the following calculation:
type
TTaskBarPos = (_TOP, _BOTTOM, _LEFT, _RIGHT);
var
iScrW, iScrH: Longint;
iScrW := GetSystemMetrics(SM_CXSCREEN);
iScrH := GetSystemMetrics(SM_CXSCREEN);
if (rectTaskbar.Top > iScrH div 2) and (rectTaskbar.Right >= iScrW) then
Result := _BOTTOM
else if (rectTaskbar.Top < iScrH div 2) and (rectTaskbar.Bottom <= iScrW div 2) then
Result := _TOP
else if (rectTaskbar.Left < iScrW div 2) and (rectTaskbar.Top <= 0) then
Result := _LEFT
else
Result := _RIGHT;
They should be enough to solve your current problem. However, if you need to know (for another reason) the current taskbar settings of the autohide and always-on-top states, you can use SHAppBarMessage(ABM_GETSTATE, ...).
If you need to be notified that that the taskbar's autohide or always-on-top state has changed, you have to intercept ABN_STATECHANGE message.
Are you are using or have access to .NET in your project?
If so, you can use the Screen.PrimaryScreen.WorkingArea.Height property to determine the bottom of the screen excluding the task bar.
You can also grab the total screen height by getting the Screen.PrimaryScreen.Bounds.Height property (which includes the task bar in the total height value).
Comparing these values, if they're the same, the task bar isn't present. Otherwise, the task bar is and you can adjust accordingly.

Resources