in Search

Unable to call the function for Buy and Sell

Last post 12-22-2005, 2:52 by wabbit. 18 replies.
Sort Posts: Previous Next
  •  12-15-2005, 16:17 12974

    Unable to call the function for Buy and Sell

    I have written a ExtFml function that plays either a "BUY" or "SELL" wav file. It works fine. I want to call the function with "BUY" only when the C>O and "SELL" when C<O. However, no matter what I do, it calls and plays both "BUY" and "SELL" on every bar. Please help.
  •  12-16-2005, 6:05 12990 in reply to 12974

    Re: Unable to call the function for Buy and Sell

    What is the function and how are you calling it?
    Traders' Consortium
  •  12-16-2005, 15:21 12995 in reply to 12974

    Re: Unable to call the function for Buy and Sell

    Hi,
    Thanks for the reply....
    I am using following formulaes for the buy

    B1 := LastValue(If(C>O, 2,0));
    ExtFml( "MSXDll.MakeMyPosition", "MSFT",B1,"Test MSFT")

    For Sell
    If(O>C, ExtFml( "MSXDll.MakeMyPosition", "MSFT", -1, "Test MSFT"), 0) ;
    If(O>C,1,0);

    Thanks
    Madhavi
  •  12-16-2005, 20:26 13011 in reply to 12974

    Re: Unable to call the function for Buy and Sell

    Hi Madhavi,

    Let me just first say that I hope you're not appying this to real-time data, because you'll have sound files being played nearly everytime a tick comes in. That could get really annoying!

    The next thing I need to say is that it doesn't look like we're getting the entire picture here. Your question seems pretty clear, but your formulae seem to reference things and perform a lot more functionality than you expressed in the first post. For example, what are all the references to MSFT? Why are you using a formula to test if close > open when you've already written an MSX DLL that could compute that kind of thing hundreds or thousands of times faster? Needless to say, I'm a little confused by your problem.

    In order to overcome that confusion, I'm just going to address your first post and assume that you want a function in an MSX DLL to play a wave file (like Buy.wav perhaps) when close > open and another one (like Sell.wav) when close < open. Hopefully that will help. If not, post again! :)

    Your formula that calls the external function can be very simple. You don't even need one for buy and one for sell. Just use the following formula:

    Code:
    ExtFml( "MSXDll.MakeMyPosition" )


    The function in your MSX DLL doesn't need to take any parameters, unless you want to pass the wave file names or something like that. One thing you want to be careful to do is only look at the last bar. Every time your MSX function is called, it is given all the data. You certainly don't want to play a sound for every bar, just the last one. Here is some example code:

    Code:

    BOOL __stdcall MakeMyPosition(const MSXDataRec *a_psDataRec,
    const MSXDataInforRecArgsArray *a_psDataInfoArgs,
    const MSXNumericArgsArray *a_psNumericArgs,
    const MSXStringArgsArray *a_psCustomArgs,
    const MSXCustomArgsArray *a_psCustomArgs,
    MSXResultRec *a_psResultRec)
    {
    // First, we're going to play a sound, not return any results, so zero
    // out a_psResultRec via the iFirstValid and iLastValid fields in a_psResultRec.
    a_psResultRec->psResultArray->iFirstValid = 0;
    a_psResultRec->psResultArray->iLastValid = -1;

    // Second, get the open and close value from the last bar in the input data.
    // (You'll want to do some error checking here, like making sure that there is
    // actually any data in the record, and making sure that the open and close
    // data both end on the same record. For the sake of brevity and clarity, I
    // haven't included that kind of error checking for this example.)
    float open = a_psDataRec->sOpen.pfValue[a_psDataRec->sOpen.iLastValid];
    float close = a_psDataRec->sClose.pfValue[a_psDataRec->sClose.iLastValid];

    // Third, compare the open and close and play the appropriate file.
    // (Note that there are three possibilities here. Open could be greater than
    // close, open could be less than close, or open could be equal to close. You
    // didn't say anything about doing something if they are equal, so here I test
    // for the greater than and less than conditions, and do nothing for the equal
    // condition.)
    if(open > close)
    {
    // Insert your code to play Sell.wav here!
    }
    else if(open < close)
    {
    // Insert your code to play Buy.wav here!
    }

    // We're done, return
    return MSX_SUCCESS;
    }


    You didn't specify a programming language, so I hope C is ok.

    I hope this helps. If it doesn't meet your requirements, it's because you didn't give them all. :P Just post again if you still have problems.

    Good luck,
    Rich
  •  12-19-2005, 21:11 13109 in reply to 12974

    Re: Unable to call the function for Buy and Sell

    Hi Rich,
    Thanks for the reply.

    I am doing this with MS reat time only. I want to call this external function every minute instead of every tick with buy and sell signals. In general, I want to call my function with buy signal when buy signal is displayed on the MS chart and sell signal when the sell signal is displayed on the MS chart.

    Plz help.

    Thanks
    Madhavi Roy
  •  12-19-2005, 22:47 13111 in reply to 12974

    Re: Unable to call the function for Buy and Sell

    I'm not sure I understand what you're doing.

    It is built into MetaStock (in an Expert Advisor) to be able to play a sound file based on a formula.
    Is this what you want?

    MSX DLL functions need to be passed all data, not just one data point.
    It will then do anything you have the function do (play sounds, calculations, etc) and then return an array of data to be plotted on the chart.
    Equis International, Inc.
    A Thomson Reuters Company
    90 S. 400 W Suite 620
    SLC, UT 84101
  •  12-20-2005, 19:19 13137 in reply to 12974

    Re: Unable to call the function for Buy and Sell

    Hi,
    This is what I need. I want to call my dll function whenever MS chart shows buy/sell. That is, if the MS shows buy, then for that tick, I want to call my external function with the buy signal and same for sell.

    This should remain same for whatever MS formulae is used in the experts advisor. In general i want to pass on the sell/buy tick to the dll function whenevr they occur.

    Hope now the question is much more clearer.

    Thanks
    Madhavi Roy
  •  12-21-2005, 16:52 13152 in reply to 12974

    Re:

    roymadhavi:
    Hi,
    This is what I need. I want to call my dll function whenever MS chart shows buy/sell. That is, if the MS shows buy, then for that tick, I want to call my external function with the buy signal and same for sell.

    This should remain same for whatever MS formulae is used in the experts advisor. In general i want to pass on the sell/buy tick to the dll function whenevr they occur.

    Hope now the question is much more clearer.

    Thanks
    Madhavi Roy


    That's not how it works.
    MSX DLL functions need to be passed all data, not just one data point.
    It will then do anything you have the function do (play sounds, calculations, etc) and then return an array of data to be plotted on the chart.

    You will have to pass it all the data. You could have it look at just the last value, but it has to know what makes the data a buy or sell signal. You could pass that information in as one of the parameters.
    Equis International, Inc.
    A Thomson Reuters Company
    90 S. 400 W Suite 620
    SLC, UT 84101
  •  12-21-2005, 18:03 13154 in reply to 12974

    Re: Re:

    Another thing to consider is that the expert advisor will play sound files. Have you looked into that functionality? Is there something you need that it does not do?

    -Rich
  •  12-21-2005, 18:09 13155 in reply to 12974

    Re: Re:

    Ok, I understand this. Actually what is happening now is that my function is getting called for every tick. No doubt that all the data is passed to this function. My requirement is to call this function only when either a buy signal or a sell signal is generated/shown on the MS chart.
    I am new to MS development, so I have no idea if this is at all possible. Plz let me knoe if it is really possible and if yes than how??

    Thanks
    Madhavi Roy
  •  12-21-2005, 18:18 13156 in reply to 12974

    Re: Re:

    What don't you keep it simple ....

    IF (condition,ExtFml( "MSXDll.MakeMyPosition"),0)


    I don't understand what the issue is here ...
    If I understood poperly your function only calls up a sound file, that's all it does right?

    Patrick :mrgreen:
    My Site
  •  12-21-2005, 18:46 13158 in reply to 12974

    Re: Re:

    Sorry for bothering u again.....
    Now instead of playing the sound file, i am writing the data sent to the dll in a file, just to learn how MS works and how the external functions are called.

    If(Cross(C,O), ExtFml("MSXDll.MakeMyPosition"), 0);
    Cross(C,O);

    I am using the above formulae to call my function. Now this shows the buy signal on the chart and calls my external function. So the number of times my function is called and this buy signal is displayed on the MS chart should be same, but I find that my function is called more number of times than the buy signal displayed on the MS chart screen. I just want to synchronize these two.[/list]
  •  12-21-2005, 19:14 13162 in reply to 12974

    Re: Re:

    [quote user="MSXDll.MakeMyPosition"), 0);
    Cross(C,O);

    I am using the above formulae to call my function. Now this shows the buy signal on the chart and calls my external function. So the number of times my function is called and this buy signal is displayed on the MS chart should be same, but I find that my function is called more number of times than the buy signal displayed on the MS chart screen.

    The problem that you are running into here is that there is no "short circuiting" in the MetaStock formula language. This means that if you have a formula like
    Code:
    if(O>C, ExtFml( "MSXDll.MakeMyPosition" ), 0);

    your external formula will be called every time, not just when O>C. The point of the if statement is not to keep something from being called, but to select which results will be returned by the if statement. So, in the above formula, the external function is called every time, as is anything in the other case of the if statement, but the results of the external function are used for the results of the if statement only when O>C.

    It is important to remember that a formula does not calculate on a row-by-row basis. The above code is evaluated in only three steps, not three steps per row. This allows some huge performance gains. Here's what happens:

    1. O>C is evaluated.
    2. ExtFml( "MSXDll.MakeMyPosition" ) is evalualted.
    3. 0 is evaluated. In this case (where we're dealing with a constant), that means that an array the size of the input data is filled with the constant, or zero.
    4. The if statement results are produced, choosing at each data point either the results from step 2 or from step 3, depending on the results of step 1.

    Hope this helps. By the way, it's not a bother. This is a common question, and it's discussion here should benefit many. So, please keep posting your questions.

    -Rich
  •  12-21-2005, 19:56 13165 in reply to 12974

    Re: Re:

    Ok then from the data passed to the external function, how can i figure out that buy signal is sent...Probably i will need to check the condition in the function itself...Am I right??
  •  12-21-2005, 20:06 13166 in reply to 12974

    Re: Re:

    Ok then from the data passed to the external function, how can i figure out that buy signal is sent...Probably i will need to check the condition in the function itself...Am I right??

    You can either check the condition in the function, or you can check the condition in the formula language and pass that in to the function, like so:

    Code:
    ExtFml( "MSXDll.MakeMyPosition", C>O )


    This is the simplest way to write it. Checking for the condition in the function itself is more efficient though, if you want to do it that way.

    -Rich
  •  12-21-2005, 20:34 13167 in reply to 12974

    Re: Re:

    when i call this way, it says "This variable or expression must contain only constant data.". Am I missing something...

    Thanks
  •  12-21-2005, 21:39 13169 in reply to 12974

    Re: Re:

    You know, after attempting to implement this myself, I have come to the conclusion that not only is it more efficient to calculate the BUY/SELL condition in the DLL, it is simpler also, because you don't have to pass a parameter to the DLL, and you don't have to determine if you are looking for a BUY signal or a SELL signal or write two seperate functions or any of that. Here's the formula I'd use:

    Code:
    ExtFml( "MSXDll.MakeMyPosition" )


    Here is the MSXNthFunction function:

    Code:
    BOOL __stdcall MSXNthFunction(int a_iNthFunc, MSXFuncDef* a_psFuncDef)
    {
    BOOL l_bRtrn = MSX_SUCCESS;

    switch(a_iNthFunc)
    {
    case 0:
    strcpy(a_psFuncDef->szFunctionName, "MakeMyPosition");
    strcpy(a_psFuncDef->szFunctionDescription, "MakeMyPosition Description");
    a_psFuncDef->iNArguments = 0;
    break;
    default:
    l_bRtrn = MSX_ERROR;
    break;
    }

    return l_bRtrn;
    }


    Here's the MSXNthArg function:

    Code:
    BOOL __stdcall MSXNthArg(int a_iNthFunc, int a_iNthArg, MSXFuncArgDef *a_psFuncArgDef)
    {
    // There are no args, this shouldn't be called
    return MSX_ERROR;
    }


    This doesn't require any custom strings, so your MSXNthCustomString function can just always return MSX_ERROR, just like the MSXNthArg function.

    Finally, your MakeMyPosition function as I posted some time ago:

    Code:
    BOOL __stdcall MakeMyPosition(const MSXDataRec *a_psDataRec,
    const MSXDataInforRecArgsArray *a_psDataInfoArgs,
    const MSXNumericArgsArray *a_psNumericArgs,
    const MSXStringArgsArray *a_psCustomArgs,
    const MSXCustomArgsArray *a_psCustomArgs,
    MSXResultRec *a_psResultRec)
    {
    // First, we're going to play a sound, not return any results, so zero
    // out a_psResultRec via the iFirstValid and iLastValid fields in a_psResultRec.
    a_psResultRec->psResultArray->iFirstValid = 0;
    a_psResultRec->psResultArray->iLastValid = -1;

    // Second, get the open and close value from the last bar in the input data.
    // (You'll want to do some error checking here, like making sure that there is
    // actually any data in the record, and making sure that the open and close
    // data both end on the same record. For the sake of brevity and clarity, I
    // haven't included that kind of error checking for this example.)
    float open = a_psDataRec->sOpen.pfValue[a_psDataRec->sOpen.iLastValid];
    float close = a_psDataRec->sClose.pfValue[a_psDataRec->sClose.iLastValid];

    // Third, compare the open and close and play the appropriate file.
    // (Note that there are three possibilities here. Open could be greater than
    // close, open could be less than close, or open could be equal to close. You
    // didn't say anything about doing something if they are equal, so here I test
    // for the greater than and less than conditions, and do nothing for the equal
    // condition.)
    if(open > close)
    {
    // Insert your code to play Sell.wav here!
    }
    else if(open < close)
    {
    // Insert your code to play Buy.wav here!
    }

    // We're done, return
    return MSX_SUCCESS;
    }


    Hope that helps,
    Rich
  •  12-22-2005, 2:39 13184 in reply to 12974

    Re: Re:

    Is this whole issue just a matter of suppressing multiple signals? If so, you might check the FAQ or search the forum for some remedies. It sounds like you are calling your dll fine, but can't control when it is being called. Look for flip-flop control (training video on latches) or use of the Alert() function.

    Patrick:
    MetaStock sends a message every time the alert conditions is tested and found to be true. Normally, the alerts are tested for each tick that comes in. You can change it to be tested only after a bar is completed by changing the following option: TOOLS | OPTIONS, under the real-time tab, uncheck the ‘Recalcualate Expert Live’ option.

    Apart from precise control, a dirtier method might be to control it from the tool/ options as Patrick posted eons ago.

    HTH,

    George
    Traders' Consortium
  •  12-22-2005, 2:52 13189 in reply to 12974

    Re: Re:

    I might not have read this thread correctly, so forgive me if I am wrong in my assumptions:

    I am assuming the goal is to play a .wav file iff (if and only if) a particular condition is met. Isn't that what the MS expert advisor alerts are for?

    If the alert keeps triggering when the condition is met earlier in the history, like it does, and you dont feel clicking the "skip all remaining alerts" checkbox each time, just add to your condition:

    soundAlert:=myBuyCondition and cum(1)=LastValue(cum(1));
    soundAlert;

    so the alert will only sound on the latest bar.

    This might be the simple solution, or my simplistic view of the problem, but the I am a firm believer in Occam.



    wabbit :D

    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

View as RSS news feed in XML