Buy order doesn't fire

Questions about MultiCharts and user contributed studies.
quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Buy order doesn't fire

Postby quod_erat » 24 May 2021

I've done plenty of coding in EasyLanguage, and so far, I could always work out bugs when they came up. But I can't figure out this issue; it feels like I'm missing something basic but I can't put my finger on it.

I'm trying to 'prime' a program, so that once event A occurs, it then looks for event B to occur, at which point it sends a buy order. To give an example with dummy code (that matches the code logic of my script):

Code: Select all

inputs: Price(Close); vars: var(0), numentries(0); if Price < CloseD(1) then var = 1; if var = 1 and Price > CloseD(1) and numentries = 0 then buy next bar market; if marketposition = 1 then var = 0; numentries = 1;
Expected behavior is for the program to 'prime' the buy function whenever Price falls below the previous day's close (var = 1). Then -- and only then -- it waits for Price to rise above previous day close, at which point it sends a buy order. It then resets var1 to 0, and numentries to 1, so that the buy can only be executed once.

But: no buy order is sent even when the conditions are met. Thoughts?

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: Buy order doesn't fire

Postby rrams » 24 May 2021

Hello quod_erat,
1. The code:

Code: Select all

if marketposition=1 then var = 0; numentries = 1;
is missing Begin and End statements which makes numentries = 1; ignore the if clause.
2. It is a crime against humanity to use the reserved word "var" as the name of a variable. (Also using numentries as a true/false flag is confusing as it sounds like it should contain the actual number of entries.)
3. If possible avoid code that is waiting for events to happen (forward acting). Instead, test for the most recent condition and then look back in the history of the time series for any other conditions that need to be met. This makes tighter code that is easier to debug. So if your intent is an intraday signal, then maybe something like:

Code: Select all

if C>CloseD(1) and LowD(0)<CloseD(1) and EntriesToday(D)=0 then buy next bar market;
Let me know if that is not what you intended.

quod_erat
Posts: 33
Joined: 14 Mar 2020
Has thanked: 4 times
Been thanked: 1 time

Re: Buy order doesn't fire

Postby quod_erat » 24 May 2021

Thanks -- I think #1 is exactly right! My primary coding is done in python, where indents matter ... I sometimes forget that they don't here!

And I agree on #2. My actual code has a more descriptive variable name. This is just lazy dummy code :)


Return to “MultiCharts”