[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Answer: Writing a "trading system"



PureBytes Links

Trading Reference Links




Hello Anthony and Markus,
 
yes, there is a difference between the two 
coding techniques. However, both coding techniques may sometimes produce 
conflicting (i.e., ambiguous) 
signals on exactly the same trading day. Please be advised that this is related 
and inherent to the particular design of the setup of that "trading 
system" but not to the programming code itself!
 
Now, I exemplarily refined my previously 
presented programming example and furthermore, I also included various 
comments explaining the details of the following code and its underlying 
fundamentals.
 
Hopefully that solves how to program your 
trading problem with my little coding example (see below)!
 
Best regards,
 
Udo
 
Coding 
Example:

/*
In this programming example, the first 2 conditions for purchasing and 
selling a stock are more specific than the latter two buying and selling 
statements. For this reason, the last two statements obtain more general results 
and therefore more stocks!
The first two purchasing and selling conditions only search for those stocks 
whose current high value is actually higher than the most recent high-price 
data, and whose current low value is indeed lower than the prices obtained until 
fairly recently. However, the last two statements additionally(!) search for 
rather "flat" stock-price data, which means that their highest or lowest 
stock-price figures have not changed substantially over a given period of 
time.
Note that a large database containing at least 2000 stocks normally yields 
enough candidates for further investigation when utilizing this programming code 
over the most recent 30-50 trading days, for example!
Remark: In a similar way, it is straightforward to apply the same ideas of 
this programming code to differing conditions for shorting and covering a stock. 
The only requirement is then to exchange the corresponding names of the lvalues 
standing on the left side of the 4 buying and selling assignments.
*/<FONT 
face="Courier New" color=#009300 size=1>
// The AFL library functions HHV() and LLV() also consider the current bar 
plus the preceding bars. Here are 2 different possibilities to code the task for 
finding the highest or lowest stock-price value over a given period of time but 
with totally differing trading objectives!
// Note that the periods are (24 + 1) and (9 + 1) bars in the 1st and 2nd 
statement and similarly, we consider 25 and 10 bars in the 3rd and 4th 
statement, respectively.
// Note also that the results of the following two different sets of buying 
and selling terms commonly do not yield identical results:<FONT 
face="Courier New" color=#00008b size=1>
purchase = <FONT face="Courier New" color=#800080 
size=1>Ref<FONT face="Courier New" color=#00008b 
size=1>(<FONT face="Courier New" color=#800080 
size=1>HHV<FONT face="Courier New" color=#00008b 
size=1>(<FONT face="Courier New" color=#ff6820 
size=1>high, 
24<FONT 
face="Courier New" color=#00008b size=1>), -<FONT face="Courier New" 
color=#ff00ff size=1>1) 
< <FONT face="Courier New" color=#ff6820 
size=1>high;
sold = <FONT face="Courier New" color=#800080 
size=1>Ref<FONT face="Courier New" color=#00008b 
size=1>(<FONT face="Courier New" color=#800080 
size=1>LLV<FONT face="Courier New" color=#00008b 
size=1>(<FONT face="Courier New" color=#ff6820 
size=1>low, 
9<FONT 
face="Courier New" color=#00008b size=1>), -<FONT face="Courier New" 
color=#ff00ff size=1>1) 
> low<FONT 
face="Courier New" color=#00008b size=1>;<FONT face="Courier New" 
color=#ff6820 size=1>
buy = 
HHV<FONT 
face="Courier New" color=#00008b size=1>(<FONT face="Courier New" 
color=#ff6820 size=1>high<FONT face="Courier New" color=#00008b 
size=1>, 25<FONT 
face="Courier New" color=#00008b size=1>) == <FONT face="Courier New" 
color=#ff6820 size=1>high<FONT face="Courier New" color=#00008b 
size=1>;
sell = 
LLV<FONT 
face="Courier New" color=#00008b size=1>(<FONT face="Courier New" 
color=#ff6820 size=1>low<FONT face="Courier New" color=#00008b 
size=1>, 10<FONT 
face="Courier New" color=#00008b size=1>) == <FONT face="Courier New" 
color=#ff6820 size=1>low<FONT face="Courier New" color=#00008b 
size=1>;
// In some situations, the aforementioned 2 different buying and selling sets 
can produce conflicting signals on the same trading day! Such conflicting 
signals are ambiguous and should therefore always be avoided when designing a 
robust and unambiguous trading system:<FONT face="Courier New" 
color=#00008b size=1>
ambiguous = purchase <FONT face="Courier New" color=#ff6820 
size=1>AND sold 
AND<FONT 
face="Courier New" color=#00008b size=1> <FONT face="Courier New" 
color=#ff6820 size=1>buy<FONT face="Courier New" color=#00008b 
size=1> <FONT face="Courier New" color=#ff6820 
size=1>AND 
sell<FONT 
face="Courier New" color=#00008b size=1>;<FONT face="Courier New" 
color=#ff6820 size=1>
filter = 
ambiguous;
AddColumn<FONT face="Courier New" color=#00008b 
size=1>(<FONT face="Courier New" color=#ff6820 
size=1>high, 
"high"<FONT 
face="Courier New" color=#00008b size=1>, format = <FONT 
face="Courier New" color=#ff00ff size=1>1.4<FONT face="Courier New" 
color=#00008b size=1>);
// Here is the 1st set of the purchasing and selling 
conditions:
AddColumn(purchase, 
"purchase"<FONT 
face="Courier New" color=#00008b size=1>, format = <FONT 
face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
color=#00008b size=1>);<FONT face="Courier New" color=#800080 
size=1>
AddColumn(sold, 
"sold"<FONT 
face="Courier New" color=#00008b size=1>, format = <FONT 
face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
color=#00008b size=1>);
// Now we render the 2nd set of the aforementioned buying and selling 
conditions:
AddColumn<FONT face="Courier New" color=#00008b 
size=1>(<FONT face="Courier New" color=#ff6820 
size=1>buy, 
"buy"<FONT 
face="Courier New" color=#00008b size=1>, format = <FONT 
face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
color=#00008b size=1>);<FONT face="Courier New" color=#800080 
size=1>
AddColumn<FONT face="Courier New" color=#00008b 
size=1>(<FONT face="Courier New" color=#ff6820 
size=1>sell, 
"sell"<FONT 
face="Courier New" color=#00008b size=1>, format = <FONT 
face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
color=#00008b size=1>);
// The following textual and background colors may illustrate the occurrences 
of ambiguous signals in further details:<FONT face="Courier New" 
color=#00008b size=1>
textColor = <FONT face="Courier New" color=#800080 
size=1>IIF(ambiguous == 
true<FONT 
face="Courier New" color=#00008b size=1>, <FONT face="Courier New" 
color=#ff6820 size=1>colorRed<FONT face="Courier New" color=#00008b 
size=1>, <FONT face="Courier New" color=#ff6820 
size=1>colorDefault<FONT face="Courier New" color=#00008b 
size=1>);
backgroundColor = <FONT face="Courier New" color=#800080 
size=1>IIF(ambiguous == 
true<FONT 
face="Courier New" color=#00008b size=1>, <FONT face="Courier New" 
color=#ff6820 size=1>colorLightYellow<FONT face="Courier New" 
color=#00008b size=1>, <FONT face="Courier New" color=#ff6820 
size=1>colorDefault<FONT face="Courier New" color=#00008b 
size=1>);
AddColumn<FONT face="Courier New" color=#00008b 
size=1>(<FONT face="Courier New" color=#ff6820 
size=1>filter, 
"filter"<FONT 
face="Courier New" color=#00008b size=1>, format = <FONT 
face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
color=#00008b size=1>, textColor, backgroundColor);
 
<BLOCKQUOTE 
>
  -----Ursprüngliche Nachricht----- 
  <DIV 
  >Von: 
  IVA GmbH 
  An: <A title=amibroker@xxxxxxxxxxxxxxx 
  href="">amibroker@xxxxxxxxxxxxxxx 
  Gesendet: Freitag, den 21. Februar 2003 
  23:43 Uhr
  Betreff: Re: [amibroker] Writing a 
  "trading system"
  
  Hi Udo,
   
  testing your code in explore mode with Comp-I, I 
  got on some days all for conditions to be 1.
   
  That can´t mean that I simultaneously buy, sell, 
  cover AND short all on the same day, right?
   
  But WHAT does in mean???
   
  Markus
  <BLOCKQUOTE 
  >
    ----- Original Message ----- 
    <DIV 
    >From: 
    <A title=Udo.Harke@xxxxxxxxxxx 
    href="">Udo.Harke@xxxxxxxxxxx 
    To: <A title=amibroker@xxxxxxxxxxxxxxx 
    href="">amibroker@xxxxxxxxxxxxxxx 
    Sent: Friday, February 21, 2003 10:02 
    PM
    Subject: [amibroker] Writing a "trading 
    system"
    
    Hi Mr. IVA GMBH,
     
    this is what you really want to 
    do:
    <FONT face="Courier New" color=#009300 
    size=1>
    // The AFL library functions HHV() and LLV() consider the current bar as 
    well as the preceding ones and
    // therefore you must(!) use the equal to operator, ==:<FONT 
    face="Courier New" color=#00008b size=1><FONT face="Courier New" 
    color=#ff6820 size=1>
    buy = 
    HHV<FONT 
    face="Courier New" color=#00008b size=1>(<FONT face="Courier New" 
    color=#ff6820 size=1>high<FONT face="Courier New" color=#00008b 
    size=1>, 25<FONT 
    face="Courier New" color=#00008b size=1>) == <FONT 
    face="Courier New" color=#ff6820 size=1>high<FONT 
    face="Courier New" color=#00008b size=1>;<FONT 
    face="Courier New" color=#ff6820 size=1>
    sell = 
    LLV<FONT 
    face="Courier New" color=#00008b size=1>(<FONT face="Courier New" 
    color=#ff6820 size=1>low<FONT face="Courier New" color=#00008b 
    size=1>, 10<FONT 
    face="Courier New" color=#00008b size=1>) == <FONT 
    face="Courier New" color=#ff6820 size=1>low<FONT 
    face="Courier New" color=#00008b size=1>;<FONT 
    face="Courier New" color=#ff6820 size=1>
    short = 
    LLV<FONT 
    face="Courier New" color=#00008b size=1>(<FONT face="Courier New" 
    color=#ff6820 size=1>low<FONT face="Courier New" color=#00008b 
    size=1>, 25<FONT 
    face="Courier New" color=#00008b size=1>) == <FONT 
    face="Courier New" color=#ff6820 size=1>low<FONT 
    face="Courier New" color=#00008b size=1>;<FONT 
    face="Courier New" color=#ff6820 size=1>
    cover = 
    HHV<FONT 
    face="Courier New" color=#00008b size=1>(<FONT face="Courier New" 
    color=#ff6820 size=1>high<FONT face="Courier New" color=#00008b 
    size=1>, 10<FONT 
    face="Courier New" color=#00008b size=1>) == <FONT 
    face="Courier New" color=#ff6820 size=1>high<FONT 
    face="Courier New" color=#00008b size=1>;<FONT face="Courier New" 
    color=#009300 size=1>
    // This filter is very convenient for the exploration 
    mode:
    filter = 
    buy<FONT 
    face="Courier New" color=#00008b size=1> <FONT face="Courier New" 
    color=#ff6820 size=1>OR<FONT face="Courier New" color=#00008b 
    size=1> <FONT face="Courier New" color=#ff6820 
    size=1>sell 
    OR<FONT 
    face="Courier New" color=#00008b size=1> <FONT face="Courier New" 
    color=#ff6820 size=1>short<FONT face="Courier New" color=#00008b 
    size=1> <FONT face="Courier New" color=#ff6820 
    size=1>OR 
    <FONT face="Courier New" color=#ff6820 
    size=1>cover<FONT face="Courier New" color=#00008b 
    size=1>;
    // Now you can see more data of a chosen stock received from the first 
    filter:
    // filter = 1;<FONT face="Courier New" color=#00008b 
    size=1>
    AddColumn<FONT face="Courier New" color=#00008b 
    size=1>(<FONT face="Courier New" color=#ff6820 
    size=1>buy, 
    "buy"<FONT 
    face="Courier New" color=#00008b size=1>, format = <FONT 
    face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
    color=#00008b size=1>);<FONT face="Courier New" color=#800080 
    size=1>
    AddColumn<FONT face="Courier New" color=#00008b 
    size=1>(<FONT face="Courier New" color=#ff6820 
    size=1>sell, 
    "sell"<FONT 
    face="Courier New" color=#00008b size=1>, format = <FONT 
    face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
    color=#00008b size=1>);<FONT face="Courier New" color=#800080 
    size=1>
    AddColumn<FONT face="Courier New" color=#00008b 
    size=1>(<FONT face="Courier New" color=#ff6820 
    size=1>short, 
    "short"<FONT 
    face="Courier New" color=#00008b size=1>, format = <FONT 
    face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
    color=#00008b size=1>);<FONT face="Courier New" color=#800080 
    size=1>
    AddColumn<FONT face="Courier New" color=#00008b 
    size=1>(<FONT face="Courier New" color=#ff6820 
    size=1>cover, 
    "cover"<FONT 
    face="Courier New" color=#00008b size=1>, format = <FONT 
    face="Courier New" color=#ff00ff size=1>1.0<FONT face="Courier New" 
    color=#00008b size=1>);
    Have fun IVA!
    Best regards,
    Udo
    <BLOCKQUOTE 
    >
      -----Ursprüngliche Nachricht----- 
      <DIV 
      >Von: 
      IVA GmbH 
      An: <A 
      title=amibroker@xxxxxxxxxxxxxxx 
      href="">amibroker@xxxxxxxxxxxxxxx 
      
      Gesendet: Freitag, den 21. Februar 
      2003 17:36 Uhr
      Betreff: [amibroker] Writing a 
      "trading system"
      
      Hello,
       
      I´m trying to write a simple trading system 
      to get my feet wet with AB´s backtest ability.
       
      I want to buy when the high is higher than 
      the highest high of the last 25 days and sell and the low is lower then 
      the last 10 days.
       
      On the short side, I want to short when the 
      low is lower than the lowest low ofthe preceeding 25 days and cover the 
      position when the high is higher than the highest high ofthe last 10 
      days.
       
      Why does this NOT work:
       
      
      Buy=<FONT 
      face="Courier New" color=#000000>High<FONT face="Courier New" 
      color=#6600aa>><FONT face="Courier New" 
      color=#0000ff>HHV<FONT face="Courier New" 
      color=#6600aa>(<FONT face="Courier New" 
      color=#000000>High<FONT face="Courier New" 
      color=#6600aa>,<FONT face="Courier New" 
      color=#ff00ff>25<FONT face="Courier New" 
      color=#6600aa>);
      Sell=<FONT 
      face="Courier New" color=#000000>Low<FONT face="Courier New" 
      color=#6600aa><<FONT face="Courier New" 
      color=#0000ff>LLV<FONT face="Courier New" 
      color=#6600aa>(<FONT face="Courier New" 
      color=#000000>Low<FONT face="Courier New" 
      color=#6600aa>,<FONT face="Courier New" 
      color=#ff00ff>10<FONT face="Courier New" 
      color=#6600aa>);
      Short=<FONT 
      face="Courier New" color=#000000>Low<FONT face="Courier New" 
      color=#6600aa><<FONT face="Courier New" 
      color=#0000ff>LLV<FONT face="Courier New" 
      color=#6600aa>(<FONT face="Courier New" 
      color=#000000>Low<FONT face="Courier New" 
      color=#6600aa>,<FONT face="Courier New" 
      color=#ff00ff>25<FONT face="Courier New" 
      color=#6600aa>);
      Cover=<FONT 
      face="Courier New" color=#000000>High<FONT face="Courier New" 
      color=#6600aa>><FONT face="Courier New" 
      color=#0000ff>HHV<FONT face="Courier New" 
      color=#6600aa>(<FONT face="Courier New" 
      color=#000000>High<FONT face="Courier New" 
      color=#6600aa>,<FONT face="Courier New" 
      color=#ff00ff>10<FONT face="Courier New" 
      color=#6600aa>);Post AmiQuote-related 
      messages ONLY to: amiquote@xxxxxxxxxxxxxxx (Web page: <A 
      href="">http://groups.yahoo.com/group/amiquote/messages/)Check 
      group FAQ at: <A 
      href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
      Your use of Yahoo! Groups is subject to the <A 
      href="">Yahoo! Terms of Service. 
      Post AmiQuote-related messages ONLY to: 
    amiquote@xxxxxxxxxxxxxxx (Web page: <A 
    href="">http://groups.yahoo.com/group/amiquote/messages/)Check 
    group FAQ at: <A 
    href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
    Your use of Yahoo! Groups is subject to the <A 
    href="">Yahoo! Terms of Service. 
    Post 
  AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx (Web page: <A 
  href="">http://groups.yahoo.com/group/amiquote/messages/)Check 
  group FAQ at: <A 
  href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
  Your use of Yahoo! Groups is subject to the <A 
  href="">Yahoo! Terms of Service. 







Yahoo! Groups Sponsor


  ADVERTISEMENT









Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)

Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html



Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.