Cumulate Orders

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #25597 quote
    quo
    Participant
    Average

    Hi All, i have a system with this logic:

    1.-When conditions are set, buy 1000 eur.

    2.-Once in, if the close is lower than the previous buy, buy again 2000.

    3.-If the close is again below the second buy, buy 3000.

    4.-And if the close is below the third buy, buy 4000.

    No more than 4 buys is admited.

    I have coded this but everytime i try to put the third buy gives me an error.

    IF LONGONMARKET=0 THEN
    IF c1 THEN
    BUY 1000 cash AT MARKET
    ENDIF
    ELSE
    if close<close[Barindex - Tradeindex] then
    buy 2000 cash at market
    endif
    endif

    Any help?

    thanks

    #25598 quote
    Nicolas
    Keymaster
    Master

    What is the error? No more cash? 🙂

    #25629 quote
    quo
    Participant
    Average

    Hi Nicolas, there is no error in the code. It is the way i use it, but i want to improve it with the changes stated before.

    I dont know how to limit operations to 4. Also i can´t find how to tell the system for the third buy: the close must be lower the second buy (not the first one).

    If you can tell me if this is discused before in the forum or the command i have to use i appreciated.

    thanks

    #25633 quote
    Nicolas
    Keymaster
    Master

    I’m not sure it will work like the way you want, please make your tests to see how it goes..Thanks.

    once amount = 1000
    
    IF NOT LONGONMARKET THEN
     IF c1 THEN
      BUY amount cash AT MARKET  
     ENDIF
    ELSE
     if close<positionprice(1) and barindex>tradeindex(1) and countofposition<4 then
      amount = abs(countofposition)*1000
      buy amount cash at market  
     endif
    endif
    #30590 quote
    quo
    Participant
    Average

    Hi Nicolas, i am afaid is not what i want. You gave me a clue and i have been trying to find out if there is a way to get what i want but unfortunatelly not.

    I give you the code again and i explain what happen:

    // Definición de los parámetros del código
    DEFPARAM CumulateOrders = True // Acumulación de posiciones activada
    
    // Condiciones para entrada de posiciones largas
    
    c1 = close<Average[5](close) and close[1]<average[5] and close>average[200](close)
    IF LONGONMARKET=0 THEN
    IF c1 THEN
    BUY 1000 cash AT MARKET
    ENDIF
    ELSE
    if close<close[Barindex - Tradeindex] then
    buy 2000 cash at market
    endif
    endif
    IF close>average[5] then
    sell at market
    endif

    It works as i want but i have unlimited orders and i want a maximum of 4.

    Also it buys 1000 in the first order and 2000 in the rest and i want only four entries (1000 eur, 2000 eur, 3000 eur, 4000 eur)( i mean 4 entries if the price goes lower but a trade can be 1,2,3 or 4 buys)

    It is a kind of pyramiding buying more when you get lower prices.

    i have read this

    https://www.prorealcode.com/blog/trading/averaging-techniques-automated-trading/

    but i trade cash no contracts and i want to increase the cuantity every time i buy (limited to 4) and was no possible to adapt it.

    I also studied this advice

    Reply To: Limiting accumulating orders & Fuse

    but doesn´t work, maybe because this for intraday and in shares.

    I also reached this code. It increase the quantity as i want but it does it in the same day!!

    // Definición de los parámetros del código
    
    DEFPARAM CumulateOrders = True // Acumulación de posiciones activada
    
    // Condiciones para entrada de posiciones largas
    
    c1 = close<Average[5](close) and close[1]<average[5] and close>average[200](close)
    
    if not longonmarket then
    if c1 then
    buy 1000 cash at market
    endif
    else
    if close<close[barindex -Tradeindex] and countofposition buy 2000 cash at market
    endif
    if close<close[barindex -Tradeindex] and countofposition buy 3000 cash at market
    endif
    if close<close[barindex -Tradeindex] and countofpositionaverage[5] then
    sell at market
    endif


    I dont want to use positionprice as you suggest, it is not important the midprice. Other thing: What happen with countofposition? if i skip it in the last code remains the same! can not be used with cash?

    thanks for your help

    #30591 quote
    quo
    Participant
    Average

    i am sorry i used “insert PRT code” but it did not work

    <br>
    // Definición de los parámetros del código
    DEFPARAM CumulateOrders = True // Acumulación de posiciones activada
    
    // Condiciones para entrada de posiciones largas
    
    c1 = close<Average[5](close) and close[1]<average[5] and close>average[200](close)
    
    if not longonmarket then
    if c1 then
    buy 1000 cash at market
    endif
    else
    if close<close[barindex -Tradeindex] and countofposition<2000 then
    buy 2000 cash at market
    endif
    if close<close[barindex -Tradeindex] and countofposition<3000 then
    buy 3000 cash at market
    endif
    if close<close[barindex -Tradeindex] and countofposition<6000 then
    buy 4000 cash at market
    endif
    endif
    
    IF close>average[5] then
    sell at market
    endif
    #30600 quote
    AutoStrategist
    Participant
    Veteran

    Did you look at the documentation? This should make it clear what you need to change in your code:

    COUNTOFPOSITION

    #30610 quote
    quo
    Participant
    Average

    Thank you, its true, it is very clear but it seems countofposition works well with contract and shares buy not with cash.

    This code with shares works as i want

     

    // Definición de los parámetros del código
    DEFPARAM CumulateOrders = True // Acumulación de posiciones activada
    
    // Condiciones para entrada de posiciones largas
    
    c1 = close<Average[5](close) and close[1]<average[5] and close>average[200](close)
    
    IF NOT LONGONMARKET and c1 THEN
    BUY 1000 shares AT MARKET
    ENDIF
    maxshares = countofposition <=10000
    if close<close[Barindex - Tradeindex]  and longonmarket and countofposition=1000 and maxshares then
    buy  2000 shares at market
    endif
    if close<close[Barindex - Tradeindex]  and longonmarket and countofposition=3000 and maxshares then
    buy  3000 shares at market
    endif
    if close<close[Barindex - Tradeindex]  and longonmarket and countofposition=6000 and maxshares then
    buy  4000 shares at market
    endif
    IF close>average[5] then
    sell at market
    endif
    

     

    but… as i trade stocks and they have different price i need to use cash.

    i have tried dividing cash into price in order to know the number of shares but does not work, (maybe because it takes different close price every time?)

     

    // Definición de los parámetros del código
    DEFPARAM CumulateOrders = True // Acumulación de posiciones activada
    
    // Condiciones para entrada de posiciones largas
    
    c1 = close<Average[5](close) and close[1]<average[5] and close>average[200](close)
    
    
    IF NOT LONGONMARKET and c1 THEN
    BUY (1000/close) shares AT MARKET
    ENDIF
    maxshares = countofposition <=10000
    if close<close[Barindex - Tradeindex]  and longonmarket and countofposition=(1000/close) and maxshares then
    buy  (2000/close) shares at market
    endif
    if close<close[Barindex - Tradeindex]  and longonmarket and countofposition=(3000/close) and maxshares then
    buy  (3000/close) shares at market
    endif
    if close<close[Barindex - Tradeindex]  and longonmarket and countofposition=(6000/close) and maxshares then
    buy  (4000/close) shares at market
    endif
    IF close>average[5] then
    sell at market
    endif
    

    i almost got it. Any idea?

    #30622 quote
    AutoStrategist
    Participant
    Veteran
    
    //You can do it by keeping track of the number of tractions yourself
    
    ONCE Transactions = 0
    
    c1 = close<Average[5](close) and close[1]<average[5] and close>average[200](close)
    
    
    if not LONGONMARKET and c1 then
    BUY (1000/close) SHARES AT MARKET
    Transactions = 1
    elsif LONGONMARKET then
    if close<close[Barindex - Tradeindex]  and Transactions = 1 then
    buy  (2000/close) shares at market
    Transactions = 2
    endif
    if close<close[Barindex - Tradeindex]  and Transactions = 2 then
    buy  (3000/close) shares at market
    Transactions = 3
    endif
    if close<close[Barindex - Tradeindex] and Transactions = 3 then
    buy  (4000/close) shares at market
    Transactions = 4
    endif
    endif
    
    If close>average[5] then
    sell at market
    Transactions = 0
    endif
    quo thanked this post
    #30623 quote
    AutoStrategist
    Participant
    Veteran

    *Comment should read ‘transactions’.

    And Insert PRT code is losing the indentation.

    #30626 quote
    quo
    Participant
    Average

    good idea. I though was the solution but unfortunatelly not. We have the same problem. It buys several times the same day. It does not understand it must wait until next lower close happen.

    thanks anyway.

    #30629 quote
    AutoStrategist
    Participant
    Veteran

    What timeframe chart are you using?

    #30630 quote
    quo
    Participant
    Average

    I use end of date data.

    It depends of the system but in this case weekly and daily charts.

    #30645 quote
    AutoStrategist
    Participant
    Veteran

    Ah ok I re-used your code without checking it thoroughly enough, when onmarket it needs to do only one purchase per bar so needs to use an elsif.

    ONCE Transactions = 0
    
    c1 = close<Average[5](close) and close[1]<average[5] and close>average[200](close)
    
    
    if not LONGONMARKET and c1 then
    BUY (1000/close) SHARES AT MARKET
    Transactions = 1
    elsif LONGONMARKET then
    //maxshares = countofposition <=10000
    if close<close[Barindex - Tradeindex]  and Transactions = 1 then
    buy  (2000/close) shares at market
    Transactions = 2
    elsif close<close[Barindex - Tradeindex]  and Transactions = 2 then
    buy  (3000/close) shares at market
    Transactions = 3
    elsif close<close[Barindex - Tradeindex] and Transactions = 3 then
    buy  (4000/close) shares at market
    Transactions = 4
    endif
    endif
    
    If close>average[5] then
    sell at market
    Transactions = 0
    endif
    Nicolas thanked this post
    #30753 quote
    quo
    Participant
    Average

    Great!

    thank you so much

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.

Cumulate Orders


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
quo @quo Participant
Summary

This topic contains 14 replies,
has 3 voices, and was last updated by quo
8 years, 10 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 02/19/2017
Status: Active
Attachments: No files
Logo Logo
Loading...